Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
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  
  Can't see behind transparent object with another transparent object?  (Read 571 times)
0 Members and 1 Guest are viewing this topic.
Offline CyanPrime
« Posted 2011-12-12 19:47:26 »

Okay, so my problem is that I've got two enemy sprites on screen, and they each show whats behind them, like if I have a wall behind them then it shows them, then the wall, but if one enemy is half behind the other it gets cut off. Any idea why this is happening?



This is how I got openGL set up:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
private void initGL() {

      /* OpenGL */
      int width = Display.getDisplayMode().getWidth();
      int height = Display.getDisplayMode().getHeight();

      GL11.glViewport(0, 0, width, height); // Reset The Current Viewport
     GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
     GL11.glLoadIdentity(); // Reset The Projection Matrix
     GLU.gluPerspective(45.0f, ((float) width / (float) height), 0.1f, 1000.0f); // Calculate The Aspect Ratio Of The Window
     GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix
     GL11.glLoadIdentity(); // Reset The Modelview Matrix

      GL11.glShadeModel(GL11.GL_SMOOTH); // Enables Smooth Shading
     GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
     GL11.glClearDepth(1.0f); // Depth Buffer Setup
     GL11.glEnable(GL11.GL_DEPTH_TEST); // Enables Depth Testing
     GL11.glDepthFunc(GL11.GL_LEQUAL); // The Type Of Depth Test To Do
     GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST); // Really Nice Perspective Calculations
     GL11.glEnable(GL11.GL_TEXTURE_2D);
      GL11.glEnable(GL11.GL_BLEND);
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
      }
Offline Mickelukas

JGO Ninja


Medals: 39
Projects: 2


Java guru wanabee


« Reply #1 - Posted 2011-12-12 20:02:59 »

In which order do you draw them? Using blending you always need to draw the objects furthest away first (it's quite annoying, I know Wink)

Mike

Offline CyanPrime
« Reply #2 - Posted 2011-12-12 20:05:43 »

Oh my! is there a way around that? Maybe using something else?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Roquen

JGO Ninja


Medals: 66



« Reply #3 - Posted 2011-12-12 20:10:47 »

Are you sure about the alpha channel in the texture?
Offline CyanPrime
« Reply #4 - Posted 2011-12-12 20:12:18 »

Well, I can see behind it when it's behind a wall, so yeah.
Offline princec
« League of Dukes »

JGO Kernel


Medals: 196
Projects: 3


Eh? Who? What? ... Me?


« Reply #5 - Posted 2011-12-12 20:19:54 »

Problem is the depth buffer. The easiest way around it is to sort by distance, as said. It's not as hard as it sounds.

Cas Smiley

Offline Mickelukas

JGO Ninja


Medals: 39
Projects: 2


Java guru wanabee


« Reply #6 - Posted 2011-12-12 20:32:54 »

If you only use alpha values of 255 and 0 (nothing in-between) you can use something like
1  
2  
GL11.glEnable(GL11.GL_ALPHA_TEST);
GL11.glAlphaFunc(GL11.GL_GREATER, 0.9f);


Then you don't have to sort it. But if you ever want to add something semi transparent you better build the engine correctly now already Smiley

Mike

Offline CyanPrime
« Reply #7 - Posted 2011-12-12 21:03:40 »

If you only use alpha values of 255 and 0 (nothing in-between) you can use something like
1  
2  
GL11.glEnable(GL11.GL_ALPHA_TEST);
GL11.glAlphaFunc(GL11.GL_GREATER, 0.9f);


Then you don't have to sort it. But if you ever want to add something semi transparent you better build the engine correctly now already Smiley

Mike
That worked, thank you Grin
Offline theagentd
« Reply #8 - Posted 2011-12-13 04:34:50 »

You should read up on how the depth buffer (AKA z-buffer) works, or in this case, doesn't. It doesn't work for transparent surfaces unless you hack around it with sorting and disabling depth writes or alpha testing.

Myomyomyo.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (120 views)
2013-05-17 21:29:12

alaslipknot (127 views)
2013-05-16 21:24:48

gouessej (158 views)
2013-05-16 00:53:38

gouessej (149 views)
2013-05-16 00:17:58

theagentd (163 views)
2013-05-15 15:01:13

theagentd (148 views)
2013-05-15 15:00:54

StreetDoggy (192 views)
2013-05-14 15:56:26

kutucuk (216 views)
2013-05-12 17:10:36

kutucuk (215 views)
2013-05-12 15:36:09

UnluckyDevil (218 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.144 seconds with 21 queries.