2013-05-20 92 views
-1

我有一个程序,我从我的主窗口创建了一个子窗口。问题在于它总是在那里,我的目标是按下一个键,然后将该子窗口“打开”,然后再次按下时,使其消失。我设法用glutDistroyWindow摧毁它,但后来我不知道如何让它再次出现。这是我的代码:Glut子窗口切换

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:我不断收到downvotes,因为人们并不真正理解这个问题。键盘功能是多余的,因为什么也没有,那是什么IM要求you.But为你们在这里它的缘故:

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

-1,代码不完整。 – genpfault

回答

0

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

此外,您不需要多次调用诸如glutReshapeFuncglutKeyboardFunc之类的函数,只需在初始化期间执行一次即可。如果您需要他们根据条件做不同的事情,请在传递给他们的功能中使用if。