Sign in to follow this  
Aqua Costa

[DirectX 10/Assimp] Textures coordinates are incorrect

Recommended Posts

I dont know why but the texture coordinates are incorrect... I dont know where the error is... Can someone please take a look?
Mesh MeshLoader::importMesh(ID3D10Device* d3dDevice, std::string &file, LPCSTR meshName, int actorType, int meshType)
{
	//initializes the mesh
	ID3DX10Mesh* dxMesh;
	Mesh mesh = Mesh();
	mesh.meshName = meshName;
	mesh.numSubsets = 0;

	char texturesLoc[20];

	//imports the mesh and creates an aiScene containing the mesh
	scene = importer.ReadFile( file, 
        aiProcess_CalcTangentSpace  |
        aiProcess_Triangulate |
        aiProcess_MakeLeftHanded |
        aiProcess_JoinIdenticalVertices |
        aiProcess_SortByPType |
        aiProcess_CalcTangentSpace |
        aiProcess_JoinIdenticalVertices |
        aiProcess_GenSmoothNormals |
        aiProcess_LimitBoneWeights |
        aiProcess_RemoveRedundantMaterials |
		aiProcess_GenUVCoords |
		aiProcess_TransformUVCoords |
		aiProcess_OptimizeMeshes);

	if(!scene)
	{	
		MessageBox(NULL, "There is a mesh missing. Please try again or reeinstall the game.", "Mesh missing", MB_OK);
		PostQuitMessage(1);
	}

	//creates the vertexDesc
	D3D10_INPUT_ELEMENT_DESC vertexDesc[] =
	{
			{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0},
			{"TANGENT",  0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0},
			{"NORMAL",   0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 24, D3D10_INPUT_PER_VERTEX_DATA, 0},
			{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT,    0, 36, D3D10_INPUT_PER_VERTEX_DATA, 0},
	};

	//retrieves the meshes from the main scene;
	aiMesh** meshes = scene->mMeshes;
	aiMaterial** materials = scene->mMaterials;

	aiNode* sceneRoot = scene->mRootNode;
	aiNode** children = sceneRoot->mChildren;
	aiNode* child;

	UINT* meshID;
	UINT materialID;

	UINT numVertices = 0;
	UINT numTriangles = 0;

	Vertex* vertices;
	DWORD* indices;
	UINT* attributeBuffer;

	UINT i = 0;
	bool rootNode = true;

	while(i < sceneRoot->mNumChildren)
	{
		if(rootNode)
		{
			child = sceneRoot;
			rootNode = false;
		} else
		{
			child = children[i];
			//child = sceneRoot;
			i++;
		}

		if(!(child->mNumMeshes > 0))
			continue;
		
		meshID = child->mMeshes;

		mesh.numSubsets += child->mNumMeshes;

		for(UINT x = 0; x < child->mNumMeshes; x++)
		{
			numVertices += meshes[meshID[x]]->mNumVertices;
			numTriangles += meshes[meshID[x]]->mNumFaces;
		}

		vertices = new Vertex[numVertices];
		indices = new DWORD[numTriangles*3];
		attributeBuffer = new UINT[numTriangles];

		UINT nextVertex = 0;
		UINT nextIndex = 0;

		for(UINT x = 0; x < child->mNumMeshes; x++)
		{
			UINT meshStartIndex = nextVertex;

			for(UINT  j = 0; j < meshes[meshID[x]]->mNumVertices; j++)
			{
				vertices[nextVertex].pos = aiVec3ToAqVector3(meshes[meshID[x]]->mVertices[j]);
				vertices[nextVertex].tangent = aiVec3ToAqVector3(meshes[meshID[x]]->mTangents[j]);
				vertices[nextVertex].normal = aiVec3ToAqVector3(meshes[meshID[x]]->mNormals[j]);
				vertices[nextVertex].texC = aiVec3ToAqVector2(meshes[meshID[x]]->mTextureCoords[0][j]);

				nextVertex++;
			}

			for(UINT j = 0; j < meshes[meshID[x]]->mNumFaces; j++)
			{
				UINT n = 0;
				if(x != 0)
				{
					n =  j + meshes[meshID[x-1]]->mNumFaces;
				} else
				{
					n = j;
				}

				indices[nextIndex++] = meshes[meshID[x]]->mFaces[j].mIndices[0] + meshStartIndex;
				indices[nextIndex++] = meshes[meshID[x]]->mFaces[j].mIndices[1] + meshStartIndex;
				indices[nextIndex++] = meshes[meshID[x]]->mFaces[j].mIndices[2] + meshStartIndex;
				attributeBuffer[n] = meshes[meshID[x]]->mMaterialIndex;
			}
		}
	}

	D3DX10CreateMesh(d3dDevice, vertexDesc, 4, vertexDesc[0].SemanticName, numVertices, numTriangles, D3DX10_MESH_32_BIT, &dxMesh);

	dxMesh->SetVertexData(0, vertices);
	dxMesh->SetIndexData(indices, numTriangles*3);
	dxMesh->SetAttributeData(attributeBuffer);

	dxMesh->GenerateAdjacencyAndPointReps(0.001f);
	dxMesh->Optimize(D3DX10_MESHOPT_ATTR_SORT|D3DX10_MESHOPT_VERTEX_CACHE,0,0);
	dxMesh->CommitToDevice();

	mesh.mesh = dxMesh;

	if(meshType == TRIANGLE_MESH)
	{
		mesh.actor = Physics::createTriangleMesh(indices, numTriangles, vertices, numVertices, actorType, meshName);
	} else if(meshType == CONVEX_MESH)
		mesh.actor = Physics::createConvexMesh(vertices, numVertices, actorType, meshName);

	delete[] vertices;
	delete[] indices;
	delete[] attributeBuffer;

	return mesh;
}

AqVector3 MeshLoader::aiVec3ToAqVector3(const aiVector3D& source)
{
	AqVector3 vector;
	vector.x = 0;
	vector.y = 0;
	vector.z = 0;

	vector.x = source.x;
	vector.y = source.y;
	vector.z = source.z;

	return vector;
}

AqVector2 MeshLoader::aiVec3ToAqVector2(const aiVector3D& source)
{
	AqVector2 vector;

	vector.x = source.x;
	vector.y = source.y;

	return vector;
}

This is how the mesh is being rendered (incorrect texture coordinates): description of your image This is how the mesh SHOULD be rendered: description of your image

Share this post


Link to post
Share on other sites
Have you tried to exclude:


aiProcess_GenUVCoords
aiProcess_TransformUVCoords



from the parameter-list to "importer.ReadFile"?

I don't know if this will make any difference but it may be worth a try.

Share this post


Link to post
Share on other sites
are you sure the texcoords are being loaded correctly? they seem to be being stored in a 3D vecotor (guess this from the conversion function "aiVec3ToAqVector2"), which could mean that something like this is happening:


Stored: x y x y x y x y x y y z
Loaded: x y z x y z x y z x y z

Share this post


Link to post
Share on other sites
Try to flip the y coordinate of the UVs (y=1.f-y). That's a typical issue with DirectX, which chooses the opposite UV convention than Assimp. Note that you can configure Assimp to do this for you early in its pipeline by specifying the aiProcess_FlipUVs flag in addition to your existing setup.

Bye, Alex

Share this post


Link to post
Share on other sites
Quote:
Original post by Porthos
Try to flip the y coordinate of the UVs (y=1.f-y). That's a typical issue with DirectX, which chooses the opposite UV convention than Assimp. Note that you can configure Assimp to do this for you early in its pipeline by specifying the aiProcess_FlipUVs flag in addition to your existing setup.

Bye, Alex


You were right I added aiProcess_FlipUVs flag and now its working properly.

Thanks for your replys

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