Hello all!
I'm trying to develop my 3D skills, particularly GLSL, but unfortunately I've gotten a bit stuck as I want to do something that I can't find any online documentation for. I've been a longtime reader of Polycount so I thought I'd sign up and see if I could get any assistance from the community
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 please help me out? I'd really appreciate it!
(For those who have used the UDK/UE3 material editor, I am basically trying to recreate the 'rotator' node)
Replies
While I have to rotate the UVs with sine/cosine I am unsure exactly how to access the coords of the UV vertices in GLSL. I have been learning it from online tutorials which have been great for getting results, but haven't always explained what is actually going on when I use things like gl_texcoords[0] or gl_multitexcoords0 etc.
For example I know that the following GLSL will scroll a texture...
vec2 Texcoord = gl_MultiTexCoord0.st;
float offset_x = 2.0;
float offset_y = 2.0;
vec4 example = texture2D( baseMap, vec2(Texcoord.s+offset_x, Texcoord.t+offset_y) );
...but I don't know what is going in in regard to the actual UV coords.
Could anyone give me a quick example of how I would go about rotating UVs?
Thanks again for the help so far.
So do you see how your fragment program is a piece of dynamic code: it always uses different values, depending on which pixel it's currently running for? And do you see how the UV coordinates are not "changed", they're just modified just before they are used in the code?
ask if you don't get it yet, it's important that you grasp this stuff
EDIT:
I have this frag shader now. It seems to be rotating the texture which is great, but it only works on a quad. If I apply it to a cube, for example, the texture is rotated across the whole object rather than each face. I also wonder if this is the most efficient way I could be doing this. :poly142:
uniform sampler2D baseMap;
uniform float timer;
uniform float rotspeed;
uniform vec2 offset;
varying vec2 Texcoord;
void main( void )
{
float cosX,sinX;
float cosY,sinY;
sinX = sin(rotspeed*timer);
cosX = cos(rotspeed*timer);
sinY = sin(rotspeed*timer);
cosY = cos(rotspeed*timer);
mat2 rotationMatrix = mat2( cosX, -sinX,
sinY, cosX);
float scaletimer=timer;
vec2 newcoords=((Texcoord-offset)*(rotationMatrix));
newcoords+=offset;
gl_FragColor = texture2D( baseMap, newcoords );
}
If you want it to look like every face of the cube is rotating around its' own centrepoint, you'll have to unwrap every face overlapping, in Max this would be the default unwrapping of a cube. You can't really have unique unwrapping rotating around its own face centre...
Maybe show what you're trying to achieve with a screenshot?
I am viewing the shader in RenderMonkey and it seems that the default cube model it comes with has its UVs layed out as a cross shape. I exported a default cube from Max and the shader looks perfect now. It was only a simple effect - I was trying to make a fan blade spin.
Thanks for all the help. I think I need to find a book or two about GLSL!