   Heightmap Issues
  I'm trying to create a heightmap, but seem to be coming across and insane amount of errors, my latest error being:First-chance exception at 0x7c812a5b in WeatherSim.exe: Microsoft C++ exception: long at memory location 0x0012faec..which happens:g_pd3dDevice-&gt;DrawIndexedPrimitive(D3DPT_TRIANGLELIST, // PrimitiveType0, // Base Vertex Index0, // MinIndex  m_dwTerrainVertices, // NumVertices0, // StartIndexm_dwTerrainPrimitives);// PrimitiveCountI think it is going off the end, but then I don't know how to fix it.Any help would be appreciated.[Edited by - Roadkill247 on February 4, 2008 5:14:46 AM]
  It is very hard to guess what's causing this without seeing more code, espcially the code that computes m_dwTerrainVertices and m_dwTerrainPrimitives.   ;#define TERRAIN_X 64#define TERRAIN_Y 64m_TerrainVertices = (TERRAIN_X + 1) * (TERRAIN_Y + 1);m_TerrainPrimitives = TERRAIN_X * TERRAIN_Y * 2; [Edited by - Roadkill247 on February 4, 2008 5:32:15 AM];  I'm going to guess that your heightmap size is 64x64, in which case your code should be:#define TERRAIN_X 64#define TERRAIN_Y 64m_TerrainVertices = TERRAIN_X * TERRAIN_Y;m_TerrainPrimitives = (TERRAIN_X - 1) * (TERRAIN_Y - 1) * 2;In other words, do TERRAIN_X/Y mark the number of vertices per row/column, or the number of quads (if it's vertices, your code is wrong)?   ;  ok, that seemed to have fixed that bit, and it is computing the number of vertices and primitives (hopefully).and my next error is a memory violation in the code:m_pTerrainIB-&gt;Lock(0,0,(void**)&pIndexData,0); [Edited by - Roadkill247 on February 4, 2008 5:23:25 AM];  Again, it's hard to tell what's the problem without seeing more code. Maybe m_pTerrainIB is NULL? What's the exact error the compiler is giving you?   ;#include "CTerrain.h"#include "Defines.h"#include &lt;iostream&gt;#include &lt;fstream&gt;CTerrain::CTerrain(){m_pTerrainVB = NULL;m_pTerrainIB = NULL;m_TerrainVertices = TERRAIN_X * TERRAIN_Y;m_TerrainPrimitives = (TERRAIN_X) * (TERRAIN_Y) * 2 - 2;CreateTerrain();}void CTerrain::CreateTerrain(){unsigned char aHeightMap[TERRAIN_X + 1][TERRAIN_Y + 1];D3DVERTEX* pVertexData = NULL;short* pIndexData = NULL;ifstream File;//load height information from fileFile.open("heightmap.raw",ios::binary);for(unsigned y = 0;y &lt; (TERRAIN_Y + 1);++y) { for(unsigned x = 0;x &lt; (TERRAIN_X + 1);++x)  {  aHeightMap[x][y] = File.get();  } }File.close();//create vertex buffer and fill in vertciesg_pd3dDevice-&gt;CreateVertexBuffer(sizeof(D3DVERTEX) * m_TerrainVertices,D3DUSAGE_WRITEONLY,   D3DFVF_XYZ | D3DFVF_DIFFUSE, D3DPOOL_MANAGED,   &m_pTerrainVB,NULL);m_pTerrainVB-&gt;Lock(0,0,(void**)&pVertexData,NULL);for(unsigned y = 0;y &lt; (TERRAIN_Y + 1);++y) { for(unsigned x = 0;x &lt; (TERRAIN_X + 1);++x)  {  pVertexData[x + y * (TERRAIN_X + 1)].fX = (float)x;  pVertexData[x + y * (TERRAIN_X + 1)].fY = (float)y;  pVertexData[x + y * (TERRAIN_X + 1)].fZ = (float)aHeightMap[x][y] / 15.0f;  pVertexData[x + y * (TERRAIN_X + 1)].colour = 0xffffffff;  } }m_pTerrainVB-&gt;Unlock();//create index buffer and fill in indicesg_pd3dDevice-&gt;CreateIndexBuffer(sizeof(short) * m_TerrainPrimitives * 3, D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,    D3DPOOL_MANAGED, &m_pTerrainIB,  NULL);m_pTerrainIB-&gt;Lock(0,0,(void**)&pIndexData,0);for(unsigned y = 0;y &lt; TERRAIN_Y;++y) { for(unsigned x = 0;x &lt; TERRAIN_X;++x)  {  *pIndexData++ = x + y * (TERRAIN_X + 1);  *pIndexData++ = x + 1 + y * (TERRAIN_X + 1);  *pIndexData++ = x + 1 + (y + 1) * (TERRAIN_X + 1);  *pIndexData++ = x + y * (TERRAIN_X + 1);  *pIndexData++ = x + 1 + (y + 1) * (TERRAIN_X + 1);  *pIndexData++ = x + (y + 1) * (TERRAIN_X + 1);  } }m_pTerrainIB-&gt;Unlock();}void CTerrain::Draw(){g_pd3dDevice-&gt;SetStreamSource(0,m_pTerrainVB,0,sizeof(D3DVERTEX));g_pd3dDevice-&gt;SetIndices(m_pTerrainIB);g_pd3dDevice-&gt;DrawIndexedPrimitive(D3DPT_TRIANGLELIST,   // PrimitiveType0,   // Base Vertex Index0,   // MinIndex    m_TerrainVertices,   // NumVertices0,   // StartIndex  m_TerrainPrimitives);  // PrimitiveCount}//DrawAnd the error is just a memory one[Edited by - Roadkill247 on February 4, 2008 5:18:39 AM];Quote:Original post by Roadkill247...m_TerrainVertices = TERRAIN_X * TERRAIN_Y;...//create vertex buffer and fill in vertciesg_pd3dDevice->CreateVertexBuffer(sizeof(D3DVERTEX) * m_TerrainVertices, D3DUSAGE_WRITEONLY, D3DFVF_XYZ | D3DFVF_DIFFUSE, D3DPOOL_MANAGED, &m_pTerrainVB, NULL);m_pTerrainVB->Lock(0,0,(void**)&pVertexData,NULL);for(unsigned y = 0;y < (TERRAIN_Y + 1);++y) { for(unsigned x = 0;x < (TERRAIN_X + 1);++x)  {  pVertexData[x + y * (TERRAIN_X + 1)].fX = (float)x;  pVertexData[x + y * (TERRAIN_X + 1)].fY = (float)y;  pVertexData[x + y * (TERRAIN_X + 1)].fZ = (float)aHeightMap[x][y] / 15.0f;  pVertexData[x + y * (TERRAIN_X + 1)].colour = 0xffffffff;  } }TERRAIN_X * TERRAIN_Y = 4096;if y = 64 (which is less than TERRAIN_Y + 1) then you do:pVertexData[x + 64 * 65] x + 4160 > 4096Buffer overflow?Edited to break up long line;Quote:unsigned char aHeightMap[TERRAIN_X + 1][TERRAIN_Y + 1];Quote:m_TerrainVertices = TERRAIN_X * TERRAIN_Y;Make up your mind. How many vertices are there?You might consider using more descriptive names ("TILES_ACROSS" / "TILES_DOWN" versus "TILE_VERTICES_ACROSS" / "TILE_VERTICES_DOWN"), and using both of those pairs of constants. Relate them mathematically, and use real constants:const int TILES_ACROSS = 64;const int TILE_VERTICES_ACROSS = TILES_ACROSS + 1;const int TILES_DOWN = 64;const int TILE_VERTICES_DOWN = TILES_DOWN + 1;This way, the constants have (a) scope and (b) type, and (c) if you change one, the corresponding value still stays in sync, and yet (d) you aren't constantly wondering what all the +1's and -1's are for.See also.;  Thanks, took both into consideration, but now the problem is just a purely black screen. I have enabled wireframing as a renderstate but get nothing.   