Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.

Dave A

Terrain in DX9

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

Hi, Im havin a number of problems with my heightmap loading code in directx 9!the main problem is it draws extra faces and vertices at the far end and in other places.There are a number of other problems i am haqving as well including my texturing if anyone has any ideas please help?? bool CGfxEntityTerrain::Initialise(const char* terrainFilename) { int x=0;int z=0;int i=0; HRESULT hr=D3DXCreateTextureFromFileEx(gD3dDevice,terrainFilename,0,0,1,0,D3DFMT_A8R8G8B8, D3DPOOL_MANAGED,D3DX_FILTER_NONE,D3DX_FILTER_NONE,0,NULL,NULL,&m_texture); if (FAILED(hr)) return FALSE; D3DLOCKED_RECT locked; hr= m_texture->LockRect(0,&locked,NULL,0); if (FAILED(hr)) return FALSE; BYTE *bytePointer=(BYTE*)locked.pBits; D3DSURFACE_DESC textureDesc; m_texture->GetLevelDesc(0,&textureDesc); int height=textureDesc.Height; int width=textureDesc.Width; //Retrieve Colour values //////////////////////// for (DWORD y=0;y<=height;y++) { for (DWORD x=0;x<=width;x++) { DWORD index=(x*4+(y*(locked.Pitch))); // Blue BYTE b=bytePointer[index]; // Green BYTE g=bytePointer[index+1]; // Red BYTE r=bytePointer[index+2]; // Alpha BYTE a=bytePointer[index+3]; } } m_texture->UnlockRect(0); //Create Vertex Buffer //////////////////////////// gD3dDevice->CreateVertexBuffer( ((textureDesc.Width+1)*(textureDesc.Height+1))*sizeof(CUSTOMVERTEX), D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX, D3DPOOL_MANAGED, &m_Terrainvb, NULL ); CUSTOMVERTEX* pTerrainVertices; m_Terrainvb->Lock( 0, 0, (void**)&pTerrainVertices, 0 ); int index=0; i=0; float Mag=0; for (x=0;x<=width;x++) for(z=0;z<=height;z++) { index=(z*4+(x*(locked.Pitch)));//index incrementing way 2 fast pTerrainVertices.p.x= (float)x; pTerrainVertices[i].p.y= ((float)bytePointer[index]/255.0f)*20.0f; pTerrainVertices[i].p.z= (float)z; Mag = sqrt((double)((pTerrainVertices[i].p.x * pTerrainVertices[i].p.x) + (pTerrainVertices[i].p.y * pTerrainVertices[i].p.y) + (pTerrainVertices[i].p.z * pTerrainVertices[i].p.z))); pTerrainVertices[i].n.x= (float)pTerrainVertices[i].p.x/Mag; pTerrainVertices[i].n.y= (float)pTerrainVertices[i].p.y/Mag; pTerrainVertices[i].n.z= (float)pTerrainVertices[i].p.z/Mag; pTerrainVertices[i].u=(float)x/width; pTerrainVertices[i].v=(float)z/height; i++; } m_Terrainvb->Unlock(); //Create index Buffer ///////////////////// int numindices = ((height)*(width))*2; gD3dDevice->CreateIndexBuffer(2*numindices*3,D3DUSAGE_WRITEONLY,D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_Terrainib, NULL); WORD *indices=NULL; m_Terrainib->Lock( 0, 0, (void**)&indices, 0); i=0; int j=0; for (x=0;xUnlock(); ///////////////////////////////////////////////////////// D3DXCreateTextureFromFile(gD3dDevice,"texture1.bmp",&terrainTexture); return TRUE; }; void CGfxEntityTerrain::Render() { gD3dDevice->SetMaterial( &m_material ); gD3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); gD3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); gD3dDevice->SetTexture(0,terrainTexture); gD3dDevice->SetStreamSource( 0, m_Terrainvb,0, sizeof(CUSTOMVERTEX) );//pVertices gD3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX ); gD3dDevice->SetIndices(m_Terrainib); gD3dDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME); gD3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,0,0,16641,0,32768); }

Share this post

Link to post
Share on other sites
Don''t have too much time to go through all your code, but from what I can see I can help you with your first problem.

When you are using triangle strips, you cannot simply stop a strip and start it again in another row. To solve this problem there are 2 things you can do, one is good, one is bad.

The bad (and easy) way, is to make one strip for every row. Then simply make n DrawIndexedPrimitive calls where n is the number of rows.

The good (and harder) way is to introduce degenerate triangles.

You can read about them here:


This should tell you all you need to know.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!