Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
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 751 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 Mike

JGO Wizard


Medals: 69
Projects: 2
Exp: 5 years


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

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
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
« 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

JGO Kernel


Medals: 282
Projects: 3
Exp: 16 years


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 Mike

JGO Wizard


Medals: 69
Projects: 2
Exp: 5 years


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

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
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.

 

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

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (25 views)
2014-04-15 18:08:23

BurntPizza (20 views)
2014-04-15 03:46:01

UprightPath (36 views)
2014-04-14 17:39:50

UprightPath (18 views)
2014-04-14 17:35:47

Porlus (34 views)
2014-04-14 15:48:38

tom_mai78101 (60 views)
2014-04-10 04:04:31

BurntPizza (118 views)
2014-04-08 23:06:04

tom_mai78101 (218 views)
2014-04-05 13:34:39

trollwarrior1 (185 views)
2014-04-04 12:06:45

CJLetsGame (192 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!