Jump to content
  • Advertisement
Sign in to follow this  
MARS_999

Hemisphere lighting per pixel

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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;


Share this post


Link to post
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 this post


Link to post
Share on other sites
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?

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!