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

Author Topic: Vertex array with a texture is invisible  (Read 1168 times)

0 Members and 1 Guest are viewing this topic.

Justbod

  • Newbie
  • *
  • Posts: 9
    • View Profile
Vertex array with a texture is invisible
« on: November 28, 2022, 08:47:13 pm »
I want to draw a single vertex array with a texture. The texture is a 34x34 pixel .png file

I have the following function :

void drawMap(sf::RenderWindow& renderWindow)
{
        // create a quad
        sf::VertexArray quad(sf::Quads, 4);

        // define it as a square, located at (0, 0) and with size 34x34
        quad[0].position = sf::Vector2f(0.f, 0.f);
        quad[1].position = sf::Vector2f(34.f, 0.f);
        quad[2].position = sf::Vector2f(34.f, 34.f);
        quad[3].position = sf::Vector2f(0.f, 34.f);

        // define its texture area to be a 34x34 square starting at (0, 0)
        quad[0].position = sf::Vector2f(0.f, 0.f);
        quad[1].position = sf::Vector2f(34.f, 0.f);
        quad[2].position = sf::Vector2f(34.f, 34.f);
        quad[3].position = sf::Vector2f(0.f, 34.f);

        renderWindow.draw(quad, &grass_block);

}
 

This is how I initialize & load the texture


//initialize grassblock
sf::Texture grass_block;



        if (!grass_block.loadFromFile("grass_block.png"))
        {
                std::cout << "Could not load grass_block!";
        }
 

When my texture does not load, a white square gets drawn with right size & position. When my texture does load properly nothing shows up.
« Last Edit: November 29, 2022, 01:42:11 pm by Justbod »
🧃🦔

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Vertex array with a texture is invisible
« Reply #1 on: November 29, 2022, 12:03:23 am »
You're setting the position twice. The second block of code should use texCoords instead of position.