# Help! heightmap to id3dxmesh

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

## Recommended Posts

ok, im having a hard time trying to create a mesh using the heightmap data. The terrain class works fine as it is, i can import heightmap data, and render and texture. At the end of the chapter it says that you can go a step further and create a id3dxmesh, well im trying to do that, but when i do my program exists , thus im having the problem. Here is my attempted code i wrote, any help would be apprecieted. Im kinda a noob so please write an easy explination if you do. thanks here is the function that makes the mesh.
HRESULT hr = 0;

hr = D3DXCreateMeshFVF(
_numTriangles,
_numVertices,
D3DXMESH_MANAGED,
TerrainVertex::FVF,
device,
&m);

TerrainVertex* v =0;

int startX = -_width / 2;
int startZ =  _depth / 2;

// coordinates to end generating vertices at
int endX =  _width / 2;
int endZ = -_depth / 2;
// compute the increment size of the texture coordinates
// from one vertex to the next.
float uCoordIncrementSize = 1.0f / (float)_numCellsPerRow;
float vCoordIncrementSize = 1.0f / (float)_numCellsPerCol;

m->LockVertexBuffer(0, (void**)&v);

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;

v[index] = TerrainVertex(
(float)x,
(float)_heightmap[index],
(float)z,
(float)j * uCoordIncrementSize,
(float)i * vCoordIncrementSize);

j++; // next column
}
i++; // next row
}

m->UnlockVertexBuffer();

//index buffer

WORD* indices = 0;
m->LockIndexBuffer(0, (void**)&indices);

// index to start of a group of 6 indices that describe the
// two triangles that make up a quad
int baseIndex = 0;

// loop through and compute the triangles of each quad
for(i = 0; i < _numCellsPerCol; i++)
{
for(int j = 0; j < _numCellsPerRow; j++)
{
indices[baseIndex]     =   i   * _numVertsPerRow + j;
indices[baseIndex + 1] =   i   * _numVertsPerRow + j + 1;
indices[baseIndex + 2] = (i+1) * _numVertsPerRow + j;

indices[baseIndex + 3] = (i+1) * _numVertsPerRow + j;
indices[baseIndex + 4] =   i   * _numVertsPerRow + j + 1;
indices[baseIndex + 5] = (i+1) * _numVertsPerRow + j + 1;

baseIndex += 6;
}
}

m->UnlockIndexBuffer();
//end of function


[Edited by - Coder on June 15, 2005 6:14:32 PM]

##### Share on other sites
Friendly hint, but code in these forums is much easier to read when in [ source ] ... [ /source ] tags [smile]

Quote:
 well im trying to do that, but when i do my program exists

As in, your whole application just fails / dies?

If this is the case it hints at a severe runtime error - you need to break out the debugging kit for something like this!

• You don't specify what compiler/IDE you're using, but if you've got a copy of VStudio create a "Debug" profile and run it through the IDE and it'll usually spit out debug information and/or a message when your application screws up.

• If that doesn't point you to the answer, try the D3D debug runtimes - you might be lucky enough to find a warning/error send to the debug output just before it crashes. See this page if you're not sure about this.

The next thing..

You say that you can load your heightmap and render it? Moving it to an ID3DXMesh should only require you to change where the VB/IB is locked, the rest of the VB/IB filling code should remain pretty much the same.

hth
Jack

##### Share on other sites
Im using visual c++ 6.0

im not to keen on how the debugger works. But your right the loading process should be the same? im wondering if im doing anything wrong in creating the mesh.

The code above was the code i also use to extract the vertex and indexes to form the vb and ib. The program shuts down, when i call Mesh->DrawSubset(0)

Im not really using the mesh for texturing or rendering since i can do that already. Im using the mesh for the function D3DXIntersect() i was only trying to render the mesh to see if i loaded the gemotry correctly. so i guess what i want to ask, is ...... am i creating the id3dxmesh correctly? what am i doing wrong or missing?

thanks

##### Share on other sites
From just your code fragment I can't see anything obviously wrong.

Although, from your description it sounds like you could be trampling on some memory that isn't yours... stick in some boundary-checking for the arrays, make sure you aren't indexing any further than you should (or think you should).

Did you try the debug runtime? it's always going to be difficult for you (or me!) to try and fix this sort of error without having as much information to hand as possible. Note that the D3D debug runtimes are not the same as the VStudio debugging tools - essentially all the D3D ones will do is churn out a tonne of text/status information [smile]

What version of the SDK are you using? it is worth noting that VC++6 is no longer a supported development platform. I was pretty sure that any compatability issues would appear during the compile (which you don't seem to have problems with), but I suppose there's no reason why they won't pop-up randomly elsewhere [oh]

hth
Jack

1. 1
Rutin
28
2. 2
3. 3
4. 4
5. 5

• 11
• 13
• 11
• 10
• 13
• ### Forum Statistics

• Total Topics
632952
• Total Posts
3009438
• ### Who's Online (See full list)

There are no registered users currently online

×