Reputation: 143
Ok, so I'm developing a small program that randomly generates a single sheet of terrain using a recursive function to subdivide between four given points, and the terrain is made of of quads. The centre point found on each recursion is given a random height value, and then all of the vertices are saved to an array, which is then looped through when drawing.
Originally, I was defining the 2D array used to store the vertices on the stack, however depending on the number of subdivisions this can become quite large. A depth of 3 for example yields 256 vertices. So, I decided defining this array on the heap where possible will be much more efficient for larger sheets of terrain. In the Terrain class I define the following array of float pointers.
float* vertices[];
Then, in the constructor for this class, I do the following to initialize it's size on the heap. 4 to the power of the number of given subdivisions plus one yields 256, and number of vertices that are created.
int vertexCount = pow(4.0, subdivisions+1);
*vertices = new float[vertexCount];
Then, each of these pointers points to an array of float values in the heap, defined as follows.
for(int i = 0; i<vertexCount + 1; i++)
{
float* temp = new float[3];
vertices[i] = temp;
delete temp;
temp = 0;
}
My recursive function used to create the terrain looks as follows:
void Terrain::Subdivide(float* a, float* b, float* c, float* d, int subdivisions, float maxHeight)
{
if(subdivisions>0)
{
float e[3];
float f[3];
float g[3];
float h[3];
float centre[3];
int i = 0;
srand(time(NULL));
for(i = 0; i<3; i++) if(i!= 1) e[i] = (a[i]+b[i])/2;
for(i = 0; i<3; i++) if(i!= 1) f[i] = (b[i]+c[i])/2;
for(i = 0; i<3; i++) if(i!= 1) g[i] = (c[i]+d[i])/2;
for(i = 0; i<3; i++) if(i!= 1) h[i] = (d[i]+a[i])/2;
centre[0] = (h[0]+f[0])/2;
centre[2] = (e[2]+g[2])/2;
e[1] = 0;
f[1] = 0;
g[1] = 0;
h[1] = 0;
centre[1] = (float)rand()/((float)RAND_MAX/maxHeight);
Subdivide(a, e, centre, h, subdivisions-1 , maxHeight);
Subdivide(e, b, f, centre, subdivisions-1, maxHeight);
Subdivide(centre, f, c, g, subdivisions-1, maxHeight);
Subdivide(h, centre, g, d, subdivisions-1, maxHeight);
}
else
{
int i = 0;
for(i = 0; i<3; i++) {vertices[vertexCounter][i] = a[i];} vertexCounter++;
for(i = 0; i<3; i++) {vertices[vertexCounter][i] = b[i];} vertexCounter++;
for(i = 0; i<3; i++) {vertices[vertexCounter][i] = c[i];} vertexCounter++;
for(i = 0; i<3; i++) {vertices[vertexCounter][i] = d[i];} vertexCounter++;
}
}
The code seems to work when I simply define vertices as a 2D array on the stack (with some changes elsewhere), however this code produces an unhandled exception in Glut.h on the following line, when calling glutInit in my main function: (Line 486)
static void APIENTRY glutInit_ATEXIT_HACK(int *argcp, char **argv) { __glutInitWithExit(argcp, argv, exit); }
I've looked through the code so many times now and I can't see where the problem lies, it's even stranger that the exception should be thrown from Glut.h and not the Terrain class. Can anyone see anything wrong here?
Upvotes: 1
Views: 612
Reputation: 87944
Here's an error
for(int i = 0; i<vertexCount + 1; i++)
should be
for(int i = 0; i<vertexCount; i++)
The reason we count arrays from zero is so you don't have to add one.
Here's another mistake
for(int i = 0; i<vertexCount; i++)
{
float* temp = new float[3];
vertices[i] = temp;
delete temp;
temp = 0;
}
should be
for(int i = 0; i<vertexCount; i++)
{
vertices[i] = new float[3];
}
For some reason you allocate the memory, free it again immediately but continue to use the freed memory in your vertex array. That's going to crash very quickly.
Third (hopefully final) mistake is
*vertices = new float[vertexCount];
should be
vertices = new float*[vertexCount];
To simulate a 2D array, the first dimension is an array of of float pointers. That's what the fixed code allocates.
Upvotes: 1