1
Graphics / Re: Rotating Texture
« on: March 06, 2013, 01:05:48 am »I'm trying to make a small Mode 7 project (And yes, I know it's not real 3D) http://en.wikipedia.org/wiki/Mode_7
It's looking good, but I need to rotate the texture (Not the sprite) so that it can be navigated in this mode, if you understand what I'm trying to say.
Hey Aaron,
Who doesn't like Mode 7? I actually did something similar in Processing a while back, so it was easy to reuse the code to work with my current application. It's GLSL, I hope you don't mind:
// Mode 7 Shader
uniform sampler2D Buf1;
uniform float Sin1, Cos1, Sin2, Cos2;
vec2 RepeatXY(in vec2 XY){
return vec2(mod(XY.x, 1.0), mod(XY.y, 1.0));
}
void main(void)
{
vec2 uv = gl_TexCoord[0].st;
float Scale = 128.0;
float Distance = -2.0;
float LookY = -0.3;
vec2 warped;
// perform Mode 7 transformation on uvs
warped = vec2( (uv.x-0.5)/(uv.y+LookY-0.5), 1.0/(uv.y+LookY-0.5) - Distance)/Scale;
// rotate the new uvs
warped = vec2(Sin1*warped.x, Sin2*warped.x) + vec2(Cos1*warped.y, Cos2*warped.y);
// set the uvs to repeat in space
warped = RepeatXY(warped);
// horizon check
if(uv.y < 0.5 - LookY)
// sample the input texture with new uvs
gl_FragColor = texture2D(Buf1, warped);
else
// sky
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
uniform sampler2D Buf1;
uniform float Sin1, Cos1, Sin2, Cos2;
vec2 RepeatXY(in vec2 XY){
return vec2(mod(XY.x, 1.0), mod(XY.y, 1.0));
}
void main(void)
{
vec2 uv = gl_TexCoord[0].st;
float Scale = 128.0;
float Distance = -2.0;
float LookY = -0.3;
vec2 warped;
// perform Mode 7 transformation on uvs
warped = vec2( (uv.x-0.5)/(uv.y+LookY-0.5), 1.0/(uv.y+LookY-0.5) - Distance)/Scale;
// rotate the new uvs
warped = vec2(Sin1*warped.x, Sin2*warped.x) + vec2(Cos1*warped.y, Cos2*warped.y);
// set the uvs to repeat in space
warped = RepeatXY(warped);
// horizon check
if(uv.y < 0.5 - LookY)
// sample the input texture with new uvs
gl_FragColor = texture2D(Buf1, warped);
else
// sky
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
and in your C++ program:
Mode7Shader.SetParameter("Sin1", sin(Time*Val));
Mode7Shader.SetParameter("Cos1", cos(Time*Val));
Mode7Shader.SetParameter("Sin2", sin(Time*Val + 0.5*3.1415926535897932384626433832795));
Mode7Shader.SetParameter("Cos2", cos(Time*Val + 0.5*3.1415926535897932384626433832795));
Mode7Shader.SetParameter("Cos1", cos(Time*Val));
Mode7Shader.SetParameter("Sin2", sin(Time*Val + 0.5*3.1415926535897932384626433832795));
Mode7Shader.SetParameter("Cos2", cos(Time*Val + 0.5*3.1415926535897932384626433832795));
Where "Time" if a float counting by +1.f every frame, and "Val" is any small float. You can substitute "Time*Val" for just one float describing the rotation.
It's not set up for anything but y-rotation and basic movement, but it should give you something to work with.
Hope it helps!
[attachment deleted by admin]