Sign in to follow this  
Hassanbasil

how to texture d3dx meshes

Recommended Posts

Hello, everyone! i'm working on a little project, and im having some troubles texturing an object created with D3DXCreate*, at the moment, i have: first, object structure:
struct hOBJECT
{
	unsigned int ID;
	ID3DXMesh *baseMesh;
	D3DXMATRIX WorldMatrix;
	D3DMATERIAL9 mtrl;
	IDirect3DTexture9 *Tex;
	bool Textured;
	int texStage;
	float xPos, yPos, zPos;
	float xAng, yAng, zAng;
};
//.. //...
hOBJECT hCORE::MakeObjectSphere ( int ID )
{
	for ( unsigned int i = 0; i < ObjectV.size(); i++ )
		if ( ObjectV[i].ID == ID )
		{
			ObjectV[i].baseMesh->Release ( );
			ObjectV.erase ( ObjectV.begin ( ) + i );
			break;
		}
	hOBJECT outObj;
	outObj.ID = ID;
	outObj.xPos = outObj.yPos = outObj.zPos = outObj.xAng = outObj.yAng = outObj.zAng = 0.0f;

	D3DXCreateSphere ( device, 2, 12, 12, &outObj.baseMesh, 0 );

	D3DXCOLOR RandColor ( float(float((rand()%5))/10)+0.5, float(float((rand()%5))/10)+0.5, float(float((rand()%5))/10)+0.5, 1 );

	outObj.mtrl.Ambient =	RandColor;
	outObj.mtrl.Diffuse =	RandColor;
	outObj.mtrl.Specular =	RandColor;
	outObj.mtrl.Emissive =	D3DXCOLOR ( 0, 0, 0, 1 );
	outObj.mtrl.Power = 5.0f;
	
	outObj.Textured = false;
	outObj.texStage = 0;

	ObjectV.push_back ( outObj );
	return outObj;
}
this creates a sphere, works perfectly, after that, i texture it using
void hCORE::TextureObject ( int ID, LPSTR Filename, int texStage )
{
	for ( int i = 0; i < ObjectV.size(); i++ )
	{
		if ( ObjectV[i].ID == ID )
		{
			if ( FAILED ( D3DXCreateTextureFromFileA ( device, Filename, &ObjectV[i].Tex ) ) )
			{
				hERROR error;
				error.active = true;
				error.fatal = false;
				error.Err = "TextureObject : unknown object ID";
				ErrorLog.push_back ( error );
				return;
			}
			ObjectV[i].texStage = texStage;
			ObjectV[i].Textured = true;
			return;
		}
	}
	hERROR error;
	error.active = true;
	error.fatal = false;
	error.Err = "TextureObject : unknown object ID";
	ErrorLog.push_back ( error );
}
loading the texture works fine, i get no errors but then, in the render, i just get a black object, why is that? this is the render:
device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, RGB ( 200, 20, 60 ), 1.0f, 0);
	device->BeginScene();

	for ( unsigned int i = 0; i < ObjectV.size(); i++ )
	{
		device->SetMaterial ( &ObjectV[i].mtrl );
		if ( ObjectV[i].Textured )
			device->SetTexture ( ObjectV[i].texStage, ObjectV[i].Tex );
		else
			device->SetTexture ( 0, 0 );

		D3DXMATRIX mpos, mx, my, mz;

		D3DXMatrixTranslation ( &mpos,  ObjectV[i].xPos, ObjectV[i].yPos,  ObjectV[i].zPos );
		D3DXMatrixRotationX ( &mx, ObjectV[i].xAng );
		D3DXMatrixRotationY ( &my, ObjectV[i].yAng );
		D3DXMatrixRotationZ ( &mz, ObjectV[i].zAng );

		ObjectV[i].WorldMatrix = ( mz * mx * my ) * mpos;

		device->SetTransform(D3DTS_WORLD, &ObjectV[i].WorldMatrix);
		
		ObjectV[i].baseMesh->DrawSubset(0);
	}

	device->EndScene();
	device->Present(0, 0, 0, 0);
do i need to do some other stuff in order to get textures? or i am doing something wrong? looking forward for a reply thanks in advance -Hassan [Edited by - Hassanbasil on March 4, 2010 11:01:08 AM]

Share this post


Link to post
Share on other sites
There are no texture coordinates in the D3DX built-in shapes.

In order to allocate space for texture coordinates, you can clone the mesh and specify a vertex format that does have space for them. Then, you have to calculate the texcoords and update the vertex buffer of the mesh by locking, copying (the data in) and unlocking it.

Calculating texcoords is a complete artform by itself - for example, it isn't actually easy at all to uniformly parametrize a sphere to 2D texture space.

Share this post


Link to post
Share on other sites
Quote:
Original post by Nik02
There are no texture coordinates in the D3DX built-in shapes.

In order to allocate space for texture coordinates, you can clone the mesh and specify a vertex format that does have space for them. Then, you have to calculate the texcoords and update the vertex buffer of the mesh by locking, copying (the data in) and unlocking it.

Calculating texcoords is a complete artform by itself - for example, it isn't actually easy at all to uniformly parametrize a sphere to 2D texture space.


i see, thanks.
what about custom loaded objects? ( D3DXLoadMeshFromX ) do i also need to se custom coords?

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