Texturing Simple Heightmap

Started by
14 comments, last by Roadkill247 16 years, 1 month ago
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]
Advertisement
I'd say the problem lies with your vertex format as D3DFVF_TEX0 means 0 texture coordinates.

Regards,
ViLiO
Richard 'ViLiO' Thomasv.net | Twitter | YouTube
Hey,

Thanks for the input Vilio, tried all the combinations from Tex0 - Tex8 but no success with any of them.

Any other ideas ?
Still no success with anything I have tried, I must be doing something wrong else where...
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
Richard 'ViLiO' Thomasv.net | Twitter | YouTube
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
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 ?
Try enabling the Debug Runtime and see if you get any warnings.

EDIT: Fixed the link.

[Edited by - Gage64 on February 20, 2008 12:38:18 PM]
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 ?
What do you mean by "doesn't load like it should"? Also, what's the grass texture's size?

This topic is closed to new replies.

Advertisement