Ano-Tech Computers
Enter keyword:

OpenGL: glDrawElements() example code
Problem:
The OpenGL man page for glDrawElements() confused me greatly because it introduces a concept of "indices". Unfortunately, it leaves the reader with no clue as to what exactly is supposed to be indexed, and there is no example code illustrating the relationship between the various tables.
 
Solution:
void example_DrawElements()
{
GLfloat vertices[] = {
0,0,0,1,
0,0,0,1,
0,0,0,1
};
GLfloat colors[] = {
0,0,0,1,
0,0,0,1,
0,0,0,1
};
GLfloat texcoords[] = {
0,0,0,1,
0,0,0,1,
0,0,0,1
};
GLfloat vertex_normals[] = {
0,0,0,
0,0,0,
0,0,0
};
GLushort faces[] = {
0,1,2
};
int triangles = 1;

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);

glVertexPointer(4, GL_FLOAT, 0, vertices); // x,y,z,w
glColorPointer(4, GL_FLOAT, 0, colors); // r,g,b,a
glTexCoordPointer(4, GL_FLOAT, 0, texcoords); // t,s,r,q
glNormalPointer(GL_FLOAT, 0, vertex_normals); // x,y,z

glDrawElements(GL_TRIANGLES, triangles * 3, GL_UNSIGNED_SHORT, faces);

}

 
Discuss this solution
Did this article solve your problem? Yes No Did not apply

We welcome anyone who is willing to contribute to this public knowledge base, contact siteadmin@atc.no if you have information you would like to share. The idea is not to replace the commercial support sites, but to publish those hard-to-find solutions you've found yourself looking for over and over again.

Show all articles