PDA

View Full Version : glutMainLoop



Mukund
04-15-2010, 11:17 AM
Hello everyone.
I needed some clarification regardin this:
void display()
{

glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,640.0,0.0,480.0,-500.0,500.0);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();

}



void draw()
{
int x=10,y=200;
glColor3f(0.0,0.0,0.0);

for(int i=0;i<10;i++){
glBegin(GL_LINE_LOOP);
glVertex2i(x,y);
glVertex2i(x , y+30);
glVertex2i(x + 30,y+30);
glVertex2i(x + 30,y);
glEnd();
y+=30;
}
glFlush();


}


void main(int argc,char ** argv)
{
glutInit(&amp;argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowSize(640,480);
glutCreateWindow("Rect");


glutDisplayFunc(display);
draw();
glutMainLoop();
}
Here the boxes are not getting drawn!
However when i give it in my display callback, it works!!
Why is this?Has it got to do domething with glutMainLoop()

FrimaMichael
04-15-2010, 12:26 PM
Basically the glutMainLoop() call starts an infinite loop calling the function your specified before. You only specified a display function but could also specify a reshape function and a mouse handling function for example (with glutReshapeFunc() and glutMouseFunc() respectively)

So when you specify glutDisplayFunc(display), you basically say to glut that each time you need to render, call the "display" function. Of course you have to put your draw(); function in that display() function or it will not be called.

The code you pasted here will basically render 1 time then never again (thus you see nothing being drawn).

Mick

Mukund
04-15-2010, 07:31 PM
Thanks Mick

You said:
Of course you have to put your draw(); function in that display() function or it will not be called.

But the control was going into the draw() function.
Also like you said,it should have rendered it once right?
But that the rendering was not done.Can you please explain why?

Thanks.

FrimaMichael
04-16-2010, 06:26 AM
Yes, It will render one time but your eyes will not have time to see it since the buffer is cleared immediately after (on the next frame).

The glClear(GL_COLOR_BUFFER_BIT); clears the buffer and all that was in it and reset all pixels to the color specified by glClearColor (in your case perfect white). So basically you need to but your draw() function just after the glClear call.

Mukund
04-16-2010, 01:53 PM
Yeah got it! Thanks a lot..