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

Author Topic: Texture downscale not working as expected  (Read 1784 times)

0 Members and 1 Guest are viewing this topic.

Pezzza

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Texture downscale not working as expected
« on: March 31, 2019, 10:03:54 am »
Hi,

I am trying to "pixelate" a texture by applying successive downscale->render->upscale iterations on it but it doesn't work properly. For iteration count higher than 1, the result is unchanged and I really don't understand why.

I attach a minimal test code. Key A increases the iteration count and E decreases it.



#include <SFML/Graphics.hpp>

// The interesting function
void downscale(sf::RenderTexture& tex, sf::RenderTexture& tmp)
{
    // Downscale
    sf::Sprite down_sprite(tex.getTexture());
    down_sprite.scale(0.5f, 0.5f);
    tmp.draw(down_sprite);
    tmp.display();

    // Upscale
    sf::Sprite up_sprite(tmp.getTexture());
    up_sprite.scale(2.0f, 2.0f);
    tex.draw(up_sprite);
    tex.display();
}

int main()
{
    constexpr uint32_t  WIN_WIDTH = 1600;
    constexpr uint32_t WIN_HEIGHT = 900;

    sf::RenderWindow window(sf::VideoMode(WIN_WIDTH, WIN_HEIGHT), "TEST", sf::Style::Default);
    window.setVerticalSyncEnabled(false);
    window.setKeyRepeatEnabled(true);

    sf::RenderTexture render_target, tmp;
    render_target.create(WIN_WIDTH, WIN_HEIGHT);
    tmp.create(WIN_WIDTH, WIN_HEIGHT);

    float time = 0.0f;
    uint8_t intensity = 0;

    while (window.isOpen())
    {
        sf::Vector2i mousePosition = sf::Mouse::getPosition(window);

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

            if (event.type == sf::Event::KeyPressed)
            {
                switch (event.key.code)
                {
                case sf::Keyboard::Escape:
                    window.close();
                    break;
                case sf::Keyboard::A:
                    ++intensity;
                    break;
                case sf::Keyboard::E:
                    --intensity;
                    break;
                default:
                    break;
                }
            }
        }

        time += 0.1;

        render_target.clear();

        // Debug rectangle
        sf::RectangleShape rectangle(sf::Vector2f(250, 100));
        rectangle.setPosition(WIN_WIDTH / 2, WIN_HEIGHT / 2);
        rectangle.setRotation(time);

        render_target.draw(rectangle);
        render_target.display();

        for (uint8_t i(0); i < intensity; ++i)
        {
            downscale(render_target, tmp);
        }

        window.clear(sf::Color::Black);

        window.draw(sf::Sprite(render_target.getTexture()));

        window.display();
    }

    return 0;
}

 

Thank you for your help.

« Last Edit: March 31, 2019, 05:02:49 pm by Pezzza »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Texture downscale not working as expected
« Reply #1 on: March 31, 2019, 03:22:05 pm »
You should directly post your code instead of attaching it.
Laurent Gomila - SFML developer

Pezzza

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Texture downscale not working as expected
« Reply #2 on: March 31, 2019, 05:03:32 pm »
Thank you for your answer, I updated my post accordingly.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Texture downscale not working as expected
« Reply #3 on: March 31, 2019, 06:09:34 pm »
Why would you expect multiple iterations to produce different results? After the first iteration, you have a downscaled image that is just stretched; downscaling it again will produce the same output as the first time.

To pixelate further you should change the scale factor instead (with a single pass).
Laurent Gomila - SFML developer

Pezzza

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Texture downscale not working as expected
« Reply #4 on: April 01, 2019, 09:43:28 am »
Yes obviously... I'm sorry for this, I don't know why I had my mind stuck on the idea it would work.

Thank you for your (impressive) reactivity.