glLight Seemingly Following Camera

Started by Zerolife, September 23, 2011, 11:11:25

Previous topic - Next topic

Zerolife

I'm just getting into lighting, and I have 1 single light setup in a scene atm. Whenever the camera moves, the light seems to follow the camera, and also, the lighting difference between light and dark is very slim.

My light values are set to this.
private static float lightAmbient[] = { 0.5f, 0.5f, 0.5f, 1.0f };// Ambient Light Values
private static float lightDiffuse[] = { 0.5f, 0.5f, 0.5f, 1.0f };// Diffuse Light Values
private static float lightPosition[] = { 0.0f, 1.0f, 0.0f, 1.0f };// Light Position


Im setting up my light like this, called once when I init OpenGL.
ByteBuffer temp = ByteBuffer.allocateDirect(16);
temp.order(ByteOrder.nativeOrder());
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, (FloatBuffer)temp.asFloatBuffer().put(lightAmbient).flip());// Setup The Ambient Light
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_DIFFUSE, (FloatBuffer)temp.asFloatBuffer().put(lightDiffuse).flip());// Setup The Diffuse Light
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_POSITION,(FloatBuffer)temp.asFloatBuffer().put(lightPosition).flip());// Position The Light
GL11.glEnable(GL11.GL_LIGHT1);// Enable Light One
GL11.glEnable(GL11.GL_LIGHTING);// Enable Lights


And I am setting my normals like this.
float[] normal = new float[3];
Vector3f U = null;
U = Vector3f.sub(p2, p1, U);
Vector3f V = null;
V = Vector3f.sub(p3, p1, V);
		
normal[0] = (U.y*V.z) - (U.z*V.y);
normal[1] = (U.z*V.x) - (U.x*V.z);
normal[2] = (U.x*V.y) - (U.y*V.x);


If any more of my code is needed, just ask. Thanks for any help in advance :D

Chuck

Are you placing the lights after your camera transform?  You need to place them before you apply your camera transform.  For example:

    void render() {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glPushMatrix();

        glLight(GL_LIGHT0, GL_POSITION, position);

        glTranslatef(0,0,-300);
        glRotatef(zRot, 0.0f, 0.0f, 1.0f);
        glRotatef(xRot, 1.0f, 0.0f, 0.0f);
        glRotatef(yRot, 0.0f, 1.0f, 0.0f);

        glutSolidTeapot(50);
        glPopMatrix();
    }



Zerolife

I actually just set my lights up when i setup the opengl at the moment. I don't ever move the light. Should i reset the light every time i render?

Chuck

Hmm, setting the light position once should work.  I think you might need to paste more of the code including the setup part and render loop.

As for the difference between light and dark areas, try bringing the ambient light values down and the diffuse ones up.

Suds

Actually, I'm having this issue too. My code is a lot more messy than Zerolife's, but the basic order seems to be the same with the only difference being that I only initialize lighting when a key is pressed.

Relevant part of the Camera's render method:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(_fov, _aspect, _zNear, _zFar);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glRotatef(_pitch, 1.0f, 0.0f, 0.0f);
glRotatef(_yaw, 0.0f, 1.0f, 0.0f);

glTranslatef(_position.getX(), _position.getY(), _position.getZ());


All the OpenGL'ey stuff from my init method:
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glClearColor(0.13f, 0.13f, 0.13f, 1.0f);


My light enable block (resides in the main game update method):
if(Keyboard.isKeyDown(LT_KEY))
        {
            if(!_lightingToggled)
            {
                _lightingToggled = true;
                _lighting  = !_lighting;

                if(_lighting)
                {
                    
			FloatBuffer white = BufferUtils.createFloatBuffer(4);
		        white.put(1.0f).put(1.0f).put(1.0f).put(1.0f).flip();
		
		        FloatBuffer POS = BufferUtils.createFloatBuffer(4);
		        POS.put(1.0f).put(1.0f).put(1.0f).put(0.0f).flip();
         
                        glLight(GL_LIGHT0, GL_AMBIENT, white);
			glLight(GL_LIGHT0, GL_POSITION, POS);
			glEnable(GL_LIGHTING);
                        glEnable(GL_LIGHT0);
                }
                else
                    glDisable(GL_LIGHTING);
            }
        }
        else _lightingToggled = false;


All this code combines to produce a game camera that works as normal except that when lighting is enabled, the light seems to fluctuate based on the camera's rotation from full brightness to full darkness. The models are also all lit by half in a direction about 45degrees from the direction of the camera to the object.

Screenies: [Note in particular that the light levels vary greatly depending on which direction the camera faces and the model shading is always exactly the same no matter where its being viewed from]