SFML community forums

Help => Graphics => Topic started by: aaroncm on February 26, 2013, 11:44:33 am

Title: Rotating Texture
Post by: aaroncm on February 26, 2013, 11:44:33 am
Hey guys. Have a small problem, not sure if I'm handling it the correct way and I'm not a 3D expert yet =P

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 (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.

I haven't mucked with OpenGL in a long time, if that's even how it should be done. But yeah, confused, any ideas on how I can get what I'm trying to achieve? Any help appreciated!! :D

Thanks!

Pic: This is what I kinda have it doing currently: http://puu.sh/28DkV (http://puu.sh/28DkV)
Title: Re: Rotating Texture
Post by: Laurent on February 26, 2013, 12:09:52 pm
Quote
I need to rotate the texture (Not the sprite)
I can't imagine why you would have to do that. A texture is not made to be rotated, it's a pixel array, not a graphical entity.

Quote
so that it can be navigated in this mode, if you understand what I'm trying to say.
Absolutely not ;D
Title: Re: Rotating Texture
Post by: krzat on February 26, 2013, 12:17:46 pm
No idea why you need this, but you can "rotate texture" by rotating texture coords of the quad.
Title: Re: Rotating Texture
Post by: aaroncm on March 02, 2013, 12:23:44 pm
Hmm, ok I'll put it this way:

How should I go about drawing a textured 3D looking plane, similar to that of Mode 7 used in certain SNES titles. I've had a few attempts but it all seems to end pretty badly  :P

Am I able to use OpenGL through SFML?

Any help appreciated. Thanks (BTW: Using SFML 2.0)
Title: Re: Rotating Texture
Post by: Ancurio on March 03, 2013, 06:05:47 pm
Just rotate the sprite you're drawing the texture with??
Title: AW: Rotating Texture
Post by: eXpl0it3r on March 03, 2013, 10:22:05 pm
Btw you'd be probably better off by learning some basic 3D stuff rather than recreating Mode 7.

Yes you can use OpenGL with SFML.

As said to rotate a texture, either change the quad coords or rotate the sprite.
Title: Re: Rotating Texture
Post by: Strobe 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 (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);
}

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));
 

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]