Terrain: Source Code

By Shamus Posted Saturday Feb 25, 2006

Filed under: Programming 17 comments

For those of you who expressed interest in the source for the terrain project, I am making it available for download. Some people suggested I release it under the GPL, but I’m not going to do anything that formal. I’m releasing it under the “as is” system, where you take it, study it, use it, and see what you can do with it. I do request that if you make use of it you give credit. It’s your conscience, though.

Download Terrain Project (4.3mb)

The project is a Microsoft Visual Studio 6.0 project. I tried to keep the windows-centric code all in one place to make it easier to port to other platforms, although I’ve never tried to port anything so I don’t make any claim as to how easy it would be.

I know at the very beginning I said this was all new code from scratch, but there are actually a number of helper modules I wrote some time ago that I need to mention. The gl* files (glVector.cpp, glMatrix, glBbox, etc) has code that I use in ALL my OpenGL projects. It has stuff for calculating dot products, reflection vectors, manipulating rgba color values, adding vectors together, interpolating values, and a bunch of other stuff that you just need when working with OpenGL. I’m pretty fond of this code. In a lot of ways I think these are worth a lot more than the terrain engine itself. This stuff is just key, and every programmer should have these tools on his or her bat-belt. I could have taken all of these and moved them into a seperate static library, I suppose. That might be a good move if you plan on working with the code for any length of time.

In regards to the rest of the code, I’ve tried to add comments and explain things as well as I could. If you have questions please post them in the comments on this post so I can avoid answering the same questions multiple times.

Also, I’ve included all of the textures I used in the project, but I should warn you that I got the textures via Google Image Search, so I don’t know who owns them. I didn’t even look. You ought to replace them if you plan to release the results of your work for the world to see.

For further reading, here are several Publications by Peter Lindstrom, who (as far as I can tell) is the one who came up with the triangulation technique I allude to in Terrain Part 2 . The one I read is the third from the top, “Terrain Simplification Simplified”. If you read the article (beware, it can be a bit heavy) you’ll see a lot of it focuses on terrain which is optimized every frame. This is different from my system, where I spend many frames on a single set of terrain polygons. Both systems have their merits. Mine is easier on the CPU, but is less than ideal if you plan to jump from one area of the terrain to another very quickly. Also, here is NeHe OpenGL, which is a great site that teaches OpenGL by example, with lots of tutorials and miles of source code.

And just to show that lots of people are into this sort of thing, here are some other more ambitious terrain projects:

Chunked LOD
Virtual Terrain Project

(Hat tip: This MeFi thread)

Good luck. I hope it’s useful to you.


From The Archives:

17 thoughts on “Terrain: Source Code

  1. Elenthir says:


    I found your articles really interresting. I got your source code and tryed to compile it with visual studio 2003. I get really easy few errors like fmod doesn’t know which prototype to chose and paths for gl/glext.h But after it is compiled and running i get no texture displayed. I see the cursor texture and i can see the terrain rendered in white but no textures. I traced and they are loaded but seem to never be binded ie i never go into the draw layer function. Do i have to change something or do something ?

    Anyway good job

  2. JFonseca says:

    I liked your series a lot but the link to the source is broken. Is it intentional?
    If not how can I get the source archive?
    Tanks a lot for your lucid explanations

  3. Shamus says:

    Link was broken. Fixed now. Sorry.

  4. dhandapani says:

    when i tried to execute terrain.cpp file ,i got the message “fatal error C1083: Cannot open include file: ‘gl\glext.h’: No such file or directory”

  5. Mook says:

    Don’t really need the source as ur site was very much helpful on getting the idea of how to make this type of terrain system happen. I had no idea a high detail terrain could be reduced so much.

    Have u thought of makeing a site to cover the process of makeing a terrain engine for a fps?

    Nehow… just popped onto this site from google looking for similar how to’s, and urs is by far the best i have found !

    Nice Job :o)

  6. leo says:

    When I compil Terrain project,to appear……

    ‘error C2065: ‘GL_MIRRORED_REPEAT’ : undeclared identifier’

    How to solve it. thank you.

  7. Shamus says:

    Your GL hear files are different from mine. Without knowing more about your dev environment I can’t tell what might be going wrong here.

  8. Michael McHenry says:

    Elenthir, leo, dhandapani, those problems are fairly easy to solve.

    I use VS 2005 and had those same problems. The fmod and pow compile-time errors are from new overloads. You just have to cast the arguments to the functions properly to get the correct overload to run.

    The problem with glext.h can be solved by downloading a current version of the file from http://oss.sgi.com/projects/ogl-sample/ABI/glext.h

    More info is available on this page: http://www.opengl.org/resources/faq/technical/extensions.htm

    Put the file in the same directory as gl.h and glu.h

    Then the demo compiles in VS 2005 just fine. VS 2003 probably requires similar steps.

  9. Roland says:

    It is interesting to read, but it doesn’t work at all.

    I don’t have time to debug it, but here are some obvious bugs:

    CLAMP(a,b,c) (a < b ? b : a > c ? c : a))
    LIMIT(x) (CLAMP(x,0,MAP_SIZE))

    where the map is defined like:

    It will goes of range (Remember in C++ arrays are from 0 ot N-1).

    If you fix that it generates stack overflow.

  10. Durga Prasad says:

    Hello Shamus,
    This is “The Best” and perhaps ‘only’ comprehensive
    article over the entire Internet on Terrain generation.
    Definitely ,I did not come across this quality stuff elsewhere .I appreciate ur approach in explaining complex things in a much simpler manner.10/10 to you.
    I request you to extend this work/explaination
    in using USGS DTED/SRTM,wherein lot of people need guidance
    in using them for loading continuous stretches of real world data.

  11. Spektre says:

    Hi there,… I have successfully ported this source to borland turbo explorer c++ (bds2006). So it is portable ,… but first it must be repaired because source is little bit messy (VC++ related, spend few hours on that). If you want to do the same, here is the recipe:

    1. repair map.cpp bug, to:
    #define LIMIT(x) (CLAMP(x,0,MAP_SIZE-1))
    2. rename math.h,math.cpp to maths.h,maths.cpp
    because cascade include sometimes not work
    correctly with “”/
    3. remove all #includes in all sub files
    4. in main application source add this:
    #include “glext.h”

    #include “app.h”
    #include “time.h”
    #include “macro.h”
    #include “glTypes.h”
    #include “picture.h”

    #include “camera.h”
    #include “console.h”
    #include “entity.h”
    #include “ini.h”
    #include “map.h”
    #include “maptexture.h”
    #include “maths.h”
    #include “pointer.h”
    #include “render.h”
    #include “sky.h”
    #include “texture.h”
    #include “terrain.h”
    #include “win.h”
    #include “world.h”

    #include “glBbox.cpp”
    #include “glQuat.cpp”
    #include “glRgba.cpp”
    #include “glMatrix.cpp”
    #include “glVector2.cpp”
    #include “glVector3.cpp”

    #include “camera.cpp”
    #include “console.cpp”
    #include “entity.cpp”
    #include “ini.cpp”
    #include “map.cpp”
    #include “maptexture.cpp”
    #include “maths.cpp”
    #include “pointer.cpp”
    #include “render.cpp”
    #include “sky.cpp”
    #include “texture.cpp”
    #include “terrain.cpp”
    #include “win.cpp”
    #include “world.cpp”

    of course if you do not use vcl.h dont add it (borland related). glext.h can be also in gl\ directory, i prefer to have it localy in source code therefore “”.

    5. add this:

    #ifndef _sourcefilename_sourcefileext
    #define _sourcefilename_sourcefileext
    source code

    to files in which the compiler detect duplicit deklaration/definition

    6. change the name for some duplicit named
    constants like buffer in single files (use replace)
    7. that is all good luck :)

    P.S. if you use builder do not include files
    with ADD to Project !!!

    Shamus if you can see my mail contact me , i send you the source so you can add it here for others

  12. snowbell says:

    Hello friends,

    Computer Programing Source Codes and Hardware

    Turbo C, C++, C#, java, HTML, PHP, Joomla, MYSQL, CSS, 3DS MAX, Adobe Flash, Computer Architecture, Pipe-lining, MIPS, Assembly language

    i am very new and please visit and give your valuable comments on my new blogs with many complete project
    with full source codes. so just follow the link below :-



  13. Anup says:

    Thank You for providing us with the Source Code.

  14. Dawn says:

    it is nice.

  15. Bandu says:

    Thanks for Source code….. for gl_mirrored_repeat

  16. leech says:

    Thank you very much for sharing ideas.It’s nice!

Thanks for joining the discussion. Be nice, don't post angry, and enjoy yourself. This is supposed to be fun. Your email address will not be published. Required fields are marked*

You can enclose spoilers in <strike> tags like so:
<strike>Darth Vader is Luke's father!</strike>

You can make things italics like this:
Can you imagine having Darth Vader as your <i>father</i>?

You can make things bold like this:
I'm <b>very</b> glad Darth Vader isn't my father.

You can make links like this:
I'm reading about <a href="http://en.wikipedia.org/wiki/Darth_Vader">Darth Vader</a> on Wikipedia!

You can quote someone like this:
Darth Vader said <blockquote>Luke, I am your father.</blockquote>

Leave a Reply

Your email address will not be published.