Abhijit Joshi

ENGINEER. PROGRAMMER. ARTIST.

OpenGL Tutorial 002: Moving Triangle

In this tutorial, we find out how to render a simple triangle using OpenGL. As we learn in subsequent tutorials, the surface of any 3D object can be modeled using an appropriate number of triangular facets.

This tutorial uses the "old" OpenGL commands like glBegin() and glEnd(), which are now considered obsolete. In subsequent tutorials, we use more powerful ways of specifying geometry, using vertex arrays and vertex buffer objects or VBOs.  

Finally, this tutorial describes how to create a simple OpenGL animation by changing the position of the triangle.


#include <GLFW/glfw3.h>

// function to render a triangle in the graphics window using OpenGL

void drawTriangle(float yLoc)
{
    // select background color to be black
    float R = 0, G = 0, B = 0, alpha = 0;
    glClearColor(R, G, B, alpha);

    // clear all pixels
    glClear(GL_COLOR_BUFFER_BIT);

    // pick this color to "fill" the triangle with
    R = 1.0; G = 0.85; B = 0.35;
    glColor3f(R, G, B);

    // default screen coordinates in OpenGL
    //
    //     (-1,1)                         (1,1)
    //           +-----------+-----------+
    //           |           |           |
    //           |           |           |
    //           |           |           |
    //           |           |           |
    //           |           | (0,0)     |
    //           +-----------+-----------+
    //           |           |           |
    //           |           |           |
    //           |           |           |
    //           |           |           |
    //           |           |           |
    //           +-----------+-----------+
    //    (-1,-1)                         (1,-1)

    // draw a triangle
    glBegin(GL_TRIANGLES);
    {  
        glVertex3f(  0.0,  yLoc + 0.3, 0.0);
        glVertex3f( -0.2,  yLoc - 0.3, 0.0);
        glVertex3f(  0.2,  yLoc - 0.3, 0.0);
    }
    glEnd();
}

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

    GLFWwindow *window;

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

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

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

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

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

    float yLoc = 0.0, dy = 0.002;
    while(!glfwWindowShouldClose(window))
    {
        // OpenGL rendering
        drawTriangle(yLoc);

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

        // poll for and processs events
        glfwPollEvents();

        // update position of triangle
        yLoc += dy;
        if (yLoc >  0.7) dy = -dy;
        if (yLoc < -0.7) dy = -dy;
    }

    // clean up
    glfwDestroyWindow(window);
    glfwTerminate();

    return 0;
}

To compile this code, use the following Makefile:

NAME = movingTriangle

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