C ++ Opengl - lighting using a spotlight

I have a model that should be under a spotlight / directional light,

Meaning, I need to switch between modes (Spotlight and directional).

Here is the code with some explanation:

I can rotate the model / light source with mouse movements, so I use

glRotate and glTranslate for this.

As soon as the user has pressed the "L" key, I have to switch between modes.

here is the code for the zipper:

void LightBall::projectLight(void) { if(LIGHT == _lightMode){ printf("Entering LIGHT mode\n"); <--- Supposed to be a directional light glDisable(GL_LIGHT1); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, GL_POSITION, _light_position); } if(SPOT_LIGHT == _lightMode){ printf("Entering SPOTLIGHT mode\n"); <--- Supposed to be a spotlight glDisable(GL_LIGHT0); glEnable(GL_LIGHT1); glLightfv(GL_LIGHT1, GL_POSITION, _light_position); glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 10.0); glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 2.0); glLightfv(GL_LIGHT1,GL_SPOT_DIRECTION,_spotlight_position); } } 

The problem is that when you switch between them, you always get the same lighting mode,

What it is:

directional

And another example after switching between 2 light modes and the same light beam

source with rotation of the light source (small ball):

rotation

How can I get the desired result?

Here are the definitions of LIGHT0 and LIGHT1:

  GLfloat light_ambient[] = { 1.0, 0.0, 0.0, 1.0 }; GLfloat light_diffuse[] = { 1.0, 0.0, 0.0, 1.0 }; GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 }; _light_position[0] = 0.0; _light_position[1] = 1.0; _light_position[2] = 0.0; _light_position[3] = 0.0; _spotlight_position[0] = 0.0; _spotlight_position[1] = -1.0; _spotlight_position[2] = 0.0; glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular); 

Thanks!

+7
source share
1 answer

Whether the GL light is a directional light or a spotlight depends on the w (fourth) component of its position. If the coordinate is 0, it is directed. If nonzero (usually 1), this is a spotlight. You will need to change _lightPosition before calling glLightfv(..., GL_POSITION, ...) .

+8
source

All Articles