SFML community forums

Help => Graphics => Topic started by: Sholar on May 06, 2013, 05:04:35 am

Title: Rendering with sf::Vertex and blending
Post by: Sholar on May 06, 2013, 05:04:35 am
Hi guys.
the following is code from the tutorial hge
quad.tex=hge->Texture_Load("particles.png");
// Set up quad which we will use for rendering sprite
quad.blend=BLEND_ALPHAADD | BLEND_COLORMUL | BLEND_ZWRITE;
      for(int i=0;i<4;i++)
     {
        // Set up z-coordinate of vertices
        quad.v[i].z=0.5f;
        // Set up color. The format of DWORD col is 0xAARRGGBB
        quad.v[i].col=0xFFFFA000;
     }

// Set up quad's texture coordinates.
// 0,0 means top left corner and 1,1 -
// bottom right corner of the texture.
quad.v[0].tx=96.0/128.0; quad.v[0].ty=64.0/128.0;
quad.v[1].tx=128.0/128.0; quad.v[1].ty=64.0/128.0;
quad.v[2].tx=128.0/128.0; quad.v[2].ty=96.0/128.0;
quad.v[3].tx=96.0/128.0; quad.v[3].ty=96.0/128.0;
...

hge->RenderQuad(&quad);
 
result:
(http://s61.radikal.ru/i172/1305/24/a9cf8378e6e8.png)

And i tryed repeat it on sfml
int main()
{
    // Create the main window
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");

        sf::Texture tex;
        if(!tex.loadFromFile("particles.png"))
        {
                return -1;
        }

        sf::Vertex v[4];
        v[0].position.x = 300;          v[0].position.y = 300;
        v[1].position.x = 300+32;                     v[1].position.y = 300;
        v[2].position.x = 300+32;                     v[2].position.y = 332;
        v[3].position.x = 300;          v[3].position.y = 332;

        for(int i = 0; i<4; i++)
                v[i].color = sf::Color(255,160,0);

       
        v[0].texCoords.x = 96.0/128.0;          v[0].texCoords.y = 64.0/128.0;
        v[1].texCoords.x = 128.0/128.0;         v[1].texCoords.y = 64.0/128.0;
        v[2].texCoords.x = 128.0/128.0;         v[2].texCoords.y = 96.0/128.0;
        v[3].texCoords.x = 96.0/128.0;          v[3].texCoords.y = 96.0/128.0;

       

        sf::RenderStates state;

        state.blendMode = sf::BlendMode::BlendAdd;
        state.texture = &tex;
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }


      //  window.clear();
       
        window.draw(v,4,sf::PrimitiveType::Quads,state);
        window.display();
    }
    return EXIT_SUCCESS;
}

 

but I see the black screen. Why? How i can fix it? Thanks.
P.S. sorry for my english )
Title: Re: Rendering with sf::Vertex and blending
Post by: Laurent on May 06, 2013, 07:49:15 am
Texture coordinates are not normalized, as written in red in the tutorial.
Title: Re: Rendering with sf::Vertex and blending
Post by: Sholar on May 06, 2013, 02:18:33 pm
Thank you, Laurent. I fixed it so:
v[0].texCoords.x = 96.0;                v[0].texCoords.y = 64.0;
v[1].texCoords.x = 128.0;               v[1].texCoords.y = 64.0;
v[2].texCoords.x = 128.0;               v[2].texCoords.y = 96.0;
v[3].texCoords.x = 96.0;                v[3].texCoords.y = 96.0;
 

Now it looks as
(http://i076.radikal.ru/1305/76/f6f25fdb22e6.png)

but it doesn't look like the first screenshot.  What ideas?
Title: Re: Rendering with sf::Vertex and blending
Post by: Laurent on May 06, 2013, 02:34:02 pm
The initial code uses multiplicative blending (sf::BlendMultiply), not additive.
Title: Re: Rendering with sf::Vertex and blending
Post by: Sholar on May 06, 2013, 03:38:29 pm
if set sf::BlendMultiply, black screen appears again.
Title: Re: Rendering with sf::Vertex and blending
Post by: Laurent on May 06, 2013, 03:42:51 pm
Can you upload particles.png?
Title: Re: Rendering with sf::Vertex and blending
Post by: Sholar on May 06, 2013, 03:51:36 pm
http://rghost.ru/45806114
Title: Re: Rendering with sf::Vertex and blending
Post by: Laurent on May 06, 2013, 04:43:21 pm
You should get a different result, with additive and multiplicative blending.

I'll test your code and see if I can reproduce it.
Title: Re: Rendering with sf::Vertex and blending
Post by: Sholar on May 06, 2013, 07:26:41 pm
Laurent, thank you very much for your answers! If uncommented window.clear () and a set of SF :: BlendAdd, everything works fine. I'm so ashamed)
Title: Re: Rendering with sf::Vertex and blending
Post by: Laurent on May 06, 2013, 07:34:48 pm
Ah, I should have seen it, shame on me too ;D