2012-12-28 38 views
0

我有一個問題,我使用過量(openGL實用程序工具包)。我正在一個主窗口中創建一個子窗口。主窗口顯示一個簡單的圖形,子窗口顯示另一個視圖的圖形。該圖旋轉,因此應始終重新顯示主窗口和子窗口。OpenGL子窗口不會同時響應

但是隻有一個顯示旋轉圖。因此,當我啓動程序時,主窗口中的圖形旋轉,但在子窗口中不旋轉,它只是靜止不動。

當我在子窗口中移動鼠標並按任意鍵時,角色發生變化,圖形在子窗口中旋轉,並在主窗口中靜止。

我該如何讓它們同時顯示。我跟着燈塔的教程,但沒有給我答案。 我必須對我的視口做些什麼嗎?

* GLUT Shapes Demo 
* 
* Written by Nigel Stewart November 2003 
* 
* This program is test harness for the sphere, cone 
* and torus shapes in GLUT. 
* 
* Spinning wireframe and smooth shaded shapes are 
* displayed until the ESC or q key is pressed. The 
* number of geometry stacks and slices can be adjusted 
* using the + and - keys. 
*/ 
#include <windows.h> 
#ifdef __APPLE__ 
#include <GLUT/glut.h> 
#else 
#include <GL/glut.h> 
#endif 
#include <math.h> 
#include <stdlib.h> 

static int slices = 16; 
static int stacks = 16; 
int m=0; 


int mainWindow,SubWindow, SubWindow2; 

/* GLUT callback Handlers */ 

static void resize(int width, int height) 
{ 
    const float ar = (float) width/(float) height; 

    glViewport(0, 0, width, height); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    glOrtho(-10,10,-10,10,-10,10); 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity() ; 
} 

static void key(unsigned char key, int x, int y) 
{ 
    switch (key) 
    { 
     case 27 : 
     case 'q': 
      exit(0); 
      break; 

     case '+': 
      slices++; 
      stacks++; 
      break; 

     case '-': 
      if (slices>3 && stacks>3) 
      { 
       slices--; 
       stacks--; 
      } 
      break; 
    } 


    //glutPostRedisplay(); 
} 
void keyp(int key, int xx, int yy) { 


    glutSetWindow(mainWindow); 
    glutPostRedisplay(); 

} 

void displaysub() 
{const double t = glutGet(GLUT_ELAPSED_TIME)/1000.0; 
    const double a = t*90.0; 

    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); 
    glClearColor(20,1,1,1); 
    glLoadIdentity(); 
    glOrtho(-5,5,-5,5,-5,5); 
    glColor3f(0,0,0); 
glRotated(a,0,0,10); 
    glPushMatrix(); 
     glTranslated(0,0,0); 
     glutSolidSphere(2,10,10); 
    glPopMatrix(); 

    glutSwapBuffers(); 
} 

void display() 
{const double t = glutGet(GLUT_ELAPSED_TIME)/1000.0; 
    const double a = t*90.0; 
     glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); 
    glClearColor(3,0,0,1); 
glLoadIdentity(); 
    glOrtho(-10,10,-10,10,-10,10); 
    glRotated(a,10,10,0); 


displaysub(); 


} 

static void idle(void) 
{ 
    glutPostRedisplay(); 
} 

/* Program entry point */ 
void init() 
{ 


    glClearColor(3,0,0,1); 
    glEnable(GL_DEPTH_TEST); 
    glEnable(GL_CULL_FACE); 

    // register callbacks 
    glutIgnoreKeyRepeat(1); 
    glutKeyboardFunc(key); 
    glutSpecialFunc(keyp); 

} 



int main(int argc, char *argv[]) 
{ 
    glutInit(&argc, argv); 
    glutInitWindowSize(640,480); 
    glutInitWindowPosition(10,10); 
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); 


    mainWindow = glutCreateWindow("GLUT Shapes"); 
     glutSetWindow(mainWindow); 
     glClearColor(3,0,0,1); 
     glutDisplayFunc(display); 
     init(); 


    SubWindow = glutCreateSubWindow(mainWindow,0,0,50,50); 
     glutSetWindow(SubWindow); 
     glClearColor(3,0,0,1); 
     glutDisplayFunc(displaysub); 
     init(); 



    glutIdleFunc(idle); 
    glutMainLoop(); 

    return 1; 
} 
+2

請向我們顯示您的代碼。 – datenwolf

+0

我已編輯代碼 – abcdef

回答

6

glutPostRedisplay上的文檔指定只在當前窗口的顯示FUNC將被調用。在這種情況下,有兩個窗口。我使用的過剩,但我建議兩個轉變

display()功能刪除displaysub()和重寫idle()

static void idle() 
{ 
    int currentWindow = glutGetWindow(); 
    glutSetWindw(mainWindow); 
    glutPostRedisplay(); 
    glutSetWindw(subWindow); 
    glutPostRedisplay(); 
    glutSetWindow(currentWindow); 
} 

glutPostRedisplay只是標誌着主循環用於更新的窗口,我的猜測是不是專家一個與鼠標焦點。通過爲每個窗口發佈獨立於當前窗口的信息,所有窗口都將收到各自的顯示呼叫

+0

謝謝,它的作品!但爲什麼它有效?你是否更新主窗口中的子窗口? – abcdef

+1

@abcdef我相應地編輯了答案 –