Sign in to follow this  

Texturing Simple Heightmap

This topic is 3569 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
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
Well the grass texture is 512x512 and i want it to be placed on a 64x64 heightmap and well it runs normally in Visual without any errors, but isnt displaying the texture on the heightmap.

But when I use PIX the dialog boxes come up saying that they were unable to create the texture from the file.

Share this post


Link to post
Share on other sites
It's possible that the debug runtime will give you more information. See my earlier reply for a link describing how to enable it.

Also, are you sure that the texture's path is correct?

Share this post


Link to post
Share on other sites
ok, tried that, no warnings then only things that I get now that I didnt' before are:

Direct3D9: (INFO) :======================= Hal SWVP device selected

Direct3D9: (INFO) :Using FF to PS converter

Direct3D9: (INFO) :Using X3D PSGP

Direct3D9: (INFO) :Using FF to VS converter in software vertex processing

Share this post


Link to post
Share on other sites
I found out what the problem was after all this time, I was using the fixed pipeline for it and thus I needed to set the material, I will move it all to use a pixel shader now in the aim to make it more flexible.

Share this post


Link to post
Share on other sites

This topic is 3569 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this