PDA

View Full Version : Problem with positioning of light.



Paras_wolf
03-29-2011, 12:06 AM
Hi, I am a beginner in OpenGL. I am trying to integrate code for water caustics (available online) with water wave dynamics code which I have written.

The problem is the caustics code uses lighting in order to get the effect and I haven't used lighting, when i integrated the code it got compiled but everything appears dark as if light is switched off. So I made some changes with positioning but they aren't effective.

Following is my glutReshapeFunc

void reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

float aspectRatio = (float)w / h;

gluPerspective(60.0f, (float)(w)/h, 1.0f, 1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

}

and my lightning positioning code inside display is

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

/* Reposition the light source. */
lightPosition[0] = 12*cos(lightAngle);
lightPosition[1] = lightHeight;
lightPosition[2] = 12*sin(lightAngle);
if (directionalLight) {
lightPosition[3] = 0.0;
} else {
lightPosition[3] = 1.0;
}

glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);

where lightangle is 0.0,
where lightHeight is 20.0,


My confusion is, in gluPerspective is the z near and far plane is 1.0 and 1000.0, where is my 3D water mesh drawn and then where do I position my light?

Many Thanks in advance..

BionicBytes
03-29-2011, 01:41 AM
Given lightangle is 0.0, then lightPosition evaluates as (12,20,0).

The command
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition) will multiply lightPosition with the current MODELVIEW matrix resulting in GL_LIGHT0.position = lightPosition because the MODELVIEW matrix was last set to identity.

Given your perspective code:


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0f, (float)(w)/h, 1.0f, 1000.0f);

this has the effect of setting the scene's depth z values between -1.0 and -1000.0.

Therefore your light0 is positioned at a z =0 and will be clipped as it's outside the camera view fustrum.

You have not shown how you have setup fixed function lighting and the other Light0 properties.
Also you have not indicated whether your model has correct normals defined and in which direction they are facing.
How tesselated is your model(s)? Without a high level fixed function lighting may very well appear to be doing little (as models are lit per-vertex) and thus giving the scene a very dark appearance.

Paras_wolf
03-29-2011, 11:42 PM
Thankyou for your reply,

Haven't paid specific attention to details you explained, will check if the changes work.

Many thanks,
~Paras

Paras_wolf
03-30-2011, 02:09 AM
ok, I had messed up with giving normals and other properties

problem solved, thanks for pointing