1. Terrain Class
Jump To:
heightfield.h Source
heightfield.cpp Source
Heightfield.H File: This tutorial is going on the basis that you have read at least some of the previous OpenGL tutorials. It pretty much starts off where the second camera tutorial ends. This tutorial unlike my previous OpenGL tutorials, is going to expand into multiple custom files include header and source code files while my previous tutorials just kept everything in the one file, and became quite messy. Anyway, time to begin. First off we are going to look into our heightfield.h file. This is going to be our global header file for the creation of our terrain. I am going to create a custom class in here called SwiftHeightField with the following code: // class SwiftHeightField { In here, I am going to start off with two public functions. The first is a boolean function that will handle that loading and creation of our heightfield, while the second will render our terrain. We will start off by creating our Create function. The code will look like: // class SwiftHeightField { // public: The first variable hFileName will be the filename for the .RAW file we store our terrain height data in. The next two are the width and the height of the file in pixels so that we can load it in correctly. The next function I am going to add is the Render function. This is just a basic void function that doesn’t return any value. Our heightfield.h file will look like this: // class SwiftHeightField { // public: This is all we are going to do in our heightfield.h file for now. Seems quite basic, but I want to keep it as basic as possible 🙂
|
||||
Heightfield.CPP File: Here is the start of our heightfield.cpp source code file. The first thing we need to do now is load in our heightfield.h file with the following line: // #include "heightfield.h" Our creation and rendering functions are going to be pretty much empty, because in this tutorial we are just starting off and getting everything ready for later when we really get into coding. So we have our Create function: // bool SwiftHeightField::Create(char *hFileName, int hWidth, int hHeight){ And our Render function: // void SwiftHeightField::Render(void){ // }
|
||||
Main.CPP File: Here we have our main source code file. This is not going to change much until later on in the tutorials. It is pretty much as I said, the seconds camera tutorial. The first thing we need to do here is include our header file. So we add the line: // #include "heightfield.h" Then we need to add a variable that ties into our heightfield class. This is going to be added with: // SwiftHeightField hField; Now we have a class to hold our heightfield information for our terrain. So we need to Create our heightfield. So we call our Create function inside an initialization function, before we start drawing. So our Init function becomes: // void Init (void) { // glEnable(GL_DEPTH_TEST); // hField.Create("", 1024, 1024); Note here that I am not actually loading anything. The blank "" will not throw back an error looking for a file named nothing, because our Create function does not try to load anything yet. (Remember, it’s blank inside, hehe). I just decided to place the integer 1024 for the width and height of the file, because that is the size I am going to be using later on. We then need to move onto calling our Render function. This goes inside our display function after we perform our camera rotations. Our display function will look as such: // void display (void) { // hField.Render(); // glutSwapBuffers(); This is all we need for now. Check out the next tutorial on loading the heightfield data in here 🙂 If you have any questions, just email me at swiftless@gmail.com
|
||||
Download: Download heightfield.h Source Code for this Tutorial |
Why the tutorial steps and code are center aligned? It’s hard to read and a distraction. Please left align so its easy to read! Thanks!
I like your stuff but it’s Windows based. Would you be able to comment on how these tutorials could be adapted to compile on OSX, BSD, or Linux
Hi Ste,
Most of the stuff should be easily portable, as I have already ported most of it to OSX. All you need to really change is all the Windows only calls, which I believe from memory are only really the calls to load the file. Remove “#include ” from everywhere, and see what comes up as an error.
Cheers,
Swiftless
Excellent tutorials!!
Please keep up with new OpenGL themes so we can learn more!!