PDA

View Full Version : 2D normal map rotation



larnin
07-30-2017, 11:41 AM
Hi

I'm new with GLSL shader.
For a small project with the SFML (who is based on OpenGL), i try to add a normal map on my textures to make a better light effect.
For that, i have wrote a shader, who work fine, but i still have a problem.

When the render of a sprite is not transformed, i have the result that i wanted, but if i add a rotation on my sprite the normal don't rotate.
For simple shapes, i can use the transform of the object to change the normal.

But this way don't work on "complex" objects, like a tilemap.
2423
Here i have the same border on the top and left of the block, but rotated. You can see that the light look to be facing the 2 tiles but normaly it's just up.
Is there a way to know the transformation of an UV in the fragment shader ?
If not, is there a workaround ?

Here you can found the fragment shader that i use for the tilemap
https://pastebin.com/YM8p4bLn

Thanks for your help

GClements
07-30-2017, 01:34 PM
Is there a way to know the transformation of an UV in the fragment shader ?

You can apply the dFdx() and dFdy() pseudo-functions to the texture coordinates. That gives you the transformation from screen coordinates to texture coordinates. You can use that to obtain a transformation to or from other coordinate systems.

If you're using OpenGL ES, you need the OES_standard_derivatives extension.

larnin
07-30-2017, 03:00 PM
If i understand well, theses functions allow you to get the others pixel of the fragment, and with that i can found the transform of the current ?

But, what if the current pixel is on the border of a tile, and the others on the fragment is on a other tile ?

GClements
07-31-2017, 03:46 AM
But, what if the current pixel is on the border of a tile, and the others on the fragment is on a other tile ?
The implementation will calculate values for the surrounding fragments if these are necessary to calculate differences. The values will be based upon the vertex shader outputs for the current primitive.