Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: glutMainLoop

  1. #1
    Junior Member Regular Contributor
    Join Date
    Mar 2010
    Posts
    209

    glutMainLoop

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

  2. #2
    Junior Member Newbie
    Join Date
    Apr 2010
    Location
    Québec, Canada
    Posts
    29

    Re: glutMainLoop

    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

  3. #3
    Junior Member Regular Contributor
    Join Date
    Mar 2010
    Posts
    209

    Re: glutMainLoop

    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.

  4. #4
    Junior Member Newbie
    Join Date
    Apr 2010
    Location
    Québec, Canada
    Posts
    29

    Re: glutMainLoop

    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.

  5. #5
    Junior Member Regular Contributor
    Join Date
    Mar 2010
    Posts
    209

    Re: glutMainLoop

    Yeah got it! Thanks a lot..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •