OK, so I fixed the seg fault, but now all I am getting is a blank, black screen. I am wondering if I am loading and rendering the .raw file and the texture correctly.
Here is where I load the files
Code:
bool cTerrain::Initialize(LPDIRECT3DDEVICE9 pDevice, char *RawFile, char*TerrainTexture) {
Release();
// Load height map
char path[MAX_PATH] = {0};
cUtility::GetMediaFile(RawFile, path);
std::ifstream HeightMap;
HeightMap.open(path, std::ios::binary);
if (HeightMap.fail() ) {
SHOWERROR("Could not open HeightMap file.", __FILE__, __LINE__);
return false;
}
// Get number of vertices
HeightMap.seekg(0, std::ios::end);
m_numVertices = HeightMap.tellg();
HeightMap.seekg(0, std::ios::beg);
// Allocate memory and read the data
m_pHeight = new UCHAR[m_numVertices];
HeightMap.read( (char*)m_pHeight, m_numVertices);
HeightMap.close();
// Generate vertices
UINT width = (int)sqrt( (float)m_numVertices);
cuCustomVertex::PositionTextured* pVertices = NULL;
cTriangleStrip::GenerateVerticesWithHeight(&pVertices, width,
width, m_pHeight);
m_vb.CreateBuffer(pDevice, m_numVertices, D3DFVF_XYZ | D3DFVF_TEX1,
sizeof(cuCustomVertex::PositionTextured) );
m_vb.SetData(m_numVertices, pVertices, 0);
// Generate indices
USHORT* pIndices = NULL;
m_numIndices = cTriangleStrip::GenerateIndices(&pIndices, width, width);
m_ib.CreateBuffer(pDevice, m_numIndices, D3DFMT_INDEX16);
m_ib.SetData(m_numIndices, pIndices, 0);
m_vb.SetIndexBuffer(&m_ib);
cUtility::GetMediaFile(TerrainTexture, path);
if (FAILED(D3DXCreateTextureFromFile(pDevice, path, &m_pTexture) ) ) {
SHOWERROR("Unable to load terrain textures.", __FILE__, __LINE__);
return false;
}
return true;
}
My TriangleStrip and CustomVertex
Code:
void cTriangleStrip::GenerateVerticesWithHeight
(cuCustomVertex::PositionTextured** ppVertices,
int verticesAlongWidth,
int verticesAlongLength, UCHAR* pHeight) {
SAFE_DELETE_ARRAY(*ppVertices);
*ppVertices = new cuCustomVertex::PositionTextured
[verticesAlongLength * verticesAlongWidth];
for (int z = 0; z < verticesAlongLength; z++) {
for ( int x = 0; x < verticesAlongWidth; x++ ) {
float halfWidth = ((float)verticesAlongWidth - 1.0f) / 2.0f;
float halfLength = ((float)verticesAlongLength - 1.0f) / 2.0f;
(*ppVertices)[z * verticesAlongLength + x] =
cuCustomVertex::PositionTextured(
(float)x - halfWidth,
(float)pHeight[z * verticesAlongLength + x],
(float)z - halfLength,
(float)x / (verticesAlongWidth - 1),
(float)z / (verticesAlongLength - 1) );
}
}
return;
}
typedef struct PositionTextured
{
public:
PositionTextured() : X(0), Y(0), Z(0), Tu(0), Tv(0) {}
PositionTextured( float x, float y, float z, float tu, float tv )
: X(x), Y(y), Z(z), Tu(tu), Tv(tv) {}
float X, Y, Z;
float Tu, Tv;
} PositionTextured;
My vertex and Index Buffers
Code:
bool cVertexBuffer::CreateBuffer(LPDIRECT3DDEVICE9 pDevice, UINT numVertices,
DWORD FVF, UINT VertexSize, bool dynamic) {
Release();
m_numVertices = numVertices;
m_FVF = FVF;
m_VertexSize = VertexSize;
// Dynamic buffers can't be in D3DPOOL_MANAGED
D3DPOOL pool = dynamic ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED;
DWORD usage =
dynamic ? D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC : D3DUSAGE_WRITEONLY;
if(FAILED(pDevice->CreateVertexBuffer(m_numVertices * m_VertexSize,
usage, m_FVF, pool, &m_pVB, NULL) ) ) {
SHOWERROR("CreateVertexBuffer failed.", __FILE__, __LINE__);
return false;
}
return true;
}
bool cIndexBuffer::CreateBuffer(LPDIRECT3DDEVICE9 pDevice, UINT numIndices,
D3DFORMAT format, bool dynamic) {
Release();
m_numIndices = numIndices;
// Dynamic buffers can't be in D3DPOOL_MANAGED
D3DPOOL pool = (dynamic ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED);
UINT size = ( (format == D3DFMT_INDEX32) ? sizeof(UINT) : sizeof(USHORT) );
DWORD usage =
(dynamic ? D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC : D3DUSAGE_WRITEONLY);
if(FAILED(pDevice->CreateIndexBuffer(m_numIndices * size, usage, format,
pool, &m_pIB, NULL) ) ) {
SHOWERROR("CreateIndexBuffer failed.", __FILE__, __LINE__);
return false;
}
return true;
}
And here is where I render it
Code:
void cGameApp::RenderFrame(LPDIRECT3DDEVICE9 pDevice, float ElapsedTime) {
sprintf(m_fps, "%.2f fps", m_pFramework->GetFPS() );
pDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(100, 200, 255), 1.0f, 0);
pDevice->BeginScene();
m_terrain.Render(pDevice);
// Display framerate and instructions
m_pTextSprite->Begin(D3DXSPRITE_ALPHABLEND | D3DXSPRITE_SORT_TEXTURE);
m_font.Print(m_fps, 5, 5, D3DCOLOR_XRGB(255, 0, 0), m_pTextSprite);
if (m_showInstructions) {
m_font.Print(g_instruct,5,20, D3DCOLOR_XRGB(255, 255, 255), m_pTextSprite);
} else {
m_font.Print("Hit F12 to view the instructions.", 5, 20,
D3DCOLOR_XRGB(255, 255, 255), m_pTextSprite);
}
m_pTextSprite->End();
pDevice->EndScene();
pDevice->Present(0, 0, 0, 0);
return;
}
void cTerrain::Render(LPDIRECT3DDEVICE9 pDevice) {
pDevice->SetTransform(D3DTS_WORLD, GetTransform() );
pDevice->SetTexture(0, m_pTexture);
m_vb.Render(pDevice, m_numIndices - 2, D3DPT_TRIANGLESTRIP);
return;
}
I would appreciate it if anyone could offer any suggestions as to what I am doing wrong as this is really starting to irritate me