Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: image deformation (skewing/perspective)  (Read 10868 times)

0 Members and 1 Guest are viewing this topic.

legacyblade

  • Newbie
  • *
  • Posts: 25
    • View Profile
image deformation (skewing/perspective)
« on: February 07, 2014, 03:50:20 am »
Hello everyone. I'm working on making a (super basic) 2D skeletal animation system. However, I'm not sure how to skew an image. My plan is to have each piece of the body chopped up from the front, left, right, and back. Animating things on the side view will be pretty simple. I've become very familiar lately with how to move/rotate images relative to other images :P

From a couple hours of googling, it seems doing this used to be impossible because older versions of SFML didn't support vertex shaders. But after poking around in the documentation, I notice those have been added.

How would I go about skewing an image? I'm unfamiliar with shaders in general, so if anyone could post some code to get me going in the right direction that'd be awesome. I just want to be able to shrink/grow the edges of the image in real time to simulate perspective.

Thanks for reading :) I appreciate any advice you can leave.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: image deformation (skewing/perspective)
« Reply #1 on: February 07, 2014, 08:21:26 am »
If you have no idea about shaders you should go and learn GLSL. I doubt someone will write parts of it, unless it's from an old project.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

legacyblade

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: image deformation (skewing/perspective)
« Reply #2 on: February 07, 2014, 04:26:53 pm »
Alright. Any advice on where to start?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: image deformation (skewing/perspective)
« Reply #3 on: February 07, 2014, 07:02:03 pm »
To skew an image, you can use VertexArrays with quads and manipulate the coords of its corners.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Re: image deformation (skewing/perspective)
« Reply #4 on: February 07, 2014, 07:13:21 pm »
To skew an image, you can use VertexArrays with quads and manipulate the coords of its corners.
Unfortunately that won't work, since a quad is just two triangles set next to each other. Thus if you move a corner that doesn't connect the two triangles only parts of one triangle will get scewed, the rest stays the same.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: AW: Re: image deformation (skewing/perspective)
« Reply #5 on: February 07, 2014, 07:18:16 pm »
To skew an image, you can use VertexArrays with quads and manipulate the coords of its corners.
Unfortunately that won't work, since a quad is just two triangles set next to each other. Thus if you move a corner that doesn't connect the two triangles only parts of one triangle will get scewed, the rest stays the same.
A skewed quad is drawn as two triangles but it is stored as only four vertices so when one vertex is moved, any triangles that use that vertex use the updated position.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

legacyblade

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: image deformation (skewing/perspective)
« Reply #6 on: February 07, 2014, 07:21:51 pm »
The quad did exactly what I wanted, lol. Thanks for pointing that out to me Golden Eagle. I only stumbled across the concept of a quad trying to figure out how to do it in openGL, XD You're very helpful :D

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: image deformation (skewing/perspective)
« Reply #7 on: February 07, 2014, 07:30:14 pm »
Here's some code that should clarify.

  • This creates two quads. One that is double size of the original texture and one that is manipulated. There are eight vertices in "quads" because there are four per quad.
  • The image in this example was 48x48 and the texture positions were hard-coded. Sorry :p
  • If you try to use this code, you should change the texture filename too ;)

#include <SFML/Graphics.hpp>

int main()
{
        sf::RenderWindow window(sf::VideoMode(800, 600), "Skew Image", sf::Style::Default);

        sf::Texture texture;
        if (!texture.loadFromFile("image (48x48).png"))
        {
                return EXIT_FAILURE;
        }

        // vertexarray with 8 vertices (4 per quad)
        sf::VertexArray quads(sf::Quads, 8);

        // first quad (vertices 0 - 3)
        quads[0].position = sf::Vector2f(100, 100);
        quads[1].position = sf::Vector2f(196, 100);
        quads[2].position = sf::Vector2f(196, 196);
        quads[3].position = sf::Vector2f(100, 196);
        quads[0].texCoords = sf::Vector2f(0, 0);
        quads[1].texCoords = sf::Vector2f(47, 0);
        quads[2].texCoords = sf::Vector2f(47, 47);
        quads[3].texCoords = sf::Vector2f(0, 47);

        // second quad (vertices 4 - 7)
        quads[4].position = sf::Vector2f(210, 160);
        quads[5].position = sf::Vector2f(320, 100);
        quads[6].position = sf::Vector2f(292, 250);
        quads[7].position = sf::Vector2f(180, 240);
        quads[4].texCoords = sf::Vector2f(0, 0);
        quads[5].texCoords = sf::Vector2f(47, 0);
        quads[6].texCoords = sf::Vector2f(47, 47);
        quads[7].texCoords = sf::Vector2f(0, 47);

        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                        {
                                window.close();
                        }
                }
                window.clear();

                // draw both quads using the texture
                window.draw(quads, &texture);

                window.display();
        }
        return EXIT_SUCCESS;
}
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

legacyblade

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: image deformation (skewing/perspective)
« Reply #8 on: February 07, 2014, 07:36:32 pm »
Oh o.o I didn't know I could have multiple quads in a vertex array. If I ever decide to make an isometric tile game, that would simplify things quite a bit. I could have three quads that take flat textures and build a cube from the proper perspective. The documentation talks about how to make a custom drawable using quads, so I could just do something like

tile newTile("left_texture.png", "right_texture.png","top_texture.png");
newTile.set_position(2,3,5); //position 2,3 with a height of 5

Thanks for showing me that it could do that. I can see this knowledge being super useful in the future lol.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: image deformation (skewing/perspective)
« Reply #9 on: February 07, 2014, 07:40:33 pm »
I didn't know I could have multiple quads in a vertex array.
This also reduces the number of times you need to call draw() which is good for performance ;)
Remember that you can only use one texture per vertexarray though, so either use multiple vertexarrays or use multiple parts of one texture (like spritesheets/tilemaps, for example).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Re: AW: Re: image deformation (skewing/perspective)
« Reply #10 on: February 07, 2014, 07:43:39 pm »
A skewed quad is drawn as two triangles but it is stored as only four vertices so when one vertex is moved, any triangles that use that vertex use the updated position.
Yes, but imagine an image with the triangles:
00000
00001
00011
00111
01111
And now you move the lower right corner. And the image would be:
00000
00001
00011
0011
011

As you see the upper triangle stayed the same while only the lower one got skewed.

But yeah it really depends on what you want to do. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

legacyblade

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: image deformation (skewing/perspective)
« Reply #11 on: February 07, 2014, 07:57:14 pm »
@eXpl0it3r, All I wanted to do was shrink one edge and grow the other. So it does exactly what I need it to! I need to make sure to read the documentation more often XD

@Golden Eagle, didn't know that! Guess it's a good thing I decided to check back on this topic. I guess I'll either have an image with the top,left, and right textures in it, or a whole sprite sheet. (I'd have each row be a new tile texture, with each column being a different side of the cube) I'm excited to try that out sometime!

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: image deformation (skewing/perspective)
« Reply #12 on: February 07, 2014, 08:00:26 pm »
That's true, eXpl0it3r; it skews each triangle individually. It can create some pretty cool effects too, but might not do everything you could want from a quad deformation.

legacyblade seems to want to keep his quads symmetrical, however, so this should be an acceptable solution.

Plus, I have no idea how to do OpenGL and its scripting. I've looked but it's currently beyond me.
« Last Edit: February 07, 2014, 08:02:41 pm by Golden Eagle »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: image deformation (skewing/perspective)
« Reply #13 on: February 07, 2014, 08:05:06 pm »
@Golden Eagle, didn't know that! Guess it's a good thing I decided to check back on this topic. I guess I'll either have an image with the top,left, and right textures in it, or a whole sprite sheet. (I'd have each row be a new tile texture, with each column being a different side of the cube) I'm excited to try that out sometime!

You could have all of the tiles (all tiles with all sides) in one vertexarray and use the same texture for all of them ;) (i.e. store all the different textures you use in one texture and call draw once for the entire set of tiles)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*