SFML community forums

Help => Graphics => Topic started by: Bogdan on August 29, 2017, 03:58:21 pm

Title: Keeping texture resolution and position independently of zoom level
Post by: Bogdan on August 29, 2017, 03:58:21 pm
Hi there,

I'm playing around with textured vertex array triangles and have some questions.

More specifically it's about keeping the same texture resolution independently of the zoom level and with my formula the resolution really stays the same (as intended), but the texture moves around strangely whenever I zoom in or out.

Is there an algorithm to prevent this from happening.
What I need is a solution, where the (endlessly repeated) texture moves with the view while zooming the view.

I know that I have to add an unknown number/calculation to my formula.........
Many thanks in advance for any help!

#include <SFML/Graphics.hpp>

int main()
{
        sf::RenderWindow window(sf::VideoMode(1000,1000), "Map", sf::Style::Close);
        sf::View view(sf::Vector2f(500, 500), sf::Vector2f(1000, 1000));
       
        float zoomvariable = 1;
       
        sf::Texture texture;
        texture.loadFromFile("grass.png");
        texture.setRepeated(true);
       
        sf::VertexArray triangle(sf::Triangles, 3);
        triangle[0].position = sf::Vector2f(0, 0);
        triangle[1].position = sf::Vector2f(900, 0);
        triangle[2].position = sf::Vector2f(600, 900);
        triangle[0].texCoords = sf::Vector2f(triangle[0].position.x,triangle[0].position.y);
        triangle[1].texCoords = sf::Vector2f(triangle[1].position.x,triangle[1].position.y);
        triangle[2].texCoords = sf::Vector2f(triangle[2].position.x,triangle[2].position.y);
       
        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        switch (event.type)
                        {
                        case sf::Event::Closed:
                                window.close();
                                break;
                        case sf::Event::KeyPressed:
                                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Add))
                                {
                                        zoomvariable=zoomvariable*0.9f;
                       
                                        triangle[0].texCoords = sf::Vector2f(triangle[0].position.x/zoomvariable,triangle[0].position.y/zoomvariable);
                                        triangle[1].texCoords = sf::Vector2f(triangle[1].position.x/zoomvariable,triangle[1].position.y/zoomvariable);
                                        triangle[2].texCoords = sf::Vector2f(triangle[2].position.x/zoomvariable,triangle[2].position.y/zoomvariable);
                       
                                        view.zoom(0.9f);
                                }
                                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Subtract))
                                {
                                        zoomvariable=zoomvariable*1.1f;
                       
                                        triangle[0].texCoords = sf::Vector2f(triangle[0].position.x/zoomvariable,triangle[0].position.y/zoomvariable);
                                        triangle[1].texCoords = sf::Vector2f(triangle[1].position.x/zoomvariable,triangle[1].position.y/zoomvariable);
                                        triangle[2].texCoords = sf::Vector2f(triangle[2].position.x/zoomvariable,triangle[2].position.y/zoomvariable);
                       
                                        view.zoom(1.1f);
                                }
                        }
                }
        window.clear();
        window.setView(view);
        window.draw(triangle,&texture);
        window.display();
        }
        return 0;
}

Title: Re: Keeping texture resolution and position independently of zoom level
Post by: Hapax on August 30, 2017, 03:53:12 pm
From what it sounds like you want to do, you would need to scale the texture co-ordinates in the same way that you scale the vertices' positions.
Since you need to scale them manually anyway, it might be simpler/clearer to calculate the positions' scaling manually. That way, you can simply just 'copy' the positions to the texture co-ordinates the way you do at the beginning.