Hemisphere lighting per pixel

Started by
6 comments, last by PixelPhil 18 years, 7 months ago
Hi, I am working on adding hemisphere lighting to my terrain engine. I can do it per vertex but what would need to be changed to make it per pixel? I am using GLSL. I know I need to do some calculations in the fragment shader but unsure of what those would be... I am using the method described in the "Shader for Game Programmers" book... Thanks
Advertisement
i assume you'd have to make the same calculations you'd usually do in the vertex shader in the pixel shader.
Quote:Original post by l0calh05t
i assume you'd have to make the same calculations you'd usually do in the vertex shader in the pixel shader.


I tried that no luck? Well it still looks like per vertex. The only calculations done in the vertex shader are lerp of colors and determining the factor which is
factor = (dot(normal, float3(0, -1.0)) + 1.0) / 2.0;

Lightmap?
You almost had it by yourself... just a little dtail was missing.

factor = (dot(normalize(normal), float3(0, -1.0, 0)) + 1.0) / 2.0;


you can optimize that to :
factor = normalize(normal).y * -0.5 + 0.5;

which will be compiled as a MAD (multiply add) and will eat up way less instructions.

If you don't normalize per pixel your y component is linearly interpolated... which will give you the same result than per vertex.

in response to anonymous: No...

I hope this helps,
Phil
Quote:Original post by PixelPhil
You almost had it by yourself... just a little dtail was missing.

factor = (dot(normalize(normal), float3(0, -1.0, 0)) + 1.0) / 2.0;


you can optimize that to :
factor = normalize(normal).y * -0.5 + 0.5;

which will be compiled as a MAD (multiply add) and will eat up way less instructions.

If you don't normalize per pixel your y component is linearly interpolated... which will give you the same result than per vertex.

in response to anonymous: No...

I hope this helps,
Phil



Thanks for the reply. So I need to move the factor amount into the fragment shader then? So the only thing I will be doing in the vertex shader is transforming the position and texture coordinates?
yes, otherwise you'd only be linearly interpolating which would be the same as doing it per vertex (just slower)
yeah, just output the normal in world space in your vertex shader. And perform that computation per fragment.

Digression...
You might also want to normalize your normal with a normalization cubemap (which is faster on older hardware) but there is a huge religious debate about that.

Phil

This topic is closed to new replies.

Advertisement