Solution 1: Rotate your UVs in your vertex shader
The vertex shader is where per-vertex attributes can be modified before rendering. These values are then linearly interpolated to generate values for all fragments of your polygons. As a rotation of your your UVs is a linear transformation, you only need to recalculate the values in the vertices, and linear interpolation will take care of all intermediate fragment values. (Note that in case of a non-linear transform such as u_new = sin( u_old ), you'd have to calculate the new UVs for every fragment individually. In this case, the transformation belongs in your fragment shader.)
The UVs are a 2D coordinate pair with values ranging from 0 to 1: [0, 1] x [0, 1]. (These values can sometimes be outside of the 0,1 range, in this case clamping, wrapping or repeating of the texture occurs, depending on how you specified your OpenGL texture hints)
In your vertex shader, you could rotate your UVs prior to the texture sampling to obtain a rotation. I'll assume you want to rotate the texture around its center coordinate (0.5, 0.5) (if the rotation origin varies from texture to texture, you could pass it as a 2f uniform). To enable animation, I'd suggest passing the angle as a 1f uniform.
You can calculate the new UVs using a concatenation of three transforms. You can't just do a single transform (a rotation) because this would rotate the texture around the (0, 0) point. Instead you want to use (0.5 ; 0.5) as your origin of rotation. The image by Jon at the bottom of this answer illustrates this. You can use these transformations:
- Translate the UVs to the origin using T[-0.5, -0.5]. This means: UVs = UVs + vec2(-0.5f, -0.5f);
- Rotate your UVs by the angle specified in the uniform using R[uniformAngle]. This can be done by multiplying with a 2x2 rotation matrix. This means: *UVs = mat2(cos(uniformAngle), sin(uniformAngle), -sin(uniformAngle), cos(uniformAngle)) * UVs;*
- Translate the UVs back to the center of the texture using T[0.5, 0.5]. This means: UVs = UVs + vec2(0.5f, 0.5f);
By sampling your texture using these new UVs, a rotation will have been applied. You can increase your angle uniform over time to apply an animation to the rotation.
Solution 2: Rotate your UVs on the CPU
If the angle of rotation remains constant throughout your program, it might be better to rotate the UVs on the CPU. This means you rotate them before rendering (thus while filling up your buffer objects). The procedure is exactly the same. You could use the GLM library to perform the matrix and vector operations for you. The procedure is the same as described above.
In summary, you will want to rotate your UVs to have the effect of a rotation on your texture. You can do this in two places. If the rotation changes over time, it is best to perform the calculation on your GPU, in the vertex shader. If the angle is constant, you can do the calculation on the CPU while filling up your vertex buffer, to prevent performing the same calculation every frame.