# 4. Terrain Triangle Strips

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. 9. 10. 11. 12. 13. 14. #include class SwiftHeightField {     private:     int hmHeight;     int hmWidth;     public:     bool Create(char *hFileName, const int hWidth, const int hHeight);          void Render(void);          BYTE hHeightField; };

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. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. #include #include #include “heightfield.h” bool SwiftHeightField::Create(char *hFileName, const int hWidth, const int hHeight){      hmHeight = hHeight;     hmWidth = hWidth;          FILE *fp;          fp = fopen(hFileName, “rb”);          fread(hHeightField, 1, hWidth * hHeight, fp);          fclose(fp);          return true; } void SwiftHeightField::Render(void){     for (int hMapX = 0; hMapX < hmWidth; hMapX++){         for (int hMapZ = 0; hMapZ < hmHeight; hMapZ++){             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();         }     } }

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