-1

I had an issue where the rendered triangle would disappear after what I think is one iteration of the glutMainLoop.

I am running this on linux.

// g++ main.cpp -lglut -lGL -o main
#include <stdlib.h>
#include <iostream>
#include <GL/freeglut.h>
#include <GL/gl.h>

void keyboard(unsigned char key, int x, int y) {

    // 27 is ascii for the escape key
    if (key == 27) {
        std::cout << "Closed because user hit escape key." << std::endl;
        glutDestroyWindow(glutGetWindow());
    }
}

void draw_triangle() {

    glClearColor(0.4, 0.4, 0.4, 0.4);
    glClear(GL_COLOR_BUFFER_BIT);

    glColor3f(1.0, 1.0, 1.0);
    glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);

        glBegin(GL_TRIANGLES);
            glVertex3f(-0.7,  0.7,  0);
            glVertex3f( 0.7,  0.7,  0);
            glVertex3f( 0  ,   -1,  0);
        glEnd();
    
    glFlush();
}

int main(int argc, char **argv) {

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("");

    glutKeyboardFunc(keyboard);
    glutDisplayFunc(draw_triangle);

    glutMainLoop();

}
genpfault
  • 51,148
  • 11
  • 85
  • 139

1 Answers1

-1
glClearColor( 0.4, 0.4, 0.4, 0.4 );

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 );

glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

I'd move these lines out of the drawing function, as there's no point in calling glOrtho repeatedly if nothing changes in its parameters.

Azarien
  • 159
  • 3