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 (600)
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 651 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.
http://imgur.com/YaP7tf4

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.
http://pastebin.java-gaming.org/4b71c810c98

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: 114
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:
1  
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 https://www.opengl.org/sdk/docs/man/html/glBlendFunc.xhtml, 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:

1  
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  
 
 
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 (29 views)
2014-12-15 09:26:44

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

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

BurntPizza (75 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 (46 views)
2014-11-29 21:32:03

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

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