Advertisement

Texture Problem???

Started by January 24, 2020 04:47 AM
1 comment, last by isu diss 4 years, 9 months ago

Hi, I successfully loaded data from fbx files. There's a problem of rendering model. Here's the code and ss. you'll understand the problem. Pls help me???


Mesh FBXCLoader::ProcessMesh(ID3D11Device * dev, ID3D11DeviceContext *devcon, FbxMesh * mesh)
{
	std::vector<VERTEX> meshvertices;
	ID3D11ShaderResourceView *meshtexture = nullptr;

	FbxVector4 *vertices = mesh->GetControlPoints();

	for (int j = 0; j < mesh->GetPolygonCount(); j++)
	{
		int numVertices = mesh->GetPolygonSize(j);

		FbxLayerElementArrayTemplate<FbxVector2> *uvVertices = 0;
		mesh->GetTextureUV(&amp;amp;amp;uvVertices, FbxLayerElement::eTextureDiffuse);

		for (int k = 0; k < numVertices; k++)
		{
			int controlPointIndex = mesh->GetPolygonVertex(j, k);

			CtrlPoint* currCtrlPoint = new CtrlPoint();
			XMFLOAT3 currPosition;
			currPosition.x = (float)vertices[controlPointIndex].mData[0];
			currPosition.y =(float)vertices[controlPointIndex].mData[1];
			currPosition.z = (float)vertices[controlPointIndex].mData[2];
			XMFLOAT2 currTex;
			currTex.x = (float)uvVertices->GetAt(mesh->GetTextureUVIndex(j, k)).mData[0];
			currTex.y = 1-(float)uvVertices->GetAt(mesh->GetTextureUVIndex(j, k)).mData[1];
			currCtrlPoint->mPosition = currPosition;
			currCtrlPoint->Tex = currTex;
		//	mControlPoints[controlPointIndex] = currCtrlPoint;
			VERTEX vertex;
			vertex.pos = currPosition;
			vertex.tex = currTex;
			meshvertices.push_back(vertex);
		//	delete currCtrlPoint;
		}
	}

	int materialcount = mesh->GetNode()->GetSrcObjectCount<FbxSurfaceMaterial>();

	for (int i = 0; i < materialcount; i++)
	{
		FbxSurfaceMaterial *material = (FbxSurfaceMaterial*)mesh->GetNode()->GetSrcObject<FbxSurfaceMaterial>(i);

		if (material)
		{
			FbxProperty prop = material->FindProperty(FbxSurfaceMaterial::sDiffuse);

			const FbxTexture* texture = FbxCast<FbxTexture>(prop.GetSrcObject<FbxTexture>(0));
			const FbxFileTexture* filetexture = FbxCast<FbxFileTexture>(texture);

			/*if (!meshtexture)*/{
			ID3D11ShaderResourceView *meshctexture	= LoadTexture(dev, devcon, filetexture->GetFileName());
			meshtexture = meshctexture;}
		}
	}

	const FbxVector4 lT = mesh->GetNode()->GetGeometricTranslation(FbxNode::eSourcePivot);
	const FbxVector4 lR = mesh->GetNode()->GetGeometricRotation(FbxNode::eSourcePivot);
	const FbxVector4 lS = mesh->GetNode()->GetGeometricScaling(FbxNode::eSourcePivot);

	FbxAMatrix geometryTransform = FbxAMatrix(lT, lR, lS);

	for (unsigned int deformerIndex = 0; deformerIndex < (UINT)mesh->GetDeformerCount(); ++deformerIndex)
	{
		FbxSkin* skin = reinterpret_cast<FbxSkin*>(mesh->GetDeformer(deformerIndex, FbxDeformer::eSkin));
		if (!skin)
			continue;

		for (unsigned int clusterIndex = 0; clusterIndex < (UINT)skin->GetClusterCount(); ++clusterIndex)
		{
			FbxCluster* cluster = skin->GetCluster(clusterIndex);
			std::string jointname = cluster->GetLink()->GetName();
			unsigned int jointIndex = FindJointIndex(jointname);

			FbxAMatrix transformMatrix;
			FbxAMatrix transformLinkMatrix;
			FbxAMatrix globalBindposeInverseMatrix;

			cluster->GetTransformMatrix(transformMatrix);
			cluster->GetTransformLinkMatrix(transformLinkMatrix);
			globalBindposeInverseMatrix = transformLinkMatrix.Inverse() * transformMatrix * geometryTransform;

			skeleton.mJoints[jointIndex].mGlobalBindposeInverse = globalBindposeInverseMatrix;
			skeleton.mJoints[jointIndex].mNode = cluster->GetLink();

			int numOfIndices = cluster->GetControlPointIndicesCount();
			for (int i = 0; i < numOfIndices; ++i)
			{
				BlendingIndexWeightPair currBlendingIndexWeightPair;
				currBlendingIndexWeightPair.mBlendingIndex = jointIndex;
				currBlendingIndexWeightPair.mBlendingWeight = cluster->GetControlPointWeights()[i];
				mControlPoints[cluster->GetControlPointIndices()[i]]->mBlendingInfo.push_back(currBlendingIndexWeightPair);
			}

			FbxAnimStack* animstack = fbxScene->GetSrcObject<FbxAnimStack>(0);
			FbxString animstackname = animstack->GetName();
			FbxTakeInfo* takeinfo = fbxScene->GetTakeInfo(animstackname);
			FbxTime start = takeinfo->mLocalTimeSpan.GetStart();
			FbxTime end = takeinfo->mLocalTimeSpan.GetStop();
			FbxLongLong animationlength = end.GetFrameCount(FbxTime::eFrames30) - start.GetFrameCount(FbxTime::eFrames30) + 1;
			Keyframe** anim = &amp;amp;amp;skeleton.mJoints[jointIndex].mAnimation;

			for (FbxLongLong i = start.GetFrameCount(FbxTime::eFrames30); i <= end.GetFrameCount(FbxTime::eFrames30); ++i)
			{
				FbxTime time;
				time.SetFrame(i, FbxTime::eFrames30);
				*anim = new Keyframe();
				(*anim)->mFrameNum = i;
				FbxAMatrix transformoffset = mesh->GetNode()->EvaluateGlobalTransform(1.0) * geometryTransform;
				(*anim)->mGlobalTransform = transformoffset.Inverse() * cluster->GetLink()->EvaluateGlobalTransform(time);
				anim = &amp;amp;amp;((*anim)->mNext);
			}
		}
	}

	BlendingIndexWeightPair currBlendingIndexWeightPair;
	currBlendingIndexWeightPair.mBlendingIndex = 0;
	currBlendingIndexWeightPair.mBlendingWeight = 0;
	for(auto itr = mControlPoints.begin(); itr != mControlPoints.end(); ++itr)
	{
		for(unsigned int i = (unsigned int)itr->second->mBlendingInfo.size(); i <= 4; ++i)
		{
			itr->second->mBlendingInfo.push_back(currBlendingIndexWeightPair);
		}
	}
	
	/* for (int i = 0; i < mesh->GetPolygonCount(); ++i)
	{
		for (int j = 0; j < mesh->GetPolygonSize(i); ++j)
		{
			int ctrlPointIndex = mesh->GetPolygonVertex(i, j);
			CtrlPoint* currCtrlPoint = mControlPoints[ctrlPointIndex];

			VERTEX vertex;

			vertex.pos.x = (float)currCtrlPoint->mPosition.x;
			vertex.pos.y = (float)currCtrlPoint->mPosition.y;
			vertex.pos.z = (float)currCtrlPoint->mPosition.z;

			vertex.tex.x = (float)currCtrlPoint->Tex.x;
			vertex.tex.y = (float)currCtrlPoint->Tex.y;

			vertex.boneids.x = currCtrlPoint->mBlendingInfo[0].mBlendingIndex;
			vertex.boneids.y = currCtrlPoint->mBlendingInfo[1].mBlendingIndex;
            vertex.boneids.z = currCtrlPoint->mBlendingInfo[2].mBlendingIndex;
            vertex.boneids.w = currCtrlPoint->mBlendingInfo[3].mBlendingIndex;

			vertex.weights.x =  (float) currCtrlPoint->mBlendingInfo[0].mBlendingWeight;
			vertex.weights.y = (float) currCtrlPoint->mBlendingInfo[1].mBlendingWeight;
			vertex.weights.z =  (float) currCtrlPoint->mBlendingInfo[2].mBlendingWeight;
			vertex.weights.w =  (float) currCtrlPoint->mBlendingInfo[3].mBlendingWeight;

			meshvertices.push_back(vertex);
		}
	}

	for(auto itr = mControlPoints.begin(); itr != mControlPoints.end(); ++itr)
	{
		delete itr->second;
	}
	mControlPoints.clear();
*/

	return Mesh(dev, meshvertices, meshtexture);
}

here's the screenshot

Mesh FBXCLoader::ProcessMesh(ID3D11Device * dev, ID3D11DeviceContext *devcon, FbxMesh * mesh)
{
	std::vector<VERTEX> meshvertices;
	ID3D11ShaderResourceView *meshtexture = nullptr;

	FbxVector4 *vertices = mesh->GetControlPoints();

	for (int j = 0; j < mesh->GetPolygonCount(); j++)
	{
		int numVertices = mesh->GetPolygonSize(j);

		FbxLayerElementArrayTemplate<FbxVector2> *uvVertices = 0;
		mesh->GetTextureUV(&amp;amp;uvVertices, FbxLayerElement::eTextureDiffuse);

		for (int k = 0; k < numVertices; k++)
		{
			int controlPointIndex = mesh->GetPolygonVertex(j, k);

			CtrlPoint* currCtrlPoint = new CtrlPoint();
			XMFLOAT3 currPosition;
			currPosition.x = (float)vertices[controlPointIndex].mData[0];
			currPosition.y =(float)vertices[controlPointIndex].mData[1];
			currPosition.z = (float)vertices[controlPointIndex].mData[2];
			XMFLOAT2 currTex;
			currTex.x = (float)uvVertices->GetAt(mesh->GetTextureUVIndex(j, k)).mData[0];
			currTex.y = 1-(float)uvVertices->GetAt(mesh->GetTextureUVIndex(j, k)).mData[1];
			currCtrlPoint->mPosition = currPosition;
			currCtrlPoint->Tex = currTex;
			mControlPoints[controlPointIndex] = currCtrlPoint;
		/*	VERTEX vertex;
			vertex.pos = currPosition;
			vertex.tex = currTex;
			meshvertices.push_back(vertex);*/
		//	delete currCtrlPoint;
		}
	}

	int materialcount = mesh->GetNode()->GetSrcObjectCount<FbxSurfaceMaterial>();

	for (int i = 0; i < materialcount; i++)
	{
		FbxSurfaceMaterial *material = (FbxSurfaceMaterial*)mesh->GetNode()->GetSrcObject<FbxSurfaceMaterial>(i);

		if (material)
		{
			FbxProperty prop = material->FindProperty(FbxSurfaceMaterial::sDiffuse);

			const FbxTexture* texture = FbxCast<FbxTexture>(prop.GetSrcObject<FbxTexture>(0));
			const FbxFileTexture* filetexture = FbxCast<FbxFileTexture>(texture);

			/*if (!meshtexture)*/{
			ID3D11ShaderResourceView *meshctexture	= LoadTexture(dev, devcon, filetexture->GetFileName());
			meshtexture = meshctexture;}
		}
	}

	const FbxVector4 lT = mesh->GetNode()->GetGeometricTranslation(FbxNode::eSourcePivot);
	const FbxVector4 lR = mesh->GetNode()->GetGeometricRotation(FbxNode::eSourcePivot);
	const FbxVector4 lS = mesh->GetNode()->GetGeometricScaling(FbxNode::eSourcePivot);

	FbxAMatrix geometryTransform = FbxAMatrix(lT, lR, lS);

	for (unsigned int deformerIndex = 0; deformerIndex < (UINT)mesh->GetDeformerCount(); ++deformerIndex)
	{
		FbxSkin* skin = reinterpret_cast<FbxSkin*>(mesh->GetDeformer(deformerIndex, FbxDeformer::eSkin));
		if (!skin)
			continue;

		for (unsigned int clusterIndex = 0; clusterIndex < (UINT)skin->GetClusterCount(); ++clusterIndex)
		{
			FbxCluster* cluster = skin->GetCluster(clusterIndex);
			std::string jointname = cluster->GetLink()->GetName();
			unsigned int jointIndex = FindJointIndex(jointname);

			FbxAMatrix transformMatrix;
			FbxAMatrix transformLinkMatrix;
			FbxAMatrix globalBindposeInverseMatrix;

			cluster->GetTransformMatrix(transformMatrix);
			cluster->GetTransformLinkMatrix(transformLinkMatrix);
			globalBindposeInverseMatrix = transformLinkMatrix.Inverse() * transformMatrix * geometryTransform;

			skeleton.mJoints[jointIndex].mGlobalBindposeInverse = globalBindposeInverseMatrix;
			skeleton.mJoints[jointIndex].mNode = cluster->GetLink();

			int numOfIndices = cluster->GetControlPointIndicesCount();
			for (int i = 0; i < numOfIndices; ++i)
			{
				BlendingIndexWeightPair currBlendingIndexWeightPair;
				currBlendingIndexWeightPair.mBlendingIndex = jointIndex;
				currBlendingIndexWeightPair.mBlendingWeight = cluster->GetControlPointWeights()[i];
				mControlPoints[cluster->GetControlPointIndices()[i]]->mBlendingInfo.push_back(currBlendingIndexWeightPair);
			}

			FbxAnimStack* animstack = fbxScene->GetSrcObject<FbxAnimStack>(0);
			FbxString animstackname = animstack->GetName();
			FbxTakeInfo* takeinfo = fbxScene->GetTakeInfo(animstackname);
			FbxTime start = takeinfo->mLocalTimeSpan.GetStart();
			FbxTime end = takeinfo->mLocalTimeSpan.GetStop();
			FbxLongLong animationlength = end.GetFrameCount(FbxTime::eFrames30) - start.GetFrameCount(FbxTime::eFrames30) + 1;
			Keyframe** anim = &amp;amp;skeleton.mJoints[jointIndex].mAnimation;

			for (FbxLongLong i = start.GetFrameCount(FbxTime::eFrames30); i <= end.GetFrameCount(FbxTime::eFrames30); ++i)
			{
				FbxTime time;
				time.SetFrame(i, FbxTime::eFrames30);
				*anim = new Keyframe();
				(*anim)->mFrameNum = i;
				FbxAMatrix transformoffset = mesh->GetNode()->EvaluateGlobalTransform(1.0) * geometryTransform;
				(*anim)->mGlobalTransform = transformoffset.Inverse() * cluster->GetLink()->EvaluateGlobalTransform(time);
				anim = &amp;amp;((*anim)->mNext);
			}
		}
	}

	BlendingIndexWeightPair currBlendingIndexWeightPair;
	currBlendingIndexWeightPair.mBlendingIndex = 0;
	currBlendingIndexWeightPair.mBlendingWeight = 0;
	for(auto itr = mControlPoints.begin(); itr != mControlPoints.end(); ++itr)
	{
		for(unsigned int i = (unsigned int)itr->second->mBlendingInfo.size(); i <= 4; ++i)
		{
			itr->second->mBlendingInfo.push_back(currBlendingIndexWeightPair);
		}
	}
	
	for (int i = 0; i < mesh->GetPolygonCount(); ++i)
	{
		for (int j = 0; j < mesh->GetPolygonSize(i); ++j)
		{
			int ctrlPointIndex = mesh->GetPolygonVertex(i, j);
			CtrlPoint* currCtrlPoint = mControlPoints[ctrlPointIndex];

			VERTEX vertex;

			vertex.pos.x = (float)currCtrlPoint->mPosition.x;
			vertex.pos.y = (float)currCtrlPoint->mPosition.y;
			vertex.pos.z = (float)currCtrlPoint->mPosition.z;

			vertex.tex.x = (float)currCtrlPoint->Tex.x;
			vertex.tex.y = (float)currCtrlPoint->Tex.y;

			vertex.boneids.x = currCtrlPoint->mBlendingInfo[0].mBlendingIndex;
			vertex.boneids.y = currCtrlPoint->mBlendingInfo[1].mBlendingIndex;
            vertex.boneids.z = currCtrlPoint->mBlendingInfo[2].mBlendingIndex;
            vertex.boneids.w = currCtrlPoint->mBlendingInfo[3].mBlendingIndex;

			vertex.weights.x =  (float) currCtrlPoint->mBlendingInfo[0].mBlendingWeight;
			vertex.weights.y = (float) currCtrlPoint->mBlendingInfo[1].mBlendingWeight;
			vertex.weights.z =  (float) currCtrlPoint->mBlendingInfo[2].mBlendingWeight;
			vertex.weights.w =  (float) currCtrlPoint->mBlendingInfo[3].mBlendingWeight;

			meshvertices.push_back(vertex);
		}
	}

	for(auto itr = mControlPoints.begin(); itr != mControlPoints.end(); ++itr)
	{
		delete itr->second;
	}
	mControlPoints.clear();


	return Mesh(dev, meshvertices, meshtexture);
}

here's the ss:

 std::map<unsigned int, CtrlPoint*> mControlPoints; 

struct VERTEX

{

   XMFLOAT3 pos;

   XMFLOAT2 tex;

   XMUINT4 boneids;

   XMFLOAT4 weights;



/*   VERTEX()

   {

       boneids = XMUINT4( 0, 0, 0, 0 );

       weights = XMFLOAT4( 0, 0, 0, 0 );

   }*/

};



struct BlendingIndexWeightPair

{

unsigned int mBlendingIndex;

double mBlendingWeight;



BlendingIndexWeightPair() :

 mBlendingIndex(0),

 mBlendingWeight(0)

{}

};



struct CtrlPoint

{

XMFLOAT3 mPosition;

XMFLOAT2 Tex;

std::vector<BlendingIndexWeightPair> mBlendingInfo;



CtrlPoint()

{

 mBlendingInfo.reserve(4);

}

};

I found the problem and fixed it. Thanks for your help.

This topic is closed to new replies.

Advertisement