Sign in to follow this  
Followers 0
TheChubu

OpenGL
Resources on heightmaps?

7 posts in this topic

Hi! Could somebody point me to some information dealing with heightmaps? (best image format to store them, load them, how to convert them to polygonal geometry and viceversa, etc). Specific OpenGL oriented implementation details would also appreciated!

 

While I'm looking for details on how to implement them myself, most articles I found deal with already existing tools so they're not useful to me : /

0

Share this post


Link to post
Share on other sites

This has been discussed several times. I'll do a quick recap of heightmaps and terrain algorithms.

Terrain algorithms: dead. Just brute force. Caveat: perhaps using pixel-based tracing might make sense.

 

Heightmaps: they are 2D array of values.

Format to use: everything lossless will fit. I like PNG 16-bit grayscale. A raw dump might be more appropriate in some cases. Multichannel recomposition (value = red + (green << 8) + (blue << 16)) is hard to work with but at least it won't get garbled if some processing program does not support it. Floating-point based formats are to be taken with extra super special care.

How to convert them: they are a bunch of triangles! In the past, several methods have been proposed. In the end, they're not really required unless going for AAA quality, and even in this case, you'll find out many games triangolate trivially.

OpenGL implementation: there are plenty.

 

You're better with a specific question.

1

Share this post


Link to post
Share on other sites

Great, I already have a PNG loader.

 

Now, as for specific question. Well, what pixels in a heightmap represent? Vertices of a triangle? Quads?

 

I was thinking that maybe I could draw one with triangle strips, considering each pixel as a vertex, and using the (X,Y) positions as indexes for indexed drawing, though I'm not so sure since I can't picture it on my head yet. Got set up the loader and a PNG file made up of random noise. I'm trying to code it right now.

 

I'm using LWJGL by the way.

Edited by TheChubu
0

Share this post


Link to post
Share on other sites
Generally the pixels are consodered to be the vertices, and you draw triangles betwwen them.

There are different ways to draw the triangles though.

4 triangles with an imaginary middle vertex that is the average of the 4 pixel vertices in the corners

2 triangles

2 triangles with alternating direction for diagonal (to make the tiling not so obvious?)
1

Share this post


Link to post
Share on other sites

Thanks! Thats useful information.

 

Anyway, I did ended up rendering something out of a grayscale image, though I just set up GL_TRIANGLE_STRIP and that's it, so the vertices have no coherence at all. I have no idea what is going on the upper side of the pic (all those horizontal lines).

 

Now I'll try to correct the vertices...

 

 

0

Share this post


Link to post
Share on other sites

Yes! Got it! Though there is something terribly wrong with my depth test I think.


GL11.glEnable(GL11.GL_CULL_FACE);
GL11.glCullFace(GL11.GL_BACK);
GL11.glFrontFace(GL11.GL_CW);
 
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthMask(true);
GL11.glDepthFunc(GL11.GL_GEQUAL);
GL11.glDepthRange(0.0f, 1.0f);
 

If i change it to GL_LEQUAL seems like every depth test fails and I see nothing. Probably there is something wrong with the data or my perspective matrix, maybe I'm not drawing in the correct order.

0

Share this post


Link to post
Share on other sites
Terrain algorithms: dead. Just brute force.

Though I kind of agree with that, "dead" is not 100% right. It still makes sense to use some kind of LOD in many situations, for two reasons.

 

1. Brute force is OK for small to medium view ranges but simply impossible otherwise, unless your resolution is extremely poor. If you have, say, a resolution of 1 meter and you brute force with a 2 kilometer visible range (which is about half of what you see in real life), that's a ... huge ... budget. Now if you think 1 meter is too coarse, you might want to add tesselation, which further increases the number of vertices.

 

2. Brute force creates a lot of small triangles in the distance. Small triangles are fragment shader poison. One does not normally want to have too many triangles that are "too small", in particular one does not want triangles that are hardly larger than a fragment (or, worse, smaller).

 

Insofar I would word it as "terrain algorithms are not THAT important any more, and may be unnecessary in some cases, while in other cases something very coarse is totally sufficient".

1

Share this post


Link to post
Share on other sites

Hi! Well, I got it working. I wasn't clearing the depth buffer so It added up after each frame and I wasn't seeing anything biggrin.png

 

I load the image and get the (x,y,z) coords like this: The x is the X of the pixel in the image, the y is the avg of the red green and blue colors at that pixel (pointless to avg in grayscale though) and the z is the Y coordinate of the pixel of the image. Color of the vertex is the color of the pixel.

 

Got some rotations/translations working too, so here are the views from above and below the mesh (from below it looks like some crazy cloudy thing).

 

Now I need to implement a proper camera.

 

Thanks for the help guys!

0

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  
Followers 0