HLSL Point Lights

Started by
1 comment, last by Namethatnobodyelsetook 18 years, 5 months ago
Hello, I was wondering how do you calculate a point light in HLSL? Thanks in Advance
Advertisement
2D or 3D?

3D -
In the vertex shader's output, you include the untransformed position of the vertex too. It will linearly interpolate it between vertices, which is just fine as the pixels are also linearly interpolated in the same place. So, in the vertex shader, output the transformed position (untransformed position multiplied by the view matrix), the texcoord and the untransformed position.

In the pixel shader, sample a texture using the input texcoord, and multiply it based on the distance between the untransformed position and the light.

Example hlsl shader, also includes bumpmapping

2D - If your using 2D, which chances are you aren't, post here and I'll tell you how.

Hope this helps :)
You can do this all in the vertex shader, or partially in both the vertex and pixel shader.

For per pixel light, typically you pass vertex position as a UV from the vertex shader to the pixel shader, as the previous poster said. You'll also need to pass the normal. In the pixel shader you can compute the direction and distance:

Regardless of pixel or vertex shader, you'd do this:

lightvec = (vertexpos - lightpos);
lightdist = length(lightvec);
lightvec = normalize(lightvec);

You can then apply attenutation based on the distance, do a directional light calculation based on the lightvector and the normal. The pointlight color value is (lightvec dot normal) * attenuation * lightcolor * materialcolor.

This topic is closed to new replies.

Advertisement