Abhijit Joshi

ENGINEER. PROGRAMMER. ARTIST.

OpenGL Tutorial 005: Using Vertex Arrays

OpenGL vertex arrays store coordinate, normal, color and any other conceivable information about the vertices in your geometry. The entire geometry can then be displayed using a single command called glDrawArrays

This tutorial shows you how to use vertex arrays and display three triangles on the screen.

Note how OpenGL automagically carries out color interpolation while filling the triangles.


#include <GLFW/glfw3.h>  // GLFW

static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GL_TRUE);
}

int main(void)
{
    //--------------------------------
    //   Create a WINDOW using GLFW
    //--------------------------------

    GLFWwindow *window;

    // initialize the library
    if(!glfwInit())
    {
        return -1;
    }

    // window size for displaying graphics
    int WIDTH  = 600;
    int HEIGHT = 600;

    // set the window's display mode
    window = glfwCreateWindow(WIDTH, HEIGHT, "Triangles", NULL, NULL);
    if(!window)
    {
        glfwTerminate();
        return -1;
    }

    // make the windows context current
    glfwMakeContextCurrent(window);

    // enable quitting the graphics by pressing ESC
    glfwSetKeyCallback(window, key_callback);


    // vertex data for three separate triangles (9 vertices)
    const float vertices[] =
    {
    // ----------
    // Triangle 0
    // ----------
    //   X     Y     Z     R  G  B  A
       -0.5, -0.5,  0.0,   1, 0, 0, 1,  // point 1
        0.5,  0.0,  0.0,   0, 1, 0, 1,  // point 2
       -0.5,  0.5,  0.0,   0, 0, 1, 1,  // point 3
    // ----------
    // Triangle 1
    // ----------
    //   X     Y     Z     R  G  B  A
       -0.4, -0.6,  0.0,   1, 1, 0, 1,  // point 1
        0.6, -0.6,  0.0,   1, 0, 1, 1,  // point 2
        0.6, -0.1,  0.0,   0, 1, 1, 1,  // point 3
    // ----------
    // Triangle 2
    // ----------
    //   X     Y     Z     R  G  B  A
        0.0,  0.4,  0.0,   1, 1, 0, 1,  // point 1
        0.6,  0.4,  0.0,   1, 0, 0, 1,  // point 2
        0.6,  0.8,  0.0,   1, 1, 1, 0   // point 3
    };

    //---------------------------------------
    // Loop until the user closes the window
    //---------------------------------------

    while(!glfwWindowShouldClose(window))
    {
        // select background color to be black
        float R = 0, G = 0, B = 0, alpha = 0;
        glClearColor(R, G, B, alpha);

        // clear all pixels in the window with the color selected above
        glClear(GL_COLOR_BUFFER_BIT);

        // enable use of vertex coordinate information from the array
        glEnableClientState(GL_VERTEX_ARRAY);

        glVertexPointer(3,                      // number of coordinates per vertex (X,Y,Z)
                        GL_FLOAT,               // type of numbers
                        sizeof(float)*7,        // stride - gap between each set of (X,Y,Z)
                        &vertices[0]);          // offset - location of initial (X,Y,Z)

        // enable use of vertex color information from the array
        glEnableClientState(GL_COLOR_ARRAY);

        glColorPointer(4,                       // number of color values per vertex (R,G,B,A)
                       GL_FLOAT,                // type of values
                       sizeof(float)*7,         // stride - gap between each set of (R,G,B,A)
                       &vertices[3]);           // offset - location of initial (R,G,B,A)

        // draw command
        glDrawArrays(GL_TRIANGLES,   // type of GL element
                     0,              // starting offset
                     9);             // number of vertices to be used for rendering

        // swap front and back buffers
        glfwSwapBuffers(window);

        // poll for and processs events
        glfwPollEvents();
    }

    glfwDestroyWindow(window);
    glfwTerminate();
    return 0;
}

To compile this code, use the following Makefile:

NAME = useVertexArray

all:
    g++ -I /usr/local/include/ -L /usr/local/lib ${NAME}.cpp -o cpp.x -framework OpenGL -framework Cocoa -framework IOKit -lglfw

clean:
    rm *.x