Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Begun learning OpenGL, need help figuring out this problem

Tags:

c++

opengl

So I have begun learning OpenGL, reading from the book "OpenGL Super Bible 5 ed.". It's explains things really well, and I have been able to create my first gl program myself! Just something simple, a rotating 3d pyramid.

Now for some reason one of the faces are not rendering. I checked the vertecies (plotted it on paper first) and it seemed to be right. Found out if I changed the shader to draw a line loop, it would render. However it would not render a triangle. Can anyone explain why?

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);
}
like image 573
Ioncannon Avatar asked Nov 18 '10 22:11

Ioncannon


2 Answers

You've most likely defined the vertices in clockwise order for the triangle that isn't showing, and in counterclockwise order (normally the default) for those that are. Clockwise winding essentially creates an inward facing normal and thus OpenGL won't bother to render it when culling is enabled.

The easiest way to check this is to set glCullFace(GL_FRONT)--that should toggle it so you see the missing triangle and no longer see the other three.

like image 79
Drew Hall Avatar answered Sep 19 '22 16:09

Drew Hall


The only thing I see that affects polygons here is glEnable(GL_CULL_FACE);.
You shouldn't have that, because if you plot your vertices backwards, the polygon won't render.
Remove it or actually call glDisable(GL_CULL_FACE); to be sure.
In your case, it's not likely that you want to draw a polygon that you can see from one side only.

like image 43
BeemerGuy Avatar answered Sep 19 '22 16:09

BeemerGuy