2012-02-06 87 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