0
votes

I'm trying to use GLUT for a specific openGL project. I've put glut32.dll/glut.h/glut32.lib in their required directories. After adding a source file to the project in Visual Studio, when I hit debug/run, it doesn't show any error what so ever. The source code I'm using is that of a colored cube that rotates. Now after hitting debug, the output console does show up with the colored cube, but only for a split second, which was not supposed to happen.

the code I'm using:

#include <GL/glut.h>
#define window_width  640
#define window_height 480
// Main loop
void main_loop_function() {
    // Z angle
    static float angle;
    // Clear color (screen)
    // And depth (used internally to block obstructed objects)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    // Load identity matrix
    glLoadIdentity();
    // Multiply in translation matrix
    glTranslatef(0, 0, -10);
    // Multiply in rotation matrix
    glRotatef(angle, 0, 0, 1);
    // Render colored quad
    glBegin(GL_QUADS);
    glColor3ub(255, 000, 000);
    glVertex2f(-1, 1);
    glColor3ub(000, 255, 000);
    glVertex2f(1, 1);
    glColor3ub(000, 000, 255);
    glVertex2f(1, -1);
    glColor3ub(255, 255, 000);
    glVertex2f(-1, -1);
    glEnd();
    // Swap buffers (color buffers, makes previous render visible)
    glutSwapBuffers();
    // Increase angle to rotate
    angle += 0.25;
}
// Initialze OpenGL perspective matrix
void GL_Setup(int width, int height) {
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glEnable(GL_DEPTH_TEST);
    gluPerspective(45, (float) width / height, .1, 100);
    glMatrixMode(GL_MODELVIEW);
}
// Initialize GLUT and start main loop
int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitWindowSize(window_width, window_height);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutCreateWindow("GLUT Example!!!");
    glutIdleFunc(main_loop_function);
    GL_Setup(window_width, window_height);
    glutMainLoop();
}

Can someone tell me what might be causing this? The code doesn't have any error. And since the output is indeed showing for only half of a second, I'm assuming the GLUT files have been placed correctly. Then what might be causing the console to go away within a second?

1

1 Answers

0
votes

With glut you should not draw things from the function attached to glutIdleFunc but rather glutDisplayFunc.

Use glutDisplayFunc(main_loop_function); and create a new timer function doing the angle += 0.25; and connect the callback with glutTimerFunc(...) to rotate in a timed fashion rather than on every redraw, which may not happen in regular intervals.