LWJGL
May 20, 2013, 05:38:18 *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: LWJGL 2.9.0 released!
 
   Home   Help Search Login Register  



Pages: [1]
  Print  
Author Topic: glLight Seemingly Following Camera  (Read 1763 times)
Zerolife
Newbie
*
Posts: 3


« on: September 23, 2011, 04:11:25 »

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.
Code:
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.
Code:
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.
Code:
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 Cheesy
Logged
Chuck
Newbie
*
Posts: 42


aka sproingie on freenode


« Reply #1 on: September 23, 2011, 13:50:41 »

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

Code:
    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();
    }

Logged
Zerolife
Newbie
*
Posts: 3


« Reply #2 on: September 23, 2011, 21:58:07 »

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?
Logged
Chuck
Newbie
*
Posts: 42


aka sproingie on freenode


« Reply #3 on: September 24, 2011, 09:01:56 »

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.
Logged
Suds
Newbie
*
Posts: 11


« Reply #4 on: February 16, 2012, 03:49:01 »

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:
Code:
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:
Code:
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):
Code:
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]


Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines
SMFAds for Free Forums
Valid XHTML 1.0! Valid CSS!