GLSL rotation. How do I do it?!

Community Forums/General Help/GLSL rotation. How do I do it?!

Robb(Posted 2010) [#1]
I've been learning GLSL from online tutorials and I seem to be getting on quite well. Unfortunately however, I want to do something that I can't find any documentation for.

Basically, within my shader program I'm trying to rotate a texture around an arbitrary point (specifically I want to use the centre point as the origin). While I can translate horizontally/vertically or warp my textures I have no idea how to rotate them. Could anyone help me out?

(For anyone who has used the UDK material editor, I am trying to recreate the 'rotator' node)


Noobody(Posted 2010) [#2]
I'm not that familiar with Unity, but as far as I remember, you should have access to the OpenGL API in scripts (well, only in Unity Pro). If that's possible, just apply glRotate to the GL_TEXTUREMATRIX and you're done.

If that doesn't work, you have to do it by hand in the vertex shader:
gl_TexCoord[ 0 ] = gl_MultiTexCoord0*RotationMatrix;

How to build the rotation matrix is described on various pages on the internet (Wikipedia has a pretty good article on rotation matrices). Since you only want 2D rotation, it's actually pretty straightforward:
mat4 RotationMatrix = mat4( cos( Angle ), -sin( Angle ), 0.0, 0.0,
			    sin( Angle ),  cos( Angle ), 0.0, 0.0,
			             0.0,           0.0, 1.0, 0.0,
				     0.0,           0.0, 0.0, 1.0 );

I'm not sure if GLSL uses rads or degrees, but if I had to guess it's rads.


Kryzon(Posted 2010) [#3]
Noobody, if I may ask you... how did you learn how matrices work?
I just can't make any sense out of them.

EDIT: Learned, and the GLSL has built-in matrices for the texture operations - these are the Texture Matrices (search for them).
So using a uniform matrix isn't necessary.


Sauer(Posted 2010) [#4]
I know you didn't ask me but...

Linear algebra is a good topic to study if you want to learn about matrices and the effects they have on vectors.

And this book is really good for everything 3d math:

http://www.amazon.com/Primer-Graphics-Development-Wordware-Library/dp/1556229119/ref=sr_1_1?ie=UTF8&s=books&qid=1270830586&sr=8-1

Don't mind me just my two cents ;)


Kryzon(Posted 2010) [#5]
By all means, I didn't intend that question for Noobody only.

Any advice is welcome :) (and thanks for that book suggestion)