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

Author Topic: Textured Quad blending problem (Solved)  (Read 5122 times)

0 Members and 2 Guests are viewing this topic.

yewbie

  • Newbie
  • *
  • Posts: 6
    • View Profile
Textured Quad blending problem (Solved)
« on: February 19, 2015, 08:52:21 pm »
I am having a strange problem, and forgive me if this has been solved as I've looked through quite a bit of the forums and found one other issue similar to this but it wasn't really resolved other than "Write a pixel shader" (which I don't really know how to do).

I am currently rending a 2d grid of 64x64 tiles using a vertexarray and quads.

I am using the following function to add quads with per vertex alpha blending to my vertex array:
 // //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public void AddGroundTileWithAlphaAndColorAndTransparency(float X, float Y, int TileNumber, int Scale, Color V0, Color V1, Color V2, Color V3)
        {

            FloatRect MyGraphic = Tool_GetRectForTile(TileNumber, 64, Texture);
            Vertex MyVert1 = new Vertex(new Vector2f(X, Y), V0, new Vector2f(MyGraphic.Left, MyGraphic.Top));
            Vertex MyVert2 = new Vertex(new Vector2f(X + Scale, Y), V1, new Vector2f(MyGraphic.Left + MyGraphic.Width, MyGraphic.Top));
            Vertex MyVert3 = new Vertex(new Vector2f(X + Scale, Y + Scale),V2, new Vector2f(MyGraphic.Left + MyGraphic.Width, MyGraphic.Top + MyGraphic.Height));
            Vertex MyVert4 = new Vertex(new Vector2f(X, Y + Scale), V3, new Vector2f(MyGraphic.Left, MyGraphic.Top + MyGraphic.Height));
           
            VT_VertexData.Append(MyVert1);
            VT_VertexData.Append(MyVert2);
            VT_VertexData.Append(MyVert3);
            VT_VertexData.Append(MyVert4);
        }
 

Using this method I am getting some strange blending results which I can only assume are coming from OpenGL translating my quad into 2 triangles.

Here is an example of 2 different "fringes" being generated on the fly, #1 (V0, V1, V3) with an alpha of zero this works.
But #2 (V0,V1,V2) with an alpha of zero does the blend improperly.



Here is a close of how I think the triangles are being rendered properly.


Here is an improper alpha blend:


Is there anything I can do to make the alpha blend occour across the whole quad and not just the individual triangles?
A
« Last Edit: February 19, 2015, 10:43:05 pm by yewbie »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Textured Quad blending problem
« Reply #1 on: February 19, 2015, 10:00:46 pm »
You might be interested in this thread: http://en.sfml-dev.org/forums/index.php?topic=17525.0

It's an OpenGL thing. So to my knowledge the best method would be to go with your own texture for consistent blending.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

yewbie

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: AW: Textured Quad blending problem
« Reply #2 on: February 19, 2015, 10:13:07 pm »
You might be interested in this thread: http://en.sfml-dev.org/forums/index.php?topic=17525.0

It's an OpenGL thing. So to my knowledge the best method would be to go with your own texture for consistent blending.

Ah I see, darn I was hoping there was some OpenGL flag I could toggle, I wonder if depending on the orientation of my vertex if I can change the triangles to blend (this won't solve all of my problems) but perhaps on a NorthEast corner blend instead of Appending V1,V2,V3,V4 I append V2,V3,V4,V1.

Edit:
I tried this and it does indeed render the triangles and the blend properly by doing this if anyone else needs a semi work around

edit edit:
Here is the result, not perfect but probably (good enough)
« Last Edit: February 19, 2015, 10:20:21 pm by yewbie »

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Textured Quad blending problem
« Reply #3 on: February 19, 2015, 10:23:24 pm »
Like eXpl0it3r said, this is an OpenGL "thing". GPUs nowadays, and even long ago, only ever understood how to work with triangles, so any time you send it anything else such as quads, they will be split into 2 triangles as the GPU sees fit. If you want to control how the triangles are generated, you will have to do it yourself.

Like any other hardware/software, the OpenGL pipeline is nothing but a big machine that you stick stuff into and it produces visible output on your monitor. SFML relies on legacy OpenGL which did not make use of a programmable pipeline (i.e. shaders). The pipeline always performs the same operations on vertex data, and in your case, it is interpolating the vertices over the "wrong" triangles which it had to guess since you sent it quads.

The most elegant way to solve this is by exploiting the programmability of the shader pipeline through your own shaders. Since you don't want to use them, the easiest solution for you would be to do the quad splitting yourself (use a VertexArray of triangles instead). When you run into a corner that gets blended wrong, just change the split direction and it should work properly again.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

yewbie

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Textured Quad blending problem
« Reply #4 on: February 19, 2015, 10:28:09 pm »
Thank you for the reply, I ended up just appending my triangles in a different order to make OpenGL render the triangle on the opposite axis.

Turned out good once I got all the cases for the flip in: