I’m an experienced C++ programmer tying to learn OpenGL. Among others, I’m reading Richard Wright’s OpenGL Super Bible. The support libraries there are hacker-head C, loosely translated into C++. Being a bit of a purist, I’ve translated most of the support code into C++. And I’m working in QT rather than Glut. I’ve gotten the examples from Chapter 2 and the “Primitives” example from Chapter 3 working, but can’t get the “GeoTest” example to work right. Here’s a simplified example of the problem:
void geotest::paintGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_CULL_FACE);
model_view_matrix.push(view_frame);
float red[] = { 1.0f, 0.0f, 0.0f, 1.0f };
shader.use(
transform_pipeline.get_model_view_matrix(),
transform_pipeline.get_projection_matrix(),
red);
glBegin(GL_TRIANGLES);
glVertex3f(-1.2f, -1.2f, 0.0f);
glVertex3f( 1.2f, -1.2f, 0.0f);
glVertex3f( 1.2f, 1.2f, 0.0f);
glEnd();
model_view_matrix.pop();
}
The shader is my adaptation of the book’s GLT_SHADER_DEFAULT_LIGHT. I’ve cut down the drawing to isolate the problem. When I run this code I get a seemingly blank window. The code that I haven’t shows provides support for rotating the image, and when I rotate it 180 degrees around the x or y axis I see the other side of the triangle, but it’s black instead of red. So it looks like the surface normal is initially pointing away from the viewer, so it gets culled.
I’ve tried adding 1 to 3 calls to glNormal3f(0.0f, 0.0f, -1.0f) (and 1.0f) everywhere I could imagine between the glBegin and glEnd calls, with no effect that I could see; the triangle is always drawn with its back face showing.
Am I missing something fundamental about normals? I’m pretty sure that the shader is okay (I get the same problem with the normal if I use the book’s GLT_SHADER_FLAT, although when the triangle appears it’s red, as it should be).
EDIT here’s the code for the shader:
const char *default_light_vp = "uniform mat4 mvMatrix;"
"uniform mat4 pMatrix;"
"varying vec4 vFragColor;"
"attribute vec4 vVertex;"
"attribute vec3 vNormal;"
"uniform vec4 vColor;"
"void main(void) { "
" mat3 mNormalMatrix;"
" mNormalMatrix[0] = mvMatrix[0].xyz;"
" mNormalMatrix[1] = mvMatrix[1].xyz;"
" mNormalMatrix[2] = mvMatrix[2].xyz;"
" vec3 vNorm = normalize(mNormalMatrix * vNormal);"
" vec3 vLightDir = vec3(0.0, 0.0, 1.0); "
" float fDot = max(0.0, dot(vNorm, vLightDir)); "
" vFragColor.rgb = vColor.rgb * fDot;"
" vFragColor.a = vColor.a;"
" mat4 mvpMatrix;"
" mvpMatrix = pMatrix * mvMatrix;"
" gl_Position = mvpMatrix * vVertex; "
"}";
const char *default_light_fp =
#ifdef OPENGL_ES
"precision mediump float;"
#endif
"varying vec4 vFragColor; "
"void main(void) { "
" gl_FragColor = vFragColor; "
"}";
Culling isn’t based on the direction of the normals, it’s based on the winding order. Try switching the direction that you are drawing your vertices, and see if that makes a difference. To change the winding order, you can use
glFrontFacewithGL_CWandGL_CCW, which defines which face is front and which is back.