开发者

why not display in openGL?

开发者 https://www.devze.com 2023-04-01 05:24 出处:网络
int main(int argc,char * argv[]){ srand(time(NULL)); glutInit(&argc,argv); // Setup GLUT glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB| GLUT_DEPTH | GLUT_STENCIL);
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
    glutReshape开发者_JAVA百科Func(&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 glFlush but you don't swap buffers (wtih glutSwapBuffers). 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 calling glutSwapBuffers.

But you should not draw anything in another method than the glutDisplayFunc callback, anyway. Just set some flag in the mouse function (like ssell suggested) and call glutPostRedisplay. Your updated question is correct, display is only called when neccessary, therefore you have to call glutPostRedisplay whenever 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.


What is happening is that no matter what, every loop draw_method_one( ) is being called. When you click the mouse, draw_method_two( ) is called only once, and so during the next loop the screen is redrawn using method one.

To get your desired result, do something like this:

bool drawFirstMethod = true;

...

void mouse(int button, int state, int x, int y )
{
    //toggle draw modes
    if( state == GLUT_DOWN )
    {
        if( drawFirstMethod )
            drawFirstMethod = false;
        else
            drawFirstMethod = true;
    }

    ...
}

display( )
{
    ...

    if( drawFirstMethod )
        draw_method_one( );
    else
        draw_method_two( );

    ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消