2. Terrain Loading
Jump To:
heightfield.h Source
heightfield.cpp Source
Heightfield.H File:
Our first changes will take place in our heightfield.h file. First we need to include the windows.h header file because we are using the BYTE variable type specified within the windows.h file for loading in from our heightfield.raw image file.
// #include <windows.h>
We then need to declare a variable with the BYTE variable which I am calling hHeightField like so:
// BYTE hHeightField[1024][1024];
1. 2. 3. |
#include <windows.h>
class SwiftHeightField { void Render(void); BYTE hHeightField[1024][1024]; }; |
Heightfield.CPP File:
First off here, just as above we are including another header file, only this one is stdio.h and it is used to output our loaded in data to the console window to show that we have actually loaded in some data.
// #include <stdio.h>
Then we are going to play with our Create function. First off we need to declare a FILE variable, which I am just calling fp as so:
// FILE *fp;
We then need to actually open our heightfield.raw file into our fp variable with the following line:
// fp = fopen(hFileName, "rb");
I am using the expression "rb" which stands for read as binary so we are actually reading data in, and not writing it out. Once we have opened our file, we need to read in the data. We are reading the data into our hHeightField variable starting at the start of the file, and going until it reaches the position hWidth * hHeight, which in this case is 1024 * 1024, which equals 1048576. (That’s gonna be alot of vertices ^_^).
// fread(hHeightField, 1, hWidth * hHeight, fp);
Finally we just need to close the file, which is simple with the line:
// fclose(fp);
The next 5 lines make up a look which will go through each piece of data we read in to our hHeightField variable and print it to the console window.
// for (int hMapX = 0; hMapX < hWidth; hMapX++){
// for (int hMapZ = 0; hMapZ < hHeight; hMapZ++){
// printf("%d, %d, %d\n", hMapX, hMapZ, hHeightField[hMapX][hMapZ]);
// }
// }
1. 2. 3. 4. 5. 6. 7. 16. 25. |
#include “heightfield.h”
bool SwiftHeightField::Create(char *hFileName, const FILE *fp; fp = fopen(hFileName, “rb”); fread(hHeightField, 1, hWidth * hHeight, fp); fclose(fp); for (int hMapX = 0; hMapX < hWidth; hMapX++){ for (int hMapZ = 0; hMapZ < hHeight; hMapZ++){ } return true; void SwiftHeightField::Render(void){ } |
Main.CPP File:
Hehe, this is a simple change. All we need to do here is change our Create call to load in the file, heightfield.raw. So our Create line looks like:
// hField.Create("heightfield.raw", 1024, 1024);
Check out the next tutorial on rendering the heightfield here 🙂
If you have any questions, just email me at swiftless@gmail.com
1. 2. 3. 4. 5. 6. 7. 8. 9. 18. 27. 36. 45. 54. 63. 72. 81. 90. 99. 108. 117. 126. |
#include <GL/glew.h> #include <GL/gl.h> #include <GL/glext.h> #include <math.h> #include <windows.h> #include <assert.h> #include “heightfield.h” #pragma comment(lib,“glew32.lib”) float xpos = 851.078, ypos = 351.594, zpos = 281.033, xrot = 758, yrot = 238, float lastx, lasty; float bounce; SwiftHeightField hField; void camera (void) { int posZ = (int)zpos; glRotatef(xrot,1.0,0.0,0.0); glTranslated(–xpos,–ypos,–zpos); void display (void) { glClearColor (0.0,0.0,0.0,1.0); glLoadIdentity(); hField.Render(); glutSwapBuffers(); void Init (void) { hField.Create(“heightfield.raw”, 1024, 1024); void mouseMovement(int x, int y) { int diffx=x–lastx; lasty=y; } void keyboard (unsigned char key, int x, int y) { if (key == ‘w’) yrotrad = (yrot / 180 * 3.141592654f); xpos += float(sin(yrotrad)) * cScale; ypos –= float(sin(xrotrad)) ; } if (key == ‘s’) yrotrad = (yrot / 180 * 3.141592654f); xpos –= float(sin(yrotrad)) * cScale; ypos += float(sin(xrotrad)); } if (key == ‘d’) yrotrad = (yrot / 180 * 3.141592654f); zpos += float(sin(yrotrad)) * cScale; if (key == ‘a’) yrotrad = (yrot / 180 * 3.141592654f); zpos –= float(sin(yrotrad)) * cScale; } void reshape (int w, int h) { glMatrixMode (GL_PROJECTION); glMatrixMode (GL_MODELVIEW); int main (int argc, char **argv) { glutInit(&argc, argv); glutInitWindowSize(500, 500); Init(); glutReshapeFunc(reshape); glutMainLoop (); |
Download:
Download heightfield.h Source Code for this Tutorial
Download heightfield.cpp Source Code for this Tutorial
looks good, but the download files not found 🙁