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

Author Topic: Render a rectangle with vertices using a texture and a color  (Read 1985 times)

0 Members and 1 Guest are viewing this topic.

cepro

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Render a rectangle with vertices using a texture and a color
« on: August 18, 2014, 03:22:54 am »
Hello,

This is maybe easy to achieve but (as I just started learning Graphics Programming) I couldn't manage to get it working as intended.
So I have a red rectangle rendered via a vertex array :

Now I want to draw on top of it a texture (white question mark with transparent background) using "textCoord" :

so the final result would be like this :

But this is what I get instead:

I assume this because SFML infernally multiplies the texture's pixels by the vertices' colors. If that's true, how can I change the blend mode used to draw the rectangle? And what's the appropriate mode to use in this case?

Thank you in advance.

PS: Here is a minimal code ("questionMark.png" is attached to this post):
int main()
{
    sf::RenderWindow window(sf::VideoMode(400, 240), "My window");

    sf::Texture texture;
    if (!texture.loadFromFile("questionMark.png"))
    {
        // error...
    }

    // create an array of 4 vertices that define a quad primitive
    sf::VertexArray quad(sf::Quads, 4);

    // define the position of the quad's points
    quad[0].position = sf::Vector2f(10, 10);
    quad[1].position = sf::Vector2f(100, 10);
    quad[2].position = sf::Vector2f(100, 100);
    quad[3].position = sf::Vector2f(10, 100);

    // define the color of the quad's points
    quad[0].color = sf::Color::Red;
    quad[1].color = sf::Color::Red;
    quad[2].color = sf::Color::Red;
    quad[3].color = sf::Color::Red;

    // define its texture area
    quad[0].texCoords = sf::Vector2f(0, 0);
    quad[1].texCoords = sf::Vector2f(8, 0);
    quad[2].texCoords = sf::Vector2f(8, 8);
    quad[3].texCoords = sf::Vector2f(0, 8);

    // define the render states
    sf::RenderStates states;
    //states.blendMode = ???;
    states.texture = &texture;


    // run the program as long as the window is open
    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                window.close();
        }

        // clear the window with black color
        window.clear(sf::Color::Black);

        // draw everything here...
        window.draw(quad, states);

        // end the current frame
        window.display();
    }

    return 0;
}
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Render a rectangle with vertices using a texture and a color
« Reply #1 on: August 18, 2014, 07:20:40 am »
As you said, you want to draw the question mark on top of the square, you don't want to turn your square into a question mark. So don't try to modify the square to achieve this, just use two separate entities.
Laurent Gomila - SFML developer

cepro

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: Render a rectangle with vertices using a texture and a color
« Reply #2 on: August 18, 2014, 10:19:59 am »
Thank you for the reply!

Well I guess I thought I could save up some vertices by drawing the background and the texture together (I'll be drawing a lot of rectangle entities). Too bad!

Premature optimization is the root of all evil :D