SFML community forums

Help => Graphics => Topic started by: notnowlewis on July 08, 2013, 10:58:50 am

Title: Transforming a sprite with a 3x3 matrix - where to start?
Post by: notnowlewis on July 08, 2013, 10:58:50 am
I have no idea how the values in a 3x3 matrix work to transform a sprite. I have done a little reading on the math of matrix transformations, but I can seem to make the connection between the values in the matric and how the look of my sprite changes. Where can I find some good examples and tutorials on using them?
Title: Re: Transforming a sprite with a 3x3 matrix - where to start?
Post by: notnowlewis on July 08, 2013, 11:41:08 am
So you know what I want to achieve, I want to take an image like this: http://i.imgur.com/qH8YCRS.png and apply a perspective effect, so it ends up like this: http://i.imgur.com/AUvpyKd.png

I know I can do that type of transformation in GIMP with a 3x3 matrix, but I want to understand *how* it works :)
Title: Re: Transforming a sprite with a 3x3 matrix - where to start?
Post by: Laurent on July 08, 2013, 03:25:05 pm
It's not going to work with SFML. This kind of perspective transformation requires depth values (which SFML doesn't have), otherwise the texture mapping will be incorrect.
Title: Re: Transforming a sprite with a 3x3 matrix - where to start?
Post by: notnowlewis on July 08, 2013, 04:45:16 pm
Ah, maybe I misunderstood or didn't explain myself very well. i don't want a 3d transform, I want to take a texture which is currently in a rectangle http://i.imgur.com/HYbDVos.png and squish it into a trapezoid http://i.imgur.com/uVl0d1y.png

Is that possible? Some of the reading I've done suggests "yes", some suggests "no". I just want to make sure in case I wasn't clear when i first asked :)
Title: Re: Transforming a sprite with a 3x3 matrix - where to start?
Post by: Laurent on July 08, 2013, 05:03:53 pm
It was very clear, and my answer still applies. There was a thread about this exact problem a few years ago, but I can't find it.
Title: Re: Transforming a sprite with a 3x3 matrix - where to start?
Post by: notnowlewis on July 08, 2013, 05:35:10 pm
Thanks, i tried searching but kept getting DB errors.

Thanks for clarifying things. I'll have to do a bit more thinking!
Title: Re: Transforming a sprite with a 3x3 matrix - where to start?
Post by: Laurent on July 08, 2013, 05:59:46 pm
Quote
i tried searching but kept getting DB errors.
Don't keep all the forums selected, only check the relevant ones.
Title: Re: Transforming a sprite with a 3x3 matrix - where to start?
Post by: Laurent on July 08, 2013, 06:02:41 pm
There's this (old) topic, not the one that I was looking for but it's the same issue:
http://en.sfml-dev.org/forums/index.php?topic=2893.0

I found the recent one on the french forum:
http://fr.sfml-dev.org/forums/index.php?topic=12121.0

If you don't understand you can just have a look at this picture:
https://en.wikipedia.org/wiki/File:Perspective_correct_texture_mapping.jpg
Title: Re: Transforming a sprite with a 3x3 matrix - where to start?
Post by: zsbzsb on July 08, 2013, 06:51:34 pm
squish it into a trapezoid

As laurent said the depth part of what you want is impossible to do with SFML, but if all you want is a trapezoid then use an sf::VertextArray to draw your texture and on one edge move your points closer/farther away from each other.
Title: Re: Transforming a sprite with a 3x3 matrix - where to start?
Post by: notnowlewis on July 08, 2013, 07:00:12 pm
squish it into a trapezoid

As laurent said the depth part of what you want is impossible to do with SFML, but if all you want is a trapezoid then use an sf::VertextArray to draw your texture and on one edge move your points closer/farther away from each other.

Would that clip the texture, or would I end up with the whole texture, but squished at one edge?
Title: Re: Transforming a sprite with a 3x3 matrix - where to start?
Post by: Laurent on July 08, 2013, 07:10:06 pm
Quote
Would that clip the texture, or would I end up with the whole texture, but squished at one edge?
If you assign a rectangular texture area, it will be squished. But as I said (many times in this thread) it will lead to incorrect texture mapping as shown in the various links that I posted.
Title: Re: Transforming a sprite with a 3x3 matrix - where to start?
Post by: notnowlewis on July 08, 2013, 07:22:03 pm
Quote
Would that clip the texture, or would I end up with the whole texture, but squished at one edge?
If you assign a rectangular texture area, it will be squished. But as I said (many times in this thread) it will lead to incorrect texture mapping as shown in the various links that I posted.

Ah, I missed those, I didn't have notifications turned on for this thread.

Thanks for being patient with me, I really appreciate the time you put in on the forums, and on SFML. Also, I finally understand the issues now!