Jump to content
  • Advertisement
Sign in to follow this  
masha2

Help with terrain normal map for pixel shader

This topic is 2482 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

Hello

I 'm trying to improve my terrain quality with per-pixel shading in GLSL. Firstly I did per-pixel shading in "vertex resolution" ( size of triangle).
Now I would like to add more detail and to do it with passing a normal map (precalculated normals for each pixel) to pixel shader.

I'm GLSL beginner and my question is - is there any way to now what are current processed pixel x,y coordinates in pixel shader assuming that I know the vertex coordinates?
What would be the most efficient way to pass all pixel coordinates to a pixel shader? Or maybe there is a way to calculate or retrieve them somehow? Of course one way is to create vector and fill it with each pixel x,y,z, but that looks kind of waste.

Thanks a lot in advance

Share this post


Link to post
Share on other sites
Advertisement

Hello

I 'm trying to improve my terrain quality with per-pixel shading in GLSL. Firstly I did per-pixel shading in "vertex resolution" ( size of triangle).
Now I would like to add more detail and to do it with passing a normal map (precalculated normals for each pixel) to pixel shader.

I'm GLSL beginner and my question is - is there any way to now what are current processed pixel x,y coordinates in pixel shader assuming that I know the vertex coordinates?
What would be the most efficient way to pass all pixel coordinates to a pixel shader? Or maybe there is a way to calculate or retrieve them somehow? Of course one way is to create vector and fill it with each pixel x,y,z, but that looks kind of waste.

Thanks a lot in advance

There is special GLSL variable gl_FragCoord
You can read the actual viewport pixel coordinate from there (i.e. they are normally scaled in range like 0-1000, NOT 0-1).
From these you can apply reverse transformation pipeline and get the view/world/object coordinates of given pixel.

Share this post


Link to post
Share on other sites
Pass your world-space vertex position through to the pixel shader, and pass the min/max bounds of the terrain in a uniform variable.

The texture coords of your terrain texture can be determined by finding where this world-space position is within the bounds, as a fraction.

e.g. world-space pos xz == (100,200). Terrain min bound xz == (-400,-400). Terrain max bound xz == (400,400)
[font="Courier New"]size = maxBound - minBound == (800,800)[/font]
[font="Courier New"]relativePos = pos - minBound == (500,600)[/font]
[font="Courier New"]texture uv = relativePos/size == (0.625,0.75)[/font]

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!