Textures displaying incorrectly?

Started by
3 comments, last by MCafe 17 years, 11 months ago
Hi guys, i have been working on a custom 3d model file format recently, however when i render the models with directx using ID3DXMesh, the textures appears to be in the wrong place as show below. DirectX i am pretty sure that i have exported the correct vertex information becos when i tried rendering the models with my previous opengl based engine, everything seems fine. Opengl the below code shows how i load the vertex information to ID3DXMesh


void CMGS::ConvertToD3DMesh()
{
	int i,j;

	D3DVERTEXELEMENT9 decl[] = 
	{
		{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
		{0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},
		{0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
		{0, 32, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TANGENT, 2},
		{0, 44, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BINORMAL, 3},
		D3DDECL_END()
	};

	//create the vertex declaration
	g_d3dDevice->CreateVertexDeclaration(decl, &m_decl);

	if(FAILED(D3DXCreateMesh(m_totalFaceCount,m_totalVertexCount,D3DXMESH_MANAGED | D3DXMESH_32BIT,decl,g_d3dDevice,&m_pMesh)))
		ErrorMsg("Failed to ceate d3d mesh");

	XYZ_ST_TBN_VERTEX *tempVertices = NULL;	
	m_pMesh->LockVertexBuffer(0, (void**)&tempVertices);

	for(i = 0; i < pMeshes.size(); ++i)
	{
		for(j = 0; j < pMeshes.numVertices; ++j)
		{
			tempVertices->vertex = pMeshes.pVertices[j].vertex; 
			tempVertices->normal = pMeshes.pVertices[j].normal; 
			tempVertices->texcoord = pMeshes.pVertices[j].texCoord;
			tempVertices++;
		}
	}

	m_pMesh->UnlockVertexBuffer();

	DWORD *tempIndices = NULL;
	m_pMesh->LockIndexBuffer(0, (void**)&tempIndices);

	for(i = 0; i < m_totalFaceCount*3; ++i)
	{
		*tempIndices = i;
		tempIndices++;
	}

	m_pMesh->UnlockIndexBuffer();

	DWORD *tempAttrib = NULL;
	m_pMesh->LockAttributeBuffer(0, &tempAttrib);

	//sort mesh by material
	for(i = 0; i < pMeshes.size(); ++i)
	{
		for(j = 0; j < pMeshes.numFaces; ++j)
		{
			*tempAttrib = pMeshes.matID;
			tempAttrib++;
		}
	}

	m_pMesh->UnlockAttributeBuffer();

	DWORD *pAdjacency = new DWORD[m_pMesh->GetNumFaces()*3];
	m_pMesh->GenerateAdjacency(1e-6f, pAdjacency);

	if(FAILED(D3DXComputeNormals(m_pMesh,pAdjacency)))
		ErrorMsg("Compute normals for MGS failed");
		
	if(FAILED(D3DXComputeTangent(m_pMesh,0,2,3,1,pAdjacency)))
		ErrorMsg("Compute tangents for MGS failed");

	//D3DXMESHOPT_ATTRSORT
	if(FAILED(m_pMesh->OptimizeInplace(D3DXMESHOPT_VERTEXCACHE, pAdjacency, NULL, NULL, NULL)))
		ErrorMsg("OptimizeInplace for MGS failed");

	SAFE_DELETE(pAdjacency);
}


I have been stuck with this problem for a week now....[help]any help would be deeply appreciated.
Advertisement
Hi , what is your 3d-model file's format???? .X????
If so , which tool you use to make model??????
By the way , are you a fun of MGS(metal gear solid)?????????
Have you debugged/outputted which texture is getting associated with which subset?

Strikes me that the obvious answer is that they're getting muddled up - either by a mistake on your part (not looking up/using the right texture) or due to some difference in the way that the data is being stored.

With a bit of debug output you should be able to see if theres a pattern to where the data is getting misinterpretted. This'll be easier if you have some reference data (e.g. the ordering/pairing for your OpenGL renderer) to compare against.

I can't think of any reason why D3D would intentionally swap the textures around [smile]

Cheers,
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

If the model in the screenshot has been unwrapped to create a uvmap (one mesh with one texture mapped over it properly [wink]) ..then it might be a case of the uv-coords needing inverted/flipped. You could try flipping the u-coords and/or the v-coords and seeing if that makes a difference.

If it is a tiled texture over different mesh subsets then it is mostly likely a case of things getting muddled up as Jack said [smile]

Regards,
ViLiO
Richard 'ViLiO' Thomasv.net | Twitter | YouTube
thx for the sugguestions, i managed to solved the problem by simply inverting the v-coordinates juz as ViLiO mentioned.

Thx again for the help guys[smile]

Regards,
MCafe

This topic is closed to new replies.

Advertisement