Newbee is back with spotlight question...

First I have to thank tonyo_au for his help with implementing my shaders. My next goal is to add headlights to my little car, but it’s giving me some issues…

I’ve got sphere’s implemented on the front of the car and when I try to have them give off light they act really weird.

First off I have to set the vec4 to have a last entry of “1” or I get no light effect at all. According to Angel’s book I need to set the point source to zero to get a light, exact opposite of what I’m seeing…

Second, although it moves with the car, the light ALWAYS points towards the rear of the scene (see attached jpeg file)… I can drive around and turn, but it is always pointing “back” as opposed to the direction the car is facing… The entire code base is attached in a zip file, but here is what I’m trying to work on:

//Now the headlights (re-introduce material properties to modify them)
mvstack.push(mv);
//Left headlight
mv = mv*Translate(2.0, 2.1, 5.0)*Scale(0.5, 0.5, 0.1);
glUniformMatrix4fv(model_view, 1, GL_TRUE, mv);
glVertexAttrib4f(vAmbientDiffuseColor, 1.0, 1.0, 1.0, 0.0);
glVertexAttrib4fv(vSpecularColor, vec4(1.0f,1.0f,1.0f,1.0f));
glVertexAttrib1f(vSpecularExponent, 150.0);
glUniform4fv(light_position, 1, mv * vec4(0, -10, 90, 1));
glUniform4fv(light_color, 1, vec4(1,1,1,1));
glUniform4fv(ambient_light, 1, vec4(1, 1, 1, 2));
glDrawArrays(GL_TRIANGLES, 0, 1140);
mv = mvstack.pop(); //restore back to the car transform
mvstack.push(mv);

//Right headlight
mv = mv*Translate(-2.0, 2.1, 5.0)*Scale(0.5, 0.5, 0.1);
glUniformMatrix4fv(model_view, 1, GL_TRUE, mv);
glVertexAttrib4f(vAmbientDiffuseColor, 1.0, 1.0, 1.0, 0.0);
glDrawArrays(GL_TRIANGLES, 0, 1140);
mv = mvstack.pop(); //restore back to the car transform

Attached is a picture of running this code and the behavior of the headlights. FYI it’s the “light_position” vec4 uniform that I’m modifying…

Can anybody give me some help on these lights? Am I way off base here?

Thanks in advance,
–Pete

Nobody?

I’m trying to Google this and I keep getting a mix of OpenGL 1.X & 2.X implementations which is only confusing me more… Plus I gotta use 3.0+

Thanks to Tonyo I have now properly shaded ALL my objects, even the “stage”, looks great. But now I have to implement “headlights” on the car.

Once I figured out objects and placing them I added flattened sphere’s on the front of the car that I assumed I could add lighting properties to and make into headlights…

Am I off my rocker here? Is this not possible? By changing my vec4 input to my light point input it looks like I can, but I see no way to “aim” it…

–Pete

In homogenous coords (vec4 for 3D), x,y,z,1 is a normalized point. x,y,z,0 is a vector.

For positional lights (your headlights – i.e. relatively near lights conceptually emitting light from a point based light source), your light source “position” is a point.

For directional lights (e.g. sun/moon – i.e. relatively far away but very bright “light sources” which emitting light from a constant direction vector), your light source “position” is commonly represented as a vector.

Angel must’ve been talking about something else there. Maybe the vector from the surface point toward the light source. That is always a vector. In the directional light source case you just use the light source direction vector. In the positional light source case, you typically compute a vector from the surface point to the light source point.

Second, although it moves with the car, the light ALWAYS points towards the rear of the scene (see attached jpeg file)…

This probably means you’re not transforming your light source position properly. If you’re doing your lighting calcs in EYE-space (the more common case), you need to take the WORLD-space position of the point light source and transform it by the VIEWING matrix to get it into EYE-space (do this whenever the VIEWING matrix changes).

If you have your light source position defined in EYE-space, then that would cause an effect similar to what you’re talking about.

Now of course if your LIGHT source is actually defined in a local OBJECT-space (as in your case with the headlights defined in the car’s OBJECT-space), you’ll of course first need to transform the light source’s OBJECT-space position by the car’s MODELING transform to get up to WORLD-space. Then take it on down to EYE-space with the VIEWING transform. Or in other words, transform the point light source’s OBJECT-space position by the car’s MODELVIEW transform to get it into EYE-space.

Ah ha, I think I’m following you…

First thanks for responding; I’m actually on break in a class right now (not the programming class) so I’ll look further when I get back home to my PC.

And I learned earlier I was misreading Angel, it was me that was backwards not him!

But it’s good to know I can emit light from my sphere object. That’s assuming I’m reading you right…

I did read something about transforming in another post earlier today; and I am using eye space for my shading light properties. So I’m pretty sure the answer is in there someplace. The light is “pointing” where the camera is pointing… But with all the pre 3.X options out there it can get a little confusing…

Thanks I’ll keep digging in that area!!

–Pete

Thanks for answering Dark Photon, I have been stuck with my own problems with transparency and haven’t hard time to answer Pete