开始学习OpenGL,需要帮助解决这个问题

时间:2010-11-18 22:24:23

标签: c++ opengl

所以我开始学习OpenGL,阅读“OpenGL Super Bible 5 ed。”一书。这很好地解释了事情,我已经能够自己创建我的第一个gl程序了!简单的东西,一个旋转的三维金字塔。

现在由于某种原因,其中一个面不会渲染。我检查了椎体(首先在纸上绘制)并且看起来是正确的。如果我更改着色器以绘制线环,则会发现它会渲染。但是它不会呈现三角形。任何人都可以解释原因吗?

void setupRC()
{
 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

 shaderManager.InitializeStockShaders(); 

 M3DVector3f vVerts1[] = {-0.5f,0.0f,-0.5f,0.0f,0.5f,0.0f,0.5f,0.0f,-0.5f}; 

 M3DVector3f vVerts2[] = {-0.5f,0.0f,-0.5f,0.0f,0.5f,0.0f,-0.5f,0.0f,0.5f}; 
     M3DVector3f vVerts3[] = {-0.5f,0.0f,0.5f,0.0f,0.5f,0.0f,0.5f,0.0f,0.5f};  
 M3DVector3f vVerts4[] = {0.5f,0.0f,0.5f,0.0f,0.5f,0.0f,0.5f,0.0f,-0.5f};  



 triangleBatch1.Begin(GL_LINE_LOOP, 3);
 triangleBatch1.CopyVertexData3f(vVerts1); 
 triangleBatch1.End();

 triangleBatch2.Begin(GL_TRIANGLES, 3);
 triangleBatch2.CopyVertexData3f(vVerts2); 
 triangleBatch2.End();
 triangleBatch3.Begin(GL_TRIANGLES, 3);
 triangleBatch3.CopyVertexData3f(vVerts3); 
 triangleBatch3.End();
 triangleBatch4.Begin(GL_TRIANGLES, 3);
 triangleBatch4.CopyVertexData3f(vVerts4); 
 triangleBatch4.End();

 glEnable(GL_CULL_FACE);
}

float rot = 1;

void renderScene()
{
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

 GLfloat vRed[] = {1.0f, 0.0f, 0.0f, 0.5f};
 GLfloat vBlue[] = {0.0f, 1.0f, 0.0f, 0.5f};
 GLfloat vGreen[] = {0.0f, 0.0f, 1.0f, 0.5f};
 GLfloat vWhite[] = {1.0f, 1.0f, 1.0f, 0.5f};

 M3DMatrix44f transformMatrix;

 if (rot >= 360)
  rot = 0;
 else
  rot = rot + 1;

 m3dRotationMatrix44(transformMatrix,m3dDegToRad(rot),0.0f,1.0f,0.0f); 

 shaderManager.UseStockShader(GLT_SHADER_FLAT, transformMatrix, vRed);
 triangleBatch1.Draw();
 shaderManager.UseStockShader(GLT_SHADER_FLAT, transformMatrix, vBlue);
 triangleBatch2.Draw();
 shaderManager.UseStockShader(GLT_SHADER_FLAT, transformMatrix, vGreen);
 triangleBatch3.Draw();
 shaderManager.UseStockShader(GLT_SHADER_FLAT, transformMatrix, vWhite);
 triangleBatch4.Draw();

 glutSwapBuffers();
 glutPostRedisplay();
 Sleep(10);
}

2 个答案:

答案 0 :(得分:6)

我看到的唯一影响多边形的是glEnable(GL_CULL_FACE); 你不应该这样,因为如果你向后绘制顶点,多边形将不会渲染 删除它或实际调用glDisable(GL_CULL_FACE);以确定。
在您的情况下,您不太可能想要绘制一个只能从一侧看到的多边形。

答案 1 :(得分:6)

您最有可能以顺时针顺序为未显示的三角形定义顶点,并按逆时针顺序(通常为默认值)定义顶点。顺时针缠绕基本上创建了一个向内的法线,因此当启用剔除时,OpenGL将无需渲染它。

检查此问题的最简单方法是设置glCullFace(GL_FRONT) - 应该切换它,以便您看到缺少的三角形,不再看到其他三个。