# calc hlsl lighting...

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

## Recommended Posts

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 on other sites
There's a bunch of different models you can use. [google] Phong shading.

##### Share on other sites
Quote:
 Original post by yaroslavdThere's a bunch of different models you can use. [google] Phong shading.

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 on other sites
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 on other sites
Quote:
 Original post by devroniousDot 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].