Glut子窗口切换

时间:2013-05-20 15:01:08

标签: opengl glut

我有一个程序,我从主窗口创建了一个子窗口。问题是它始终在那里,我的目标是按一个键,然后将该子窗口“打开”,当我再次按下它时,让它消失。我设法用glutDistroyWindow销毁它,但后来我不知道怎么让它再次出现。这是我的代码:

void init(void)
{

    // pregatim o scena noua in opengl
    if(glutGetWindow() == mainWindow)
        glClearColor(0.0, 0.0, 0.0, 0.0);   
    else
        glClearColor(1.0, 1.0, 1.0, 1.0); fereastra
    glEnable(GL_DEPTH_TEST);            
    glShadeModel(GL_SMOOTH);            
    glEnable(GL_LIGHTING);              
    glEnable(GL_NORMALIZE);             
}

void reshape2(int w,int h){

    glViewport(0,0,(GLsizei) w,(GLsizei) h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45,(float)w/h,1.0,40.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    init();

}

void reshape(int w, int h)
{
    // Main Window
    glViewport(0,0, (GLsizei) w, (GLsizei) h);
    // calculare aspect ratio ( Width/ Height )
    GLfloat aspect = (GLfloat) w / (GLfloat) h;


    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluPerspective(45, aspect, 1.0, 100);

    // init context
    init();

    if(damageWindow != -1)
        glutDestroyWindow(damageWindow);

    damageWindow=glutCreateSubWindow(mainWindow,0,0,w/5,h/5);
    glutDisplayFunc(display);
    glutReshapeFunc(reshape2);
    glutKeyboardFunc(keyboard);
    glutSpecialFunc(keyboard);
    glutKeyboardUpFunc(keyboardup);
    glutMouseFunc(mouse);
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
    int w = 800, h= 600;
    glutInitWindowSize(w,h);
    glutInitWindowPosition(100,100);

    // Main window
    mainWindow=glutCreateWindow("Tema4 - Asteroid Attack!");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutKeyboardUpFunc(keyboardup);
    glutReshapeFunc(reshape);
    glutSpecialFunc(keyboard);
    glutMouseFunc(mouse);


    // Initializeaza scena 3D
    initScene();

    glutMainLoop();
    return 0;
}

好的,这些是重要的功能。在我的键盘功能中,我想切换damageWindow。我怎么做 ?我知道如何摧毁它,但我似乎无法再破坏它。

LE:我一直在投票,因为人们并不真正理解这个问题。键盘功能是多余的,因为那里什么都没有,这就是我问你的问题。但是为了你们这里的人们是:

void keyboard(unsigned char ch,int x,int y){
    switch(ch){
        case 27: exit(0);break;
        case 'n':
            view_subwindow=!view_subwindow;
            if(view_subwindow == false)
                glutDestroyWindow(damageWindow);
            else{
                //here i want to recreate my window DONT KNOW HOW
                damageWindow=glutCreateSubWindow(mainWindow,0,0,w/5,h/5);
                glutDisplayFunc(display);
                glutReshapeFunc(reshape2);
                glutKeyboardFunc(keyboard);
                glutSpecialFunc(keyboard);
                glutKeyboardUpFunc(keyboardup);
                glutMouseFunc(mouse);
            }
    }
}

1 个答案:

答案 0 :(得分:0)

在你打电话给glutMainLoop后,我认为你不应该创建或销毁任何窗口。在初始化期间,您应该创建子窗口,然后您应该使用glutHideWindow隐藏它,可能在调用glutSetWindow后使其成为当前窗口。要再次显示,请致电glutShowWindow

此外,您不需要多次调用glutReshapeFuncglutKeyboardFunc等函数,在初始化期间只执行一次。如果您需要它们根据条件执行不同的操作,请在传递给它们的函数中使用if。