OpenGL Terrain Technology – An Introduction

Introduction

NOTE: For the HTML model of this text with pictures and downloads please seek advice from the next hyperlink:

http://www.codeproject.com/opengl/OPENGLTG.asp

I have always been interested in computer graphics and their applications. Data representation and visualization is one of the main areas of HCI (Human Computer Interaction), and the better you make the interaction between a machine and a human, the more productivity will be generated by both the human and the machine. I had some experience with OpenGL during my undergraduate studies while attending the California Polytechnic University. Unfortunately, I never got a chance to pursue the more advanced features of the OpenGL library, given my time and work responsibilities.

You can find more about OpenGL at http://www.opengl.org. There are additionally a bunch of excellent literature to be had on the subject of Pc Snap shots and OpenGL you can refer for additional developments. Please take a look at the Heritage/Reference part for an inventory of some reference subject material that I’ve used, usually, for pc pics.

The next mission is a quite simple instance demonstrating learn how to generate a terrain in keeping with a bitmap file. The target of the venture is to generate a 3 dimensional terrain in response to some information file. Please word, that this can have been any information file, however for the aim of our instance, we’re going to be the usage of a 32×32 dimensional bitmap file. We can have simply used a textual content file, and outlined common sense for every phrase or letter to signify it graphically.

The venture additionally comprises a just right Home windows framework that can be utilized in your different OpenGL functions. The present mission permits you to rotate the digital camera the usage of your mouse.

As soon as once more, it is a easy technique to terrain era, which is usually a very tough process in complicated environments.

Heritage/Reference

Seeing that Pc Pics is more or less a sophisticated matter, it’s important to have at the least some king of figuring out and publicity to the ideas and theories within the box. Alternatively, this doesn’t imply that you’re going to now not be capable to use the next code or have in mind it. I’ve made it so simple as imaginable, and with a bit of luck, it’ll offer you a excellent begin, or some further supply of data that you need to use on your tasks. Additionally, please word that it is important to have a just right working out of C/C++ programming.

Some books I’ve used for studying Pc Pics and OpenGL programming:

Books I used whereas attending the California Polytechnic College:

OpenGL Programming Information, or higher be aware of because the Purple Ebook.

Laptop Photos The usage of OpenGL, 2nd Model.

Books I used whereas attending the California Lutheran College:

OpenGL: A Optimum, 2nd Model.

Interactive Laptop Pix: A High-Down Way The usage of OpenGL, 4th Model.

What’s a Terrain?

Some historical past knowledge on a terrain and their makes use of in a recreation utility: A terrain in an setting is without doubt one of the most important parts within the scene that’s being rendered. It could actually simply be the biggest 3D object within the challenge. Rendering the terrain can transform a frightening activity, taking probably the most time to render in a scene. To maintain the terrain engine working in actual time is usually a troublesome process, and it requires some idea out tactics and modeling for it to be adequate.

To be efficient, the terrain desires to fulfill numerous necessities, lots of which may also be contradicting each and every different. A terrain must look like steady to the top person, but the mesh will have to be simplified or culled the place that you can imagine, to cut back the burden on the portraits card.

In a gaming device, as an instance, some engines draw the terrain simply past the purpose a participant can attain, after which use a terrain drawn onto a skybox to simulate hills or mountains within the distance. The terrain will have to seem real looking to the environment for the surroundings, but this can also be taxing on the video card, and a stability must be maintained. Element textures are frequently used with reference to the digital camera, permitting the areas additional off to be rendered extra speedy.

What’s a Top Map?

The very first thing required for terrain rendering is a illustration of the terrain’s form. Whereas there are a selection of constructions that can be utilized to function the job, probably the most extensively used is the peak map. Others embody: NURBS, which will also be maintained via numerous keep watch over factors, and voxels, which permit for overhangs and caves.

There’s one problem to a top map, and that’s, for each level on the XZ-aircraft, there can handiest be one top worth. You will find that this limits the illustration of overhangs and caves by way of a peak map. This may also be overcome by using the use of two separate fashions.

Any other problem is that peak maps take a considerable amount of reminiscence, as every top need to be represented. Then again, peak maps lend themselves to the advent of standard meshes simply. It is usually simple to decide the peak at any given place, which turns out to be useful for collision in opposition to the terrain in addition to laying dynamic shadows onto the terrain.

A top map is represented with the aid of a 2D array of values, the place for any level (X, Y, Z), the X and Z are the indexes into the array, and the worth of the array is the Y worth which is similar to the peak worth.

The next is an instance of this kind of illustration:

int peak[5][5] =

zero, zero, 1, 1, 2 ,

zero, 1, 2, 2, three ,

zero, 1, three, 2, three ,

zero, 1, 2, 1, 2 ,

zero, zero, 1, 1, 1 ;

The Method!

There are various developed algorithms to generate terrains; I’m the usage of a quite simple answer for the aim of this undertaking.

In a nutshell, I used a 32 x 32 grayscale bitmap to characterize a peak-box that’s used to generate the terrain. The terrain itself is split right into a grid of peak values, the results of which is a mesh representing the terrain within the scene.

We create a grid of vertices which might be spaced evenly aside however have various heights, in line with the peak-box information. The colour worth of each and every bit is used to resolve the peak worth of each and every grid area; on this case, for a 24-bit grayscale bitmap, the values for the colour vary from zero to 255.

As soon as the bitmap has been learn and the values loaded in reminiscence, we’ve got the information wanted to symbolize the terrain. We additionally use a variable known as a MAP_SCALE to permit us to scale the map up or down. It is a scale issue; we use this to set the gap between each and every peak vertex. This permits us to extend or lower the dimensions of the terrain.

After we in reality assign the vertex coordinates for each and every grid region, we want to follow the MAP_SCALE issue, which is multiplying it with the grid region index in line with the coordinate component, i.e.:

Terrain[X][Z][0] = Drift(X)*MAP_SCALE;

Terrain[X][Z][1] = (flow) imageData[(Z*MAP_SCALE+X)*3];

Terrain[X][Z][2] = Glide(Z)*MAP_SCALE;

The terrain map is represented in a grid of top values, which is internally saved in a 2D array of vertex coordinates. It extends alongside the X and Z-axis, with the Y-Axis representing the terrain top.

To render the terrain map, we use GL_TRIANGLE_STRIP for each and every row of grid values alongside the Z-axis. To render the terrain as it should be, we wish to specify the purpose in a selected order.

This requires us to begin on the finish of the row and transfer alongside the sure X-axis through drawing the vertices in a Z sample:

(*)==========>(*)

/

/

/

/

/

/

/

/

/

/

/

(*)=========>(*) ….

The usage of the Code

I’ll best listing the code that offers with the terrain technology right here. There’s extra code within the undertaking that you would be able to take a look at. It’s neatly documented, so you should not have any issues. The answer used to be compiled the use of MS Visible Studio 2003, so that you will have to be capable to assemble and run it simply. It is important to have the OpenGL libraries and DLL, which I may also present as a obtain choice simply if you happen to should not have them. Make lifestyles a bit of more straightforward so that you should not have to seek for them on-line.

So the vast majority of the code is for making ready the home windows to render the scene correctly. As you’ll find under, the code for producing and rendering the terrain may be very quick. To offer an outline, the very first thing that occurs is for the home windows to get created, after which we initialize OpenGL, and skim within the BMP file and assign it to the 2D array we mentioned above. Then, the feel is utilized to the outside of the mesh, and the scene rendered to the reveal.

With out additional ado, the next checklist is the the element of the code which initializes and generates the terrain:

.

.

.

// InitializeTerrain()

// desc: initializes the heightfield terrain information

void InitializeTerrain()

// loop via the entire heightfield factors, calculating

// the coordinates for each and every level

for (int z = zero; z http://www.nicksoftware.com

by means of-ascitconsultancy services and products

Posted in techinnovation and tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , .