Original Post
I'm working on a quick terrain demo for a side project and am having an issue with generating the flat terrain tri grid. I've checked both the vertex values and index values, ensuring that it is properly placed and wound in a clockwise fashion, but nothing seems to be rendering.
Generate Code
Render code
Generate Code
void Generate(IDirect3DDevice9* pDevice, int cellsWide, int cellsHigh, float xStep, float zStep) { numVertsX = cellsWide + 1; numVertsZ = cellsHigh + 1; pDevice->CreateVertexBuffer(numVertsX * numVertsZ * sizeof(sCUSTOMVERTEX), D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX, D3DPOOL_MANAGED, &m_pVertexBuffer, NULL); pDevice->CreateIndexBuffer(cellsWide * 2 * cellsHigh * 3 * sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_pIndexBuffer, NULL); sCUSTOMVERTEX* vertices = 0; D3DXVECTOR3 minBound = D3DXVECTOR3(cellsWide * xStep * -0.5f, 0.0f, cellsHigh * zStep * -0.5f); m_pVertexBuffer->Lock(0,0,(void**)&vertices,0); D3DXVECTOR3 pos(minBound.x, 0.0f, minBound.z); int count = 0; //generate vertices for(unsigned z = 0; z < numVertsZ; ++z) { pos.x = minBound.x; for(unsigned x = 0; x < numVertsX; ++x) { vertices[count].pos = pos; pos.x += xStep; ++count; } pos.z += zStep; } m_pVertexBuffer->Unlock(); //generate indices count = 0; int vIndex = 0; WORD* indices = 0; m_pIndexBuffer->Lock(0,0,(void**)&indices,0); for(int z = 0; z < cellsHigh; ++z) { for(int x = 0; x < cellsWide; ++x) { //first triangle indices[count++] = vIndex; indices[count++] = vIndex + numVertsX; indices[count++] = vIndex + numVertsX + 1; //second triangle indices[count++] = vIndex; indices[count++] = vIndex + numVertsX + 1; indices[count++] = vIndex + 1; ++vIndex; } ++vIndex; } m_pIndexBuffer->Unlock(); }Render code
bool CDirectX::Render(){ // Error check if( !m_pD3DDevice) return false; // Clear the screen m_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 150), 1.0f, 0); // Draw other things here //create transform matrices and set to identity D3DXMATRIX worldMat; D3DXMATRIX transMat; D3DXMATRIX scaleMat; D3DXMATRIX rotMat; D3DXMatrixIdentity(&worldMat); D3DXMatrixIdentity(&transMat); D3DXMatrixIdentity(&scaleMat); D3DXMatrixIdentity(&rotMat); if(SUCCEEDED(m_pD3DDevice->BeginScene())) { m_pD3DDevice->SetFVF(D3DFVF_CUSTOMVERTEX); //set FVF m_pD3DDevice->SetTransform(D3DTS_WORLD, &worldMat); D3DXMatrixScaling(&scaleMat, 1.0f, 1.0f, 1.0f); //set scale D3DXMatrixRotationYawPitchRoll(&rotMat, timeGetTime() / 1000.0f, timeGetTime() / 1000.0f, timeGetTime() / 1000.0f); //set rotation D3DXMatrixTranslation(&transMat, 0.0f, 0.0f, 0.0f); //set translation D3DXMatrixMultiply(&scaleMat, &rotMat, &scaleMat); // Overwrite scale w/ result D3DXMatrixMultiply(&worldMat, &scaleMat, &transMat); // world = rot*scale*trans m_pD3DDevice->SetTransform(D3DTS_WORLD, &worldMat); m_pD3DDevice->SetIndices(terrain.m_pIndexBuffer); m_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, terrain.numVertsX * terrain.numVertsZ, 0, (terrain.numVertsX - 1) * (terrain.numVertsZ - 1) * 2); m_pD3DDevice->EndScene(); } // Display backbuffer m_pD3DDevice->Present(NULL, NULL, NULL, NULL); return true;}