A Windows 3D Model Viewer for OpenGL
by Jawed Karim 


Listing One
typedef struct
{
     float x, y, z;  /* coordinates */
} make_vertex_list;

Listing Two
typedef struct
{
    int a, b, c;    /* array indices */
    int a_s, a_t,   /* (s, t) texture coordinates */
    b_s, b_t,
    c_s, c_t;
} make_index_list;

Listing Three
(a)
(vertex_list[index_list[0].a].x, vertex_list[index_list[0].a].y, 
                                       vertex_list[index_list[0].a].z)

(b)
(vertex_list[index_list[0].b].x, vertex_list[index_list[0].b].y, 
                                        vertex_list[index_list[0].b].z)

(c)
(vertex_list[index_list[0].c].x, vertex_list[index_list[0].c].y, 
                                        vertex_list[index_list[0].c].z)

Listing Four
typedef struct
{
     make_vertex_list *vertex;
} make_frame_list;

Listing Five:
(a)
frame_list[F].vertex[index_list[P].a].x
frame_list[F].vertex[index_list[P].a].y
frame_list[F].vertex[index_list[P].a].z

(b) 
frame_list[F].vertex[index_list[P].b].x
frame_list[F].vertex[index_list[P].b].y
frame_list[F].vertex[index_list[P].b].z

(c)
frame_list[F].vertex[index_list[P].c].x
frame_list[F].vertex[index_list[P].c].y
frame_list[F].vertex[index_list[P].c].z


Listing Six

(a)
R: m_palette_buffer [ m_pixel_buffer[0]]
G: m_palette_buffer [ m_pixel_buffer[1]]
B: m_palette_buffer [ m_pixel_buffer[2]]

(b)
R: m_palette_buffer [3 * m_pixel_buffer[P]+0]
G: m_palette_buffer [3 * m_pixel_buffer[P]+1]
B: m_palette_buffer [3 * m_pixel_buffer[P]+2]

(c)
R: m_palette_buffer [3 * m_pixel_buffer[X + Y*Width]+0]
G: m_palette_buffer [3 * m_pixel_buffer[X + Y*Width]+1]
B: m_palette_buffer [3 * m_pixel_buffer[X + Y*Width]+2]


Listing Seven
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

Listing Eight
glEnable(GL_CULL_FACE);
glEnable(GL_TEXTURE_2D);
glPolygonMode (GL_FRONT, GL_FILL);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

Listing Nine
Glubyte *unScaled = new GLubyte [m_iWidth * m_iHeight * 4];
for (j = 0; j < m_iHeight; j++) {
  for (i = 0; i < m_iWidth; i++) {
    unScaled[4*(j * m_iWidth + i)+0] = 
             (GLubyte) m_palette_buffer[3*m_pixel_buffer[j*m_iWidth+i]+0];
    unScaled[4*(j * m_iWidth + i)+1] = 
             (GLubyte) m_palette_buffer[3*m_pixel_buffer[j*m_iWidth+i]+1];
    unScaled[4*(j * m_iWidth + i)+2] = 
             (GLubyte) m_palette_buffer[3*m_pixel_buffer[j*m_iWidth+i]+2];
    unScaled[4*(j * m_iWidth + i)+3] = (GLubyte) 255;
  }
}

Listing Ten
/* allocate memory for the new rescaled texture */
glTexture = new GLubyte [m_iscaledWidth * m_iscaledHeight * 4];

/* use the OpenGL function to rescale */
gluScaleImage (GL_RGBA, m_iWidth, m_iHeight, GL_UNSIGNED_BYTE, unScaled, 
             m_iscaledWidth, m_iscaledHeight, GL_UNSIGNED_BYTE, glTexture);

/* reclaim memory of the unscaled texture */
delete [] unScaled;


Listing Eleven
GL_TEXTURE_2D    :  defines a two-dimensional texture
0                :  supplying one texture as multiple resolutions
4                :  indicates which of the R, G, B, and A values are used
scaledWidth      :  new width
scaledHeight     :  new height
0                :  width of the border (no border)
GL_RGBA          :  format of the texture data
GL_UNSIGNED_BYTE :  data type of the texture data
glTexture        :  pointer to array containing texture to be rescaled


3


