2012-02-06 91 views
0

首先,我想說明我在這裏所做的只是試圖測試我的OpenGL知識的能力,僅此而已。引擎測試:在屏幕上渲染立方體(OpenGL)

以下代碼表示一個無限循環,它可以在屏幕上呈現立方體,然後將其保留在靜態投影中。無論出於何種原因,它都拒絕展示。最重要的是,我沒有使用白色背景色,而是使用黑色背景色。

代碼

MainLoop語句

const int FIELD_OF_VIEW_Y = 60; 

     glMatrixMode(GL_PROJECTION); 
     glLoadIdentity(); 

     gluPerspective(FIELD_OF_VIEW_Y, 640.0/480.0, 1.0, 1028); 

     glMatrixMode(GL_MODELVIEW); 

     glTranslatef(0.0, 0.0, -2.0f); 

     Vector3f cam(0, 0, 0); 

     QPoint3F position(3, 3, 3); 

     const double CUBE_SIZE = 1; 

     while(true) 
     { 
      while(SDL_PollEvent(mEvent)) 
      { 
       TestCubeRender(CUBE_SIZE, cam, position); 
       mControls->DoKeyHandling(mEvent); 
       mCamera->ReceiveInput(mEvent->key); 
       mCamera->UpdateCamera(); 
      } 
     } 

TestCubeRender

void TestCubeRender(const double size, const Vector3f& cameraPos, const QPoint3F& position) 
    { 
     glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); 

     glClearColor(1, 1, 1, 1); 

     int x1 = 0, x2 = 0; 
     int y1 = 0, y2 = 0; 
     int z1 = 0, z2 = 0; 

     x1 = (position.x() - size); 
     x2 = position.x() + size; 

     y1 = (position.y() - size); 
     y2 = position.y() + size; 

     z1 = (position.z() - size); 
     z2 = position.z() + size; 

     qreal camZ = cameraPos.Z; 
     qreal camY = cameraPos.Y; 
     qreal camX = cameraPos.X; 

     glMatrixMode(GL_PROJECTION); 

     //glOrtho(0.0, 0.0, 0.0, 0.0, 0.0, 0.0); 

     glMatrixMode(GL_MODELVIEW); 
     glPushMatrix(); 

     //glTranslatef(0.0, 0.0, -10.0); 

     /* 
     * if axis > mCamera.axis; then normal3f -1.0 on the axis. 
     *  axis point is axis1 
     * else if axis < mCamera.axis; then nomral3f 1.0 on the axis. 
     *  axis point is axis2 
     */ 

     /* Pattern: one axis is written twice, with the other variant of the same axis written twice as well, then the 
     * order at which those axes are written is flipped, yet still following the same pattern. 
     * Pattern2: one type of axis is still written twice, and the other group of the same axis written twice as well, 
     * the only difference being that the first group is written once, with the next group written twice, and then that 
     * first group written once again. The third axis type is written using one type, depending on the camera comparisons. 
     */ 

     glBegin(GL_QUADS); 

     glColor4f(1.0, 0, 1, 1); 

     if (z1 <= camZ) 
     { 
      glNormal3f(0.0, 0.0, -1.0); 
      glVertex3i(x1, y1, z1); //1 
      glVertex3i(x1, y2, z1); //1 
      glVertex3i(x2, y2, z1); //1 
      glVertex3i(x2, y1, z1); //1 
     } 

     if (z2 >= camZ) 
     { 
      glNormal3f(0.0, 0.0, 1.0); 
      glVertex3i(x2, y1, z2); //2 
      glVertex3i(x2, y2, z2); //2 
      glVertex3i(x1, y2, z2); //2 
      glVertex3i(x1, y1, z2); //2 
     } 

     glColor4f(0, 1, 0, 1); 

     if (y1 >= camY) 
     { 
      glNormal3f(0.0, -1.0, 0.0); 
      glVertex3i(x2, y1, z1); 
      glVertex3i(x2, y1, z2); 
      glVertex3i(x1, y1, z2); 
      glVertex3i(x1, y1, z1); 
     } 

     if (y2 <= camY) 
     { 
      glNormal3f(0.0, 1.0, 0.0); 
      glVertex3i(x1, y2, z1); 
      glVertex3i(x1, y2, z2); 
      glVertex3i(x2, y2, z2); 
      glVertex3i(x2, y2, z1); 
     } 

     glColor4f(1, 0, 0, 1); 

     if (x1 >= camX) 
     { 
      glNormal3f(-1.0, 0.0, 0.0); 
      glVertex3i(x1, y1, z1); 
      glVertex3i(x1, y1, z2); 
      glVertex3i(x1, y2, z2); 
      glVertex3i(x1, y2, z1); 
     } 

     if (x2 <= camX) 
     { 
      glNormal3f(1.0, 0.0, 0.0); 
      glVertex3i(x2, y2, z1); 
      glVertex3i(x2, y2, z2); 
      glVertex3i(x2, y1, z2); 
      glVertex3i(x2, y1, z1); 
     } 

     glEnd(); 

     glPopMatrix(); 
    } 

我已經調試和調試這很多很多次。我知道值應該至少顯示東西。如果我甚至可以看到我的背景顏色變化,我至少知道正在做某件事。 我該怎麼辦?

+1

您確定要交換窗口上的緩衝區嗎? – Lalaland 2012-02-06 05:54:36

+1

你的'SDL_GL_SwapBuffers()'調用在哪裏? – genpfault 2012-02-06 06:13:07

回答

2

缺少兩件事:將視口(glViewport)設置爲窗口內部大小。渲染後交換緩衝區(SDL_GL_SwapBuffers)。

+0

默認情況下,glViewport將覆蓋已創建OpenGL上下文的所有窗口區域。 – 2012-02-06 18:12:47

+0

僅限第一次調整大小。當第一次將OpenGL上下文附加到drawable時(並且僅在此時)纔會設置初始視口尺寸。窗口大小的後續更改必須通過適當調用glViewport來反映。 – datenwolf 2012-02-06 18:17:41

+0

當然。但這並不能解釋爲什麼沒有顯示出來。 – 2012-02-06 19:34:09