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

Author Topic: Rendering with sf::Vertex and blending  (Read 1773 times)

0 Members and 1 Guest are viewing this topic.

Sholar

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Rendering with sf::Vertex and blending
« 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:


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 )
« Last Edit: May 06, 2013, 01:48:42 pm by Sholar »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Rendering with sf::Vertex and blending
« Reply #1 on: May 06, 2013, 07:49:15 am »
Texture coordinates are not normalized, as written in red in the tutorial.
Laurent Gomila - SFML developer

Sholar

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Rendering with sf::Vertex and blending
« Reply #2 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


but it doesn't look like the first screenshot.  What ideas?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Rendering with sf::Vertex and blending
« Reply #3 on: May 06, 2013, 02:34:02 pm »
The initial code uses multiplicative blending (sf::BlendMultiply), not additive.
Laurent Gomila - SFML developer

Sholar

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Rendering with sf::Vertex and blending
« Reply #4 on: May 06, 2013, 03:38:29 pm »
if set sf::BlendMultiply, black screen appears again.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Rendering with sf::Vertex and blending
« Reply #5 on: May 06, 2013, 03:42:51 pm »
Can you upload particles.png?
Laurent Gomila - SFML developer

Sholar

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Rendering with sf::Vertex and blending
« Reply #6 on: May 06, 2013, 03:51:36 pm »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Rendering with sf::Vertex and blending
« Reply #7 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.
Laurent Gomila - SFML developer

Sholar

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Rendering with sf::Vertex and blending
« Reply #8 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)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Rendering with sf::Vertex and blending
« Reply #9 on: May 06, 2013, 07:34:48 pm »
Ah, I should have seen it, shame on me too ;D
Laurent Gomila - SFML developer

 

anything