Hi guys,
I’m newbie to OpenGL and really need your help to complete very important project to me. I have a problem with lighting in OpenGL ES. I have spent many hours to find a right way to lit my scene with spotlights, without a success .
First of all, my scene looks like this: a dark tunnel divided into number of cubic sectors. Every sector is a cube with 4 walls (top,bottom,left,right). I’m trying to put in every sector a spotlight that will lit a floor and part of walls. I came up with something like this: I set up 2 lights: ambient (to lit every wall with a dark light) and a diffuse light.
float lightAmbient[] = { 0.3f, 0.3f, 0.3f, 1.0f };
float lightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightDiffuse);
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 20.0f);
First I’m moving the player in my 3D world:
glRotatef(CameraRot, 0, 1, 0);
glTranslatef(-PlayerPosition.x, -PlayerPosition.y, -PlayerPosition.z);
Then I’m drawing my tunnel sectors in a loop:
for(int i=0;i<SecNumber;i++) {
// Set my spotlight in a center of actual sector
GLfloat lightDirection[] = { 0, -1 , 0 };
GLfloat pos0[] = {0.0,0.0,0.0,1.0};
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, lightDirection);
glLightfv(GL_LIGHT1, GL_POSITION, pos0);
Sector[i]->Draw();
// Move to the center of next sector
NextSectorPos = Sector[i]->NextSectorRelativePosition();
glTranslatef(NextSectorPos.x, NextSectorPos.y, NextSectorPos.z);
}
This is (of course) not working like I expect When I move player around the 3D world, sometimes I can see a spotlight in one of the sectors (but not in all) and this light is disappearing when I’m changing distance to it.
Could someone can point me what I’m doing wrong ? How can I achieve this lighting effect in other (not complicated ) way ?
Treat this code as a pseudocode (my orginal code is in Objective-C).
Thanks in advance for any reply!! (sorry for my english).