View Full Version : [Newbie] Lighting problem :(

07-30-2008, 02:37 PM
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 };


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);


// 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).

07-31-2008, 03:25 PM
Have a look at this page, even if it is more than 10 years old, there is still relevant information for you, in the section "2. Poor Tessellation Hurts Lighting" :

07-31-2008, 04:44 PM
First of all, ZbuffeR thanks for your reply, however I still have some questions: why sometimes I see the spotlight on the floor and sometimes not (it depends on the distance). The spotlights should be fixed in one position, right over the surface, so I think that the wall in worse or better looking way should be lit (even if it is build of 4 vertices). Moreover I don't use the specular light so the intense of light shouldn't depend on the camera position (and in my case it unfortunately does :( ). Is the method I'm trying to use to lit all sectors is wrong ?? (moving 1 spotlight from sector to sector just before drawing it [all sectors are visible])

Please help...


07-31-2008, 05:43 PM
Screenshots please, your description is not really descriptive.
Try with much higher tesselation.
Example, for each tunnel sector, subdive your polygons in 20x20 quads. It will be much easier for you to see what is going wrong.
Or you will have to use per-pixel lighting, but it means using GL ES 2.