Sign in to follow this  
programmer_tom

Specular lighting

Recommended Posts

trying to implement specular lighting in a vertex shader exactly as d3d does in the FFP. my shader's specular part looks like this: mov r2, -c13 ; Move the camera look angle to r2. add r2, r2, -c7 ; This is the half vector; the camera look angle + the ; scene light angle. dp3 r2.w, r2, r2 ; Renormalize normal by finding magnitude and dividing rsq r2.w, r2.w ; vector by it (multiplying by 1/magnitude). mul r2, r2, r2.w dp3 r3.y, r1, r2 ; Get dot product of half vector and previously ; calculated normal vector. dp3 r3.x, r1, -c7 ; Get dot product between half vector and light direction. mov r3.w, c15.x ; Move material specular power to r3.w. lit r3, r3 ; Lit creates lighting coefficients from the two dot ; products in r3.x, and r3.y, and raises to power in r3.w. mul r3, r3, c14 ; Modulate against specular material (intensity). ;----- Output Color 1 ----- mov oD1, r3.z ; Output the final specular in oD1. ; Diffuse color is output in oD0 elsewhere. i can't figure why mine comes out so wide (power). the brightness seems ok (intensity). any tips?

Share this post


Link to post
Share on other sites
From what I see, the shader looks fine except that you may want to make the material specular intensity modulation act only on r3.z. Are you doing something with v1 in the pixel shader or are you using fixed function pixel processing?

Have you tried to add the specular to oD0 and not output oD1 to see if the problem comes from having a second color that is not added correctly?

There is also a maximum specular exponent value for the lit instruction (~127) are you sure you are not exceeding it?

Share this post


Link to post
Share on other sites
for this, i'm using the FFP pixel shader.

i have tried adding the specular to oD0, same crappy look, only now u can't
disable specular lighting in the program (i guess d3d assumes it's in oD1)

the power is about 50, but not much better at 100 :(

i see a roughly correct specular angle, just not the right look.

Share this post


Link to post
Share on other sites
I just thought of something that could very well explain your problem... Unless you have a directional light, the light direction vector used to compute the half vector must not be constant. It should be computed for all vertices (as lightPos - vertexPos). Otherwise, you'll have the same half vector for all vertices.

That should do it... I think... :)

Share this post


Link to post
Share on other sites
Ok, well, what I said only applies to point and spot lights, not directional lights. But then, I wouldn't expect specular to look realistic with directional lights since specular is the reflection of the light source and that kind of light is quite large in size.

Try to make it a point light and interpolate the light direction, I'm sure you'll get better results.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this