4. Terrain Triangle Strips
Jump To:
heightfield.h Source
heightfield.cpp Source
Heightfield.H File:
Our heightfield.h file in this tutorial is going to be left untouched. Just move onto our heightfield.cpp file 🙂
1. 2. 3. 4. 5. 6. 7. 8. |
#include <windows.h>
class SwiftHeightField { bool Create(char *hFileName, const int hWidth, const int hHeight); }; |
Heightfield.CPP File:
All the changes in this tutorial, pretty much take place in this file and only really affect our Render function. We are moving from glBegin(GL_POINTS) to glBegin(GL_TRIANGLE_STRIP). This fills in the gaps between our vertices, creating polygons instead of just points where are vertices were. So looking at our code, where we had our GL_POINTS calls, we now have:
// glBegin(GL_TRIANGLE_STRIP);
// glVertex3f(hMapX, hHeightField[hMapX][hMapZ], hMapZ);
// glVertex3f(hMapX, hHeightField[hMapX][hMapZ + 1], hMapZ + 1);
// glVertex3f(hMapX + 1, hHeightField[hMapX + 1][hMapZ], hMapZ);
// glVertex3f(hMapX + 1, hHeightField[hMapX + 1][hMapZ + 1], hMapZ + 1);
// glEnd();
It is simple enough to follow, we set the vertices to our triangle strips from our current vertex in the loop, then the vertices surrounding it creating four corners. This tutorial is built with the impression that basic OpenGL knowledge is already known so I will not explain this code 🙂
1. 2. 3. 4. 5. 6. 7. 16. 25. 34. |
#include <stdio.h> #include <gl\gl.h> #include “heightfield.h” bool SwiftHeightField::Create(char *hFileName, const int hWidth, hmHeight = hHeight; void SwiftHeightField::Render(void){ for (int hMapX = 0; hMapX < hmWidth; hMapX++){ glBegin(GL_TRIANGLE_STRIP); glVertex3f(hMapX, hHeightField[hMapX][hMapZ + 1], hMapZ + 1); glVertex3f(hMapX + 1, hHeightField[hMapX + 1][hMapZ], hMapZ); glVertex3f(hMapX + 1, hHeightField[hMapX + 1][hMapZ + 1], hMapZ + 1); glEnd(); |
Main.CPP File:
In our Init function, I have just added the line:
// glDepthFunc(GL_LEQUAL);
To make sure our triangle strips display correctly 🙂
Check out the next tutorial on rendering the heightfield with textures here 🙂
If you have any questions, just email me at swiftless@gmail.com
1. 2. 3. 4. 5. 6. 15. 24. 33. 42. 51. 60. 69. 78. 87. 96. 105. 114. 123. |
#include <GL/glew.h>
#include <GL/gl.h> #include <math.h> #include <windows.h> #include <string.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; glTranslated(–xpos,–ypos,–zpos); void display (void) { glClearColor (0.0,0.0,0.0,1.0); glLoadIdentity(); glutSwapBuffers(); void Init (void) { glDepthFunc(GL_LEQUAL); void mouseMovement(int x, int y) { int diffy=y–lasty; yrot += (float) diffx; void keyboard (unsigned char key, int x, int y) { yrotrad = (yrot / 180 * 3.141592654f); xpos += float(sin(yrotrad)) * cScale; ypos –= float(sin(xrotrad)) ; } yrotrad = (yrot / 180 * 3.141592654f); xpos –= float(sin(yrotrad)) * cScale; ypos += float(sin(xrotrad)); } yrotrad = (yrot / 180 * 3.141592654f); zpos += float(sin(yrotrad)) * cScale; 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
I’m curious have you actually tested your code? Did a copy-paste to see if it’s working and a got a real mess there. Do give it a try yourself? The triangle-strip-part is wrong.
Hi RB,
Yes, all code is tested before it goes on the site. Differences in hardware *may* cause issues but I’d need a little more information to see how yours is acting up. All my code is tested on Nvidia and Intel graphics cards and most of the time on both Windows and OSX with the occasional run on Linux. The Terrain tutorial code is old, but because of that, has been tested on all of the above.
Thanks,
Swiftless