Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (601)
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  
  GL_MAX / LibGDX problem  (Read 669 times)
0 Members and 1 Guest are viewing this topic.
Offline Raweaf

Junior Newbie


Exp: 12 years



« Posted 2014-04-08 01:11:18 »

Hi there,

I want to render 2 overlapping, transparent sprites. But even where they overlap, the resulting color should not be different compared to the resulting color of one rendered sprite. I read about blending in OpenGL and i think, glBlendEquation(GL_MAX) is what i need. Here is my LibGdx example:

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  
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.*;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class GLMaxTest implements ApplicationListener {

    OrthographicCamera cam;
    SpriteBatch batch;
    Texture tex;

    @Override
    public void create() {
        Pixmap pixmap = new Pixmap(4,4, Pixmap.Format.RGBA8888);

        cam = new OrthographicCamera(2, 2);
        cam.position.set(0, 0, 0);
        cam.update();

        batch = new SpriteBatch(1000);
        batch.setProjectionMatrix(cam.combined);
        batch.enableBlending();

        Pixmap.setBlending(Pixmap.Blending.None);
        pixmap.setColor(new Color(1.0f,0.0f,0.0f,0.3f) );
        pixmap.fill();

        tex = new Texture(pixmap);

        //Gdx.gl.glBlendEquation(GL30.GL_MAX);
        //Gdx.gl.glBlendEquationSeparate(GL30.GL_FUNC_ADD, GL30.GL_MAX);
    }

    @Override
    public void dispose() { }

    @Override
    public void pause() { }

    @Override
    public void render() {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

        batch.begin();
        batch.draw(tex,0,0);
        batch.draw(tex,-1,-1);
        batch.end();
    }

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

    @Override
    public void resume() { }

    public static void main(String[] args) {
        LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();

        cfg.title = "";
        cfg.useGL30 = true;
        cfg.width = 640;
        cfg.height = 480;
        cfg.resizable = false;

        new LwjglApplication(new GLMaxTest(), cfg);
    }
}


The result:

Woking as intended so far.

When I added "Gdx.gl.glBlendEquation(GL30.GL_MAX);", this is what I got:

The alpha values seem to get completly ignored.

Then i tried "Gdx.gl.glBlendEquationSeparate(GL30.GL_FUNC_ADD, GL30.GL_MAX);".  But this line does not change anything. This is what was getting displayed again:


What am I missing? Or is this maybe even a LibGDX bug?

Oh, and yeah, first post Smiley
Offline quew8

JGO Coder


Medals: 32



« Reply #1 - Posted 2014-04-08 21:46:47 »

From your description, to me it sounds like you don't want blending enabled at all. Obviously I've got that wrong but perhaps you could explain a little more clearly or with a picture of what you want to be happening. Or perhaps it is just me being thick and everyone else will understand.
Offline ryeTech

Junior Newbie





« Reply #2 - Posted 2014-04-09 00:51:17 »

I'm not too sure how you set up is, but I''m going to assume a lot of things.

I assume:
You're using a vertex and fragment shaders (If you're not you should think about it!)

Anyway...

You could add in this to your openGL init code

1  
2  
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);


This should turn on blending where the alpha channel is not ignored
Then in your fragment shader you can multiple the 'amount of alpha you want' by the fragment shaders texture you are using

1  
2  
//Fragment Shader code line for using alpha
gl_FragColor = texture2D(uTexture, vTextureCoordinate) * myAlphaValue;



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

« JGO Bitwise Duke »


Medals: 365
Projects: 2
Exp: 8 years



« Reply #3 - Posted 2014-04-09 01:33:26 »

When blending is enabled, this is what is written to the framebuffer:

1  
framebuffer = func(sourceColor * sourceFactor, destinationColor * destinationFactor)


func = the function set by glBlendEquation(). This defaults to addition, in which case func() is a function that takes in two values and adds them together. GL_MAX = max() instead of addition.
sourceColor = the raw color you're writing (RGBA).
sourceFactor = the first argument to glBlendFunc().
destinationColor = the color already in the framebuffer.
destinationFactor = the second argument to glBlendFunc().

If you change the alpha value of the source color, the resulting RGB values are completely unaffected. To me it sounds like you want to multiply the source RGB values by the source alpha, which means you need to call glBlendFunc() with these arguments:

1  
glBlendFunc(GL_SRC_ALPHA, GL_ONE);


This results in the following blending equation:
1  
framebuffer = max(sourceColor * sourceColor.alpha, destinationColor * 1)


Myomyomyo.
Offline Raweaf

Junior Newbie


Exp: 12 years



« Reply #4 - Posted 2014-04-09 09:11:00 »

According to the OpenGl documentation, GL_MAX ignores the source and destination factors and only uses the rgba values.

But I figured it out by myself yesterday. I just did not fully understand how blending actually works when I opened this thread. I draw the sprites with glBlendEquation(GL_MAX) to an empty framebuffer now. Then I draw the framebuffer with glBlendEquation(GL_FUNC_ADD) and glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) to the screen. It works like a charm now.

Thanks for your replies anway Smiley
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 (30 views)
2014-12-15 09:26:44

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

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

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

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

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

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

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

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

toopeicgaming1999 (30 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!