OpenGL Lighting Positioning

Hey

Was wondering if anyone could give me some pointers or tips on lighting. I have done several samples, and have had lighting working without any issues. However, once I translate this lighting code into my application, nothing seems to work.

Essentially, I use the following to setup my rendering area:

width = 1024.0f
height = 600.0f
zDist = (height / 2.0) / tanf(DEGREE_TO_RAD(45.0f/2.0f);
gluPerspective(45.0f, width/height, 500, 2000)
gluLookAt(width/2.0f, height/2.0f, zDist, width/2.0f, height/2.0f, 0.0, 0.0, 1.0f, 0.0f);

The above sets up my code so that I have a rendering area of 1024x600 where z=0.

I can draw a 100x100x100 cube at 0x0x0 and have it appear in the bottom left hand corner of the screen perfectly. The issue I have is that after using the above transformations lighting no longer works.

I setup lighting as follows:
const GLfloat gLightAmbient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat l0LightDiffuse[] = { 0.5f, 0.5f, 0.5f, 1.0f };
const GLfloat l0LightPosition[] = { 512.0f, 300.0f, 4.0f, 1.0f };

glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, gLightAmbient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, l0LightDiffuse);

The global ambient lighting works. The diffuse lighting can work when I use the gluLookAt with my eye being at 0,0,4 and the center being at 0,0,0. when I try and break out of that I cannot figure out where I should position my light to get proper diffuse lighting. Any help or pointers would be appreciated. I have tried playing around with both directional and positional lighting to get something to work with little success.

Thanks

Where are you using Positional lighting?

i.e glLightfv(GL_LIGHT0, GL_POSITION, l0LightPosition);?

I have tried using it just in initialization, and I have separately tried using it after my call to gluLookAt.

Further, I have tried positioning it at 0,0,4 both at initialization and after gluLookAt. E.g.

gluPerspective(FIELD_OF_VIEW, 1.0f * width/height, Z_NEAR, Z_FAR);
const GLfloat l0LightPosition[] = { 0.0f, 0.0f, 4.0f, 1.0f };
const GLfloat l0LightPosition[] = { 512.0f, 300.0f, 4.0f, 1.0f };
//glLightfv(GL_LIGHT0, GL_POSITION, l0LightPosition);

gluLookAt(width/2.0f, height/2.0f, CALC_Z_DISTANCE(height, FIELD_OF_VIEW),
width/2.0f, height/2.0f, -1.0f,
0.0f, 1.0f, 0.0f);

//glLightfv(GL_LIGHT0, GL_POSITION, l0LightPosition);

I have tried both the commented out positions above with various values.

Try placing your light source at the location of your view point.
or make it directional. May be your light source position is not falling inside your view frustum.

Hey,

Tried that, still no go :(.

Is the below code sane?

const GLfloat gLightAmbient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat l0LightDiffuse[] = { 0.9f, 0.2f, 0.1f, 1.0f };

glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, gLightAmbient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, l0LightDiffuse);

With the above code, I would expect there to be a red shine from the light position when I position the diffuse light. Is this a correct assumption?

Then given the above code I do the following in my render loop:

glViewport(0,0, width, height);
gluPerspective(FIELD_OF_VIEW, 1.0f * width/height, Z_NEAR, Z_FAR);
gluLookAt(width/2.0f, height/2.0f, CALC_Z_DISTANCE(height, FIELD_OF_VIEW),
		  width/2.0f, height/2.0f, 0.0f,
		  0.0f, 1.0f, 0.0f);

glTranslatef(width/2.0f, height/2.0f, 0.0f);
    const GLfloat l0LightPosition[] = { 0.0f, 0.0f, 500.0f, 1.0f };
glLightfv(GL_LIGHT0, GL_POSITION, l0LightPosition);
glTranslatef(-width/2.0f, -height/2.0f, 0.0f);


    // Draw a Cube with dimensions 300x300x300
    // At position 512, 300, 400, essentially, center of the screen, and then close to my near field
cube->setSize(300,300,300);
cube->setCenterPosition(512, 300, 400);

A couple of assumptions are made that I think are correct:
a) the light position is multiplied by the GL_MODELVIEW matrix
b) Positional light emits in all directions.

I have also tried directional light with no success. Though I am a bit confused by directional light. For directional light, should I not be able to simply set the light position to go to:
{0,0,-1,0) and this would have it shine down the ‘Z’ access? I use glTranslate to go to the center of the rendering area, which I think should make that shine straight down if I understand things correctly. The ‘z’ distance from my eye point to the camera should not affect directional light correct? Still no effect though (I also tried inverting the z direction). So I think I might have a fundamental misunderstanding of how the directional should work, or my lighting setup is wrong perhaps? Should everything get a ‘red glow’?

Thanks for any pointers.

My lighting still looks incorrect, but I figured it out.
I never enabled ‘glEnable(GL_RESCALE_NORMAL);’

That fixes everything :slight_smile:

Thanks for all the help, lighting now behaves more like I expected it to.

A couple of assumptions are made that I think are correct:
a) the light position is multiplied by the GL_MODELVIEW matrix
b) Positional light emits in all directions.

(a) is correct.

(b) Need to be specific.

Positional light follows a direction with a defined starting point unlike directional light that has only direction but no starting point.

Diffuse light source (can be directional or positional) follows like a ray when incidental but scatters when reflected.

Specular (again can be directional or positional) does not scatter on hitting surface.

Is the below code sane?

const GLfloat gLightAmbient = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat l0LightDiffuse = { 0.9f, 0.2f, 0.1f, 1.0f };

glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, gLightAmbient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, l0LightDiffuse);

Yes.

When you do transformations, your already set positional light source at (512,300,4) gets transformed too.
Transform light source locally as:


glPushMatrix();
  glTranslatef(a,b,c)
  glLightfv(..) 
glPopMatrix();

You can put your light source i.e GL_LIGHT0 at some position close to cube and then do transformation separately.

Note that only the Model view matrix transformation would affect the position of your light.

Great. So lighting calculations were not proper.