Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (744)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (825)
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  
  Sprites & transparency  (Read 3417 times)
0 Members and 1 Guest are viewing this topic.
Offline rdcarvallo

Senior Devvie


Projects: 5
Exp: 15 years


2D Java games forever!


« Posted 2011-09-10 07:47:44 »

Hi everyone!!

   I'm programming a little game, faking isometric view with ortho + rotate (x & y) + billboarding sprites.

   To optimize the GL calls, my SpriteManager is using a Vertex Buffer to draw all the sprite on screen in a single call.
Creation Code
1  
2  
3  
4  
5  
    spritePool = new Sprite[MAX_SPRITES];
    vertexData = new float[3*6*MAX_SPRITES]; //6 vertices por sprite
    ByteBuffer vbb = ByteBuffer.allocateDirect(vertexData.length * 4);
    vbb.order(ByteOrder.nativeOrder());
    vertexBuffer = vbb.asFloatBuffer();

Use Code
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  
      /*
       *    p1              p2
       *      +-----------+
       *      |           |
       *      |           |
       *      |     +     |
       *      |   px,py   |
       *      |           |
       *      +-----------+
       *     p3            p4
       *
       *     p1           p2                 p2
       *      +-----------+                 -+
       *      |         +                  + |
       *      |       +                  +   |
       *      |     +                  +     |
       *      |   +                  +       |
       *      | +                  +         |  
       *      +-                 +-----------+
       *     p3                  p3         p4
       *                  
       */

    GL11.glVertexPointer(3, 0, vertexBuffer);
    GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
    GL11.glDrawArrays(GL11.GL_TRIANGLES,0,totalSprites*6);
    GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);


   Everything was working OK until I added textures, I checked and the textures are being loaded with the correct alpha. The sprites only draw the alpha > 0 part of the texture, but some quads are drawn with the background over sprites.

   The image show a sprite drawn OK (green, transparent quad), and a sprite with the error (red, background over sprtes):


   The blending and texture calls are:
1  
2  
3  
4  
5  
    GL11.glEnable(GL11.GL_BLEND);
    GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

    GL11.glEnable(GL11.GL_TEXTURE_2D);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId);  


  Do I need some sorting of the triangles? or the blending call is not correct??

 

Offline theagentd
« Reply #1 - Posted 2011-09-10 09:22:47 »

Are you using a Z-buffer? If you are, then you can't use blending. The transparent parts of the sprite will still result in a depth write, making all underlying sprites not being drawn.
Either sort your sprites from back to front and use blending. If you don't want to do that, use alpha testing instead. That will discard all fragments of the sprite that have an alpha under the threshold you decide. These fragments will not write to the depth buffer either.

Myomyomyo.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #2 - Posted 2011-09-10 17:32:09 »

Yup, when I had a similar issue it was also because of the depth buffer.

See my work:
OTC Software
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline R.D.

Senior Devvie


Medals: 2
Projects: 1


"For the last time, Hats ARE Awesome"


« Reply #3 - Posted 2011-09-12 10:41:23 »

Just a question but wouldn't help to just enable the Alpha_Test with LEQUAL function? If the sprites have alpha values of 1.0 this would work with the Z-Buffer. At least it works fpr my sprite sorting for the Slick2D Framework...
Offline theagentd
« Reply #4 - Posted 2011-09-12 18:57:00 »

With interpolation the alpha values could be anything between 1.0 and 0.0. Where you choose to put the limit for alpha testing is just an aesthetic issue. The alpha will be ignored anyway (set to 1.0) if you aren't using blending anyway.

Myomyomyo.
Offline rdcarvallo

Senior Devvie


Projects: 5
Exp: 15 years


2D Java games forever!


« Reply #5 - Posted 2011-09-22 04:30:48 »

Are you using a Z-buffer? If you are, then you can't use blending. The transparent parts of the sprite will still result in a depth write, making all underlying sprites not being drawn.
Either sort your sprites from back to front and use blending. If you don't want to do that, use alpha testing instead. That will discard all fragments of the sprite that have an alpha under the threshold you decide. These fragments will not write to the depth buffer either.

I'm using zbuffer, so i added a sort before drawing the sprites

Just a question but wouldn't help to just enable the Alpha_Test with LEQUAL function? If the sprites have alpha values of 1.0 this would work with the Z-Buffer. At least it works fpr my sprite sorting for the Slick2D Framework...

I'm also using transparency for some effects (smoke, fire), so I needed blending.


Thanks to all, with the "z-sort" now the sprites are working ok, here a screenshot

Click to Play

Offline theagentd
« Reply #6 - Posted 2011-09-22 07:02:39 »

Uh, if you're sorting the sprites, why are you using a z-buffer? I mean, sorting OR z-buffer+alpha testing should be enough, unless you have other geometry (terrain, buildings) that can occlude the sprites.
If your fire uses additive blending, then you won't have to sort it at all, just draw them all in any order. x+y = y+x. If you do want to sort your fire for more accurate blending, you can do this after drawing the sprites with alpha-testing. Just sort them, keep depth TESTING enabled, but disable depth WRITES and draw them. Sorting lists every frame isn't going to scale well with many sprites/particles... xd
Anyway, nice screenshot!

Myomyomyo.
Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (145 views)
2017-09-30 02:57:34

theagentd (213 views)
2017-09-26 18:23:31

cybrmynd (294 views)
2017-08-02 12:28:51

cybrmynd (284 views)
2017-08-02 12:19:43

cybrmynd (294 views)
2017-08-02 12:18:09

Sralse (287 views)
2017-07-25 17:13:48

Archive (966 views)
2017-04-27 17:45:51

buddyBro (1092 views)
2017-04-05 03:38:00

CopyableCougar4 (1663 views)
2017-03-24 15:39:42

theagentd (1425 views)
2017-03-24 15:32:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

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

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
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!