|
Heightfield.CPP File:
In our heightfield.cpp file, all our changes are going to take place in the Create function. First, we need to change the vertex count that we will be using throughout to tell us how many vertices we need, we do this by changing the following line:
// vhVertexCount = (int)(hmHeight * hHeight * 6) / (1 * 1);
to:
// vhVertexCount = (int)(hmHeight * hHeight * 6) / (hLOD * hLOD);
The next change we need to make is to the increments in our loops. Instead of incrementing by 1 for every vertex, we need to increment by every hLOD to get every hLOD'th vertex like so:
for (int hMapX = 0; hMapX < hmWidth; hMapX+=hLOD){
for (int hMapZ = 0; hMapZ < hmHeight; hMapZ+=hLOD){
After we have our loop setup, we need to make sure that our polygons span over every hLOD vertex, so instead of incrementing our vertices X and Z positions by 1, we need to increment them by our hLOD value as follows:
flX = (float)hMapX + ((nTri == 1 || nTri == 2 || nTri == 5) ? hLOD : 0);
flZ = (float)hMapZ + ((nTri == 2 || nTri == 4 || nTri == 5) ? hLOD : 0);
Next we are going to move onto our main.cpp file and set a value for our hLOD variable.
#include <stdio.h>
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glext.h>
#include "jpeg.h"
#include "heightfield.h"
bool SwiftHeightField::Init(void){
glGenBuffersARB(1, &vhVBOVertices);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vhVBOVertices);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, vhVertexCount * 3 * sizeof(float), vhVertices, GL_STATIC_DRAW_ARB);
glGenBuffersARB(1, &vhVBOTexCoords);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vhVBOTexCoords);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, vhVertexCount * 2 * sizeof(float), vhTexCoords, GL_STATIC_DRAW_ARB);
delete [] vhVertices;
vhVertices = NULL;
delete [] vhTexCoords;
vhTexCoords = NULL;
return true;
}
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);
vhVertexCount = (int)(hmHeight * hHeight * 6) / (hLOD * hLOD);
vhVertices = new Vert[vhVertexCount];
vhTexCoords = new TexCoord[vhVertexCount];
int nIndex = 0;
float flX;
float flZ;
for (int hMapX = 0; hMapX < hmWidth; hMapX+=hLOD){
for (int hMapZ = 0; hMapZ < hmHeight; hMapZ+=hLOD){
for (int nTri = 0; nTri < 6; nTri++){
flX = (float)hMapX + ((nTri == 1 || nTri == 2 || nTri == 5) ? hLOD : 0);
flZ = (float)hMapZ + ((nTri == 2 || nTri == 4 || nTri == 5) ? hLOD : 0);
vhVertices[nIndex].x = flX;
vhVertices[nIndex].y = hHeightField[(int)flX][(int)flZ];
vhVertices[nIndex].z = flZ;
vhTexCoords[nIndex].u = flX / 1024;
vhTexCoords[nIndex].v = flZ / 1024;
nIndex++;
}
}
}
SwiftTextureJpeg(tID, "texture.jpg", 0);
Init();
return true;
}
void SwiftHeightField::Render(void){
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tID[0]);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vhVBOTexCoords);
glTexCoordPointer(2, GL_FLOAT, 0, (char *) NULL);
glEnableClientState(GL_VERTEX_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vhVBOVertices);
glVertexPointer(3, GL_FLOAT, 0, (char *) NULL);
glDrawArrays(GL_TRIANGLES, 0, vhVertexCount);
glDisableClientState(GL_VERTEX_ARRAY);
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
} |