Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  2D layering in OpenGL  (Read 1566 times)
0 Members and 1 Guest are viewing this topic.
Offline DazKins
« Posted 2014-02-08 13:08:28 »

Having worked in a default Java 2D graphics for a good while, I'm used to the concept that the last thing that is rendered goes on top.

I'd like to simulate this similar layering in a 2D OpenGL game.

Up until now, I have been setting z values of the individual objects to decide what gets rendered on top, but this is getting very awkward and impractical especially when adding transparent objects.

I had the idea of just increasing the z value that each object is rendered on as objects are rendered each frame, but I'm using buffer objects that only get generated once and then rendered each frame. Also, the buffer objects have differing z values within them so it becomes difficult to layer them individually.

I realize a lot of this probably doesn't make any sense but any help would be greatly appreciated Cheesy

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Offline quew8

JGO Coder


Medals: 29



« Reply #1 - Posted 2014-02-08 15:39:07 »

As long as depth test is disabled (which it should be by default) then you will get the same behaviour. Somewhere in your code you must be calling glEnable(GL_DEPTH_TEST) - don't. For 2D graphics there is rarely any use for depth test and you certainly shouldn't be working in 3D points.
Offline DazKins
« Reply #2 - Posted 2014-02-08 15:44:22 »

I'm already using the depth buffer to layer objects. Is there any way i can easily layer objects without using the depth buffer?

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline quew8

JGO Coder


Medals: 29



« Reply #3 - Posted 2014-02-08 15:48:00 »

I'm saying DO NOT use the depth buffer. Then you will get them being ordered according to the order you render them. That's what you want isn't it?
Offline DazKins
« Reply #4 - Posted 2014-02-08 15:51:47 »

unfortunately OpenGL seems to render object the other way round, objects rendered first at the front

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #5 - Posted 2014-02-08 15:53:40 »

unfortunately OpenGL seems to render object the other way round, objects rendered first at the front

only if you
glEnable(GL_DEPTH_TEST);
.

If you
glDisable(GL_DEPTH_TEST);
it works the same as with awt.

Btw, awt (java2D) uses OpenGL under the hood (as one of their backends).

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline DazKins
« Reply #6 - Posted 2014-02-08 15:54:12 »

Ok, thanks, will try this and get back to you when i have a moment Cheesy

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Offline quew8

JGO Coder


Medals: 29



« Reply #7 - Posted 2014-02-08 15:54:21 »

That isn't true. If you render one primitive then render another primitive on top of it, as long as there is no depth/stencil/alpha/scissor test, the second will be rendered over the top.
Offline theagentd
« Reply #8 - Posted 2014-02-09 03:17:05 »

Front-to-back blending can be useful for two reasons.
1. It can be faster in some cases, similarly to how front-to-back sorting improves depth testing performance.
2. It produces correct alpha values in the render target, so the resulting texture (if rendering to a texture of course) has correct alpha values.

Myomyomyo.
Offline quew8

JGO Coder


Medals: 29



« Reply #9 - Posted 2014-02-09 09:41:26 »

Right but in a 2d game like this, with distinct layers (I presume), you'd be better off using the stencil buffer for front to back rendering. It must be quicker than the depth buffer and you can have as many layers as you like without worrying about depth buffer precision.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #10 - Posted 2014-02-10 01:04:42 »

The stencil buffer won't work with transparent objects though. Same problem as transparency with depth testing.

Myomyomyo.
Offline DazKins
« Reply #11 - Posted 2014-02-10 08:36:13 »

I'm now having another issue, because of layering, I want to render each tile and then render the entities within that tile. This means now instead of generated one VBO for each chunk of tiles, I have to generate a VBO for each individual tile and render it with its entities. This, although still playable, has had some big performance impacts that i could really do without.

I have tried giving each type of tile an idividual VBO and then just translating it to its wanted position each time, but this is also way too slow.

Any ideas?

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Offline DazKins
« Reply #12 - Posted 2014-02-12 18:05:56 »

No one have any ideas for this  Huh

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Offline Drenius
« Reply #13 - Posted 2014-02-12 18:43:36 »

What kind of entity are you rendering for each  tile?!
Normally entities are drawn in another seperate VBO or if there are only a few in their own VBOs or even immediatly...
Offline DazKins
« Reply #14 - Posted 2014-02-12 19:00:35 »

Each tile is a quad with a texture applied to it. Before I had this depth issue, I was adding each chunk (16 * 16) of tiles to one large buffer object, but now I need to render a row of tiles and all of the entities in that row of tiles which will mean making 16 buffer objects for each row in the chunk which seems very inefficient and has caused large frame drops. The entities in that tile usually are made up of around 6 VBO's themselves as each individual part of the entity needs to be manipulated

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Offline Drenius
« Reply #15 - Posted 2014-02-12 19:07:54 »

It would be very helpful if you explain what you are doing - not just what you are doing but what are you using it for, maybe a screenshot and some code example or something...
I do not get why you switched from rendering your tiles chunkwise to rendering them individually...
What are you doing?
Offline DazKins
« Reply #16 - Posted 2014-02-12 20:16:14 »

The reason why I switched them to rendering them individually because I needed the entities within the tiles the be rendered on top of each tile, but not rendered on top of the tiles below the entity so the layering is correct. I'm sorry if I'm being to vague I'm really bad at explaining things  Tongue.

If you really want the code i can try and paste it but it is a very long chunk of code with a lot of additional variables and functions not to do with this topic

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Offline Drenius
« Reply #17 - Posted 2014-02-12 20:32:10 »

This makes it a bit clearer...
Maybe you could trick a little bit with depth testing?
Code is not necessary, but describing your game could be helpful.
Offline DazKins
« Reply #18 - Posted 2014-02-12 21:17:33 »

That was the original plan, but managing all the z values got very messy :S

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Offline Drenius
« Reply #19 - Posted 2014-02-12 21:28:19 »

You should make a complete plan where to use which z values then.
The only other way to reduce your VBOs I can imagine is using a shader, but this is your decision.
Offline DazKins
« Reply #20 - Posted 2014-02-12 21:55:32 »

Any ideas on how to manage this system?

I triad incrementing a global z value by 0.0001 so that every time i rendered on object it would be put slightly in front all of the other objects but this doesn't really work as the VBOs are generated once and then have to be fully recompiled to change their z value

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Offline Drenius
« Reply #21 - Posted 2014-02-12 22:03:09 »

So use a shader.
One that cuts off what you do not want to see.
Sorry, very hard to help you until you finally tell what kind of project you are working on.
Offline DazKins
« Reply #22 - Posted 2014-02-12 22:16:52 »

The project that I'm working on is a tile based RPG game.

I'm not very confident using a shader at the moment, mainly cause i suck at GLSL, but also I would like to emulate some sort of layering system that's reminiscent of the whole 'last rendered is on top' feature.

Effectively I guess what I'm asking is, whats the fastest way to render out a 16 * 16 area of tiles without rendering them all in the same VBO

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Offline Drenius
« Reply #23 - Posted 2014-02-12 22:20:22 »

If there are only 256 of them immediate mode is still fast enough...
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #24 - Posted 2014-02-12 22:22:36 »

Immediate mode is more than enough to render only 256 tiles. Sure, immediate mode is not great performance wise, but don't let that fool you. It can still handle thousands of vertices. I don't recommend using it, but it isn't going to give you a weird disease or something if you do, so don't be scared to use it for basic tasks. As for your issue, maybe use a vertex array? A batcher would be the best solution.

Offline DazKins
« Reply #25 - Posted 2014-02-12 22:24:09 »

By some vague calculations i reckon i need to render around 4096 at once though  Undecided.

This is really gonna bug me. I will think while I sleep tonight and see if I come up with anything.

Certainly and interesting problem though

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #26 - Posted 2014-02-12 22:27:38 »

16 x 16 = 256?
16 x 16 x 16 = 4096

Either way, it won't be an issue. You are seriously underestimating the power of a modern day GPU. You can throw literally millions of vertices at a decent GPU, and it will perform fine. It all depends on how the developer optimizes the program, but that isn't the point.

I imagine that each tile in your game has 4 vertices.

4 * 256 = 1024 vertices

That's nothing.

Offline Phibedy

Senior Member


Medals: 8



« Reply #27 - Posted 2014-02-12 23:18:57 »

I am passing 20k values each frame to the gpu and it works fine. Just code as simple as possible and ONLY care about faster ways AFTER you have performace-issues.
Prorgammers tend to write "perfectly-efficient" code but in most cases it doesn't help. No one will care if he has 2000 or 500 fps. Even 60-100 are fast enough  Grin
So for example in 2D:
Tile: 4 edges -> (8 position-coords + 8 uv-coords) + (6 indices) ->22 values.
500 Tiles -> 11k values to pass to the gpu. On modern computers it shouldn't be a problem. Of course if the tilemap is static you could use some vertex-buffer.
Offline DazKins
« Reply #28 - Posted 2014-02-13 08:53:24 »

Surely generating a vbo for each individually tile is still quicker though?

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Offline Drenius
« Reply #29 - Posted 2014-02-13 17:12:04 »

The advantage of VBOs and ArrayLists is that there are less direct calls of the vertex function but every vertex position is already stored.
If there are not very much vertices, you could also use these vertex function, immediate mode.
Using so many VBOs might be even much slower since all the buffers have to be called separate.
Pages: [1] 2
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

ctomni231 (34 views)
2014-07-18 06:55:21

Zero Volt (30 views)
2014-07-17 23:47:54

danieldean (25 views)
2014-07-17 23:41:23

MustardPeter (27 views)
2014-07-16 23:30:00

Cero (42 views)
2014-07-16 00:42:17

Riven (44 views)
2014-07-14 18:02:53

OpenGLShaders (33 views)
2014-07-14 16:23:47

Riven (34 views)
2014-07-14 11:51:35

quew8 (30 views)
2014-07-13 13:57:52

SHC (66 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!