# Opengl lighting not working

I have rendered a spinning model in LWJGL. I have calculated normals and enabled lighting. Now I make a light:

float lightpos[] = {0, 0, 0, 0};
FloatBuffer lightposb = BufferUtils.createFloatBuffer(8);
lightposb.put(lightpos);
glLight(GL_LIGHT0, GL_POSITION, lightposb);
float light[] = {10, 10, 10, 1};
FloatBuffer lightb = BufferUtils.createFloatBuffer(8);
lightb.put(light);
glLight(GL_LIGHT0, GL_SPECULAR, lightb);
glLight(GL_LIGHT0, GL_DIFFUSE, lightb);
glLight(GL_LIGHT0, GL_AMBIENT, lightb);


The model, instead of being lit up, is a dark gray. Also, no color appears on the model, even though I set its color to cyan:

    float color[] = {0,1,1,1};
FloatBuffer colorb = BufferUtils.createFloatBuffer(8);
colorb.put(color);
glMaterial(GL_FRONT, GL_AMBIENT, colorb);


UPDATE: Even though I have calculated normals and set them with glNormal3f, the model still appears to be shaded flat.

UPDATE: I HAVE done glEnable(GL_LIGHTING) and glEnable(GL_LIGHT0); that is NOT the problem.

UPDATE: I reversed the order of the light code and the model code and now the model flashes white, then goes dark grey and stays there.

• Hi, from your code I can see that lightning is not enabled in it. Try to put this line, glEnable(GL_LIGHTING); after "lightposb.put(lightpos);". Hope this will help you. – Siddharth-Verma Feb 13 '13 at 9:42
• I already did that. – acer Feb 14 '13 at 1:57
• If it is a dark grey and not just black, then you only need to adjust the light settings. If it is black, I cannot help because the code looks fine. Maybe you could include a little more for us? – Justin Feb 14 '13 at 2:10
• This might do the trick, glMaterial(GL_FRONT_AND_BACK, GL_AMBIENT, colorb); ....... Use "glLoadIdentity();" after glLight... Hope this helps :) – Siddharth-Verma Feb 14 '13 at 5:39
• @Sid I have already done glMaterial (see above code) with no effect; glLoadIdentity makes the model dissapear. – acer Feb 15 '13 at 0:55

What Sid said.

What you are missing is:

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

• I already did that earlier in my code. – acer Feb 14 '13 at 1:57

In order to use both color and lighting you must activate openGL color materials using:

glEnable(GL_COLOR_MATERIAL);


Hope that helps.

I have solved the problem by adding code from this example: http://forum.codecall.net/topic/66017-simple-lwjgl-lighting/

• by using GL_LIGHT_MODEL_AMBIENT or GL_SHININESS ? – Siddharth-Verma Feb 15 '13 at 6:29
• @Sid by enabling color materials, and by changing the light position. – acer Feb 16 '13 at 17:06

The model showing up as grey is a bi-product of lighting being enabled but no light actually being cast on the object aside from ambient.

Without seeing the where your object is being rendered it is possible that you are rendering within the object itself.

It is also possible that you missed enabling color materials.

Also as a general helper with lighting you might want to look through this to see if you missed anything.