Jump to content
  • Advertisement
Sign in to follow this  

Question about Vertex Arrays

This topic is 3295 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am reading from Interactive Computer Graphics 4th Edition and I have come across and example in the book where it talks about vertex arrays. I understand how it works in general just not sure if I am seeing the whole picture and would like to clarify how glDrawElements(GLenum mode, GLsizei n, GLenum type, void *indices) works exactly. Here is the sample code:

#include <stdlib.h>

#ifdef __APPLE__
#include <GLUT/glut.h>
#include <GL/glut.h>

	GLfloat vertices[] = {-1.0,-1.0,-1.0,1.0,-1.0,-1.0,
	1.0,1.0,-1.0, -1.0,1.0,-1.0, -1.0,-1.0,1.0, 
	1.0,-1.0,1.0, 1.0,1.0,1.0, -1.0,1.0,1.0};

	GLfloat colors[] = {0.0,0.0,0.0,1.0,0.0,0.0,
	1.0,1.0,0.0, 0.0,1.0,0.0, 0.0,0.0,1.0, 
	1.0,0.0,1.0, 1.0,1.0,1.0, 0.0,1.0,1.0};

    GLubyte cubeIndices[]={0,3,2,1,2,3,7,6,0,4,7,3,1,2,6,5,4,5,6,7,0,1,5,4};

void display(void)

/* display callback, clear frame buffer and z buffer,
   rotate cube and draw, swap buffers */

    glColorPointer(3,GL_FLOAT, 0, colors); 
    glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, cubeIndices);

void myReshape(int w, int h)
    glViewport(0, 0, w, h);
    if (w <= h)
        glOrtho(-4.0, 4.0, -3.0 * (GLfloat) h / (GLfloat) w,
            5.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);
        glOrtho(-4.0 * (GLfloat) w / (GLfloat) h,
            4.0 * (GLfloat) w / (GLfloat) h, -3.0, 5.0, -10.0, 10.0);

main(int argc, char **argv)

/* need both double buffering and z buffer */

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(500, 500);
    glEnable(GL_DEPTH_TEST); /* Enable hidden--surface--removal */
	glVertexPointer(3, GL_FLOAT, 0, vertices);
    glColorPointer(3,GL_FLOAT, 0, colors); 

My question is I see how cubeIndices orders all the vertices but I don't get how glDrawElements() knows that is is drawing from the vertex array. Is that just how glDrawElements() work internally? I know its something simple but I am having a hard time seeing how cubeIndices gets the correct information from the related vertex array positions. Thanks for any help in clarifying what is going on.

Share this post

Link to post
Share on other sites
The glDrawElements draws the vertices that are uploaded to the graphics card. In your main function, you have glVertexPointer which uploads the vertex positions to the card. Later you call glColorPointer in the display function. This sets the colour of the vertices.

You'll the number of colours matches the number of vertex positions. This is because the nth vertex position corresponds to the nth vertex colour. When you call glDrawElements with the array of indices to draw, the indices are used for the uploaded vertex data.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!