# Hemisphere lighting per pixel

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

## Recommended Posts

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

##### Share on other sites
i assume you'd have to make the same calculations you'd usually do in the vertex shader in the pixel shader.

##### Share on other sites
Quote:
 Original post by l0calh05ti 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?

##### Share on other sites
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

##### Share on other sites
Quote:
 Original post by PixelPhilYou 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?

##### Share on other sites
yes, otherwise you'd only be linearly interpolating which would be the same as doing it per vertex (just slower)

##### Share on other sites
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

1. 1
2. 2
Rutin
20
3. 3
khawk
17
4. 4
A4L
14
5. 5

• 12
• 16
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633756
• Total Posts
3013710
×