• Advertisement

Archived

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

Building a terrain

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

How hard would it be to create a terrain from a 256 X 256 greyscale image in DirectX 8 with a texture from another image mapped on top of it? I just got into DirectX a couple weeks ago and I want to make a terrain engine. Are there any tutorials on how to do this? Any help will be appreciated. Krusty95

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
theirs a few tutorials that can do that. look up google.com and search for terrain tutorials and do your own dirty work

Share this post


Link to post
Share on other sites
If you''ve only just got into DirectX, I would suggest something simpler than a terrain engine as your first project.

Share this post


Link to post
Share on other sites
What else should I do? And I didn''t say this was my first project, it''s actually my third project (my first two were texture mapping polygons and loading a X file of a truck and driving it with the keyboard). I know I can learn how to make a terrain. I just need somewhere to start.

Share this post


Link to post
Share on other sites
Ok, that wasn't very helpful of me

Well, one way is to extract the height values from your 256*256 texture and store them as height nodes (ask if you want to know how to do this). Then do something like this-

    
UINT count = 0;
FLOAT size = 50.0f;

for ( UINT x = 0; x < 256; x++ )
{
for ( UINT y = 0; y < 256; y++ )
{

// Triangle #1

vert[x][y][count].Position = D3DXVECTOR3 ( (x * size), nodes [x][y], (y * size) );
vert[x][y][count].Normal = D3DXVECTOR3 ( 0.0f, 1.0f, 0.0f );
vert[x][y][count].tu = 0.0f;
vert[x][y][count].tv = 0.0f;
count++;

vert[x][y][count].Position = D3DXVECTOR3 ( (x * size) + size, nodes [x + 1][y], (y * size) );
vert[x][y][count].Normal = D3DXVECTOR3 ( 0.0f, 1.0f, 0.0f );
vert[x][y][count].tu = 1.0f;
vert[x][y][count].tv = 0.0f;
count++;

vert[x][y][count].Position = D3DXVECTOR3 ( (x * size), nodes [x][y + 1], (y * size) + size );
vert[x][y][count].Normal = D3DXVECTOR3 ( 0.0f, 1.0f, 0.0f );
vert[x][y][count].tu = 0.0f;
vert[x][y][count].tv = 1.0f;
count++;

// Triangle #2

vert[x][y][count].Position = D3DXVECTOR3 ( (x * size), nodes [x][y + 1], (y * size) + size );
vert[x][y][count].Normal = D3DXVECTOR3 ( 0.0f, 1.0f, 0.0f );
vert[x][y][count].tu = 0.0f;
vert[x][y][count].tv = 1.0f;
count++;

vert[x][y][count].Position = D3DXVECTOR3 ( (x * size) + size, nodes [x + 1][y], (y * size) );
vert[x][y][count].Normal = D3DXVECTOR3 ( 0.0f, 1.0f, 0.0f );
vert[x][y][count].tu = 1.0f;
vert[x][y][count].tv = 0.0f;
count++;

vert[x][y][count].Position = D3DXVECTOR3 ( (x * size) + size, nodes [x + 1][y + 1], (y * size) + size );
vert[x][y][count].Normal = D3DXVECTOR3 ( 0.0f, 1.0f, 0.0f );
vert[x][y][count].tu = 1.0f;
vert[x][y][count].tv = 1.0f;
count++;

}
}


Then generate some correct normals.

Edited by - simon_brown75 on July 19, 2001 8:20:08 PM

Share this post


Link to post
Share on other sites
  
void CDPTerrain::LoadHeightMap()
{
D3DLOCKED_RECT m_Values;

m_fYValues = new float[128*128];

m_p3DDevice->CreateImageSurface(128, 128, D3DFMT_A8R8G8B8, &m_pSurface);
D3DXLoadSurfaceFromFileA(m_pSurface, NULL, NULL, "Graphics/Heightmap_2.bmp", NULL, D3DX_DEFAULT, 0, NULL);

BYTE* pValues;
BYTE tmpByte;
m_pSurface->LockRect(&m_Values, NULL, D3DLOCK_READONLY);
pValues = (BYTE*)m_Values.pBits;

for(UINT i = 0; i < (128*128); i++)
{
tmpByte = *pValues;
m_fYValues[i] = ((float)tmpByte / 255.0f) * 20.0f;
pValues = pValues + 4;
}

m_pSurface->UnlockRect();
}


As you can see some vars are outside the function but the
heightvalue part is really easy to get.

Share this post


Link to post
Share on other sites
Yep, that''s how I do it too

btw 256 * 256 * 2 triangles = 131,072 triangles. A GF2 with hardware vertex processing can just about handle that, but if you want decent frame rates on an average PC, you''ll need to think about implementing some form of level-of-detail, to use less triangles on distant terrain.

You might like to take a look at the ROAM algorithm, described here - http://www.gvu.gatech.edu/people/peter.lindstrom/papers/siggraph96/siggraph96.pdf (need acrobat .pdf reader)

I''m currently struggling through this myself.

Share this post


Link to post
Share on other sites
Thanks everyone for all the info. I would be happy to look at anything else someone has about making a terrain engine.

Krusty95

Share this post


Link to post
Share on other sites
Have a read of these articles. I found them very helpful.

http://www.gamasutra.com/features/20000403/turner_01.htm

http://www.gamasutra.com/features/20000228/ulrich_01.htm

Share this post


Link to post
Share on other sites

  • Advertisement