PDA

View Full Version : Layer-based rendering in OpenGL



Brainer
10-01-2010, 12:12 PM
Hello everyone. :)

Take a look at the picture:
http://www.gamedev.pl/files/tutorials/wedrowanie_w_swiecie_2d/1/2502135526sytuacja1.jpg
I hope the effect I want to achieve is clear after looking at the picture. ;) How do I do that in OpenGL?

User137
10-01-2010, 12:49 PM
So player character is drawn behind the tree? There are 2 ways i can think of:

1) Treat player character and game objects same way and draw them Y-sorted.

2) Cut tree image in pieces where lower part of tree that player collides to is drawn in terrain layer and higher tree part is drawn in upper layer after terrain and player.

Brainer
10-01-2010, 01:28 PM
1) Treat player character and game objects same way and draw them Y-sorted.

Can you elaborate upon this one? :)

chronozphere
10-01-2010, 01:48 PM
This question actually doens't have anything to do with OpenGL. It's more of a high-level thing. It can be done on any graphics library. :)



1) Treat player character and game objects same way and draw them Y-sorted.


I think he means that you make a list of drawable objects and sort them by Y-coordinate.
Beware that the Y coordinate must denote the "base" of an object, where it touches the ground (e.g the trunk of a tree or the feet of a human player etc).
Objects with a lower "base" (the trunk of the tree is lower than the feet of the guy) will overlap objects with a higher "base" (higher = closer to the top of the screen).

Hope you get the idea. :)

Brainer
10-01-2010, 01:55 PM
Aw, I get it now. :)

But how could it be done in OpenGL? ::)

noeska
10-01-2010, 03:08 PM
Could not you use the z-buffer for that? I.e. enable depth buffer and set proper z coords for player background trees etc.

chronozphere
10-01-2010, 03:19 PM
Z-buffer would probably work, but i think sorting and drawing yourself is better for this kind of game. It has the following advantages over z-buffers:

> It uses less Video-RAM and CPU/GPU power than Z-buffering.
> You have to sort anyway when you want to use transparency, so why not do it from the beginning. ;)
> No Z-fighting artifacts

Brainer
10-01-2010, 04:24 PM
My first thought was to divide the whole map into three layers:
Layer 1 - holds the background tiles (i.e. grass, water, sand, etc.)
Layer 2 - holds the characters
Layer 3 - holds other scenery objects (buildings, trees, rocks, etc.)

But I don't have any idea how to do something like that. I mean, render the layers so that one overlaps another. Just a thought, but maybe assign each layer a different Z coordinate. So the first layer would be, say, (0;0;0), the second (0;0;1), and the last one (0;0;2).

What do you think?

EDIT Hehe, basically what noeska suggested. :D

User137
10-01-2010, 08:28 PM
Basically my 1) suggestion consist of only 1 layer that is ground level terrain. After that draw players, trees, rocks, walls as sprites, spell effects on top of it. Simple quads for which you can use vertex arrays or whatever to optimize. These sprites are sorted by their Y-coordinate, but only sort visible sprites so calculation isn't going slow. Maybe a entirely separate list for visible objects that is updated as screen and objects move.