ROAM with DX9

Started by
6 comments, last by Pothead 20 years, 7 months ago
I''m trying to implement a ROAMing-terrain for DX9. I can code the basics like bintrees etc. even in my dreams but how the fuck am I supposed to create an indexbuffer for my tree? I couldn''t even get bruteforce-terrain indexbuffer to work when I tried it.. best result I got was some flashing triangles on the edge of the screen..
--------------------------------------------------------I'm not crazy, It's the TV that's crazy. Aren't you, TV?--------------------------------------------------------
Advertisement
Well, I suggest you figure out how to use index buffers for the simple case before you try using them on the complicated cases.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
I thought I finally understood it.. but then not.
I'm using triangle-lists instead of strips because I really want to implement a roam terrain.. I dont think one could use strips with triangle-bintrees without alot of effort..

help me out with this : (mostly modified from some of tutorials online)

Create terrain:
HRESULT CTerrain::Create( const string &filename, LPDIRECT3DDEVICE9 ToRenderer ){  // template result for calls.  HRESULT hr;  // if no rendering device, vertex&index buffers cannot be initialized  if(!ToRenderer)    return D3DAPPERR_NODIRECT3D;  Renderer = ToRenderer;  //bitmap-object for loading and processing heightmap-data  bmpHeightMap = new Graphics::TBitmap();  bmpHeightMap->LoadFromFile( filename.c_str() );  //get bitmap width & height  MapWidth = bmpHeightMap->Width;  MapHeight = bmpHeightMap->Height;  m_iTotalNumOfVertices = MapWidth * MapHeight;  // Create vertex-buffer  hr = Renderer->CreateVertexBuffer    ( m_iTotalNumOfVertices*sizeof(TerrainVertex), 0, D3DFVF_TERRAINVERTEX, D3DPOOL_DEFAULT, &TerrainVertexBuffer, NULL );  if (FAILED(hr))    return hr;  // Next fill vertex buffer with x,y,z data from heightmap..  hr=FeedVertexBuffer();  //Create index-buffer  hr=CreateTerrainIndexBuffer();  if (FAILED(hr))    return hr;  return S_OK;}

Create vertex buffer:
   HRESULT CTerrain::FeedVertexBuffer(){  if( FAILED( TerrainVertexBuffer->Lock( 0, 0, (VOID**)&pVertices,0 ) ) )    return E_FAIL;  TerrainVertex* pVertex;  int iVertexBuffOffset = 0;  int terrScale = 4; // <- set to some parameter.. !!!!!!!!!!!!!!!!!!!!!!! TODO !!!!!!!!!!!!!!!!!!!!!!!!  for (int z = 0; z < MapHeight; z++)	//vertical rows    {      for (int x = 0; x < MapWidth; x++) //horizontal rows        {          pVertex = &( pVertices[ iVertexBuffOffset ] );          pVertex->x = (float)x * terrScale;         pVertex->z = (float)z * terrScale;          // Load height for this vertex from a pixel RGB value          pVertex->y = (float)((bmpHeightMap->Canvas->Pixels[z][x]>>16) / 2.0f)-128.0f; // TODO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! <-set to some param!!!!!!!          iVertexBuffOffset++;        }    }  TerrainVertexBuffer->Unlock();  return S_OK;}

Create index buffer:
   HRESULT CTerrain::CreateTerrainIndexBuffer(){  WORD* pIndexData;  m_iTotalNumOfPrimitives = (MapHeight-1)*(MapWidth-1)*2;  Renderer->CreateIndexBuffer(sizeof(WORD) * m_iTotalNumOfPrimitives * 3,D3DUSAGE_WRITEONLY,                              D3DFMT_INDEX16,D3DPOOL_DEFAULT,&TerrainIndexBuffer,NULL);  TerrainIndexBuffer->Lock(0,0,(void**)&pIndexData,0);  for(WORD y = 0;y < MapHeight-1;++y)   {   for(WORD x = 0;x < MapWidth-1;++x)    {    *pIndexData++ = x + y * (MapWidth);					    *pIndexData++ = x + 1 + y * (MapWidth);    *pIndexData++ = x + 1 + (y + 1) * (MapWidth);    *pIndexData++ = x + y * (MapWidth);			    *pIndexData++ = x + 1 + (y + 1) * (MapWidth);	    *pIndexData++ = x + (y + 1) * (MapWidth);		    }   }  TerrainIndexBuffer->Unlock();  return S_OK;}

Render:
   HRESULT CTerrain::Render(){   if(!Renderer)    return D3DAPPERR_NODIRECT3D;   Renderer->SetIndices( TerrainIndexBuffer );   Renderer->SetStreamSource( 0, TerrainVertexBuffer, 0, sizeof(TerrainVertex) );   Renderer->SetFVF( D3DFVF_TERRAINVERTEX );   Renderer->DrawIndexedPrimitive( D3DPT_TRIANGLELIST , 0,0, m_iTotalNumOfVertices, 0, m_iTotalNumOfPrimitives);   return S_OK;}


.. this only produces some flashing triangles on the edges of the screen.. I also checked return values from render. all ok.

[edited by - Pothead on September 1, 2003 6:05:35 AM]
--------------------------------------------------------I'm not crazy, It's the TV that's crazy. Aren't you, TV?--------------------------------------------------------
Did you set the world and view and projection matrices?
This is most probable because flashing triangles mostly appears if not valid matrices set

Also did you set a material?



be yourself.
all matrices are set properly and everything else renders fine. I also tried using a material for mi landscape. no luck.

I''ve been reading those vertex&index buffer creations over&over again and I''ve gone completely blind of my own code. there must be something trivial I''m missing every time. please help.
--------------------------------------------------------I'm not crazy, It's the TV that's crazy. Aren't you, TV?--------------------------------------------------------
The index buffer filling is true.

You use 16bit index buffer which can only handle 65536 vertices.
Your terrain size cannot be more than 255*255 if square.

be yourself.
yes, I know the 16bit limit and I''m testing it with 64x64 map..
what the hell could be the problem?
--------------------------------------------------------I'm not crazy, It's the TV that's crazy. Aren't you, TV?--------------------------------------------------------
I''ve been debugging my terrain.cpp for 2days and the actual bug was in skydome.cpp (not restoring all renderstates the way they should have been..)
--------------------------------------------------------I'm not crazy, It's the TV that's crazy. Aren't you, TV?--------------------------------------------------------

This topic is closed to new replies.

Advertisement