[GL Light] No light on backside

Problems with Canvas, OpenGL, etc...

[GL Light] No light on backside

Postby benny! » Sun Dec 12, 2010 12:03 pm

Hi,

I am trying to add a light source to one of my application - but the result it quite strange. I tried to cut down the problem without any luck. Maybe anyone of this forum could help.

For better understanding I created a small 3d application (used nehe lesson for that) in order to focus the problem. The project consists of two simple classes with one triangle (not rotating) and one quad which rotates.

I placed a light into the scene. First all looks ok:

- Both front faces of triangle and quad are lighted
- The rotating quad gets darker which also seems to be ok
- When the background of the quad gets into the scene the light does not seem to work anymore since it remains dark grey ?

Here are the relevant code bits:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ...
  2.         public void onDrawFrame(GL10 gl) {
  3.                 gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
  4.                
  5.                 gl.glMatrixMode(GL10.GL_MODELVIEW);
  6.                 gl.glLoadIdentity();
  7.                
  8.                 // Testing LIGHT
  9.                 //
  10.         gl.glEnable(GL10.GL_LIGHTING);
  11.         gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, matAmbient, 0);
  12.         gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, matDiffuse, 0);
  13.         gl.glEnable(GL10.GL_LIGHT0);
  14.        
  15.         gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, lightAmbient,     0);
  16.         gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightDiffuse,     0);
  17.         gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPos, 0);           
  18.                
  19.                 // draw triangle
  20.                 gl.glTranslatef(-1.5f, 0, -6);
  21.                 gl.glRotatef(triangleRot, 0, 1, 0);
  22.                 gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  23.                 gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
  24.                 gl.glVertexPointer(3, GL10.GL_FLOAT, 0, triangleVertexBfr);
  25.                 gl.glColorPointer(4, GL10.GL_FLOAT, 0, triangleColorBfr);
  26.                 gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3);
  27.                 gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
  28.                 gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
  29.                
  30.                 // draw quad
  31.                 gl.glLoadIdentity();
  32.                 gl.glTranslatef(1.5f, 0, -6);
  33.                 gl.glRotatef(quadRot, 1, 0, 0);
  34.                 gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
  35.                 gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  36.                 gl.glVertexPointer(3, GL10.GL_FLOAT, 0, quadVertexBfr);
  37.                 gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
  38.                 gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
  39.                
  40.                 // update rotations
  41.                 //triangleRot += 0.8f;
  42.                 quadRot -= 0.5f;
  43.         }
  44. ...
  45.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Any ideas ?

// Edit: I attached the exported Eclipse project if anyone want to test it out.
Attachments
testgl.zip
Eclipse project of test (Android1.6)
(46.18 KiB) Downloaded 99 times
User avatar
benny!
Experienced Developer
Experienced Developer
 
Posts: 69
Joined: Tue Nov 27, 2007 3:36 pm
Location: end of www

Top

Re: [GL Light] No light on backside

Postby moblcade.com » Mon Dec 13, 2010 4:12 pm

You probably haven't set up your normals properly.
moblcade.com
Junior Developer
Junior Developer
 
Posts: 19
Joined: Tue Sep 07, 2010 6:31 pm

Re: [GL Light] No light on backside

Postby benny! » Mon Dec 13, 2010 4:39 pm

That's a good point. Totally forget about normals. Guess I need to do some research on glNormalPointer() etc.

I will have a look at it later this evening. Thanks!
User avatar
benny!
Experienced Developer
Experienced Developer
 
Posts: 69
Joined: Tue Nov 27, 2007 3:36 pm
Location: end of www

Re: [GL Light] No light on backside

Postby benny! » Tue Dec 14, 2010 7:36 am

Hmmm ... I added
Code: Select all
gl.glEnable( GL10.GL_NORMALIZE );

to my setup code - but this does not change anything. In addition I played around with setting the normals
manually using glNormal3d(...) - also without any luck.
Is there anything I need to care about when rendering the back side of a mesh while using lights?
User avatar
benny!
Experienced Developer
Experienced Developer
 
Posts: 69
Joined: Tue Nov 27, 2007 3:36 pm
Location: end of www

Re: [GL Light] No light on backside

Postby moblcade.com » Tue Dec 14, 2010 3:48 pm

When you tell opengl to normalize for you, you must also make sure that you're defining your polygons' vertices in the correct winding -- clockwise or counter-clockwise. Follow the link to learn about vertex winding: http://pyopengl.sourceforge.net/documentation/manual/glFrontFace.3G.html
moblcade.com
Junior Developer
Junior Developer
 
Posts: 19
Joined: Tue Sep 07, 2010 6:31 pm

Re: [GL Light] No light on backside

Postby benny! » Wed Dec 15, 2010 8:18 am

@moblcade.com
Thanks for your answer. glFrontFace() wasnt the key. The solution was to set the LightModel to GL10.GL_LIGHT_MODEL_TWO_SIDE. Did not know that something like this exists...

Anyway, I added the following line and now it works like a charme. Thanks for you assistance!

Code: Select all
gl.glLightModelf( GL10.GL_LIGHT_MODEL_TWO_SIDE, GL10.GL_TRUE );
User avatar
benny!
Experienced Developer
Experienced Developer
 
Posts: 69
Joined: Tue Nov 27, 2007 3:36 pm
Location: end of www

Top

Re: [GL Light] No light on backside

Postby moblcade.com » Wed Dec 15, 2010 9:38 pm

What you told it was to ignore the winding and light both sides of the polygon. This does work but it is wasteful because you're drawing polygons that are facing away from the viewer. Most of the time these polygons wouldn't be seen anyway because normally things that you're displaying to the user are closed, meaning you'll only ever see the outside face of any polygon UNLESS the viewer is inside the object. How often in a game do you view the world from inside a tree or a bear or any other model? Probably never. Enabling two-sided rendering will reduce your rendering performance nearly by half. And handheld devices don't have that much performance to spare. If you find that your renderer is performing poorly, I'd go back and disable two-sided rendering, pick a winding order (clockwise or counter-clockwise) then make sure your polygons are being defined according to that order so that their normals are facing the right direction.
moblcade.com
Junior Developer
Junior Developer
 
Posts: 19
Joined: Tue Sep 07, 2010 6:31 pm

Re: [GL Light] No light on backside

Postby benny! » Thu Dec 16, 2010 7:16 am

Yup. You are of course right. The GL_LIGHT_MODEL_TWO_SIDE would not make much sense in daily game development. However, I was just curious. I had that example and could not figure out how to make the light work on the back of a simple plane. Glad that the riddle is solved now ;-)
Thanks again for your assistance.
User avatar
benny!
Experienced Developer
Experienced Developer
 
Posts: 69
Joined: Tue Nov 27, 2007 3:36 pm
Location: end of www

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 2 guests