Abhijit Joshi

ENGINEER. PROGRAMMER. ARTIST.

OpenGL Tutorial 003: Zooming In and Out

As you saw in tutorial 002, the default screen coordinates in OpenGL range from -1 to +1 along X and Y. To enable a different range, we need to use orthographic projection.

In this tutorial, you will find out how to draw the same triangle used in tutorial 002, but change the extents of the screen coordinates before each rendering. This will provide the effect of the triangle growing and shrinking with time.


#include <GLFW/glfw3.h>
// OpenGL draws a triangle 

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

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

    // replace current transformation matrix with the identity matrix
    glLoadIdentity();

    // set clipping planes in the X-Y-Z coordinate system
    float xmin = -scale, xmax = scale;
    float ymin = -scale, ymax = scale;
    glOrtho(xmin,xmax,ymin,ymax, -1.0, 1.0);

    // new screen coordinates in OpenGL
    //
    //   (xmin, ymax)                     (xmax, ymax)
    //           +-----------+-----------+
    //           |           |           |
    //           |           |           |
    //           |           |           |
    //           |           |           |
    //           |           | (0,0)     |
    //           +-----------+-----------+
    //           |           |           |
    //           |           |           |
    //           |           |           |
    //           |           |           |
    //           |           |           |
    //           +-----------+-----------+
    //  (xmin, ymin)                      (xmax, ymin)

    // 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);

    // draw a triangle
    glBegin(GL_TRIANGLES);
    {
        glVertex3f(  0.0,  0.3, -0.5);
        glVertex3f( -0.2,  -0.3, 0.4);
        glVertex3f(  0.2,  -0.3, 0.2);
    }
    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  = 600;
    int HEIGHT = 600;

    // 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 scale = 1, d_scale = 0.01;
    while(!glfwWindowShouldClose(window))
    {
        drawTriangle(scale);

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

        // poll for and processs events
        glfwPollEvents();

        scale += d_scale;
        if ((scale > 3) || (scale < 0.5)) d_scale = -d_scale;
    }

    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