Sign in to follow this  
chillypacman

textures stuck in the buffer

Recommended Posts

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)
{
	//
	// Load the XFile data.
	//
	hr = D3DXLoadMeshFromX(
		name.c_str(),
		D3DXMESH_MANAGED,
		dev,
		&adjBuffer,
		&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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
Quote:

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.


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 this post


Link to post
Share on other sites
Quote:
Original post by DesignerX
Quote:

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.


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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
Quote:
Original post by DesignerX
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;


alrighty then, will do :-)

Share this post


Link to post
Share on other sites

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