SFML community forums

Help => Graphics => Topic started by: PaperBirdMaster on September 13, 2021, 06:10:38 pm

Title: Rendering quads show nothing if texture is ussed
Post by: PaperBirdMaster on September 13, 2021, 06:10:38 pm
Hi there!

My goal is to render some quads to simulate an isometric cube, let's take Minecraft icon as reference:

(https://th.bing.com/th/id/OIP.6n1Zbe2m0LqekS6l4a3Q5AHaHa?pid=ImgDet&rs=1)

So, I'm loading this 32w16h texture without issues:

(https://i.stack.imgur.com/uTaqv.png)

And creating and drawing sf::VertexArray like this in the window.isOpen() loop:

        sf::Texture grass;
        if (!grass.loadFromFile("grass.png"))
                return 0;

...
...

                window.clear();

                sf::VertexArray cube(sf::Quads);
                const sf::Vector2f position{ 100.f, 100.f };
                constexpr float size = 50.f;

                cube.append({ position - (vertex[5] * size),    {}, { 0.f,  0.f} });
                cube.append({ position,                         {}, {16.f,  0.f} });
                cube.append({ position - (vertex[3] * size),    {}, {16.f, 16.f} });
                cube.append({ position - (vertex[4] * size),    {}, {0.f,  16.f} });

                cube.append({ position - (vertex[5] * size),    {}, {16.f,  0.f} });
                cube.append({ position - (vertex[0] * size),    {}, {32.f,  0.f} });
                cube.append({ position - (vertex[1] * size),    {}, {32.f, 16.f} });
                cube.append({ position,                         {}, {16.f, 16.f} });

                cube.append({ position,                         {}, { 0.f,  0.f} });
                cube.append({ position - (vertex[1] * size),    {}, {16.f,  0.f} });
                cube.append({ position - (vertex[2] * size),    {}, {16.f, 16.f} });
                cube.append({ position - (vertex[3] * size),    {}, {0.f,  16.f} });

                window.draw(cube, &grass);
                window.display();
This results in a black screen (except for the imgui controls I've added). The vertex static array that I'm multiplying values with is defined like this:
template <auto degrees>
constexpr auto radians = degrees * std::numbers::pi_v<decltype(degrees)> / 180;

/*
    0 0 _______ 1 60
       /   y  /|
5 300 /______/ |
      |      |x| 2 120
      |  z   | /
      |______|/
4 240        3 180
*/


const static sf::Vector2f vertex[]
{
        {std::sin(radians<  0.f>), std::cos(radians<  0.f>)},
        {std::sin(radians< 60.f>), std::cos(radians< 60.f>)},
        {std::sin(radians<120.f>), std::cos(radians<120.f>)},
        {std::sin(radians<180.f>), std::cos(radians<180.f>)},
        {std::sin(radians<240.f>), std::cos(radians<240.f>)},
        {std::sin(radians<300.f>), std::cos(radians<300.f>)},
};
It just contains the vertex possitions of an exagon, nothing special here.

If instead of rendering using a texture I just render using vertex color, there's no problem displaying the cube:

                window.clear();

                sf::VertexArray cube(sf::Quads);
                const sf::Vector2f position{ 100.f, 100.f };
                constexpr float size = 50.f;

                cube.append({ position - (vertex[5] * size),    sf::Color::Red });
                cube.append({ position,                                                 sf::Color::Red });
                cube.append({ position - (vertex[3] * size),    sf::Color::Red });
                cube.append({ position - (vertex[4] * size),    sf::Color::Red });

                cube.append({ position - (vertex[5] * size),    sf::Color::Green });
                cube.append({ position - (vertex[0] * size),    sf::Color::Green });
                cube.append({ position - (vertex[1] * size),    sf::Color::Green });
                cube.append({ position,                                                 sf::Color::Green });

                cube.append({ position,                                                 sf::Color::Blue });
                cube.append({ position - (vertex[1] * size),    sf::Color::Blue });
                cube.append({ position - (vertex[2] * size),    sf::Color::Blue });
                cube.append({ position - (vertex[3] * size),    sf::Color::Blue });

                window.draw(cube, &grass);
[code=cpp]
Result:
[img]https://i.stack.imgur.com/UDdgy.png[/img]

If I don't pass the texture to the [tt]draw[/tt] function the plain colors look brighter:
[img]https://i.stack.imgur.com/uxkTz.png[/img]

Which makes me think that it might have something to do with the lack of light on the scene? I don'
t know, I'm pretty lost here.

Any guidance would be appreciated. Thank you very much.
Title: Re: Rendering quads show nothing if texture is ussed
Post by: kojack on September 13, 2021, 07:35:16 pm
One potential issue, the UV coordinates for a vertex aren't in pixels, they are 0-1 for full width and height. Higher is wrap around (which may not be enabled, I can't remember the default).
So for example {32.f, 16.f} should be {1.f, 1.f} (the texture isn't square, so 1,1 means full width (32 pixels) and full height (16 pixels)).
Title: Re: Rendering quads show nothing if texture is ussed
Post by: PaperBirdMaster on September 13, 2021, 08:23:46 pm
One potential issue, the UV coordinates for a vertex aren't in pixels, they are 0-1 for full width and height. Higher is wrap around (which may not be enabled, I can't remember the default).
So for example {32.f, 16.f} should be {1.f, 1.f} (the texture isn't square, so 1,1 means full width (32 pixels) and full height (16 pixels)).
Nope  :-[ acoording to the documentation:

"Texture coordinates are defined in pixels (just like the textureRect of sprites and shapes). They are not normalized (between 0 and 1), as people who are used to OpenGL programming might expect".

https://www.sfml-dev.org/tutorials/2.5/graphics-vertex-array.php
Title: Re: Rendering quads show nothing if texture is ussed
Post by: kojack on September 14, 2021, 12:23:42 am
Ah, my mistake.
9+ years of teaching SFML and I've never used a textured vertex array I guess. :)

Title: Re: Rendering quads show nothing if texture is ussed
Post by: G. on September 14, 2021, 01:58:16 am
Don't set your vertex color to the default color constructor, it's black. Set it to white.
Or use the vertex constructor that only takes position and texture coordinates, its color will be white by default.
Title: Re: Rendering quads show nothing if texture is ussed
Post by: PaperBirdMaster on September 14, 2021, 09:21:47 pm
Don't set your vertex color to the default color constructor, it's black. Set it to white.
Or use the vertex constructor that only takes position and texture coordinates, its color will be white by default.
That was it! :)