Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  libdgx - Mesh Shader Blending Errors  (Read 1035 times)
0 Members and 2 Guests are viewing this topic.
Offline Phibedy

Senior Devvie


Medals: 9



« Posted 2013-01-02 22:37:06 »

Hi, I just don't get why the blue triangle is rendered, I think that Blend isn't enabled but I don't know what I could do to fix it.
You can just compile and test it on your own if you want to, the code is "working", the shaders not  Roll Eyes
What I got:

-a blue triangle
What I expected:
-a red triangle, because the opacity from the blue one is 0
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;

public class ErrorShader implements ApplicationListener {
    MeshHelper meshHelper;

    public static void main(String[] args) {
      LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
      cfg.title = "Error";
      cfg.useGL20 = true;
      cfg.width = 500;
      cfg.height = 500;
      cfg.resizable = false;
      new LwjglApplication(new ErrorShader(), cfg);
   }
   
   
    @Override
    public void create() {
        meshHelper = new MeshHelper();
        meshHelper.createMesh(new float[] { -1.0f, -1.0f, Color.toFloatBits(0, 0, 255, 0),
            0.0f, 1f, Color.toFloatBits(0, 0, 255, 0),
            1f, -1.0f, Color.toFloatBits(0, 0, 255, 0) });
    }
   
   

    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void render() {
        Gdx.graphics.getGL20().glClearColor(0.2f, 0.2f, 0.2f, 1);
        Gdx.graphics.getGL20().glClear(GL10.GL_COLOR_BUFFER_BIT);
        Gdx.graphics.getGL20().glEnable(GL20.GL_BLEND);
        Gdx.gl.glBlendFunc(GL10.GL_ONE_MINUS_SRC_ALPHA, GL10.GL_SRC_ALPHA);
        meshHelper.drawMesh();
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
        meshHelper.dispose();
    }
}

class MeshHelper {
    private Mesh mesh;
    private ShaderProgram redShader;
    private ShaderProgram blueShader;

    public MeshHelper() {
        createShader();
    }

    public void createMesh(float[] vertices) {
       mesh = new Mesh(true, vertices.length, 0,
               new VertexAttribute(Usage.Position, 2, "a_position"),
               new VertexAttribute(Usage.ColorPacked, 4, "a_color"));
        mesh.setVertices(vertices);
    }

    public void drawMesh() {
        if (mesh == null)
            throw new IllegalStateException("drawMesh called before a mesh has been created.");
       
        redShader.begin();
        mesh.render(redShader, GL20.GL_TRIANGLES);
        redShader.end();
        blueShader.begin();
        mesh.render(blueShader, GL20.GL_TRIANGLES);
        blueShader.end();
    }

    private void createShader() {
       String vertexShader = "attribute vec4 a_position;    \n"
                + "attribute vec4 a_color;       \n"
                + "varying vec4 v_color;         \n"
                + "void main()                   \n"
                + "{                             \n"
                + "   v_color = a_color;         \n"
                + "   gl_Position = a_position;  \n"
                + "}                             \n";          
       

       String fragmentBLUEShader =
             //GL ES specific stuff
               "#ifdef GL_ES\n" //
             + "#define LOWP lowp\n" //
             + "precision mediump float;\n" //
             + "#else\n" //
             + "#define LOWP \n" //
             + "#endif\n" + //
             "\n" +
             "void main() {\n" +
             "   gl_FragColor = vec4(0.0,0.0,1.0, 0.0);\n" +  //Why is that rendered?
             "}";
        blueShader = new ShaderProgram(vertexShader, fragmentBLUEShader);
        if (blueShader.isCompiled() == false)
            throw new IllegalStateException(blueShader.getLog());
   
        String fragmentREDShader =
             //GL ES specific stuff
               "#ifdef GL_ES\n" //
             + "#define LOWP lowp\n" //
             + "precision mediump float;\n" //
             + "#else\n" //
             + "#define LOWP \n" //
             + "#endif\n" + //
             "\n" +
             "void main() {\n" +
             "   gl_FragColor = vec4(1,0,0,1);\n" +
             "}";
        redShader = new ShaderProgram(vertexShader, fragmentREDShader);
        if (redShader.isCompiled() == false)
            throw new IllegalStateException(redShader.getLog());
    }
    public void dispose() {
        mesh.dispose();
        blueShader.dispose();
        redShader.dispose();
    }

}


edit:
Got it, if I use
1  
Gdx.gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
it works,
but I don't understand why the other one doesn't work. It would be great if someone could explain it
The text is german but the pictures are nice: http://wiki.delphigl.com/index.php/glBlendFunc#Beispiele


thx for help,
best regards
Offline sproingie

JGO Kernel


Medals: 202



« Reply #1 - Posted 2013-01-03 00:22:25 »

Aha, I hadn't noticed you had it reversed when I first saw the pastebin.

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
is the standard function for transparency blending for non-premultiplied alpha (the first arg is
GL_ONE
if it is premultiplied).

The way you interpret glBlendFunc is that the first arg is the factor you multiply the source color by (the color coming out of your shader or fixed function draw call), and the second is the factor you multiply the destination color by (the color already in the buffer).  The default blend equation is to then add them together to get the final color, which becomes the new destination color (you can change that equation with glBlendEquation but it's far less common).

So in the case above, it says to multiply the color of the source by its own alpha, and multiply the color of the destination by the inverse of the source's alpha, after which they'll be added together.  This means the more opaque a source color is (higher alpha) the more its color dominates and the less the destination color does.

Reversing the args effectively reverses the meaning of alpha, making 0.0 opaque and 1.0 fully transparent.  This is weird to say the least, so it's not a blend mode you see very often.
Offline davedes
« Reply #2 - Posted 2013-01-03 04:01:50 »

Two minor suggestions:

It's better practice to use Gdx.gl or Gdx.graphics.getGLCommon() for common GL functions like glClear, glClearColor, glEnable, etc. You can use GL10 or GL20 for constants.

And instead of using two shaders, you only need one. Pass the "a_color" attribute to the fragment shader in order to color your mesh. This is much more efficient as it allows you to batch the red and blue geometry in a single draw call, reduces state changes, and will speed up loading time.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Phibedy

Senior Devvie


Medals: 9



« Reply #3 - Posted 2013-01-03 09:06:51 »

thx, got it  Smiley
best regards
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (33 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (51 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (59 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!