spotlight

Started by
5 comments, last by MJP 16 years, 5 months ago
I am trying to create a pixel shade spotlight by using the theory of inner cone and outer cone, but I get a spot light with jagged edge. It's not big but noticable, especially when I set the inner cone same as the outer cone (same angle) I see a zigzag edge, instead of a smooth edge. I also see black lines in and out crossing the edge. It's annoying. Here is what I did in the Pixel function. After I calculate the direction of the object to the light, I dot it with the normal of light direction, then I used smoothstep function to interplate the cos value: float3 objTolight = light.posW - IN.posW; float spotDot = saturate(dot(normalize(-objTolight), normalize(lightDir))); float spot = smoothstep(cos(phi), cos(theta), spotDot); IN.posw is passed from Vertex function. I think it maybe caused by the floating point calculation. maybe some points within the edge are rounded to the outside? any help will be appreciated.
Advertisement
Hmm...using smoothstep like that seems unusual to me, but then again I could just be inexperienced. :P

This is what I use for my spotlight attenuation, which is pretty much taken straight from the equations given in the DirectX SDK docs.

//calculates the spotlight factor float calcSpotlight (float3 lightToPixel){	float rho = dot(lightToPixel, lightDirection);	float cosTheta = lightInnerBeamWidth;     //these have already been divided by 2 and had cosine applied	float cosPhi = lightOuterBeamWidth;	return(saturate((rho - cosPhi)/(cosTheta - cosPhi)));}
Thanks for the reply. I tried your code. it's same. By the way I was wondering what happen if theta=phi, wouldn't it cause divide by zero? But, it doesn't give me error. I assume it's all right.

I still don't know why. Here's the screen shots I made,
http://www.vr3dland.com/test/Screenshots/spotlight.JPG
http://www.vr3dland.com/test/Screenshots/spotlight2.JPG

As you can see, the edge of the spot light is ugly.

I check in my application code to ensure that cosTheta does not equal cosPhi, but if you were to let it sip through it depends on what API you're using for shaders. For example in Direct3D HLSL with Shader Model 2.0, dividing by zero produces a result of zero. However in SM3, you get an undefined value.

I don't really know what's causing those strange artifacts though. At least now you know its not related to your spotlight equations.
Hi, i'm not using mathematical calculation, but projection of "spot" texture - try it, it's quite easy (texture projection is same as shadowmap projection).

Here's my example of spotlight and shadows using texture projection, results are good enough and it's faster than mathematical calculation.

http://www.gamedev.net/community/forums/topic.asp?topic_id=457060

My current blog on programming, linux and stuff - http://gameprogrammerdiary.blogspot.com

Thank for the replies. I think I'll try to use texture projection soon, but, for now, I still want to figure it out why it doesn't work.

I found it has something to do with the projection/view matrix. If lights go straight down and the camera looks straight down, it's perfect, but as soon as the camera turns, it starts to show the artifacts.

I wonder what kind object position I should use in the pixel function? Should it be in world coordinates or worldviewprojection coordinates?

[Edited by - 3dreamer on November 24, 2007 9:49:26 PM]
Quote:Original post by 3dreamer

I wonder what kind object position I should use in the pixel function? Should it be in world coordinates or worldviewprojection coordinates?


Your positions and directional vectors should all be in either world-space or view-space (eye-space). World space is usually the easiest, since this means you won't have to transform your light positions or directions.

This topic is closed to new replies.

Advertisement