Jump to content
  • Advertisement
Sign in to follow this  
endfx

OpenGL spotlights

This topic is 5033 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm trying to create a spotlight in my openGL program. Basically its a cube world that goes from X range = -50 , 50 {ie 100 units wide} Y range = -20 , 20 {ie 40 units high} Z range = 0, -100 {100 units deep} I'm trying to get a spotlight to shine on the wall on the right side. glEnable(GL_LIGHTING); GLfloat dirVector0[]={ 1.0, 0.0, 0.0, 1.0}; GLfloat lightPos0[]={ 0.0, 0.0, -30.0, 1.0}; GLfloat ambientLight0[] = {2.2f, 2.2f, 2.2f, 1.0f}; GLfloat diffuseLight0[] = {1.0f, 1.0f, 1.0f, 1.0f}; glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight0); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight0); glLightfv(GL_LIGHT0, GL_POSITION, lightPos0); glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 30.0); glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, dirVector0); glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 2.5); glEnable(GL_LIGHT0); Now this gives unbelievable results: I lights up the right side of the world, making the right wall the brightest. It looks nothing like a spotlight effect If you change the 30.0 to anything below 25, you get no light at all. And in the dirVector0, if you change it to anything other than the X axis, it doesn't work. Anybody have any ideas? Thanks

Share this post


Link to post
Share on other sites
Advertisement
How exactly are you drawing your wall? If it's a simple quad or a couple of tris or something similar you aren't gonna get a nicely defined spot in the middle of it, the reason being OpenGL does lighting per-vertex not per-pixel so in the case of a quad it'd calculate the spot-light illumination at the four corners and interpolate the colour inbetween them.

Share this post


Link to post
Share on other sites
I drew the wall using:

glBegin(GL_POLYGON);
vertex3f...
vertex3f...
vertex3f...
glEnd();

If this won't give me a spotlight shape, can you tell me how I would draw the wall to allow for spotlights?

Thanks

Share this post


Link to post
Share on other sites
The results are completely normal, because you use standard OpenGL lighting, which is per-vertex.

This means that color is computed for each vertex of a primitive, and then gets interpolated(gouraud shading). It's not possible to get a spotlight effect this way. Also, you get no light if you set the cutoff below 30.0, because possibly all vertices are out of the spread angle of the light. Of course,part of the surface is inside the spotlight, but since lighting is computed only on the vertices, you get no light.

There are two ways to solve this problem:

1)Higher tesselation of the surfaces. This means that surfaces should be build out of more polygons, resulting in more detailed geometry.

2)Per-pixel lighting. This is an advanced feature, which bypasses the standard GL lighting. Using extensions like env_dot3 or fragment programs, you can compute the lighting for each pixel. A more advanced and popular technique is bump mapping, which also allows you not only to compute lighting per-pixel, but to specify the normal for each pixel, giving a 3D look to the textures.

Google for Opengl tutorials for per-pixel lighting or bump mapping and you should find plenty of them.

Share this post


Link to post
Share on other sites
I redefined the wall so its made of many little squares, and the spotlight works great.
Thanks so much!
Saved me alot of time messing around, thats for sure.

Thanks again!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!