Why does my texture not show?

Started by
3 comments, last by Xrystal 16 years, 10 months ago
I have created a piece of wireframe terrain.Now I want to put a textue on the terrain.I tried,but failed.After I ran my code,there is only wireframe,no texture.There are no errors in the output window.I think something wrong in my code,which it's hard for me to find.Could any one help me check some of my code. The following is the vetex structure:
    struct Vertex
     {
	Vertex(){}
	Vertex(float x,float y,float z,float u,float v)
	   {
		_x=x,_y=y,_z=z,_u=u,_v=v;
	   }
	float _x,_y,_z;
	float _u,_v;
	static const DWORD FVF;
     };



create vertex buffer:
bool Terrain::computeVertices()
{
	HRESULT hr = 0;
	hr = m_pDevice->CreateVertexBuffer(_numVertices*sizeof(Vertex),D3DUSAGE_WRITEONLY,Vertex::FVF,D3DPOOL_MANAGED,&m_pVB,0);
    if(FAILED(hr))
		return false;
	int startX = -_width/2;//set piont (0,0) the center of the terrain
	int startZ = _depth/2;
	int endX   = _width/2;
	int endZ   = -_depth/2;
	float uCoordIncrementSize = 1.0f/(float)_numCellsPerRow;//compute the increment size of texture coordinates 
    float vCoordIncrementSize = 1.0f/(float)_numCellsPerCol;//from one vertex to the next
	Vertex* v = 0;
    	m_pVB->Lock(0,0,(void**)&v,0);
	int i = 0;
	for(int z = startZ;z>=endZ;z-=_cellSpacing)//
	{
	 int j = 0;
	  for(int x = startX;x<=endX;x+=_cellSpacing)
	  {
	   int index = i*_numVertsPerRow+j;//give every vertex its coordinate
       v[index] = Vertex((float)x,(float)0,(float)z,(float)j*uCoordIncrementSize,(float)i*vCoordIncrementSize);
	 j++;
	  }
	 i++; 
	}
    
    m_pVB->Unlock();
    return true;
}



create index buffer:
bool Terrain::computeIndices()
{
	HRESULT hr = 1;
	hr = m_pDevice->CreateIndexBuffer(3*_numTriangles*sizeof(WORD),D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,D3DPOOL_MANAGED,&m_pIB,0);//
    if(FAILED(hr))
		return false;
   WORD* indices =0;
 m_pIB->Lock(0,0,(void**)&indices,0);
int baseIndex = 0;
for(int i = 0;i<_numCellsPerCol;i++)
 {
  for(int j = 0;j<_numCellsPerRow;j++)//assign every vertex a number,weave tiangles
  {
  indices[baseIndex] =       i*_numVertsPerRow+j;
  indices[baseIndex+1] =     i*_numVertsPerRow+j+1;
  indices[baseIndex+2] = (i+1)*_numVertsPerRow+j;
  
  indices[baseIndex+3] = (i+1)*_numVertsPerRow+j;
  indices[baseIndex+4] =     i*_numVertsPerRow+j+1;
  indices[baseIndex+5] = (i+1)*_numVertsPerRow+j+1;

  baseIndex +=6;
  }
 }
 m_pIB->Unlock();
   return true;
}



render function for terrain:
bool Terrain::Render()
{
m_pDevice->SetStreamSource( 0,m_pVB,0,sizeof(Vertex) );
m_pDevice->SetIndices( m_pIB );
m_pDevice->SetFVF(Vertex::FVF);
m_pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,_numVertices,0,_numTriangles);//
return true;
}



use the bmp file to create a texture,the following function is called by the terrain structure function:
bool Terrain::createTexture()
{
HRESULT hr = 0;
hr = D3DXCreateTextureFromFile(m_pDevice,"Grass.bmp",&pTex);
if(FAILED(hr))
return false;
m_pDevice->SetTexture(0,pTex);
m_pDevice->SetSamplerState(0,D3DSAMP_MAGFILTER,D3DTEXF_LINEAR);
m_pDevice->SetSamplerState(0,D3DSAMP_MINFILTER,D3DTEXF_LINEAR);
m_pDevice->SetSamplerState(0,D3DSAMP_MIPFILTER,D3DTEXF_POINT);
return true;
}



The following sentence is written in the file which the main function is: const DWORD Terrain::Vertex::FVF = D3DFVF_XYZ|D3DFVF_TEX1; The lighting is turned off,the zbuffer turned on, cull mode default. Thank you advanced. [Edited by - showin on June 8, 2007 9:14:40 AM]
Advertisement
Assuming the Texture Loading function is called all I can think of is that the actual loading command isn't returning a successful hr value. Might be worth checking that to be on the safe side.

Also, just in case the SetTexture command is reset to nothing it might be worth recalling it to use the texture you want just before drawing.

Other than that all I can suggest is to set the dxdebug running and see what it reports out if anything.

Good luck
Have you changed the rendermode to D3DFILLMODE.D3DFILL_SOLID = 3, Because you said the only thing you can see is the wireframe, you might see a slight texture forming on the lines, but set the fillmode to solid to see if the texture is displaying.

Failing that, check your texture coordinates and your texture loading is correct.

I hope this helps, take care.
Thank Armadon.
Yes,after I change the D3DRS_FILLMODE D3DFILL_WIREFRAME to D3DFILL_SOLID,I see my terrain with the right texture.
Thank you very much!And thank Xrystal too.
hehe, and there I was gonna suggest what Peter said too :)) But glad it sorted the problem out for you :D I usually have the wireframe/solid ability as a keypress test while running for that precise reason myself and to confirm that an object has drawn and just not being displayed where I am expecting it to be.

This topic is closed to new replies.

Advertisement