int main(int argc,char * argv[]){
srand(time(NULL));
glutInit(&argc,argv);
// Setup GLUT
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_STENCIL);
glutInitWindowSize (window_width,window_height);
glutCreateWindow (window_title);
graphics_init ();
// Initialize Runtime variables
initialize();
// callbacks
glutReshapeFunc(&reshape);
glutDisplayFunc(&display);
glutMouseFunc(&mouse);
glutMainLoop();
return 0;
}
void mouse(int button, int state, int x, int y ){
if(state == GLUT_DOWN){
draw_method_two(); // draw objects here using method 2
glFlush();
}else{
current_point_i = -1;
}
}
void display(){
// Clear Viewport
glClearColor(0.0f,0.0f,0.0f,1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
draw_method_one(); ///// using method 1 to draw objects
glFlush();
glutSwapBuffers();
}
There are two methods to draw objects. in mouse(), draw_objects_two is used to draw the objects when mouse is clicked. However, the objects shown in the window are still created by draw_method_one. I use glFlush in the mouse()function. but why it is not displayed in screen?
Update:
from this link, it says:
The function you pass to glutDisplayFunc is only called it is needed: that means when the window is resized, or when an another window has hidden it. If you use glutMouseFunc, for instance, you perhaps want to update (redraw) your window content according to that clic. Also, if you draw animations, you need to call glutPostRedisplay from your idle function.
So why glutDisplayFunc() is called in this case?
In your mouse function nothing is drawn, because you only call
glFlushbut you don’t swap buffers (wtihglutSwapBuffers). Therefore you only draw to the back buffer, which is not presented. Next time GLUT calls display, the back buffer is overwritten there and finally presented by callingglutSwapBuffers.But you should not draw anything in another method than the
glutDisplayFunccallback, anyway. Just set some flag in the mouse function (like ssell suggested) and callglutPostRedisplay. Your updated question is correct, display is only called when neccessary, therefore you have to callglutPostRedisplaywhenever you change something and want this to update the screen.And by the way, you should usually not need to call
glFlush, which should already be done by the buffer swap.