# textures stuck in the buffer

## Recommended Posts

chillypacman    100
Basically the texture of the first .x is loaded for all the .x textures, I have one soccer ball texture and one basketball texture, whichever I load first stays and renders both with the texture. This is how i'm loading .x files right now:
bool mesh::loadMesh(string name)
{
//
//
name.c_str(),
D3DXMESH_MANAGED,
dev,
&mtrlBuffer,
0,
&numMtrls,
&model);

if(FAILED(hr))
{
::MessageBox(0, "D3DXLoadMeshFromX() - FAILED", 0, 0);
return false;
}

if( mtrlBuffer != 0 && numMtrls != 0 )
{
D3DXMATERIAL* mtrls=(D3DXMATERIAL*)mtrlBuffer->GetBufferPointer();

for(int i = 0; i < numMtrls; i++)
{
mtrls[i].MatD3D.Ambient = mtrls[i].MatD3D.Diffuse;
Mtrls.push_back( mtrls[i].MatD3D );
if( mtrls[i].pTextureFilename != 0 )
{
IDirect3DTexture9* tex = 0;

D3DXCreateTextureFromFile(
dev,
mtrls[i].pTextureFilename,
&tex);

Textures.push_back( tex );
}
else
{
Textures.push_back( 0 );
}
}
}
d3d::Release<ID3DXBuffer*>(mtrlBuffer); // done w/ buffer

return true;
}


Should I be sticking a d3d::Release<IDirect3DTexture9*>(tex); (it's in the d3d class and it basically destroys the buffer in question) in there somewhere? I think thats what I should do but kind of hard to find where... [Edited by - chillypacman on May 6, 2007 7:13:14 AM]

##### Share on other sites
Sc4Freak    643
I'd say its got more to do with how you're setting your textures. When rendering, how are you setting which texture to render?

##### Share on other sites
chillypacman    100
Heres the function I'm using to render the .x mesh(it's all in the same class):

void mesh::renderMesh(float x, float y, float z){	//Set translation for matrix	xa = x;	ya = y;	za = z;	D3DXMatrixTranslation(&pos, xa, ya, za);	for(int i = 0; i < Mtrls.size(); i++)	{		dev->SetMaterial( &Mtrls[i] );		dev->SetTexture(0, Textures[i]);		dev->SetTransform(D3DTS_WORLD, &pos);		model->DrawSubset(i);	}}

oh and the xa = x and whatnot might be extra, this is one of the first functions I put in the code, I think I had use for the first few assigments at first but probably not anymore.

Could the problem be that I'm declaring the following globally in mesh.cpp?

std::vector<D3DMATERIAL9>Mtrls(0);
std::vector<IDirect3DTexture9*>Textures(0);

I'm not sure if that could be the cause of the problem.

I tried releasing the textures after rendering them with the following function:

void mesh::endTexture(){	for(int i = 1; i < Mtrls.size(); i++)	{		d3d::Release<IDirect3DTexture9*>( Textures[i] );	}}

But it produced an access violation, Release is as follows:

template<class T> void Release(T t)	{		if( t )		{			t->Release();			t = 0;		}	}

##### Share on other sites
DesignerX    174
Say, do you have 2 .x files, one for the soccer and one for the baseball or you load only one .x file and try changing the textures ?

##### Share on other sites
chillypacman    100
I have 2 .x files and 2 sets of textures, they're also in different directories.

##### Share on other sites
DesignerX    174
Ok, so how do you configure the texture stages ? (i.e. pD3D->SetTextureStageState(...))

##### Share on other sites
chillypacman    100
umm, I don't, I've never actually looked up that particular function.

##### Share on other sites
DesignerX    174
Quote:
 Could the problem be that I'm declaring the following globally in mesh.cpp?std::vectorMtrls(0);std::vectorTextures(0);I'm not sure if that could be the cause of the problem.

Man , how could I miss it. Your problem is because you declared Textures and Mtrls globally. You load the first mesh and everything is fine. "Textures[0] = tex1". But when you load your second mesh after the line :

Texture.push_back(tex) you will now have :

Textures[0] = tex1 (baseball)
Textures[1] = tex2 (soccer)

instead of : Textures[0] = tex2 (soccer)

And thats why tex1 one will always be loaded for the two meshes.

For each mesh instance you must have different materials and textures arrays/vectors

##### Share on other sites
chillypacman    100
Quote:
Original post by DesignerX
Quote:
 Could the problem be that I'm declaring the following globally in mesh.cpp?std::vectorMtrls(0);std::vectorTextures(0);I'm not sure if that could be the cause of the problem.

Man , how could I miss it. Your problem is because you declared Textures and Mtrls globally. You load the first mesh and everything is fine. "Textures[0] = tex1". But when you load your second mesh after the line :

Texture[i].push_back(tex) you will now have :

Textures[0] = tex1 (baseball)
Textures[1] = tex2 (soccer)

instead of : Textures[0] = tex2 (soccer)

And thats why tex1 one will always be loaded for the two meshes.

For each mesh instance you must have different materials and textures arrays/vectors

I actually tried putting those in the constructor but I got an error about 'constant', how can I actually create a vector as follows: std::vector<IDirect3DTexture9*>Textures(0); and assign it via a default contructor?

##### Share on other sites
DesignerX    174
Why passing it trhough a constructor ?

Declare the vector as a class member and use it inside the creation function when needed.

Declare the vector as follows :

vector<IDirect3DTexture9 *> m_textures;

##### Share on other sites
chillypacman    100
Quote:
 Original post by DesignerXWhy passing it trhough a constructor ?Declare the vector as a class member and use it inside the creation function when needed.Declare the vector as follows :vector m_textures;

alrighty then, will do :-)