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


Terrain Texturing

Recommended Posts

Hi, Good evening everyone. Can anyone maybe help me with generating the UV mapping coordinates for my Terrain class.
    m_pTerrainVB->Lock(0, 0, (void**)&pVertexData, 0);
    DWORD pos = 0;
    for (int y = 0; y < terrainHeight; ++y) {
        pos = y * terrainWidth;
        for (int x = 0; x < terrainWidth; ++x) {
            pVertexData[pos].x = (float)x * cellScale;
            pVertexData[pos].y = getScaledHeight(aHeightMap[pos]);
            pVertexData[pos].z = (float)y * cellScale;
            pVertexData[pos].dwColor = D3DCOLOR_XRGB(0, aHeightMap[pos] + 70, 0);
this is basically how I read in my Vertices. I was browsing through some of the posts that I searched. Cannot find anything. Is there maybe an alogorithm for calculating the mapping coordinates like maybe for the square I know the coordinates are top left u = 0 v = 0 bottom left u = 0 v = height top right u = width v = 0 bottom right u = width v = height ---DirectXSA--- Lachdannan Corp.

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
I use 2 textures for my terrain: one base texture, which I stretched all along the terrain (so yes, it appears horribly pixelated). The second texture is a detail texture, which is tiled. So, the code is similar to yours, but with texture mapping calculations:
posZ = -(float)m_Height / 2.0f * m_FactorXZ * VERTEX_OFFSET;
for(countZ = 0; countZ < m_Height; countZ ++)
posX = -(float)m_Width / 2.0f * m_FactorXZ * VERTEX_OFFSET;
for(countX = 0; countX < m_Width; countX ++)
vertices[countZ * m_Width + countX].m_X = posX * m_FactorXZ;
vertices[countZ * m_Width + countX].m_Y = getFactorizedHeight(countX, countZ);
vertices[countZ * m_Width + countX].m_Z = posZ * m_FactorXZ;
vertices[countZ * m_Width + countX].m_U1 = (float)((m_Width - 1) - countX) / (m_Width - 1);
vertices[countZ * m_Width + countX].m_V1 = (float)(countZ) / (m_Height - 1);
vertices[countZ * m_Width + countX].m_U2 = (float)countX / 8.0f;
vertices[countZ * m_Width + countX].m_V2 = (float)countZ / 8.0f;

posX += VERTEX_OFFSET * m_FactorXZ;
posZ += VERTEX_OFFSET * m_FactorXZ;

Hope it helps

Share this post

Link to post
Share on other sites
If you are stretching your texture over the terrain then just consider all you terrain height and width as 1.0 and you compute:
u(m) = m * 1.0/divisions_x;
v(n) = n * 1.0/divisions_y;

If n and m are the same, better yet.

Now, if you are using tiles, it depens about how your tilemaps are stores in your textures. Its up to you. Just remember a texture is considered as having a width and height of 1.0 always.


Share this post

Link to post
Share on other sites
Hey everyone, thanks for your replys.
I was thinking to be able to put lighting on the terrain what is the best approach to calculate the normals

1) Using Lightmaps
2) Convert this to a mesh and use the D3DXComputeNormals();
3) Compute the normals manualy iterating through all the vertices?

Thanks for your kind replys

Lachdannan Corp.

Share this post

Link to post
Share on other sites
I would Like to ask one more question.
Using lightmaps.. what are the texture stages need to be set?

mine is as follows

void setTextureStages(void)
Direct3d.pDevice->SetTexture(0, texture);
Direct3d.pDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
Direct3d.pDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
Direct3d.pDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
Direct3d.pDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
Direct3d.pDevice->SetTexture(1, textureNormals);
Direct3d.pDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_MODULATE );
Direct3d.pDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );

"LPDIRECT3DTEXTURE9 texture" being my Diffuse (color) texture
"LPDIRECT3DTEXTURE9 textureNormals" being my Lightmap B/W

all of them allready being loaded like

palEntry.peRed = 255;
palEntry.peGreen = 255;
palEntry.peBlue = 255;
palEntry.peFlags = 20;
D3DXCreateTextureFromFile(Direct3d.pDevice, "terrain.bmp", &bwTexture);
D3DXCreateTextureFromFile(Direct3d.pDevice, "terrain_tex.bmp", &texture);
D3DXComputeNormalMap(textureNormals, bwTexture, &palEntry, D3DX_NORMALMAP_COMPUTE_OCCLUSION, D3DX_CHANNEL_ALPHA, 10);

sorry for all the questions. I am trying ot learn direct3d and the SDK doesnt have good tutorials on lightmaps

Lachdannan Corp.

Share this post

Link to post
Share on other sites