[JSR-184][3D编程指南]Part V: Heightmap terrain rendering using M3G
<!-- 整理收集自网络,收藏以便日后查阅 -->
?
Compliments of Redikod:?
As you can see, the above terrain is described by three areas of greater height (the three gray hills) and the rest is a deep gorge, that is filled with water. Again, nothing but variations in height.
![[JSR-184][3D编程指南]Part V: Height地图 terrain rendering using M3G](http://img.reader8.net/uploadfile/jiaocheng/20140140/2716/2014012721161226053.gif)
?
Heightmaps
This is where heightmaps come in. They are very elegant solutions to storing variations in height and making surfaces smooth. Now let's look at this image before I start revealing anything.
?
?
The image above represents a quad that consists of two triangles. As you can see, the quad has four endpoints that are disjointed since we are using two triangles to represent it. These four corners can be given different heights and thus we have the beginning of describing heights in a 3D world. One quad is far from enough to describe an entire terrain however, we'll need a LOT of them if we want our terrain to look the least bit realistic. We'll get to that later, let's first see how we create a quad in code. We'll make it a x-z plane with variable y-coordinates, thus having varying height. Let's introduce a new method to the MeshFactory class we created in the previous tutorials and call it createQuad. All this method needs to know is the different heights at the different corners of the quad, and the culling flags. Here's the first piece of the method:
So, it's really simple to create the quads from a heightmap. After creation, you just render these quads, one after another and you have your heightmap. Now, there are some things you should know. As the resolution of the heightmap grid increases, so does the smoothness of the terrain, as you use more quads to represent the terrain. However you are also drastically increasing the memory footprint and increasing the number of polygons that the GPU has to push. This is a trade-off that needs to be done on every mobile phone depending on available memory, GPU power, etc.
Implementation
Let's see how to implement a heightmap in M3G. We already have a method that creates Quads with varying heights so all we need is to:
1. Load a heightmap
2. Create a new array that is scaled proportionally to the grid size
3. Read pixels from heightmap and store in the new array
4. Use said array to generate Quads with varying heights
It's a simple four-step procedure. Let's begin by inspecting the private members of the HeightMap class:
??????????? e.printStackTrace();
??????????? TutorialMidlet.die();
??????? }
??? }
?
Another very important thing to keep in mind is that the HeightMap in this tutorial is rendered without any culling at all. This is needed, especially on large terrains. However to keep clarity in the code I have chosen to remove any kind of space partitioning or software culling. You can see it as an exercise to only send meshes to the renderer that are visible (that is, not meshes that are too far away, or behind the camera).
Finally, what's the code for rendering the HeightMap? Here is the main draw method:
// Get the Graphics3D context
??????????? g3d = Graphics3D.getInstance();
???????????
???????? // First bind the graphics object. We use our pre-defined rendering hints.
???????? g3d.bindTarget(g, true, RENDERING_HINTS);
????????
???????? // Clear background
???????? g3d.clear(back);
????????
???????? // Bind camera at fixed position in origo
???????? g3d.setCamera(cam, camTrans);
????????
???????? // Render everything
???????? hm.render(g3d, t);
????????
???????? // Check controls for camera movement
???????? if(key[UP])
???????? {
???????????? camTrans.postTranslate(0.0f, 1.0f, 0.0f);
???????? }
???????? if(key[DOWN])
???????? {
???????????? camTrans.postTranslate(0.0f, -1.0f, 0.0f);
???????? }
???????? if(key[LEFT])
???????? {
???????????? camTrans.postRotate(5, 0.0f, 1.0f, 0.0f);
???????? }
???????? if(key[RIGHT])
???????? {
???????????? camTrans.postRotate(-5, 0.0f, 1.0f, 0.0f);
???????? }
????????
???????? // Fly forward
???????? if(key[FIRE])
???????????? camTrans.postTranslate(0.0f, 0.0f, -1.0f);
?
Conclusion
So, to continue this lesson, why don't you try loading the other heightmaps supplied in the source code zip files? See what kind of terrains come out. Or even better; create your own heightmap image!
Let your imagination go wild, put it into the MIDlet and cruise through your landscape.
?
Not much to say really. The HeightMap.render(g3d, t) method is pretty clean and straightforward. The controls might be a bit odd to you though. You move the camera with the joystick. Up, Down and rotate left and right. To actually move the camera forward, use the FIRE key.Nothing strange here. We just load the heightmap and perform some transforms on it, as it will be the transform supplied to the HeightMap's render method. We just want it back into the screen a bit and a bit up. We also scale it a large amount as a terrain is normally huge, but I just want you to see a small overview of it.All you need to do is go through the table of quads and render them at their given position in space. The user of the render method may supply a transform to be applied to each quad after the local transform, which is only putting each quad in its own place. Finally we place the water mesh at the height level defined during heightmap creation.As you see, all we do is iterate over the heightMap table and extract 4 values, which we use as the height values in the MeshFactory.createQuad method.I won't go into detail about the above code as I'd like you to look through it as an exercise. Anyhow, we start by loading the actual image into memory and then extract its pixel values. Then using the resolution parameter supplied from the constructor, we create a grid of according size and fill it with pixel values. Lastly we do a manual garbage collection to get rid of unnecessary data. This is mostly because the loadImage method is a memory intensive method and we want to make sure garbage data isn't taking up vital memory for the next few tasks.Let's dissect the above code into steps. First we check for invalid resolution values. Invalid values are values beyond 1.0f (a quad has 4 corners, thus the smallest grid sector is a 2x2) and below 0.0001f (a VERY low resolution that more or less creates the entire terrain with one Quad).They are pretty self-explanatory but I'll go through some of them. First of all the heightMap array is the scaled array that holds the heights. It is not holding the pixels from the heightmap image. The Mesh table is holding all the generated Quads that we render. Finally, the water Mesh is simply a blue plane that will represent the water in our terrain (for creating rivers etc). Let's see how we create a HeightMap then:Creating Quads from a Heightmap
With the above method we can create a Quad with varying height but as I said earlier, we need a lot of quads to make realistic terrain so now the problem is how to convert the heightmap into quads. This is really not a problem. Just look at this image:Here we created the arrays necessary for describing a Mesh in the M3G system. The VertexBuffer that holds two vertex arrays, the color array and the position array. I have intentionally left out the allocation of the color array from the above code, since I'll talk more about that later. Right now let's focus on creating the quad.Looks familiar doesn't it? A simple quad consisting of four vertrices that each have varying y-components but static x and z.
?
--------------------
源码见附件: