Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 3 123 LastLast
Results 1 to 10 of 23

Thread: How to fix light position with rotating cube

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2010
    Location
    CA
    Posts
    13

    How to fix light position with rotating cube

    This is the section of the code:

    gl.glEnable(GL10.GL_LIGHTING);
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    gl.glLoadIdentity();
    gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPositionBuffer);

    gl.glTranslatef(0.0f, -1.2f, -10);And Into The Screen 6.0
    gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
    gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);

    model.draw(gl); // Draw the cube
    With the above code, the surface shade continuously changes as the cube is being rotated but not the way I was expecting. As an example, the top surface continuously change the color from completel black to complete white gradually as the cube rotates on the y-axis.

    If I move glLightfv() to just before draw(), then as expected, the surface doesn't change at all during rotation since the light position receives the same transformation as the scene itself.

    What am I doing wrong?

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    943

    Re: How to fix light position with rotating cube

    If you intend to keep the illumination of the cube constant (and I expect you do) then moving glLightfv() after the calls which (in this case) set up the view matrix (glTranslate() etc.) is correct.

    The explanation for the variable illumination in you first scenario is simple: you're not transforming the light source position at all since right before invoking glLightfv you're setting the model-view matrix to identity. Therefore the light will be fixed in view space and the rest of the scene is transformed. As a result, the illumination of the cube changes.

    HTH.

  3. #3
    Junior Member Newbie
    Join Date
    Nov 2010
    Location
    CA
    Posts
    13

    Re: How to fix light position with rotating cube

    Thanks for the response.

    Actually what I want to do eventually is to lit a room and have objects move around. The light will stay fixed in relation to the room. Cube here is one of the object. So in my case, I don't want to see the cube illumination stay constant as it moves around.

  4. #4
    Junior Member Newbie
    Join Date
    Nov 2010
    Location
    CA
    Posts
    13

    Re: How to fix light position with rotating cube

    Here's another object in the scene. I have OBJ loader running and imported a sphere from Maya. The object has been triangulated as my app runs on Android which is a OpenGL ES.

    I get strange bands of shades and if I continue to rotate the cube, I see this discontinuity as seen here:

    https://picasaweb.google.com/1038015...86093013529666

    Could normals imported from Maya been handled wrong?

  5. #5
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    943

    Re: How to fix light position with rotating cube

    Hmm, that link doesn't work for me.

  6. #6
    Junior Member Newbie
    Join Date
    Nov 2010
    Location
    CA
    Posts
    13

    Re: How to fix light position with rotating cube

    Looks like the permission was set wrong. Can you view it now?

  7. #7
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    943

    Re: How to fix light position with rotating cube

    Nope, it reports: "Page not found!".

  8. #8
    Junior Member Newbie
    Join Date
    Nov 2010
    Location
    CA
    Posts
    13

    Re: How to fix light position with rotating cube


  9. #9
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    943

    Re: How to fix light position with rotating cube

    Well, that looks like faulty geometry to me. I guess you're rendering a sphere which is supposed to be lit smoothly? I've never worked with OpenGL on mobile devices, but isn't GLSL supported to some extent? If so, you could visualize the normals and discover discontinuities quite easily.

  10. #10
    Junior Member Newbie
    Join Date
    Nov 2010
    Location
    CA
    Posts
    13

    Re: How to fix light position with rotating cube

    Actually the goal is to be able to use any objected created within Maya to be used within my app. This particular model was also created from Maya, exported into OBJ and parsed back into my app. In this OBJ file, all normals are already defined. I don't need to calculate normals manually.

    I wrote the import code with some help from other samples.

    Here's some more illustrations.

    Here's the screen shot from Maya. It shows a model of a fish with normals displayed at each vertex.

    https://lh4.googleusercontent.com/-Q.../fish_maya.png

    And this is the result on my Android phone:

    https://lh3.googleusercontent.com/-p...sh_android.png

    I'm really puzzled by the vertical patterns shown on the body of the fish. Looking at those normal vectors on the fish, I don't think I should see any patterns on the body. Most normals point to the same direction.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •