Книга: DirectX 8 Programming Tutorial

Creating a Terrain

Creating a Terrain

To create our terrain, we will use the same techniques as we did to create our cube. The terrain will be made up from a 20?20 grid of quads (squares made up from 2 triangles). We will use an index buffer, which will reduce the number of vertices from 2400 to 441. I'm not going to post the code here because it is very similar to the code above. The only real differences are the way the index and vertex buffers are populated. You can download the full source code by clicking the "Download Source" link above. Fig 8.2 below shows our flat terrain grid. You can render your scene in "wireframe" mode with one simple call to SetRenderState which is also shown below.

//Set fill state. Possible values: D3DFILL_POINT, D3DFILL_WIREFRAME, D3DFILL_SOLID
m_pD3DDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);


Fig 8.2

Now that we have a flat grid, we can turn this into a simple terrain by setting a random value for y for each vertex. I've done this for each vertex in the grid except the ones that are around the edge so that the four edges are all the same level. If you want to create a more detailed terrain, have a look at http://www.gameprogrammer.com/fractal.html where there is a tutorial on how to generate a random fractal terrain. Fig 8.3 below shows the grid with random y values.


Fig 8.3

The last thing to do is apply a grass texture to the terrain and set the render state to solid rather than wireframe. Once you have done this, you should end up with a scene that looks like the screenshot below.


Оглавление книги


Генерация: 0.259. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз