Jump to content
  • Advertisement
Sign in to follow this  
Roadkill247

Texturing Simple Heightmap

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

I've recently written the code to produce a heightmap from loading in a file, but I seem to be struggling with the simple concept of texturing it. I have tried several different ways but feel I am missing some really easy. If anyone could assist me it would be a massive help. Below is the code which I have written in trying to texture the map.

void CTerrain::CreateTerrain( TCHAR* texFileName )
...
  LoadTexture( texFileName );


  // Create Vertices and set there UVs
  for(unsigned y = 0; y < TILE_VERTICES_DOWN; ++y)
  {
    for(unsigned x = 0; x < TILE_VERTICES_ACROSS; ++x)
    {
      pVertexData[x + y * (TILE_VERTICES_ACROSS)].vX = (float)x;
      pVertexData[x + y * (TILE_VERTICES_ACROSS)].vY = (float)aHeightMap[x][y] / MAX_HEIGHT;
      pVertexData[x + y * (TILE_VERTICES_ACROSS)].vZ = (float)y;

      // 64x64 Heightmap, 512x512 Texture
      pVertexData[x + y * (TILE_VERTICES_ACROSS)].u  = ((x * 8.0f ) / 512.0f);
      pVertexData[x + y * (TILE_VERTICES_ACROSS)].v  = ((y * 8.0f ) / 512.0f);
    }
  }
...
}

// Render the Terrain
void CTerrain::Draw()
{
	g_pd3dDevice->SetTexture( 0, m_tex );
	g_pd3dDevice->SetStreamSource(0,m_pTerrainVB,0,sizeof(CustomVertex));
	g_pd3dDevice->SetIndices(m_pTerrainIB);
	g_pd3dDevice->SetFVF( D3DFVF_XYZ /*| D3DFVF_NORMAL*/ | D3DFVF_TEX0 );
	g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, m_TerrainVertices, 0, m_TerrainPrimitives);	
}

// Load Texture from file
void CTerrain::LoadTexture( TCHAR* texFileName )	
{
    HRESULT hr = 0;

	hr = D3DXCreateTextureFromFile( g_pd3dDevice, texFileName, &m_tex);
	if(FAILED(hr))
	{
		::MessageBox(0, TEXT("D3DXCreateTextureFromFile() - FAILED"), TEXT("WeatherSim - Particle System"), 0);
	}

	g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
    g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
}

struct CustomVertex
{
	float vX, vY, vZ; // Vertex coordinates (model space position)
	//float nX, nY, nZ; // Vertex normal
	float u, v;       // Texture coordinates
};



[Edited by - Roadkill247 on February 9, 2008 6:42:35 AM]

Share this post


Link to post
Share on other sites
Advertisement
Assuming you have corrected your FVF so it is D3DFVF_XYZ | D3DFVF_TEX1, what actually gets rendered?

Have you disabled fixed function lighting? eg. g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);

Is your texture definitely loaded?

I'd also recommend learning how to use PIX (comes with the DXSDK), it is a rather invaluable tool [smile]

Regards,
ViLiO

Share this post


Link to post
Share on other sites
Well I set it to NULL in constructor:


CTerrain::CTerrain( IDirect3DDevice9* Device )
{
g_pd3dDevice = Device;
m_pTerrainVB = NULL;
m_pTerrainIB = NULL;
m_tex = NULL;

m_TerrainVertices = TILE_VERTICES_ACROSS * TILE_VERTICES_DOWN;
m_TerrainPrimitives = (TILES_ACROSS) * (TILES_DOWN) * 2;

}



then in the call:


void CTerrain::LoadTexture( TCHAR* texFileName )
{
HRESULT hr = 0;

hr = D3DXCreateTextureFromFile( g_pd3dDevice, texFileName, &m_tex);
if(FAILED(hr))
{
::MessageBox(0, TEXT("D3DXCreateTextureFromFile() - FAILED"), TEXT("WeatherSim - Particle System"), 0);
}
}



when I am running it though, when i do run it through step by step its value becomes:

- m_tex 0x01603d80 IDirect3DTexture9 *
- IDirect3DBaseTexture9 {...} IDirect3DBaseTexture9
- IDirect3DResource9 {...} IDirect3DResource9
- IUnknown {...} IUnknown
- __vfptr 0x73b24580 *
[0] 0x73bf73ba *
[1] 0x73b2d075 *
[2] 0x73b2471d *

don't know if that is any help

Share this post


Link to post
Share on other sites
Ok, really stuck on this, I've looked around for ideas and such and seemed to be doing everything like the tutorials and examples I've seen, but alas, there is still no texture on my terrain.

Anyone able to help at all ? Or even give a clue ?

Share this post


Link to post
Share on other sites
ok, after using PIX extensively I found where the problems are:

I create 3 textures all with:

HRESULT hr = 0;

hr = D3DXCreateTextureFromFile( g_pd3dDevice, texFileName, &m_tex);
if(FAILED(hr))
{
::MessageBox(0, TEXT("D3DXCreateTextureFromFile() - FAILED"), TEXT("WeatherSim - Particle System"), 0);
}



when i run it through visual I never see a problem with 2 of the textures, as my rain and snow is working, but the grass doesn't load like it should, any suggestions ?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!