Jump to content
  • Advertisement
Sign in to follow this  
evanofsky

Terrain Math

This topic is 4812 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here's the problem: I really suck at logical math and stuff like that. Here's the other problem: my geo-mipmapping math isn't working. I'm dividing the terrain (which used to be just a vertex buffer and a index buffer) into 100 meshes (or cells, whatever). First I initialize all the meshes, then I start filling the buffers:
TerrainVertex* v = 0;
int i = 0;
for(int z = startZ; z >= endZ; z -= _cellSpacing)
{
	int j = 0;
	for(int x = startX; x <= endX; x += _cellSpacing)
	{
		// compute the correct index into the vertex buffer and
                // heightmap based on where we are in the nested loop.
		int index = i * _numVertsPerRow + j;

                // convert from the range [0 - numVertices] to [0 - numMeshes]
		int meshIndexI = i * NUM_MESHES_COL / _numVertsPerCol;
		int meshIndexJ = j * NUM_MESHES_ROW / _numVertsPerRow;
		int meshIndex = meshIndexI * NUM_MESHES_COL + meshIndexJ;

                // it crashes here after several iterations of meshIndexJ
		littleMeshes[meshIndex]->LockVertexBuffer(0, (void**)&v);

		// find the normal by taking the cross product of two
		// vectors on the quad.
		D3DXVECTOR3 nn = calcNormal(j, i);

		v[index - meshIndex] = TerrainVertex(
			(float)x, // init the position
			(float)_heightmap[index],
			(float)z,
			nn.x, // the normal
			nn.y,
			nn.z,
			(float)j * uCoordIncrementSize, // texture coordinates
			(float)i * vCoordIncrementSize);
		littleMeshes[meshIndex]->UnlockVertexBuffer();
		v = 0;
		j++; // next column
	}
	i++; // next row
}
I know it's not a programming error with pointers or something because the loop iterates for a while through several meshes before crashing when it indexes in the "littleMeshes" array. Any help would be greatly appreciated!

Share this post


Link to post
Share on other sites
Advertisement
Well just so you know. Memory problems sometimes do not occur until you iterate several elements past the end.

example:

int *pIntArray = new int[50]
for (int i = 0; i < 100; i++) {
int = i;
}

I bet you it will crash around the 55th or 60th iterations.
In some case's, if you are unlucky, this won't crash at all but instead totally corrupt the future parts of your program.

Share this post


Link to post
Share on other sites
Actually, it crashes no where near the end of the array.

It works like this: meshIndexI never changes. It's always 0. meshIndexJ changes like it should. It goes up to 16, which is numVertsPerRow (160) / NUM_MESHES_ROW (10), then it resets itself to 0 for the next row. The third time meshIndexJ resets itself to 0, it gets to 12 and then crashes. That's why I think my math is wrong.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!