OpenGL point sprites rotation in fragment shader

前端 未结 3 1551
感动是毒
感动是毒 2021-02-04 20:17

I\'m following this tutorial to learn something more about OpenGL and in particular point sprites. But I\'m stuck on one of the exercises at the end of the page:

3条回答
  •  梦毁少年i
    2021-02-04 20:51

    I was stuck in the same problem too, but I found a tutorial that explain how to perform a 2d texture rotation in the same fragment shader with only with passing the rotate value (vRotation).

    #version 130
    
    uniform sampler2D tex;
    varying float vRotation;
    void main(void)
    {
    
        float mid = 0.5;
        vec2 rotated = vec2(cos(vRotation) * (gl_PointCoord.x - mid) + sin(vRotation) * (gl_PointCoord.y - mid) + mid,
                            cos(vRotation) * (gl_PointCoord.y - mid) - sin(vRotation) * (gl_PointCoord.x - mid) + mid);
    
        vec4 rotatedTexture=texture2D(tex, rotated);
        gl_FragColor =  gl_Color * rotatedTexture;
    }
    

    Maybe this method is slow but is only to prove/show that you have an alternative to perform a texture 2D rotation inside fragment shader instead of passing a Matrix.

    Note: vRotation should be in Radians.

    Cheers,

提交回复
热议问题