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

Author Topic: Ugly texture on sf::VertexArray (sf::Quads)  (Read 3376 times)

0 Members and 1 Guest are viewing this topic.

korczurekk

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • Email
Ugly texture on sf::VertexArray (sf::Quads)
« on: May 29, 2016, 02:00:06 pm »
Hi, here's my code:
#include <SFML/Graphics.hpp>

using namespace std;

const unsigned WINDOW_DEFAULT_WIDTH = 800;
const unsigned WINDOW_DEFAULT_HEIGHT = 600;

int main()
{
    sf::Texture tex;
    tex.loadFromFile("tex.png");

    sf::VertexArray quad1(sf::Quads, 4);
    quad1[0].position = sf::Vector2f(150, 110);
    quad1[3].position = sf::Vector2f(150, 210);

    quad1[0].texCoords = sf::Vector2f(0, 0);
    quad1[1].texCoords = sf::Vector2f(255, 0);
    quad1[2].texCoords = sf::Vector2f(255, 255);
    quad1[3].texCoords = sf::Vector2f(0, 255);

    sf::RenderWindow app(sf::VideoMode(WINDOW_DEFAULT_WIDTH, WINDOW_DEFAULT_HEIGHT),
                         "Qt Creator SFML template");

    sf::Clock time;
    while(app.isOpen())
    {
        /* clear screen */
        app.clear({20, 20, 20});

        /* handle events */
        for(sf::Event ev; app.pollEvent(ev);)
        {
            /* handle close button */
            if(ev.type == sf::Event::Closed)
                app.close();
        }

        quad1[1].position = sf::Vector2f(250, 110 - time.getElapsedTime().asSeconds());
        quad1[2].position = sf::Vector2f(250, 210 + time.getElapsedTime().asSeconds());

        app.draw(quad1, &tex);

        /* dipslay frame */
        app.display();
    }

    return 0;
}
 

The texture that I use:


And what I got after running my code:


Why texture looks like that?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Ugly texture on sf::VertexArray (sf::Quads)
« Reply #1 on: May 29, 2016, 02:31:14 pm »
You need to set the position of all vertices not just two.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

korczurekk

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • Email
Re: Ugly texture on sf::VertexArray (sf::Quads)
« Reply #2 on: May 29, 2016, 02:52:08 pm »
They're set in main loop.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Ugly texture on sf::VertexArray (sf::Quads)
« Reply #3 on: May 29, 2016, 03:11:13 pm »
Quads are rendered as two separate triangles. Therefore there's no way to make sure that there will be continuity of texture mapping between both -- this is only possible in 3D with perspective correction enabled.
Laurent Gomila - SFML developer

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Ugly texture on sf::VertexArray (sf::Quads)
« Reply #4 on: June 02, 2016, 01:07:19 am »
You can reduce the apparent distortion by breaking the quad (original two triangle) into multiple smaller triangles. This doesn't technically "fix" the problem but can be satisfyingly close.

If your aim is to rotate a normal sf::Sprite around as if it is a three-dimensional plane, you may be interested in Selba Ward's Sprite 3D. It automatically splits sprites (quads) into as many triangles as you want and can be used in the same way as a normal sf::Sprite.
(click to show/hide)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything