drocktapiff
drocktapiff

Reputation: 117

Creating helicopter spot light through C++ openGL

I have created a helicopter that moves around a track, now where I'm having trouble is that we're supposed to have 2 light sources in the game, one that replicates the sun ( which i've done it wasnt too hard) and the second is a spotlight located on the actual helicopter itself. This is essentially what i have so far:

GLfloat specular2[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat position2[] = { vx, vy, vz, 1.0 };
glLightfv(GL_LIGHT2, GL_DIFFUSE, ambientLight);
glLightfv(GL_LIGHT2, GL_SPECULAR, specular2);
glLightfv(GL_LIGHT2, GL_POSITION, position2);
glLightf(GL_LIGHT2, GL_SPOT_CUTOFF, 60.0f);
glLightf(GL_LIGHT2, GL_SPOT_EXPONENT, 100.0f);
glEnable(GL_LIGHT2);

But, it doesn't do anything that I notice. I've done a great deal of research on lights and i just cant seem to figure it out. By the way ( vx,vy,vz) is the current position i want the light to be in infront of the helicopter.

Upvotes: 0

Views: 447

Answers (1)

Reto Koradi
Reto Koradi

Reputation: 54642

The value you use for GL_SPOT_EXPONENT is very large:

glLightf(GL_LIGHT2, GL_SPOT_EXPONENT, 100.0f);

This controls how quickly the intensity drops off from the center of the cone. For example, with this value of 100.0, the intensity would be down to about 20% at angle of just 10 degrees off the cone center. You may want to try a much smaller value to see if it gives you a better result.

While the vales are not shown, this call looks suspicious just from the naming. It's using a variable named ambientLight to set the diffuse component of the light source:

glLightfv(GL_LIGHT2, GL_DIFFUSE, ambientLight);

Also, you do not specify a direction for the spot light. The default is (0.0, 0.0, -1.0). Unless there is anything in the negative z-direction from the helicopter, the spot light will not illuminate anything. You will need to specify GL_SPOT_DIRECTION if the default is not what you want.

It's also important to be aware that the current modelview transformation is applied when a light source position and direction are specified. So if your helicopter is transformed, it's easiest to specify the position/direction of the spotlight while the transformation is already set up, and before making the draw calls.

Upvotes: 1

Related Questions