# using heightmap to simulate 3d in an isometric 2d game

I saw a video of an 2.5d engine that used heightmaps to do zbuffering. Is this hard to do? I have more or less no idea of Opengl(lwjgl) and that stuff.

I could imagine, that you compare each pixel and its depthmap to the depthmap of the already drawn background to determine if it gets drawn or not.

Are there any tutorials on how to do this, is this a common problem? It would already be awesome if somebody knows the names of the Opengl commands so that i can go through some general tutorials on that. greets!

Great 2.5d engine with the needed effect, pls go to the last 30 seconds

Edit, just realised, that my question wasn't quite clear expressed: How can i tell Opengl to compare the existing depthbuffer with an grayscale texure, to determine if a pixel should get drawn or not?

• I think that is 2D with some effects to fake 3D. Mostly you can do that with a height map for shadows and a normal map for lighting. I've done the normal map portion before, unsure about the best way to do shadowing, but that sample doesn't seem to bother. – Sean Middleditch Jul 17 '13 at 20:14
• its 2d with shaders i guess. I'm only interested in the clipping of the sprites, not the lighting ;) – VaTTeRGeR Jul 17 '13 at 20:18
• Huh, with effects like this: i.imgur.com/MtbDbwa.png it's pretty good fake 3D. – MichaelHouse Jul 17 '13 at 20:24
• yep, he uses a normal map and a height map, these are rendered from 3d models beforehand. The heightmap(black/white) gives him the height and normalmap(purple/green/yellow) the surface direction, which is all you need to simulate 3d when rendering from one orthogonal view-direction only – VaTTeRGeR Jul 18 '13 at 8:27

This is actually a quite simple thing to do.

Then simply add the heightmap height to the fragment depth inside the shader.
Here's a little example fragment shader:

uniform sampler2D heightmap;
void main(void)
{
gl_FragDepth = gl_FragCoord.z + texture2D(heightmap, texture_coordinate.xy).r;
}


Keep in mind that you'll first need to convert the heightmap height to a non-linear value before adding it to "gl_FragCoord.z". Here's a topic that explains how to do that: https://stackoverflow.com/questions/10264949/glsl-gl-fragcoord-z-calculation-and-setting-gl-fragdepth

Also: Since you're probably using orthographic projection, be sure to properly set the sprite's z position correctly so opengl can calculate gl_FragCoord.z properly for you.

But be aware: Setting the fragment depth in the shader by yourself eliminates the possibility for early depth testing. But that shouldn't be a problem unless you draw a lot of sprites with complex shaders.