spotlights
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
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.
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
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
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement