Advertisement Jump to content
Sign in to follow this  

Help! heightmap to id3dxmesh

This topic is 4968 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

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(
 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)j * uCoordIncrementSize,
    (float)i * vCoordIncrementSize);
   j++; // next column
  i++; // next row
 //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;
   // next quad
   baseIndex += 6;
//end of function

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

Share this post

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

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.


Share this post

Link to post
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?


Share this post

Link to post
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]


Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!