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

前端之家收集整理的这篇文章主要介绍了c – 开始学习OpenGL,需要帮助解决这个问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我开始学习OpenGL,阅读“OpenGL Super Bible 5 ed.”一书.它解释得非常好,我自己也能够创建我的第一个gl程序!简单的东西,一个旋转的三维金字塔.

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

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

 shaderManager.InitializeStockShaders(); 

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

 M3DVector3f vVerts2[] = {-0.5f,0.5f}; 
     M3DVector3f vVerts3[] = {-0.5f,0.5f};  
 M3DVector3f vVerts4[] = {0.5f,-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.5f};
 GLfloat vBlue[] = {0.0f,1.0f,0.5f};
 GLfloat vGreen[] = {0.0f,0.5f};
 GLfloat vWhite[] = {1.0f,0.5f};

 M3DMatrix44f transformMatrix;

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

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

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

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

解决方法

您很可能按顺时针顺序为未显示的三角形定义顶点,并按逆时针顺序(通常为默认值)定义顶点.顺时针缠绕基本上创建了一个向内的法线,因此当启用剔除时,OpenGL不会费心去渲染它.

检查这个的最简单方法是设置glCullFace(GL_FRONT) – 应该切换它,这样你就可以看到缺少的三角形而不再看到其他三个.

猜你在找的C&C++相关文章