Search code examples
c++openglglut

Displaying and moving a square


#include <stdio.h> // this library is for standard input and output
#include "glut.h"// this library is for glut the OpenGL Utility Toolkit
#include <math.h>

float squareX = 0.0f;
float squareY = -0.3f;
float squareZ = 0.0f;

static int flag = 1;

void drawShape(void) {
    glBegin(GL_POLYGON);
    glColor3f(1.0, 0.0, 0.0); // set colour to red
    glVertex2f(162, 50);
    glVertex2f(162, 10);
    glVertex2f(220, 10);
    glVertex2f(220, 50);
    glVertex2f(162, 50);
    glTranslatef(squareX, squareY, squareZ); // moving it toward the screen a bit on creation
    glEnd();
}

void initRendering() {
    glEnable(GL_DEPTH_TEST);
}

// called when the window is resized
void handleResize(int w, int h) {
    // tell OpenGL how to convert from coordinates to pixel values
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION); // switch to setting the camera perspective
    // set the camera perspective
    glLoadIdentity(); // reset the camera
    gluPerspective(45.0,                  // the camera angle
    (double)w / (double)h, // the width-to-height ratio
    1.0,                   // the near z clipping coordinate
    200.0);                // the far z clipping coordinate
}

void drawScene() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    drawShape();
    glutSwapBuffers();
}

// float _angle = 30.0f;
void update(int value) {
    if (flag) {
        squareY += 0.001f;
        if (squareY > 0.3) {
            flag = 0;
        }
    }
    if (!flag) {
        squareY -= 0.001f;
        if (squareY < -0.3) {
            flag = 1;
        }
    }
    glutPostRedisplay(); // tell GLUT that the display has changed
    // tell GLUT to call update again in 25 milliseconds
    glutTimerFunc(25, update, 0);
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(400, 400);
    glutCreateWindow("Moving Square");
    initRendering();
    glutDisplayFunc(drawScene);
    glutReshapeFunc(handleResize);
    glutTimerFunc(25, update, 0);
    glutMainLoop();
    return(0);
}

I've added comments to the code so you can understand it. I'm trying to create a square that travels to the top of the screen, but I'm having trouble displaying the square. For some reason, it doesn't show up when I run the code. I can only display the square when I remove the code for the movement. How can I display the square and also make it go up?


Solution

  • Note, that drawing by glBegin/glEnd sequences and the fixed function pipeline matrix stack is deprecated since decades. Read about Fixed Function Pipeline and see Vertex Specification and Shader for a state of the art way of rendering.


    Anyway, glTranslatef is not allowed within glBegin/glEnd sequences. Do it before glBegin:

    void drawShape(void) {
        glTranslatef(squareX, squareY, squareZ); 
        glBegin(GL_POLYGON);
        glColor3f(1.0, 0.0, 0.0);
        glVertex2f(162, 50);
        glVertex2f(162, 10);
        glVertex2f(220, 10);
        glVertex2f(220, 50);
        glVertex2f(162, 50);
        glEnd();
    }
    

    If you want to draw the quad in window coordinates, then you have to use orthographic projection (glOrtho):

    void handleResize(int w, int h) {
    
        glViewport(0, 0, w, h);
        glMatrixMode(GL_PROJECTION); 
        glLoadIdentity(); 
        /*
        gluPerspective(45.0,   // the camera angle
        (double)w / (double)h, // the width-to-height ratio
        1.0,                   // the near z clipping coordinate
        200.0);                // the far z clipping coordinate
        */
    
        glOrtho( 0.0f, (float)w, 0.0f,(float)h, -1.0f, 1.0f );
    }  
    

    and you have to change the amount of the translation:

    void update(int value) {
        if (flag) {
            squareY += 1.0f;
            if (squareY > 350.0) {
                flag = 0;
            }
        }
        if (!flag) {
            squareY -= 1.0f;
            if (squareY < 0.0) {
                flag = 1;
            }
        }
        glutPostRedisplay(); 
        glutTimerFunc(25, update, 0);
    }
    

    Preview: