how to texture d3dx meshes

Started by
3 comments, last by Hassanbasil 14 years, 1 month ago
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.ID == ID )
		{
			ObjectV.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.ID == ID )
		{
			if ( FAILED ( D3DXCreateTextureFromFileA ( device, Filename, &ObjectV.Tex ) ) )
			{
				hERROR error;
				error.active = true;
				error.fatal = false;
				error.Err = "TextureObject : unknown object ID";
				ErrorLog.push_back ( error );
				return;
			}
			ObjectV.texStage = texStage;
			ObjectV.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.mtrl );
		if ( ObjectV.Textured )
			device->SetTexture ( ObjectV.texStage, ObjectV.Tex );
		else
			device->SetTexture ( 0, 0 );

		D3DXMATRIX mpos, mx, my, mz;

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

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

		device->SetTransform(D3DTS_WORLD, &ObjectV.WorldMatrix);
		
		ObjectV.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]
Advertisement
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.

Niko Suni

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?
If the loaded .x file contains texture-coordinates, you dont have to generate them.

You can find out by checking if(mesh->GetFVF() & D3DFVF_TEX1)
Quote:Original post by scope
If the loaded .x file contains texture-coordinates, you dont have to generate them.

You can find out by checking if(mesh->GetFVF() & D3DFVF_TEX1)



great, thank you

This topic is closed to new replies.

Advertisement