Light source just can't be still.

Tutorials concerning the OpenGL® ES cross-platform API for full-function 2D and 3D graphics on the Google-Android platform.

Light source just can't be still.

Postby Per-Erik » Sun Nov 15, 2009 5:26 pm

Hi!

I'm pretty new to this forum and this is my first post. I'm trying to do a 3D scene with OpenGL ES and have a spinning mesh on the screen. I added a light that I would like to have fixed to light up the mesh. My problem is that the light keeps spinning with the mesh. Anyone know the secret to get fixed light soures?

My code kinda look like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  3.  
  4.         gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
  5.  
  6.         gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, lightAmbientBuffer);
  7.  
  8.         gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightDiffuseBuffer);
  9.  
  10.         gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPositionBuffer);
  11.  
  12.         gl.glEnable(GL10.GL_LIGHT0);
  13.  
  14. }
  15.  
  16.  
  17.  
  18. public void onDrawFrame(GL10 gl) {
  19.  
  20.         gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); //
  21.  
  22.         gl.glLoadIdentity();
  23.  
  24.  
  25.  
  26.         gl.glPushMatrix();
  27.  
  28.         gl.glTranslatef(0, 0, -8f);
  29.  
  30.         gl.glRotatef(angle, 1, 1, 0);
  31.  
  32.         mesh.draw(gl);
  33.  
  34.         gl.glPopMatrix();
  35.  
  36.         angle += 3;
  37.  
  38. }
  39.  
  40.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
Per-Erik
Freshman
Freshman
 
Posts: 6
Joined: Wed Oct 28, 2009 10:13 am
Location: Malmö, Sweden

Top

Postby Shaka » Mon Nov 16, 2009 4:50 pm

Hiya!

How are your normals set up for the mesh?

Regards/Per
Shaka
Developer
Developer
 
Posts: 44
Joined: Fri Aug 28, 2009 3:01 pm

Postby Per-Erik » Mon Nov 16, 2009 5:24 pm

Shaka wrote:Hiya!

How are your normals set up for the mesh?

Regards/Per


Argh... I haven't set them up. Is there a automatic way for that or is it manual hacking?
Per-Erik
Freshman
Freshman
 
Posts: 6
Joined: Wed Oct 28, 2009 10:13 am
Location: Malmö, Sweden

Postby Shaka » Tue Nov 17, 2009 12:58 pm

Lo!

Are you creating the mesh yourself or loading it from some type of model file? Model files normally contain normals (afaik). Otherwise it's good oldé hacking that awaits. I'm fairly new to opengl but I assume you also have to update them every frame depending on the orientation of the faces.
Shaka
Developer
Developer
 
Posts: 44
Joined: Fri Aug 28, 2009 3:01 pm

Postby zorro » Wed Dec 16, 2009 4:29 pm

Normals do not need to be computed/updated every frame. They are computed or readed as startup and then at render time they are just used. The orientation of the normals is automatically done because they are multiplied by the modelview matrix (the same used to compute the vertices positions). You must recompute them only if the mesh changes.

Side note, not related with your code: if your mesh is rendered scaled (if you use glScalef in some sort of way before rendering the object) then you must know that the normals get scaled as well, which is not good, because they get un-normalized. To prevent that, you can call at startup glEnable(GL_NORMALIZE) to force OpenGL to automatically normalize normals at render time. Otherwise the lighting effects on scaled object look and behave weird. If you are not using scaled objects, there is no need to enable the auto-normalization.

About the code: the posted functions are complete? You enable GL_LIGHT0 but this is not enough. You must also enable GL_LIGHTING. To get rid of the rotation effect, try putting gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPositionBuffer); before mesh.draw() in onDrawFrame().
UPDATE Sorry, i wanted to say to put it right after glLoadIdentity().

Other thing which is not related to lighting is that between glLoadModelview and mesh.draw() you are not setting up the projection matrix to obtain the 3d perspective effect.
User avatar
zorro
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Mon Aug 10, 2009 3:11 pm
Location: Romania

Top

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

Who is online

Users browsing this forum: No registered users and 5 guests