PDA

View Full Version : problem with esGenSphere?



teapot
06-24-2010, 07:23 PM
I'm having a problem trying to do lighting with the esGenSphere code that's provided as sample code for the OpenGL ES 2.0 Programming Guide (purple book). Downloaded the source (v1.0.2), and am trying to generate the sphere and light it with diffuse lighting. I'm doing Phong shading, so am passing a varying normal to the fragment shader. The sphere is drawn with GL_TRIANGLE_STRIP. The problem is that half the sphere is lit incorrectly http://i48.tinypic.com/21oa1hj.png . If I turn backface culling on, I would expect every other triangle to get culled, which would be normal for a triangle strip. But I only see the culling in the half of the sphere that has problems with the lighting http://i48.tinypic.com/15xmud5.png. I've rotated the sphere slightly around the y axis to see that the culling issue gets rotated (so it seems to be a problem with the geometry, and not my shader). But I'm wondering why - In the image with backface culling on, the triangles that don't get culled get lit correctly in both halves. With backface culling off, all triangles in that half don't get lit correctly...???!! How is that happening? Is something still wrong with my shader? Thanks for your help!

static const gchar *fragment_shader = \
"varying vec3 fcolor;\n" \
"varying vec4 N;\n" \
"varying vec4 transV;\n" \
"void main()\n" \
"{\n" \
" float shininess = 10.0;\n" \
" vec3 lightDiffuse = vec3(1.0, 1.0, 1.0);\n" \
" vec4 lightDir = vec4(0, 0, -500, 0.);\n" \
" vec3 L = normalize(vec3(lightDir-transV));\n" \
" float ndotl = max(0.0, dot(vec3(N), L));\n" \
" gl_FragColor = vec4(fcolor*.9, 1.0) + vec4(fcolor*ndotl*lightDiffuse, 1.0);\n" \
"}\n";

static const gchar *vertex_shader = \
"uniform mat4 u_projMatrix;\n" \
"uniform mat4 u_transMatrix;\n" \
"\n" \
"attribute vec3 vtxnormal;\n" \
"attribute vec3 vtxpos;\n" \
"attribute vec3 vtxcolor;\n" \
"\n" \
"varying vec3 fcolor;\n" \
"varying vec4 N;\n" \
"varying vec4 transV;\n" \
"\n" \
"void main()\n" \
"{\n" \
" N = normalize(u_transMatrix * vec4(vtxnormal, 0.0));\n" \
" transV = u_transMatrix * vec4(vtxpos, 1.0);\n" \
" gl_Position = u_projMatrix * transV;\n" \
" fcolor = vtxcolor;\n" \
"}\n";

teapot
06-24-2010, 07:34 PM
oh - I forgot to add - I know I'm not transforming the normal in the vertex shader by the inverse transpose of the modelview matrix. My modelview matrix only has rotation, so the normal can just be multiplied by the the matrix itself.

teapot
06-25-2010, 01:52 PM
Figured it out...

I hadn't enabled depth buffer, so it was using painter's alg. D'oh!