Saturday, May 18, 2013

Modern OpenGL tutorial Drawing the basic shapes

This article is almost similar to my previous tutoirals series. In previous article we draw only triangle but here we are drawing other shapes.

Download
All the code in this series of articles is available from github: https://github.com/smokindinesh/Modern-OpenGL-Series  You can download a zip of all the files from that page, or you can clone the repository if you are familiar with git.

Vertex data for drawing is given below:

// Put the 18 triangle verticies into the VBO
    GLfloat vertexData[] = {
        //  X     Y     Z

        //Pentagon
        0.25f, 0.25f, 1.0f,
        0.75f, 0.25f, 1.0f,
        0.25f, 0.5f, 1.0f,

        0.25f, 0.5f, 1.0f,
        0.75f, 0.25f, 1.0f,
        0.75f, 0.5f, 1.0f,

        0.25f, 0.5f, 1.0f,
        0.75f, 0.5f, 1.0f,
        0.5f, .75f, 1.0f,

        //Triangle
        -0.25f, 0.25f, 1.0f,
        -0.5f, 0.75f, 1.0f,
        -0.75f, 0.25f, 1.0f,
        //Trapezoid
        -0.2f, -0.25f, 1.0f,
        -0.35f, -0.75f, 1.0f,
        0.35f, -0.75f, 1.0f,

        0.2f, -0.25f, 1.0f,
        -0.2f, -0.25f, 1.0f,
        0.35f, -0.75f, 1.0f,
    };

Above you can see we are using three triangle to draw Pentagon and two triangle to draw Trapezoid.
Now we have to change little things in render function. OpenGL function to render the scene is
// draw the VAO
    glDrawArrays(GL_TRIANGLES, 0, 3);
The third argument is 3 that means to draw triangle we are passing only 3 vertices. But we are going to draw other shapes and total vertices are 18. So, we have to pass 18 in third argument.
// draw the VAO
    glDrawArrays(GL_TRIANGLES, 0, 18);
Output of the program:

Next Chapter: Perspective Projection

No comments:

Post a Comment