Sign in to follow this  
devronious

calc hlsl lighting...

Recommended Posts

devronious    108
I have a transformed normal, 4 light directions and a pixel color. How will I calculate the resulting color as if in hlsl fx file? thanks in advance, Devin

Share this post


Link to post
Share on other sites
S1CA    1418
Quote:
Original post by yaroslavd
There's a bunch of different models you can use. [google] Phong shading.


Phong shading illumination [smile].

Phong 'shading' is where the normals are interpolated across the polygon (compared with Gouraud shading where the resultant light/colour is) Phong illumination is the lighting model.

The Phong model accounts for ambient, diffuse and specularly reflected light. The diffuse computation is Lambertian (D=N.L), it's the specular part which is most specific to Phong (S=R.V). Most people actually tend to use a variation known as Blinn-Phong for their specular (S=N.H) because it's cheaper to compute.

Googling for any of those terms should produce many matches, funnily enough the second link Google comes up with is: http://www.gamedev.net/reference/articles/article667.asp


You can also get plenty of already implemented HLSL code for lighting from the various hardware manufacturers sites, http://developer.nvidia.com is a goldmine for example.


For 4 lights, you'll need to compute the diffuse and specular for each light separately then add the intensities together before finally adding in the ambient.

Share this post


Link to post
Share on other sites
devronious    108
Thanks, I have had some fun looking into lighting and have a better understanding so far. Dot products play a large part from what I can see. Thanks for the nudge as far as lighting location. This is exactly what I was searching for. I was unsure how to do specular and such.

Thanks,

Devin

Share this post


Link to post
Share on other sites
S1CA    1418
Quote:
Original post by devronious
Dot products play a large part from what I can see.


Yep, dot products and lighting 101 (dodgy ASCII art time...):

Given two vectors, A, B:


A__ __B
/\ /
\ /
\_--_/
\th/
\/



One of the really useful properties of the dot product is:

A dot B == cos(theta) * ||A|| * ||B||

(theta is the angle marked th on the diagram, usually marked with a Greek letter/symbol 'theta', and ||A|| means "the length of vector A").

If A and B are both normalised vectors (i.e. each has a length of 1), then you get: cos(theta)*1*1, which of course simplifies to cos(theta).

Now if A is the normal of the surface (or vertex) being lit (usually called N), and B is the direction from the surface (or vertex) being lit towards the light (i.e. -LightDirection, usually called L), and both vectors are normalised (unit length), then:


N
^
| N dot L == 0 (i.e. 0% illuminated)
|
+----->L



N
^
| N dot L == 0 (i.e. 0% illuminated)
|
L<-----+



L (<-- both vectors pointing in exactly the same direction)
N
^
| N dot L == 1 (i.e. 100% illuminated)
|
+



N
^
| N dot L == -1 (i.e. minus 100% illuminated)
|
+
|
|
v
L


The same is true when N and L are swapped around. The result of L dot N is the intensity of the diffuse light. In the last case, you don't want objects that face away from the light to take any light away, you just want 0% light which is why lighting shaders usually do something along the lines of Id=max(0,dot(N,L)); to ensure the range of intensities stays between 0 and 1.


Et voila: that was how Lambert's cosine law relates to the dot product at the heart of many lighting models [smile].

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