2013-03-10 54 views
2

我必須有一個使用兩個視口的OpenGL程序。在其中之一,我必須有一架直升機的模型,帶有一臺工作相機,可以在每個方向旋轉。在我決定讓視口分割之前,我讓相機在模型上工作,因爲它具有更高的優先級。另一個視口應該顯示FPS計數器,但由於某種原因,我只能從特定角度看到文本。OpenGL中的多個視口和文本...也許它是相機?

我的代碼如下:

//sets up the viewports 
    void setView(int option) 
    { 
     glMatrixMode(GL_PROJECTION); 
     glLoadIdentity(); 

     switch (option) 
     { 
      case 1: 
       glViewport(0,height-150,width,150); 
       break; 
      case 2: 
       glViewport(0,0,width,height-150); 
       break; 
     } 
     if(persp) 
      gluPerspective(fovy, (GLfloat) width /(GLfloat) height , near, far); 
     else 
      glOrtho(left,right,bottom,top,-10,10); 

     glMatrixMode(GL_MODELVIEW); 
     glLoadIdentity(); 
     myCam.updateLook(); 
    } 

    void writeBitmapString(void *font, char *string) 
    { 
     char *c; 
     for (c = string; *c != '\0'; c++) glutBitmapCharacter(font, *c); 
    } 

    //main draw function 
    void display(void) 
    { 
     //Text portion of the window 
     setView(1); 
     glColor3f(1.0, 0.0, 0.0); 
     glRasterPos3f(0, 0, 0.0); 
     writeBitmapString((void*)font, "Test text"); 
     glutSwapBuffers(); 

     //Viewport for heli 
     setView(2); 
     glEnable(GL_DEPTH_TEST); 
     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);     
     glPushMatrix(); 
    //....The rest is just the modelling stuff 
    glPopMatrix(); // final pop clause 
    glFlush(); 
    glutSwapBuffers(); 

任何幫助將不勝感激。

+0

myCam.updateLook()是做什麼的? – Aeluned 2013-03-10 02:12:18

+0

它調用gluLookAt函數並設置指向斬波器中心的點。它還處理所有相機的新定位。相機的位置由IJKL鍵修改。 – Galipan 2013-03-10 14:48:03

+0

@加利潘:你爲一個教程而倒下SNAFU的人傾向於一次又一次:在渲染功能之外設置投影和視口。將該代碼移動到繪圖代碼中,事情變得明顯起來。 – datenwolf 2013-03-10 18:40:41

回答

1

這真的聽起來像你想在這裏的HUD(原諒我雙重猜測你的意圖)。渲染你的場景,然後在模型視圖&投影矩陣上做一個glLoadIdentity。然後加載你的glOrtho。最後,爲您的文本和渲染設置光柵位置。