The Ridiculous Valley – DX10

valley1 valley2

 valley3 valley4

Yesterday was a pretty cube. This time we have a series of mathematically and randomly generated valley. This very simple technique simply involves generating an MxN grid by procedurally generating the vertices and the indices. The indices are used to stitch all the vertices together to correctly draw each triangle of the grid.

Here’s the code i used to generate the vertices:
The code was adopted from Introduction to 3D Game Programming using DirectX 10

Vertices:

  1. // Create grid/terrain with valleys
  2. const INT m = 500;        // Row
  3. const INT n = 500;        // Column
  4. const FLOAT dx = 1.5f;    // Width;
  5. const FLOAT dz = 1.5f;    // Depth
  6.  
  7. m_numVertices = m * n;
  8. m_numFaces = (m – 1) * (n-1) * 2;
  9.  
  10. // Create the geometry then vertex buffer
  11. HRESULT hr;
  12. Vertex *pVertices = new Vertex[m_numVertices];
  13. PSX_Assert( pVertices , "Not enough memory." );
  14.  
  15. FLOAT halfWidth = (n – 1) * dx * 0.5f;
  16. FLOAT halfDepth = (m – 1) * dz * 0.5f;
  17.  
  18. FLOAT x, y, z;
  19. Vertex *pVert;
  20. for ( SIZE_T i = 0; i < m; ++i )
  21. {
  22.     z = halfDepthi * dz;
  23.  
  24.     for ( SIZE_T j = 0; j < n; ++j )
  25.     {
  26.         x = –halfWidth + j * dx;
  27.         y = 0.3f * ( z * Math::Cosf( 0.1f * x ) + x * Math::Cosf( 0.1f * z ) );
  28.  
  29.         pVert = pVertices + (n * i + j);
  30.         pVert->pos = Vector3( x, y, z );
  31.  
  32.         if ( y < -10.0f )
  33.             pVert->color = BEACH_SAND;
  34.         else if ( y < 5.0f )
  35.             pVert->color = LIGHT_YELLOW_GREEN;
  36.         else if ( y < 12.0f )
  37.             pVert->color = DARK_YELLOW_GREEN;
  38.         else if ( y < 20.0f )
  39.             pVert->color = DARK_BROWN;
  40.         else
  41.             pVert->color = WHITE;
  42.     }
  43. }

 

Indices:

  1. // Create index buffer
  2.     DWORD *pIndices = new DWORD[ m_numFaces * 3 ];
  3.  
  4.     DWORD k = 0;
  5.     for ( SIZE_T i = 0; i < m – 1; ++i )
  6.     {
  7.         for ( SIZE_T j = 0; j < n – 1; ++j )
  8.         {
  9.             pIndices[k] = i * n + j;
  10.             pIndices[k + 1] = i * n + j + 1;
  11.             pIndices[k + 2] = (i + 1) * n + j;
  12.  
  13.             pIndices[k + 3] = (i + 1) * n + j;
  14.             pIndices[k + 4] = i * n + j + 1;
  15.             pIndices[k + 5] = (i + 1) * n + j + 1;
  16.  
  17.             k += 6;
  18.         }
  19.     }
  20.  
  21.     m_numIndices = m_numFaces * 3;

 

– CodeSushi

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s