Sign in to follow this  
Lucky808

Isometric tilemap rendering

Recommended Posts

Lucky808    139

Hi all,

 

Has anyone read the book "Learning IOS Game Programming"? I'm current using the rendering method (for Orthogonal maps) taught in the book in my new game. I've successfully converted some of the code to work with Isometric tilemap. However, I hit a road block when it comes to rendering objects on the map in the right order. I'm using 3 map layers. (1.ground, 2.grass, 3.wall and trees). I render the first 2 layers, then the player object, then the 3rd layer. However I could not get it to render objects in correct orders. i.e. a player standing in front of a tree should show up as such, meaning the tree should be rendered 1st then the player.

I've implemented a depth buffer and set up the following:

 

 

        glOrthof(0.0f, rect.size.width, 0.0f, rect.size.height, 0.0f, 100.0f);

 

 

        glEnable(GL_DEPTH_TEST);

        glDepthMask(GL_TRUE);

        glDepthFunc(GL_LEQUAL);

        glDepthRangef(0.0f, 1.0f);

 

        glClearDepthf(1.0f);

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

 

But objects are still not being rendered in the correct order. If someone who is familiar with the rendering method in that book can you please point me to the right direction.

Any input will be appreciated!

 

Thanks in advance.

 

Share this post


Link to post
Share on other sites
ifthen    820

glOrtho() takes six parameters: left clipping plane, right clipping plane, a top one and a bottom one, and values of "near" and "far", respectively. What do the last two values mean? Other than being clipping planes, too (making the viewport of glOrtho() look like a cube in which you look into from one side), they are essential for making the depth buffer work. The depth buffer stores the values not in the linear sense, but we can roughly say that they are inversed (as in 1/depth), so the precision is lost only far away from camera where we don't see it. However, this is only "roughly" - the actual equation is much more precise and depends greatly on relationship of "near" and "far" values.

 

Your problem is that near is 0 in your case, that means that the relationship of "near" and "far" is zero division; that means you lose all of your depth buffer precision.

The solution? Set near (the second-last parameter to glOrtho()) to 1.0. It is maybe not the only cause of your depth buffer problem, but it is certainly one of them.

Edited by ifthen

Share this post


Link to post
Share on other sites
Lucky808    139

Thanks for the tips. however as soon as I adjust the second last parameter to 1.0,  the screen becomes blank. i don't see anything being rendered.

Share this post


Link to post
Share on other sites
ifthen    820

Well... you could be rendering your objects with the Z axis set to zero (if you are rendering "fake 3D" orthogonal tilemaps and not true 3D ones). Make sure that you translate them properly to the screen. If you are using the old fixed-pipeline OpenGL, you can simply translate the object like this

//in the drawing routine
void drawSomeObject()
{
int Z = 5; // from 1 up, if you want to use 1 too, lower "near" to 0.5 or something so the object is not clipped due to floating errors
glTranslate(0,0,Z);
//draw it somehow, e.g. using glBegin()/glEnd()
glTranslate(0,0,-Z);
}

 

The objects with higher Z value (i.e. farther from you) will be obscured by the objects with lower Z value if you do this.

Share this post


Link to post
Share on other sites
Lucky808    139

Yes, the objects were loaded without z-axis values. do they need z values for rendering in an isometric tilemap?

also, i tried implementing glTranslatef() into my rendering routine but made no difference. the moment i changed the 2nd last parameter to 1 in glOrtho() the screen went blank again :(

Share this post


Link to post
Share on other sites
Wyrframe    2426
If you're wanting to render using the depth buffer to do layering for you, you should ideally be actually placing your scene contents in real 3D space. At the very least, you need to have different Z values for objects at different depths, or else the depth buffer is doing nothing at all. If you want to manually composite the depth of the scene using old-school 2D-esque sorting techniques, then disable the depth buffer, set the Z bounds to -1,+1 (you're currently using 0.0f,100.0f), and go wild with objects at Z=0. There are places for mixing the two, but most of them are in the realm of scene composition effects like distortion, full-screen shaders, etc.

Share this post


Link to post
Share on other sites
Lucky808    139

Ok, think I will go with placing my scene in a 3D space. In that case, how do I set up the z value for each tile on an Isometric tilemap. x and y position are determined by their index from an 2D array.(X increases towards southeast, Y increases towards northeast) something like:

 

 

        vertex1.geometryVertex.x += (aTileCoord.x + aTileCoord.y) * (aTileSize.width/2);

              vertex1.geometryVertex.y += (aTileCoord.x - aTileCoord.y) * -(aTileSize.height/2);

              vertex2.geometryVertex.x += (aTileCoord.x + aTileCoord.y) * (aTileSize.width/2);

              vertex2.geometryVertex.y += (aTileCoord.x - aTileCoord.y) * -(aTileSize.height/2);

              vertex3.geometryVertex.x += (aTileCoord.x + aTileCoord.y) * (aTileSize.width/2);

              vertex3.geometryVertex.y += (aTileCoord.x - aTileCoord.y) * -(aTileSize.height/2);

              vertex4.geometryVertex.x += (aTileCoord.x + aTileCoord.y) * (aTileSize.width/2);

              vertex4.geometryVertex.y += (aTileCoord.x - aTileCoord.y) * -(aTileSize.height/2);

 

what should I put in for z?

Share this post


Link to post
Share on other sites

Your problem is that near is 0 in your case, that means that the relationship of "near" and "far" is zero division; that means you lose all of your depth buffer precision.

This is true for perspective projection because that's achieved by dividing the coordinates by the depth. Orthographic projection doesn't have this issue and the depth buffer in fact is linear in that case.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this