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
Help with terrain normal map for pixel shader
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.
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]
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]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement