Chapter 6. Basic Terrain Geometry
Finally, I read the sixth chapter. After reading this chapter, you should write simple terrain, look forward to ing ...
HEIGHT MAPS as Terrain INPUT DATA
Terrain, highly low. How can I determine the high and low? The easiest way is Height Map.
To put it bluntly, every point is used in a value, this value is input. This value is a height, so it is called Height. One point corresponds to a value, so it is Map.
This HEIGHT MAP is usually entered with Bitmap images. How to enter? Use grayscale. The white representative is high and the black representative is low. For example, the height value is [0,255], then a point corresponding to a pixel on the picture, the grayscale range of this pixel is also [0, 255], simple and effective.
There is also a software that can convert a real terrain into a grayscale map of Bitmap.
Procedural Height
If Height Map does not meet the needs, we should try to generate a height using the program.
The easiest way is this: Randomly generate the Height of each point, then processed until the terrain looks smooth. This "looks smooth" is the height difference of adjacent points within a certain threshold.
This method is called "Monkey Picture" by the Snook. Because it looks like a monkey to draw, then we will correct it.
There are also a lot of simple and effective ways that can be able to directly generate smooth terrain.
Midpoint Displacement
The intermediate point method is a recursive method. Although it is also a random algorithm, it can generate a smooth terrain (when the Roughness setting is appropriate).
One side of the terrain, four corner ABCD, their height is randomly generated. There is a variable delta, which is initially Maxheight / 2, here is 128 (because the map height range is 0-255).
Thus, the height of the five points of the midpoint and square center in four sides has the following calculation formula:
HEIGHT = BASE VALUE [OFFSET]
What is BASE VALUE? It is the height average of the two angles (the center is the average of four angles).
What about the OFFSET? Offset is random, the scope is [-delta, delta].
Then, go to the next step. Because the five points have already divided the large square into four small squares. Then, come back to the height of each small square. The calculation method is the same as before (four corners have been determined). However, it is worth noting that Delta needs to be amended:
Delta = Delta * Roughness
This Roughness is a factor between 0-1, which is rough factor. This value is much large, the more the terrain is. In general, it is possible to take 0.5.
Perlin noise
Perlin noise This thing has been heard for a long time, this time I am in here. This thing is invented by Ken Perlin in 1983. Because Perlin noise is influenced in graphics, Ken Perlin has been sent a Oscar Award in 1997, sweat -_- |||
For Perlin noise, it can be applied to any dimension of space, but because Perlin noise is mainly used to generate textures, it is discussed with 2D.
The way to create Perlin noise is as follows:
1) For example, the terrain is a GRID of N * N, then there is N 1 Vertex per line, a total of N 1 column. For each Vertex, we generate a 2D random vector on it. There is a very efficient method here. First, make a vector sheet, a total of 256, evenly distributed into a circle. Then select one from the inside.
2) Then, focus on the target in each GRID. For any Pixel inside the Grid, four Corner have a vector pointing to it, so there is 2 vectors on each Corner. Then, for the two vectors on each Corner, the value is recorded as this Corner's Height. (Note, not the Pixel of this Corner, this Height is just a middle variable)
3) Target the target to Pixel in the upper left corner. That can be said that this Pixel is relatively coordinate, remembers X and Y, then now you need to calculate some quantities:
SX = 6 * x ^ 5 - 15 * x ^ 4 10 * x ^ 3; SY = 6 * y ^ 5 - 15 * y ^ 4 10 * y ^ 3;
For this need to explain that the formula proposed in the earlie Perlin's paper is W = 3 * t ^ 2 - 2 * t ^ 3. This formula is compared to this fast, but the traces of artificial traces are heavy. So after Perlin improved this formula, using this now. If you want to pursue the speed, you can change it to a fast.
4) Success is not far away. Finally, as long as you do 3 linear interpolation, it is OK (where four Corner's HEIGHT is H0 - H3):
AVGX0 = H0 (SX * (H2 - H0)) AVGX1 = H1 (SX * (H3 - H1)) Result = AVGX0 (Sy * (Avgx1 - Avgx0))
This result is of course the final result, which is the height of this point.
Specifically, look at the code on the book, you understand. In addition, there is another method that uses a ratio to zoom Texture, then add them, you can get new Texture, which can also reduce manual traces.
Processing Height Map Data
To turn the grayscale graph into a height, you can establish a terrain, that is, the data is processed.
Each face is a triangle, each two triangles constitute a GRID, and then consisting of Grid Terrain.
In addition to coordinates, there is a problem with a normal. For each face, it is easy to seek its normal (any two edges to do fork), then what about Vertex's normal? If you want to make the terrain look smooth, you have to ask a mean on the normal surface around Vertex, so that the terrain looks smooth.
I heard that D3DX provides a function to ask the normal figure in the grayscale, called d3dxcomputernormalmAp, if it is true, it is too convenient.
TERRAIN Geometry Base Classes
Mainly two classes - CTERRAIN and CTERRAINSECTION.
CTERRAIN is spelled by CTERRAINSECTION, which is for easy management. For example, CTERRAIN is 256x256, and CTERRAINSECTION is 64x64, then 64 sections are needed. CTERRAINSECTION is inherited in csceneObjects, so you can put it in Render Queue, and spatial management is the same as other objects. However, CsceneObjects distinguish that CTERRAINSECTION does not store IB and VB information, which are existing in CTERRAIN, CTERRAINSECTION is just taken. Terrain Geometry Index Buffers
Here is the triangle strip to be used as Index. Among them, a small trick is used to degenerate Triangle. Degraded triangles are a triangle with an area of 0, and two of its three vertices are the same vertex. This triangle is used to connect to the triangle, so that the triangular band of the row can be turned into a triangular belt.
The method is as follows: For example, you want to generate a Grid of M * N, then a total of (m 1) * (n 1) Vertex. Set up:
Xverts = n 1yverts = m 1
Then need to calculate how many triangular belts, how many indexes and indexes have:
Total_strips = YVERTS - 1TOTAL_INDEXS_PER_STRIP = Xverts * 2 Total_indexes = (Total_indexs_per_strip * total_strips) (Total_Strips * 2) - 2
Here you need to pay attention to Total_indexes, the front Total_Indexs_per_strip * total_strips is well understood, the following act should be (total_strips - 1) * 2, this is the degraded triangle, and there is 2 degraded triangles to connect.
Initialize several variables:
Index is an array that is used for index. START_VERT = 0line_step is the number of Vertex in each row
then:
For (j = 0; j // Add Degraded Triangular IF (J 1 I have not written Xstep and YSTEP in the book, I will use ROAM, I will use it, no matter it ... The following sections are all related codes. This chapter is the foundation of the terrain, but also learned Perlin Noise, very happy.