Beispiel #1
0
Triangle::Triangle( const Vector3& v1, const Vector3& v2, const Vector3& v3){
	_v1=v1; _v2=v2; _v3=v3;
	_normal = (_v2-_v1).cross(_v3-_v1);
	_normal.normalize();

	//set normals for points to 0
	_normals[0] = _normals[1] = _normals[2] = Vector3(0,0,0);
	_normalsSet = false;

	_dominant = getDominantAxis();

	real _b[2],_c[2];
	_b[0]= _dominant==0?(_v2[1]-_v1[1]):(_v2[0]-_v1[0]);
	_b[1]= _dominant==2?(_v2[1]-_v1[1]):(_v2[2]-_v1[2]);
	_c[0]= _dominant==0?(_v3[1]-_v1[1]):(_v3[0]-_v1[0]);
	_c[1]= _dominant==2?(_v3[1]-_v1[1]):(_v3[2]-_v1[2]);

	real _div1 = (_b[1]*_c[0]-_b[0]*_c[1]);
	real _div2 = (_c[1]*_b[0]-_c[0]*_b[1]);

	_ud1 =   _c[0]/_div1;
	_ud2 = - _c[1]/_div1;
	_vd1 =   _b[0]/_div2;
	_vd2 = - _b[1]/_div2;
	_px[0] = _px[1] = _px[2] = _py[0] = _py[1] = _py[2] = -1;
}
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);
}