void LWObject::collectTextureCoordinates(const std::vector<LWMaterial> &materials, const FBMatrix &transform)
{
	// This part is a bit messy. 
	// Can't help it since LW's gotta do what LW's gotta do etc.
	LWTextureFuncs *textureFunctions = Manager::getSingleton()->getTextureFunctions();
	LWObjectInfo *objectInfo = Manager::getSingleton()->getObjectInfo();

	if((textureFunctions == 0) || (objectInfo == 0))
		return;

	LWMeshInfo *meshInfo = objectInfo->meshInfo(lwId, TRUE);
	if(meshInfo == 0)
		return;

	// Get them per face (looks ugly otherwise)
	for(unsigned int i = 0; i < faces.size(); ++i)
	{
		// If no material, no texture
		if(faces[i].materialId == -1)
			continue;

		// Each face have different material
		LWTextureID textureId = materials[faces[i].materialId].getTextureId();
		if(textureId == 0)
			continue;

		LWTLayerID layerId = textureFunctions->firstLayer(textureId);
		if(layerId == 0)
			continue;

		int projection = 0;
		textureFunctions->getParam(layerId, TXTAG_PROJ, &projection);

		for(int j = 0; j < 3; ++j)
		{
			if(projection == TXPRJ_UVMAP) // UV-map
			{
				void *map = NULL;
				textureFunctions->getParam(layerId, TXTAG_VMAP, &map);

				if(map == 0)
					continue;

				if(meshInfo->pntVSelect(meshInfo, map) != 2)
				{
					Manager::getSingleton()->getExporter()->printWarning(name + ": UV-map isn't 2-dimensional. Discarding.");
					continue;
				}

				float uv[2] = { 0 };
				LWPntID vertexId = vertices[faces[i].indices[j]].id;

				// Finally query texture coordinates
				if(meshInfo->pntVPGet(meshInfo, vertexId, faces[i].id, uv))
				{
					faces[i].uvs[j].x = uv[0];
					faces[i].uvs[j].y = -uv[1];
					faces[i].hasUvs[j] = true;
				}
				else if(meshInfo->pntVGet(meshInfo, vertexId, uv))
				{
					faces[i].uvs[j].x = uv[0];
					faces[i].uvs[j].y = -uv[1];
					faces[i].hasUvs[j] = true;
				}
			}
			else // Projected mapping
			{
				// Normals largest absolute value defines dominant axis (in world and object space)
				FBVector worldNormal = faces[i].normal;
				transform.RotateVector(worldNormal);

				int wAxis = getDominantAxis(worldNormal);
				//int wAxis = getDominantAxis(transform.GetTransformedVector(faces[i].normal));
				int oAxis = getDominantAxis(faces[i].normal);

				// Position in world and object space
				FBVector &vO = vertices[faces[i].indices[j]].position;
				FBVector vW = transform.GetTransformedVector(vO);

				double oPosition[3] = { vO.x, vO.y, vO.z };
				double wPosition[3] = { vW.x, vW.y, vW.z };
				double uv[2] = { 0 };

				textureFunctions->evaluateUV(layerId, wAxis, oAxis, oPosition, wPosition, uv);
				faces[i].uvs[j].x = static_cast<double> (uv[0]);
				faces[i].uvs[j].y = static_cast<double> (-uv[1]);
				faces[i].hasUvs[j] = true;
			}

			//faces[i].uvs2[j] = faces[i].uvs[j];
		}
	}

	// In the name of holy spaghetti. Should clean this up a bit
	for(unsigned int i = 0; i < faces.size(); ++i)
	{
		// If no material, no texture
		if(faces[i].materialId == -1)
			continue;

		// Each face have different material
		LWTextureID textureId = materials[faces[i].materialId].getLightmapId();
		if(textureId == 0)
			continue;
/*
int a = 0;
if(materials[faces[i].materialId].getName() == "Lattia")
	a = 1;
*/
		LWTLayerID layerId = textureFunctions->firstLayer(textureId);
		if(layerId == 0)
		{
			//materials[faces[i].materialId].removeLightmap();
			continue;
		}

		int projection = 0;
		textureFunctions->getParam(layerId, TXTAG_PROJ, &projection);

		for(int j = 0; j < 3; ++j)
		{
			if(projection == TXPRJ_UVMAP) // UV-map
			{
				void *map = 0;
				textureFunctions->getParam(layerId, TXTAG_VMAP, &map);

				if(meshInfo->pntVSelect(meshInfo, map) != 2)
				{
					Manager::getSingleton()->getExporter()->printWarning(name + ": No actual UV-map defined. Discarding.");
					//materials[faces[i].materialId].removeLightmap();
					continue;
				}

				if(map == 0)
				{
					//materials[faces[i].materialId].removeLightmap();
					continue;
				}

				if(meshInfo->pntVSelect(meshInfo, map) != 2)
				{
					Manager::getSingleton()->getExporter()->printWarning(name + ": UV-map isn't 2-dimensional. Discarding.");
					//materials[faces[i].materialId].removeLightmap();
					continue;
				}

				float uv[2] = { 0 };
				LWPntID vertexId = vertices[faces[i].indices[j]].id;

				// Finally query texture coordinates
				if(meshInfo->pntVPGet(meshInfo, vertexId, faces[i].id, uv))
				{
					faces[i].uvs2[j].x = uv[0];
					faces[i].uvs2[j].y = -uv[1];

					if(faces[i].hasUvs[j] == false)
					{
						faces[i].uvs[j].x = faces[i].uvs2[j].x;
						faces[i].uvs[j].y = faces[i].uvs2[j].y;
					}

					faces[i].hasUvs[j] = true;
				}
				else if(meshInfo->pntVGet(meshInfo, vertexId, uv))
				{
					faces[i].uvs2[j].x = uv[0];
					faces[i].uvs2[j].y = -uv[1];

					if(faces[i].hasUvs[j] == false)
					{
						faces[i].uvs[j].x = faces[i].uvs2[j].x;
						faces[i].uvs[j].y = faces[i].uvs2[j].y;
					}
					
					faces[i].hasUvs[j] = true;
				}
				//else
				//	materials[faces[i].materialId].removeLightmap();
			}
			//else
			//	materials[faces[i].materialId].removeLightmap();



/*
			else // Projected mapping
			{
				// Normals largest absolute value defines dominant axis (in world and object space)
				FBVector worldNormal = faces[i].normal;
				transform.RotateVector(worldNormal);

				int wAxis = getDominantAxis(worldNormal);
				int oAxis = getDominantAxis(faces[i].normal);

				// Position in world and object space
				Vector &vO = vertices[faces[i].indices[j]].position;
				Vector vW = transform.GetTransformedVector(vO);

				double oPosition[3] = { vO.x, vO.y, vO.z };
				double wPosition[3] = { vW.x, vW.y, vW.z };
				double uv[2] = { 0 };
				
				textureFunctions->evaluateUV(layerId, wAxis, oAxis, oPosition, wPosition, uv);
				faces[i].uvs2[j].x = static_cast<double> (uv[0]);
				faces[i].uvs2[j].y = static_cast<double> (-uv[1]);

				if(faces[i].hasUvs[j] == false)
				{
					faces[i].uvs[j].x = faces[i].uvs2[j].x;
					faces[i].uvs[j].y = faces[i].uvs2[j].y;
				}
				
				faces[i].hasUvs[j] = true;
			}
*/
		}
	}

	if(meshInfo->destroy)
		meshInfo->destroy(meshInfo);
}