Search code examples
c++openglfreeglut

glut idle in Windows 8 - rerendering


I am using freeglut, windows 8, vs2012 and the newest nvidia driver. But the glut idle functions has a weird behavior. It does nothing until I resize the window or click on the window.

Or somehow glut doesn't want to rerender the screen, even if some variables have changed.

#include <iostream>
#include <stdlib.h>
#include <GL/glut.h>

using namespace std;


GLfloat rotateQuad = 0;



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



}

//Draws the 3D scene

void drawScene() {

    //Clear information from last draw

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective

    glLoadIdentity(); //Reset the drawing perspective

    glRotatef(rotateQuad,0,0,1);

    glBegin(GL_QUADS); //Begin quadrilateral coordinates



    glVertex3f(-0.5f, -0.5f, 0.0f);

    glVertex3f(0.5f, -0.5f, 0.0f);

    glVertex3f(0.5f, 0.5f, 0.0f);

    glVertex3f(-0.5f, 0.5f, 0.0f);

    glEnd(); //End quadrilateral coordinates


    glutSwapBuffers(); //Send the 3D scene to the screen

}
void idle(){
    rotateQuad+=1;
    if(rotateQuad > 360) rotateQuad=0;
}
int main(int argc, char** argv) {

    //Initialize GLUT

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

    glutInitWindowSize(400, 400); //Set the window size

    //Create the window

    glutCreateWindow("Quad Rotate");

    initRendering(); //Initialize rendering

    glutIdleFunc(idle);

    glutDisplayFunc(drawScene);

    glutReshapeFunc(handleResize);

    glutMainLoop(); //Start the main loop

    return 0;

}

Any ideas what went wrong?


Solution

  • Your idle function just updates the rotation; it's not actually asking GLUT to repaint, so no repaints are happening until something else triggers it (such as a window interaction or resize). Call glutPostRedisplay in your idle function. See: http://www.lighthouse3d.com/tutorials/glut-tutorial/glutpostredisplay-vs-idle-func/