Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (764)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (852)
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  
  [SOLVED] Alpha blending causing transparency through window (LWJGL)  (Read 3042 times)
0 Members and 1 Guest are viewing this topic.
Offline nenofite

Junior Newbie

Exp: 4 years

« Posted 2014-05-01 02:26:24 »

I'm having a strange problem with alpha in LWJGL.  I'm drawing 2D graphics (so no depth-testing going on) which have transparency.  Wherever a translucent graphic is drawn onto the screen, it seems to be overriding the alpha of those pixels--so if I draw a translucent graphic over a fully opaque background, it causes the resulting image to be transparent despite the background.  This is ultimately causing my game window to have transparent holes in it that show through to whatever window is behind the game.

Here's a partial screenshot.  You can see the text from the IDE showing through spots on the screen where some translucent shadow graphics were drawn.

Here's the relevant code.  initGl() is called once during setup.  update() is called in a loop to draw the screen.  drawTexture() is just a helper function.

side note: the Texture class referenced is not from Slick-Util, it's a class I made myself, thus using image width and height of 1 instead of using getWidth() and getHeight() within the drawTexture function

I'm not new to Java but I am rather new to LWJGL and OpenGL in general.  Any help is appreciated, I've been trying to figure this out for the past couple days without any luck.
Offline matheus23

JGO Kernel

Medals: 138
Projects: 3

You think about my Avatar right now!

« Reply #1 - Posted 2014-05-01 12:29:55 »

Uh, that's an amazing effect! Grin
I have seriously never seen that before...

The only thing that comes to my mind right now might be to try to set the clear color:
GL11.glClearColor(1f, 1f, 1f, 1f);

This should set the clearing color to a white, opaque color. Maybe it uses 0, 0, 0, 0 as clear color by default, which would be transparent black Smiley

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline nenofite

Junior Newbie

Exp: 4 years

« Reply #2 - Posted 2014-05-01 23:11:52 »

I have tried setting the clear color to fully opaque using glClearColor(), however unfortunately that had no effect on the problem.  Thanks for the suggestion though Smiley

Here's my understanding of the problem so far (and keep in mind I'm new to OpenGL):

The screen alpha channel is set to opaque when the fully opaque background is drawn (the first layer of textured quads that are drawn).  When a translucent pixel is drawn over that (the problem doesn't occur with fully transparent pixels, only the translucent ones) it causes the alpha channel to fall from being fully opaque to being translucent.  Then the window manager (I'm running with Gnome 3) sees these translucent pixels on the resulting screen image and respects them, causing the window to be transparent and show the windows behind it.

I think the reason the opaque background is turning translucent has to do with how the alpha blending is occurring.  I am using the settings GL_SRC_APLHA for source and GL_ONE_MINUS_SRC_ALPHA for destination.

Referring to the OpenGL documentation at, this means that the resulting pixel value after blending a translucent graphic onto an opaque one has for its alpha value (using floats between 0 and 1):

(1 - sourceAlpha) * destAlpha + sourceAlpha * sourceAlpha

Which by the math means that blending a translucent graphic onto an opaque graphic will never produce an opaque result.  Either I am misunderstanding the math behind this blend mode and the problem is elsewhere, or this is indeed how this blending mode works, in which case the question is a matter of which blend mode I should be using instead to avoid this problem.

Hopefully this more detailed information can spark some ideas of what's going wrong or what I'm misunderstanding about the OpenGL system.  Once again, any help is appreciated.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline nenofite

Junior Newbie

Exp: 4 years

« Reply #3 - Posted 2014-05-03 02:28:18 »

I managed to solve the problem Grin  For those interested:

To test my hypothesis (above), I ran the program in Windows.  The problem went away.  This means that the problem was coming from the display having an alpha channel, causing it to have holes.  In Windows, alpha channels don't seem to be supported for the window, at least not by default.  In Linux or specifically in Gnome 3, alpha channels are supported by default.

This means that when the display was created, an alpha channel was given to it.  This channel was unwanted.  To prevent getting the alpha channel, one line of code had to be changed in the GL initialization:

Display.create(new PixelFormat(0, 0, 0));

This just asks for a display with no alpha buffer (that's the first argument to PixelFormat, the other ones are for depth and stencil, which are irrelevant to this problem).  Without an alpha buffer, the display ignores the alpha value of the blending result, and thus the entire problem is avoided.
Pages: [1]
  ignore  |  Print  

EgonOlsen (591 views)
2018-06-10 19:43:48

EgonOlsen (694 views)
2018-06-10 19:43:44

EgonOlsen (478 views)
2018-06-10 19:43:20

DesertCoockie (831 views)
2018-05-13 18:23:11

nelsongames (1026 views)
2018-04-24 18:15:36

nelsongames (1083 views)
2018-04-24 18:14:32

ivj94 (1664 views)
2018-03-24 14:47:39

ivj94 (590 views)
2018-03-24 14:46:31

ivj94 (1501 views)
2018-03-24 14:43:53

Solater (568 views)
2018-03-17 05:04:08
Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46

Deployment and Packaging
by philfrei
2018-08-19 23:53:08

Deployment and Packaging
by philfrei
2018-08-19 23:50:04

Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39 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‑
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!