Exemplo n.º 1
0
bool FArchiveXML::LoadTransformSkew(FCDObject* object, xmlNode* skewNode)
{
	FCDTSkew* tSkew = (FCDTSkew*)object;

	const char* content = FUDaeParser::ReadNodeContentDirect(skewNode);
	FloatList factors;
	factors.reserve(7);
	FUStringConversion::ToFloatList(content, factors);
	if (factors.size() != 7) return false;

	tSkew->SetAngle(factors[0]);
	tSkew->SetRotateAxis(FMVector3(factors[1], factors[2], factors[3]));
	tSkew->SetAroundAxis(FMVector3(factors[4], factors[5], factors[6]));

	// Check and pre-process the axises
	if (IsEquivalent(tSkew->GetRotateAxis(), FMVector3::Origin) || IsEquivalent(tSkew->GetAroundAxis(), FMVector3::Origin)) return false;
	tSkew->SetRotateAxis(tSkew->GetRotateAxis().Normalize());
	tSkew->SetAroundAxis(tSkew->GetAroundAxis().Normalize());

	// Register the animated values
	FArchiveXML::LoadAnimatable(&tSkew->GetSkew(), skewNode);

	tSkew->SetDirtyFlag();
	return true;
}
Exemplo n.º 2
0
FUBoundingSphere FUBoundingSphere::Transform(const FMMatrix44& transform) const
{
	if (!IsValid()) return (*this);

	FMVector3 transformedCenter = transform.TransformCoordinate(center);
	FUBoundingSphere transformedSphere(transformedCenter, 0.0f);

	// Calculate the transformed bounding sphere radius using three sample points.
	FMVector3 testPoints[3] =
	{
		FMVector3(radius, 0.0f, 0.0f),
		FMVector3(0.0f, radius, 0.0f),
		FMVector3(0.0f, 0.0f, radius)
	};

	for (size_t i = 0; i < 6; ++i)
	{
		testPoints[i] = transform.TransformVector(testPoints[i]);
		float lengthSquared = testPoints[i].LengthSquared();
		if (lengthSquared > transformedSphere.radius * transformedSphere.radius)
		{
			transformedSphere.radius = sqrtf(lengthSquared);
		}
	}

	return transformedSphere;
}
Exemplo n.º 3
0
// Based on FMVector3::Normalize, but that function uses a static member
// FMVector3::XAxis which causes irritating linker errors. Rather than trying
// to make that XAxis work in a cross-platform way, just reimplement Normalize:
static FMVector3 FMVector3_Normalize(const FMVector3& vec)
{
	float l = vec.Length();
	if (l > 0.0f)
		return FMVector3(vec.x/l, vec.y/l, vec.z/l);
	else
		return FMVector3(1.0f, 0.0f, 0.0f);
}
Exemplo n.º 4
0
float FCDPhysicsShape::CalculateVolume() const
{
	if (IsGeometryInstance())
	{
		FCDGeometry* geom = ((FCDGeometry*)geometry->GetEntity());
		if (geom->IsMesh())
		{
			FUBoundingBox boundary;
			float countingVolume = 0.0f;
			const FCDGeometryMesh* mesh = geom->GetMesh();

			if (!mesh->GetConvexHullOf().empty())
			{
				mesh = mesh->FindConvexHullOfMesh();
			}
			if (mesh == NULL) return 1.0f; // missing convex hull or of spline

			for (size_t i = 0; i < mesh->GetPolygonsCount(); i++)
			{
				const FCDGeometryPolygons* polygons = mesh->GetPolygons(i);
				const FCDGeometryPolygonsInput* positionInput = polygons->FindInput(FUDaeGeometryInput::POSITION);
				const FCDGeometrySource* positionSource = positionInput->GetSource();
				uint32 positionStride = positionSource->GetStride();
				FUAssert(positionStride == 3, continue;);
				const float* positionData = positionSource->GetData();
				size_t positionDataLength = positionSource->GetDataCount();
				for (size_t pos = 0; pos < positionDataLength;)
				{
					boundary.Include(FMVector3(positionData, (uint32)pos));
					pos += positionStride;
				}

				FMVector3 min = boundary.GetMin();
				FMVector3 max = boundary.GetMax();
				countingVolume += 
						(max.x - min.x) * (max.y - min.y) * (max.z - min.z);
				boundary.Reset();
			}
			return countingVolume;
		}
Exemplo n.º 5
0
bool FUBoundingSphere::Overlaps(const FUBoundingBox& boundingBox, FMVector3* overlapCenter) const
{
	if (radius >= 0.0f)
	{
		float rx, ry, rz;
		if (center.x > boundingBox.GetMax().x) rx = boundingBox.GetMax().x - center.x;
		else if (center.x > boundingBox.GetMin().x) rx = 0.0f;
		else rx = boundingBox.GetMin().x - center.x;
		if (center.y > boundingBox.GetMax().y) ry = boundingBox.GetMax().y - center.y;
		else if (center.y > boundingBox.GetMin().y) ry = 0.0f;
		else ry = boundingBox.GetMin().y - center.y;
		if (center.z > boundingBox.GetMax().z) rz = boundingBox.GetMax().z - center.z;
		else if (center.z > boundingBox.GetMin().z) rz = 0.0f;
		else rz = boundingBox.GetMin().z - center.z;
		bool overlaps = (rx * rx + ry * ry + rz * rz) < (radius * radius);
		if (overlaps && overlapCenter != NULL)
		{
			(*overlapCenter) = center + FMVector3(rx, ry, rz);
		}
		return overlaps;
	}
	return false;
}
Exemplo n.º 6
0
	PassIf(IsEquivalent(q2.y, 0.2f));
	PassIf(IsEquivalent(q2.z, 0.5f));
	PassIf(IsEquivalent(q2.w, 0.6f));

	// Basic arithmetic
	PassIf(IsEquivalent(q.Length(), 0.8124f));
	PassIf(IsEquivalent(q.LengthSquared(), 0.66f));
	q.NormalizeIt();
	PassIf(IsEquivalent(q.Length(), 1.0f));
	PassIf(IsEquivalent(q.LengthSquared(), 1.0f));

TESTSUITE_TEST(1, AngleAxis)
	// Angle-axis constructor.
	FMQuaternion aa(FMVector3::XAxis, 0.0f);
	PassIf(IsEquivalent(aa, FMQuaternion(0.0f, 0.0f, 0.0f, 1.0f)));
	FMQuaternion aa2(FMVector3(0.5f, -0.51f, 0.7f), FMath::DegToRad(30.0f));
	PassIf(IsEquivalent(aa2, FMQuaternion(0.1294f, -0.13199f, 0.181164f, 0.965926f)));
	FMQuaternion aa3(FMVector3(0.8f, 0.35f, 0.48734f), FMath::DegToRad(-45.0f));
	PassIf(IsEquivalent(aa3, FMQuaternion(-0.3061467f, -0.133939f, -0.1864969f, 0.9238795f)));

	// Angle-axis extraction.
	FMVector3 axis; float angle;
	aa.ToAngleAxis(axis, angle);
	PassIf(IsEquivalent(angle, 0.0f)); // Axis is irrelevant, since no rotation.
	aa2.ToAngleAxis(axis, angle);
	PassIf((IsEquivalent(axis, FMVector3(0.5f, -0.51f, 0.7f)) && IsEquivalent(angle, FMath::DegToRad(30.0f)))
		|| (IsEquivalent(axis, FMVector3(-0.5f, 0.51f, -0.7f)) && IsEquivalent(angle, FMath::DegToRad(-30.0f))));
	aa3.ToAngleAxis(axis, angle);
	PassIf((IsEquivalent(axis, FMVector3(0.8f, 0.35f, 0.48734f)) && IsEquivalent(angle, FMath::DegToRad(-45.0f)))
		|| (IsEquivalent(axis, FMVector3(-0.8f, -0.35f, -0.48734f)) && IsEquivalent(angle, FMath::DegToRad(45.0f))));
Exemplo n.º 7
0
FMMatrix44 ColladaSceneNode::CalculateTransformForTime(FCDSceneNode * originalNode, float32 time)
{
	FMMatrix44 colladaLocalMatrix;
	colladaLocalMatrix = FMMatrix44::Identity;// = FMMatrix44::Identity(); 
	
	for (int t = 0; t < (int)originalNode->GetTransformCount(); ++t)
	{
		FCDTransform * transform = originalNode->GetTransform(t);
		if (transform->IsAnimated()) // process all animations to make CalculateWorldTransform work
		{
			FCDAnimated * animated = transform->GetAnimated();
			animated->Evaluate(time);
		}
		
		if (transform->GetType() == FCDTransform::TRANSLATION)
		{
			FCDTTranslation * translation = dynamic_cast<FCDTTranslation*>(transform);
			FMVector3 point = FMVector3(0.0f, 0.0f, 0.0f);
			point = translation->GetTranslation();
			if (transform->IsAnimated()) 
			{
				FCDAnimationCurve* curve;
				
				// look for x animation
				curve = transform->GetAnimated()->FindCurve(".X");
				if (curve != 0) 
					point.x = curve->Evaluate(time);
					
				// look for y animation
				curve = transform->GetAnimated()->FindCurve(".Y");
				if (curve != 0) 
					point.y = curve->Evaluate(time);
						
				// look for z animation
				curve = transform->GetAnimated()->FindCurve(".Z");
				if (curve != 0) 
					point.z = curve->Evaluate(time);
			}
			colladaLocalMatrix = colladaLocalMatrix * FMMatrix44::TranslationMatrix(point);
		}else if (transform->GetType() == FCDTransform::ROTATION)
		{
			FCDTRotation * rotation = dynamic_cast<FCDTRotation*>(transform);
			FMVector3 axis = FMVector3(0.0f, 0.0f, 0.0f);
			float angle = 0;
			axis = rotation->GetAxis();
			angle = rotation->GetAngle();
			
			if (rotation->IsAnimated()) 
			{
				FCDAnimationCurve* curve;
				
				// look for x animation
				curve = rotation->GetAnimated()->FindCurve(".X");
				if (curve != 0) 
					axis.x = curve->Evaluate(time);
					
				// look for y animation
				curve = rotation->GetAnimated()->FindCurve(".Y");
				if (curve != 0) 
					axis.y = curve->Evaluate(time);
						
				// look for z animation
				curve = rotation->GetAnimated()->FindCurve(".Z");
				if (curve != 0) 
					axis.z = curve->Evaluate(time);
							
							// look for z animation
				curve = rotation->GetAnimated()->FindCurve(".ANGLE");
				if (curve != 0) 
					angle = curve->Evaluate(time);
			}
			colladaLocalMatrix = colladaLocalMatrix * FMMatrix44::AxisRotationMatrix(axis, angle * PI / 180.0f);
		}else
		{
			colladaLocalMatrix = colladaLocalMatrix * transform->ToMatrix();
		}
		
	}
	return colladaLocalMatrix;
}
Exemplo n.º 8
0
void FCDEffectPassState::SetDefaultValue()
{

#define SET_VALUE(offset, valueType, actualValue) *((valueType*)(data + offset)) = actualValue;
#define SET_ENUM(offset, nameSpace, actualValue) *((uint32*)(data + offset)) = nameSpace::actualValue;

	switch ((uint32) type)
	{
	case FUDaePassState::ALPHA_FUNC:
		SET_ENUM(0, FUDaePassStateFunction, ALWAYS);
		SET_VALUE(4, float, 0.0f);
		break;

	case FUDaePassState::BLEND_FUNC:
		SET_ENUM(0, FUDaePassStateBlendType, ONE);
		SET_ENUM(4, FUDaePassStateBlendType, ZERO);
		break;

	case FUDaePassState::BLEND_FUNC_SEPARATE:
		SET_ENUM(0, FUDaePassStateBlendType, ONE);
		SET_ENUM(4, FUDaePassStateBlendType, ZERO);
		SET_ENUM(8, FUDaePassStateBlendType, ONE);
		SET_ENUM(12, FUDaePassStateBlendType, ZERO);
		break;

	case FUDaePassState::BLEND_EQUATION:
		SET_ENUM(0, FUDaePassStateBlendEquation, ADD);
		break;

	case FUDaePassState::BLEND_EQUATION_SEPARATE:
		SET_ENUM(0, FUDaePassStateBlendEquation, ADD);
		SET_ENUM(4, FUDaePassStateBlendEquation, ADD);
		break;

	case FUDaePassState::COLOR_MATERIAL:
		SET_ENUM(0, FUDaePassStateFaceType, FRONT_AND_BACK);
		SET_ENUM(4, FUDaePassStateMaterialType, AMBIENT_AND_DIFFUSE);
		break;

	case FUDaePassState::CULL_FACE:
		SET_ENUM(0, FUDaePassStateFaceType, BACK);
		break;

	case FUDaePassState::DEPTH_FUNC:
		SET_ENUM(0, FUDaePassStateFunction, ALWAYS);
		break;

	case FUDaePassState::FOG_MODE:
		SET_ENUM(0, FUDaePassStateFogType, EXP);
		break;

	case FUDaePassState::FOG_COORD_SRC:
		SET_ENUM(0, FUDaePassStateFogCoordinateType, FOG_COORDINATE);
		break;

	case FUDaePassState::FRONT_FACE:
		SET_ENUM(0, FUDaePassStateFrontFaceType, COUNTER_CLOCKWISE);
		break;

	case FUDaePassState::LIGHT_MODEL_COLOR_CONTROL:
		SET_ENUM(0, FUDaePassStateLightModelColorControlType, SINGLE_COLOR);
		break;

	case FUDaePassState::LOGIC_OP:
		SET_ENUM(0, FUDaePassStateLogicOperation, COPY);
		break;

	case FUDaePassState::POLYGON_MODE:
		SET_ENUM(0, FUDaePassStateFaceType, FRONT_AND_BACK);
		SET_ENUM(4, FUDaePassStatePolygonMode, FILL);
		break;

	case FUDaePassState::SHADE_MODEL:
		SET_ENUM(0, FUDaePassStateShadeModel, SMOOTH);
		break;

	case FUDaePassState::STENCIL_FUNC:
		SET_ENUM(0, FUDaePassStateFunction, ALWAYS);
		SET_VALUE(4, uint8, 0);
		SET_VALUE(5, uint8, 0xFF);
		break;

	case FUDaePassState::STENCIL_OP:
		SET_ENUM(0, FUDaePassStateStencilOperation, KEEP);
		SET_ENUM(4, FUDaePassStateStencilOperation, KEEP);
		SET_ENUM(8, FUDaePassStateStencilOperation, KEEP);
		break;

	case FUDaePassState::STENCIL_FUNC_SEPARATE:
		SET_ENUM(0, FUDaePassStateFunction, ALWAYS);
		SET_ENUM(4, FUDaePassStateFunction, ALWAYS);
		SET_VALUE(8, uint8, 0);
		SET_VALUE(9, uint8, 0xFF);
		break;

	case FUDaePassState::STENCIL_OP_SEPARATE:
		SET_ENUM(0, FUDaePassStateFaceType, FRONT_AND_BACK);
		SET_ENUM(4, FUDaePassStateStencilOperation, KEEP);
		SET_ENUM(8, FUDaePassStateStencilOperation, KEEP);
		SET_ENUM(12, FUDaePassStateStencilOperation, KEEP);
		break;

	case FUDaePassState::STENCIL_MASK_SEPARATE:
		SET_ENUM(0, FUDaePassStateFaceType, FRONT_AND_BACK);
		SET_VALUE(4, uint8, 0xFF);
		break;

	case FUDaePassState::LIGHT_ENABLE:
		SET_VALUE(0, uint8, 0);
		SET_VALUE(1, bool, false);
		break;

	case FUDaePassState::LIGHT_AMBIENT:
		SET_VALUE(0, uint8, 0);
		SET_VALUE(1, FMVector4, FMVector4(0,0,0,1));
		break;

	case FUDaePassState::LIGHT_DIFFUSE:
	case FUDaePassState::LIGHT_SPECULAR:
	case FUDaePassState::TEXTURE_ENV_COLOR:
	case FUDaePassState::CLIP_PLANE:
		SET_VALUE(0, uint8, 0);
		SET_VALUE(1, FMVector4, FMVector4::Zero);
		break;

	case FUDaePassState::LIGHT_POSITION:
		SET_VALUE(0, uint8, 0);
		SET_VALUE(1, FMVector4, FMVector4(0,0,1,0));
		break;

	case FUDaePassState::LIGHT_CONSTANT_ATTENUATION:
		SET_VALUE(0, uint8, 0);
		SET_VALUE(1, float, 1.0f);
		break;

	case FUDaePassState::LIGHT_LINEAR_ATTENUATION:
		SET_VALUE(0, uint8, 0);
		SET_VALUE(1, float, 0.0f);
		break;

	case FUDaePassState::LIGHT_QUADRATIC_ATTENUATION:
		SET_VALUE(0, uint8, 0);
		SET_VALUE(1, float, 0.0f);
		break;

	case FUDaePassState::LIGHT_SPOT_CUTOFF:
		SET_VALUE(0, uint8, 0);
		SET_VALUE(1, float, 180.0f);
		break;

	case FUDaePassState::LIGHT_SPOT_DIRECTION:
		SET_VALUE(0, uint8, 0);
		SET_VALUE(1, FMVector3, FMVector3(0,0,-1));
		break;

	case FUDaePassState::LIGHT_SPOT_EXPONENT:
		SET_VALUE(0, uint8, 0);
		SET_VALUE(1, float, 0.0f);
		break;

	case FUDaePassState::TEXTURE1D:
	case FUDaePassState::TEXTURE2D:
	case FUDaePassState::TEXTURE3D:
	case FUDaePassState::TEXTURECUBE:
	case FUDaePassState::TEXTURERECT:
	case FUDaePassState::TEXTUREDEPTH:
		SET_VALUE(0, uint8, 0);
		SET_VALUE(1, uint32, 0);
		break;

	case FUDaePassState::TEXTURE1D_ENABLE:
	case FUDaePassState::TEXTURE2D_ENABLE:
	case FUDaePassState::TEXTURE3D_ENABLE:
	case FUDaePassState::TEXTURECUBE_ENABLE:
	case FUDaePassState::TEXTURERECT_ENABLE:
	case FUDaePassState::TEXTUREDEPTH_ENABLE:
	case FUDaePassState::CLIP_PLANE_ENABLE:
		SET_VALUE(0, uint8, 0);
		SET_VALUE(1, bool, false);
		break;

	case FUDaePassState::TEXTURE_ENV_MODE:
		memset(data, 0, dataSize);
		break;

	case FUDaePassState::BLEND_COLOR:
	case FUDaePassState::CLEAR_COLOR:
	case FUDaePassState::FOG_COLOR:
	case FUDaePassState::SCISSOR:
		SET_VALUE(0, FMVector4, FMVector4::Zero);
		break;

	case FUDaePassState::LIGHT_MODEL_AMBIENT:
	case FUDaePassState::MATERIAL_AMBIENT:
		SET_VALUE(0, FMVector4, FMVector4(0.2f,0.2f,0.2f,1.0f));
		break;

	case FUDaePassState::MATERIAL_DIFFUSE:
		SET_VALUE(0, FMVector4, FMVector4(0.8f,0.8f,0.8f,1.0f));
		break;

	case FUDaePassState::MATERIAL_EMISSION:
	case FUDaePassState::MATERIAL_SPECULAR:
		SET_VALUE(0, FMVector4, FMVector4(0,0,0,1));
		break;

	case FUDaePassState::POINT_DISTANCE_ATTENUATION:
		SET_VALUE(0, FMVector3, FMVector3(1,0,0));
		break;

	case FUDaePassState::DEPTH_BOUNDS:
	case FUDaePassState::DEPTH_RANGE:
		SET_VALUE(0, FMVector2, FMVector2(0,1));
		break;

	case FUDaePassState::POLYGON_OFFSET:
		SET_VALUE(0, FMVector2, FMVector2(0,0));
		break;

	case FUDaePassState::DEPTH_MASK:
		SET_VALUE(0, bool, true);
		break;

	case FUDaePassState::CLEAR_STENCIL:
		SET_VALUE(0, uint32, 0);
		break;

	case FUDaePassState::STENCIL_MASK:
		SET_VALUE(0, uint32, 0xFFFFFFFF);
		break;

	case FUDaePassState::CLEAR_DEPTH:
	case FUDaePassState::FOG_DENSITY:
	case FUDaePassState::FOG_END:
	case FUDaePassState::LINE_WIDTH:
	case FUDaePassState::POINT_FADE_THRESHOLD_SIZE:
	case FUDaePassState::POINT_SIZE:
	case FUDaePassState::POINT_SIZE_MAX:
		SET_VALUE(0, float, 1.0f);
		break;

	case FUDaePassState::FOG_START:
	case FUDaePassState::MATERIAL_SHININESS:
	case FUDaePassState::POINT_SIZE_MIN:
		SET_VALUE(0, float, 0.0f);
		break;

	case FUDaePassState::COLOR_MASK:
		SET_VALUE(0, bool, true);
		SET_VALUE(1, bool, true);
		SET_VALUE(2, bool, true);
		SET_VALUE(3, bool, true);
		break;

	case FUDaePassState::LINE_STIPPLE:
		SET_VALUE(0, uint16, 1);
		SET_VALUE(2, uint16, 0xFF);
		break;

	case FUDaePassState::MODEL_VIEW_MATRIX:
	case FUDaePassState::PROJECTION_MATRIX:
		SET_VALUE(0, FMMatrix44, FMMatrix44::Identity);
		break;

	case FUDaePassState::LIGHTING_ENABLE:
	case FUDaePassState::ALPHA_TEST_ENABLE:
	case FUDaePassState::AUTO_NORMAL_ENABLE:
	case FUDaePassState::BLEND_ENABLE:
	case FUDaePassState::COLOR_LOGIC_OP_ENABLE:
	case FUDaePassState::CULL_FACE_ENABLE:
	case FUDaePassState::DEPTH_BOUNDS_ENABLE:
	case FUDaePassState::DEPTH_CLAMP_ENABLE:
	case FUDaePassState::DEPTH_TEST_ENABLE:
	case FUDaePassState::DITHER_ENABLE:
	case FUDaePassState::FOG_ENABLE:
	case FUDaePassState::LIGHT_MODEL_LOCAL_VIEWER_ENABLE:
	case FUDaePassState::LIGHT_MODEL_TWO_SIDE_ENABLE:
	case FUDaePassState::LINE_SMOOTH_ENABLE:
	case FUDaePassState::LINE_STIPPLE_ENABLE:
	case FUDaePassState::LOGIC_OP_ENABLE:
	case FUDaePassState::MULTISAMPLE_ENABLE:
	case FUDaePassState::NORMALIZE_ENABLE:
	case FUDaePassState::POINT_SMOOTH_ENABLE:
	case FUDaePassState::POLYGON_OFFSET_FILL_ENABLE:
	case FUDaePassState::POLYGON_OFFSET_LINE_ENABLE:
	case FUDaePassState::POLYGON_OFFSET_POINT_ENABLE:
	case FUDaePassState::POLYGON_SMOOTH_ENABLE:
	case FUDaePassState::POLYGON_STIPPLE_ENABLE:
	case FUDaePassState::RESCALE_NORMAL_ENABLE:
	case FUDaePassState::SAMPLE_ALPHA_TO_COVERAGE_ENABLE:
	case FUDaePassState::SAMPLE_ALPHA_TO_ONE_ENABLE:
	case FUDaePassState::SAMPLE_COVERAGE_ENABLE:
	case FUDaePassState::SCISSOR_TEST_ENABLE:
	case FUDaePassState::STENCIL_TEST_ENABLE:
		SET_VALUE(0, bool, false);
		break;

	case FUDaePassState::COLOR_MATERIAL_ENABLE:
		SET_VALUE(0, bool, true);
		break;

	case FUDaePassState::COUNT:
	case FUDaePassState::INVALID:
	default:
		FUFail(break);
	}

#undef SET_ENUM
#undef SET_VALUE
}
Exemplo n.º 9
0
bool FArchiveXML::LoadPhysicsRigidBodyParameters(FCDPhysicsRigidBodyParameters* parameters, xmlNode* techniqueNode, FCDPhysicsRigidBodyParameters* defaultParameters)
{
	bool status = true;

	xmlNode* param = FindChildByType(techniqueNode, DAE_DYNAMIC_ELEMENT);
	if (param)
	{
		parameters->SetDynamic(FUStringConversion::ToBoolean(ReadNodeContentDirect(param)));
		FArchiveXML::LoadAnimatable(&parameters->GetDynamic(), param);
	}
	else if (defaultParameters != NULL)
	{
		parameters->SetDynamic(defaultParameters->GetDynamic() > 0.5f);
		if (defaultParameters->GetDynamic().IsAnimated())
		{
			defaultParameters->GetDynamic().GetAnimated()->Clone(parameters->GetDynamic().GetAnimated());
		}
	}

	xmlNode* massFrame;
	massFrame = FindChildByType(techniqueNode, DAE_MASS_FRAME_ELEMENT);
	if (massFrame)
	{
		param = FindChildByType(massFrame, DAE_TRANSLATE_ELEMENT);
		if (param)
		{
			parameters->SetMassFrameTranslate(FUStringConversion::ToVector3(ReadNodeContentDirect(param)));
			FArchiveXML::LoadAnimatable(&parameters->GetMassFrameTranslate(), param);
		}
		else if (defaultParameters != NULL)
		{
			parameters->SetMassFrameTranslate(defaultParameters->GetMassFrameTranslate());
			if (defaultParameters->GetMassFrameTranslate().IsAnimated())
			{
				defaultParameters->GetMassFrameTranslate().GetAnimated()->Clone(parameters->GetMassFrameTranslate().GetAnimated());
			}
		}
		else
		{
			// no movement
			parameters->SetMassFrameTranslate(FMVector3::Zero);
		}

		param = FindChildByType(massFrame, DAE_ROTATE_ELEMENT);
		if (param)
		{
			FMVector4 temp = FUStringConversion::ToVector4(ReadNodeContentDirect(param));
			parameters->SetMassFrameOrientation(FMAngleAxis(FMVector3(temp.x, temp.y, temp.z), temp.w));
			LoadAnimatable(&parameters->GetMassFrameOrientation(), param);
		}
		else if (defaultParameters != NULL)
		{
			parameters->SetMassFrameOrientation(defaultParameters->GetMassFrameOrientation());
			if (defaultParameters->GetMassFrameOrientation().IsAnimated())
			{
				defaultParameters->GetMassFrameOrientation().GetAnimated()->Clone(parameters->GetMassFrameOrientation().GetAnimated());
			}
		}
		else
		{
			// no movement
			parameters->SetMassFrameOrientation(FMAngleAxis(FMVector3::XAxis, 0.0f));
		}
	}
	else if (defaultParameters != NULL)
	{
		parameters->SetMassFrameTranslate(defaultParameters->GetMassFrameTranslate());
		parameters->SetMassFrameOrientation(defaultParameters->GetMassFrameOrientation());
		if (defaultParameters->GetMassFrameTranslate().IsAnimated())
		{
			defaultParameters->GetMassFrameTranslate().GetAnimated()->Clone(parameters->GetMassFrameTranslate().GetAnimated());
		}
		if (defaultParameters->GetMassFrameOrientation().IsAnimated())
		{
			defaultParameters->GetMassFrameOrientation().GetAnimated()->Clone(parameters->GetMassFrameOrientation().GetAnimated());
		}
	}
	else
	{
		// no movement
		parameters->SetMassFrameTranslate(FMVector3::Zero);
		parameters->SetMassFrameOrientation(FMAngleAxis(FMVector3::XAxis, 0.0f));
	}

	xmlNodeList shapeNodes;
	FindChildrenByType(techniqueNode, DAE_SHAPE_ELEMENT, shapeNodes);
	if (shapeNodes.empty())
	{
		FUError::Error(FUError::WARNING_LEVEL, FUError::WARNING_SHAPE_NODE_MISSING, techniqueNode->line);
	}
	for (xmlNodeList::iterator itS = shapeNodes.begin(); itS != shapeNodes.end(); ++itS)
	{
		FCDPhysicsShape* shape = parameters->AddPhysicsShape();
		status &= (FArchiveXML::LoadPhysicsShape(shape, *itS));
	}
	// shapes are not taken from the default parameters

	param = FindChildByType(techniqueNode, DAE_PHYSICS_MATERIAL_ELEMENT);
	if (param != NULL) 
	{
		FCDPhysicsMaterial* material = parameters->AddOwnPhysicsMaterial();
		FArchiveXML::LoadPhysicsMaterial(material, param);
	}
	else
	{
		param = FindChildByType(techniqueNode, DAE_INSTANCE_PHYSICS_MATERIAL_ELEMENT);
		if (param != NULL)
		{
			FCDEntityInstance* physicsMaterialInstance = FCDEntityInstanceFactory::CreateInstance(parameters->GetDocument(), NULL, FCDEntity::PHYSICS_MATERIAL);
			parameters->SetInstanceMaterial(physicsMaterialInstance);
			FArchiveXML::LoadSwitch(physicsMaterialInstance, &physicsMaterialInstance->GetObjectType(), param);
			FCDPhysicsMaterial* material = (FCDPhysicsMaterial*) physicsMaterialInstance->GetEntity();
			if (material == NULL)
			{
				FUError::Error(FUError::ERROR_LEVEL, FUError::WARNING_MISSING_URI_TARGET, param->line);
			}
			parameters->SetPhysicsMaterial(material);
		}
		else
		{
			FUError::Error(FUError::WARNING_LEVEL, FUError::WARNING_PHYS_MAT_DEF_MISSING, techniqueNode->line);
		}
	}
	// material is not taken fromt he default parameters

	param = FindChildByType(techniqueNode, DAE_MASS_ELEMENT);
	if (param)
	{
		parameters->SetMass(FUStringConversion::ToFloat(ReadNodeContentDirect(param)));
		parameters->SetDensityMoreAccurate(false);
		parameters->SetDensity(0.0f);
		FArchiveXML::LoadAnimatable(&parameters->GetMass(), param);
	}
	else if (defaultParameters != NULL)
	{
		parameters->SetMass(defaultParameters->GetMass());
		parameters->SetDensity(defaultParameters->GetDensity());
		parameters->SetDensityMoreAccurate(defaultParameters->IsDensityMoreAccurate());
		if (defaultParameters->GetMass().IsAnimated())
		{
			defaultParameters->GetMass().GetAnimated()->Clone(parameters->GetMass().GetAnimated());
		}
	}
	else
	{
		/* Default value for mass is density x total shape volume, but 
		   since our shape's mass is already calculated with respect to the
		   volume, we can just read it from there. If the user specified a 
		   mass, then this overrides the calculation of density x volume, 
		   as expected. */
		parameters->SetMass(0.0f);
		float totalDensity = 0.0f;
		parameters->SetDensityMoreAccurate(false);
		for (size_t i = 0; i < parameters->GetPhysicsShapeCount(); ++i)
		{
			FCDPhysicsShape* shape = parameters->GetPhysicsShape(i);
			parameters->SetMass(parameters->GetMass() + shape->GetMass());
			totalDensity += shape->GetDensity();
			parameters->SetDensityMoreAccurate(parameters->IsDensityMoreAccurate() || shape->IsDensityMoreAccurate()); // common case: 1 shape, density = 1.0f
		}
		parameters->SetDensity(totalDensity / parameters->GetPhysicsShapeCount());
	}
	

	param = FindChildByType(techniqueNode, DAE_INERTIA_ELEMENT);
	if (param) 
	{
		parameters->SetInertia(FUStringConversion::ToVector3(ReadNodeContentDirect(param)));
		parameters->SetInertiaAccurate(true);
		FArchiveXML::LoadAnimatable(&parameters->GetInertia(), param);
	}
	else if (defaultParameters != NULL)
	{
		parameters->SetInertia(defaultParameters->GetInertia());
		parameters->SetInertiaAccurate(defaultParameters->IsInertiaAccurate());
		if (defaultParameters->GetInertia().IsAnimated())
		{
			defaultParameters->GetInertia().GetAnimated()->Clone(parameters->GetInertia().GetAnimated());
		}
	}
	else
	{
		/* FIXME: Approximation: sphere shape, with mass distributed 
		   equally across the volume and center of mass is at the center of
		   the sphere. Real moments of inertia call for complex 
		   integration. Sphere it is simply I = k * m * r^2 on all axes. */
		float volume = 0.0f;
		for (size_t i = 0; i < parameters->GetPhysicsShapeCount(); ++i)
		{
			volume += parameters->GetPhysicsShape(i)->CalculateVolume();
		}

		float radiusCubed = 0.75f * volume / (float)FMath::Pi;
		float I = 0.4f * parameters->GetMass() * pow(radiusCubed, 2.0f / 3.0f);
		parameters->SetInertia(FMVector3(I, I, I));
		parameters->SetInertiaAccurate(false);
	}

	return status;
}
Exemplo n.º 10
0
bool CModelConverter::ReadDAE(const tstring& sFilename)
{
	if (m_pWorkListener)
		m_pWorkListener->BeginProgress();

	FCollada::Initialize();

	FCDocument* pDoc = FCollada::NewTopDocument();

	if (m_pWorkListener)
		m_pWorkListener->SetAction("Reading file", 0);

	if (FCollada::LoadDocumentFromFile(pDoc, convert_to_fstring(sFilename)))
	{
		size_t i;

		FCDocumentTools::StandardizeUpAxisAndLength(pDoc, FMVector3(0, 1, 0));

		FCDMaterialLibrary* pMatLib = pDoc->GetMaterialLibrary();
		size_t iEntities = pMatLib->GetEntityCount();

		if (m_pWorkListener)
			m_pWorkListener->SetAction("Reading materials", iEntities);

		for (i = 0; i < iEntities; ++i)
		{
			FCDMaterial* pColladaMaterial = pMatLib->GetEntity(i);
			FCDEffect* pEffect = pColladaMaterial->GetEffect();

			size_t iMaterial = m_pScene->AddMaterial(convert_from_fstring(pColladaMaterial->GetName()));
			CConversionMaterial* pMaterial = m_pScene->GetMaterial(iMaterial);

			if (pEffect->GetProfileCount() < 1)
				continue;

			FCDEffectProfile* pEffectProfile = pEffect->GetProfile(0);

			FUDaeProfileType::Type eProfileType = pEffectProfile->GetType();
			if (eProfileType == FUDaeProfileType::COMMON)
			{
				FCDEffectStandard* pStandardProfile = dynamic_cast<FCDEffectStandard*>(pEffectProfile);
				if (pStandardProfile)
				{
					pMaterial->m_vecAmbient = Vector((float*)pStandardProfile->GetAmbientColor());
					pMaterial->m_vecDiffuse = Vector((float*)pStandardProfile->GetDiffuseColor());
					pMaterial->m_vecSpecular = Vector((float*)pStandardProfile->GetSpecularColor());
					pMaterial->m_vecEmissive = Vector((float*)pStandardProfile->GetEmissionColor());
					pMaterial->m_flShininess = pStandardProfile->GetShininess();
				}
			}

			for (size_t j = 0; j < pEffectProfile->GetEffectParameterCount(); j++)
			{
				FCDEffectParameter* pEffectParameter = pEffectProfile->GetEffectParameter(j);

				FCDEffectParameter::Type eType = pEffectParameter->GetType();

				if (eType == FCDEffectParameter::SAMPLER)
				{
					FCDEffectParameterSampler* pSampler = dynamic_cast<FCDEffectParameterSampler*>(pEffectParameter);
					if (pSampler)
					{
						FCDEffectParameterSurface* pSurface = pSampler->GetSurface();
						if (pSurface)
						{
							if (pSurface->GetImageCount())
							{
								// Christ Collada why do you have to make things so damn complicated?
								FCDImage* pImage = pSurface->GetImage(0);

								pMaterial->m_sDiffuseTexture = convert_from_fstring(pImage->GetFilename());

								// Fix up a bug in the Max Collada exporter
								if (pMaterial->m_sDiffuseTexture.startswith("\\\\C\\"))
									pMaterial->m_sDiffuseTexture = "C:\\" + pMaterial->m_sDiffuseTexture.substr(4);
								else if (pMaterial->m_sDiffuseTexture.startswith("\\\\D\\"))
									pMaterial->m_sDiffuseTexture = "D:\\" + pMaterial->m_sDiffuseTexture.substr(4);
							}
						}
					}
				}
			}

			if (m_pWorkListener)
				m_pWorkListener->WorkProgress(i+1);
		}

		FCDGeometryLibrary* pGeoLib = pDoc->GetGeometryLibrary();
		iEntities = pGeoLib->GetEntityCount();

		if (m_pWorkListener)
			m_pWorkListener->SetAction("Loading entities", iEntities);

		for (i = 0; i < iEntities; ++i)
		{
			FCDGeometry* pGeometry = pGeoLib->GetEntity(i);
			if (pGeometry->IsMesh())
			{
				size_t j;

				size_t iMesh = m_pScene->AddMesh(convert_from_fstring(pGeometry->GetName()));
				CConversionMesh* pMesh = m_pScene->GetMesh(iMesh);
				pMesh->AddBone(convert_from_fstring(pGeometry->GetName()));

				FCDGeometryMesh* pGeoMesh = pGeometry->GetMesh();
				FCDGeometrySource* pPositionSource = pGeoMesh->GetPositionSource();
				size_t iVertexCount = pPositionSource->GetValueCount();

				for (j = 0; j < iVertexCount; j++)
				{
					const float* pflValues = pPositionSource->GetValue(j);
					pMesh->AddVertex(pflValues[0], pflValues[1], pflValues[2]);
				}

				FCDGeometrySource* pNormalSource = pGeoMesh->FindSourceByType(FUDaeGeometryInput::NORMAL);
				if (pNormalSource)
				{
					iVertexCount = pNormalSource->GetValueCount();
					for (j = 0; j < iVertexCount; j++)
					{
						const float* pflValues = pNormalSource->GetValue(j);
						pMesh->AddNormal(pflValues[0], pflValues[1], pflValues[2]);
					}
				}

				FCDGeometrySource* pUVSource = pGeoMesh->FindSourceByType(FUDaeGeometryInput::TEXCOORD);
				if (pUVSource)
				{
					iVertexCount = pUVSource->GetValueCount();
					for (j = 0; j < iVertexCount; j++)
					{
						const float* pflValues = pUVSource->GetValue(j);
						pMesh->AddUV(pflValues[0], pflValues[1]);
					}
				}

				for (j = 0; j < pGeoMesh->GetPolygonsCount(); j++)
				{
					FCDGeometryPolygons* pPolygons = pGeoMesh->GetPolygons(j);
					FCDGeometryPolygonsInput* pPositionInput = pPolygons->FindInput(FUDaeGeometryInput::POSITION);
					FCDGeometryPolygonsInput* pNormalInput = pPolygons->FindInput(FUDaeGeometryInput::NORMAL);
					FCDGeometryPolygonsInput* pUVInput = pPolygons->FindInput(FUDaeGeometryInput::TEXCOORD);

					size_t iPositionCount = pPositionInput->GetIndexCount();
					uint32* pPositions = pPositionInput->GetIndices();

					size_t iNormalCount = 0;
					uint32* pNormals = NULL;

					if (pNormalInput)
					{
						iNormalCount = pNormalInput->GetIndexCount();
						pNormals = pNormalInput->GetIndices();
					}

					size_t iUVCount = 0;
					uint32* pUVs = NULL;

					if (pUVInput)
					{
						iUVCount = pUVInput->GetIndexCount();
						pUVs = pUVInput->GetIndices();
					}

					fm::stringT<fchar> sMaterial = pPolygons->GetMaterialSemantic();

					size_t iCurrentMaterial = pMesh->AddMaterialStub(convert_from_fstring(sMaterial));

					if (pPolygons->TestPolyType() == 3)
					{
						// All triangles!
						for (size_t k = 0; k < iPositionCount; k+=3)
						{
							size_t iFace = pMesh->AddFace(iCurrentMaterial);

							pMesh->AddVertexToFace(iFace, pPositions[k+0], pUVs?pUVs[k+0]:~0, pNormals?pNormals[k+0]:~0);
							pMesh->AddVertexToFace(iFace, pPositions[k+1], pUVs?pUVs[k+1]:~0, pNormals?pNormals[k+1]:~0);
							pMesh->AddVertexToFace(iFace, pPositions[k+2], pUVs?pUVs[k+2]:~0, pNormals?pNormals[k+2]:~0);
						}
					}
					else if (pPolygons->TestPolyType() == 4)
					{
						// All quads!
						for (size_t k = 0; k < iPositionCount; k+=4)
						{
							size_t iFace = pMesh->AddFace(iCurrentMaterial);

							pMesh->AddVertexToFace(iFace, pPositions[k+0], pUVs?pUVs[k+0]:~0, pNormals?pNormals[k+0]:~0);
							pMesh->AddVertexToFace(iFace, pPositions[k+1], pUVs?pUVs[k+1]:~0, pNormals?pNormals[k+1]:~0);
							pMesh->AddVertexToFace(iFace, pPositions[k+2], pUVs?pUVs[k+2]:~0, pNormals?pNormals[k+2]:~0);
							pMesh->AddVertexToFace(iFace, pPositions[k+3], pUVs?pUVs[k+3]:~0, pNormals?pNormals[k+3]:~0);
						}
					}
					else
					{
						size_t iFaces = pPolygons->GetFaceCount();
						for (size_t k = 0; k < iFaces; k++)
						{
							size_t iFace = pMesh->AddFace(iCurrentMaterial);
							size_t o = pPolygons->GetFaceVertexOffset(k);
							size_t iFaceVertexCount = pPolygons->GetFaceVertexCount(k);
							for (size_t v = 0; v < iFaceVertexCount; v++)
								pMesh->AddVertexToFace(iFace, pPositions[o+v], pUVs?pUVs[o+v]:~0, pNormals?pNormals[o+v]:~0);
						}
					}
				}
			}

			if (m_pWorkListener)
				m_pWorkListener->WorkProgress(i+1);
		}

		FCDVisualSceneNodeLibrary* pVisualScenes = pDoc->GetVisualSceneLibrary();
		iEntities = pVisualScenes->GetEntityCount();
		for (i = 0; i < iEntities; ++i)
		{
			FCDSceneNode* pNode = pVisualScenes->GetEntity(i);

			size_t iScene = m_pScene->AddScene(convert_from_fstring(pNode->GetName()));
			ReadDAESceneTree(pNode, m_pScene->GetScene(iScene));
		}
	}
	else
	{
		printf("Oops! Some kind of error happened!\n");
		return false;
	}

	m_pScene->SetWorkListener(m_pWorkListener);

	m_pScene->CalculateExtends();

	for (size_t i = 0; i < m_pScene->GetNumMeshes(); i++)
	{
		if (m_bWantEdges)
			m_pScene->GetMesh(i)->CalculateEdgeData();

		if (m_pScene->GetMesh(i)->GetNumNormals() == 0)
			m_pScene->GetMesh(i)->CalculateVertexNormals();

		m_pScene->GetMesh(i)->CalculateVertexTangents();
	}

	pDoc->Release();

	FCollada::Release();

	if (m_pWorkListener)
		m_pWorkListener->EndProgress();

	return true;
}
Exemplo n.º 11
0
void CModelConverter::SaveDAE(const tstring& sFilename)
{
	if (m_pWorkListener)
		m_pWorkListener->BeginProgress();

	FCollada::Initialize();

	FCDocument* pDoc = FCollada::NewTopDocument();

	FCDocumentTools::StandardizeUpAxisAndLength(pDoc, FMVector3(0, 1, 0));

	FCDAsset* pAsset = pDoc->GetAsset();
	FCDAssetContributor* pContributor = pAsset->AddContributor();
	pContributor->SetAuthoringTool(fstring_literal("Created by SMAK using FCollada"));

	FCDMaterialLibrary* pMatLib = pDoc->GetMaterialLibrary();

	if (m_pWorkListener)
		m_pWorkListener->SetAction("Saving materials", m_pScene->GetNumMaterials());

	for (size_t iMaterial = 0; iMaterial < m_pScene->GetNumMaterials(); iMaterial++)
	{
		CConversionMaterial* pConversionMaterial = m_pScene->GetMaterial(iMaterial);

		FCDMaterial* pColladaMaterial = pMatLib->AddEntity();
		pColladaMaterial->SetName(convert_to_fstring(pConversionMaterial->GetName()));
		FCDEffect* pEffect = pMatLib->GetDocument()->GetEffectLibrary()->AddEntity();
		pColladaMaterial->SetEffect(pEffect);
		FCDEffectProfile* pEffectProfile = pEffect->AddProfile(FUDaeProfileType::COMMON);

		pEffect->SetName(convert_to_fstring(pConversionMaterial->GetName()));

		FCDEffectStandard* pStandardProfile = dynamic_cast<FCDEffectStandard*>(pEffectProfile);
		if (pStandardProfile)
		{
			pStandardProfile->SetLightingType(FCDEffectStandard::PHONG);
			pStandardProfile->SetAmbientColor(FMVector4(FMVector3((float*)pConversionMaterial->m_vecAmbient), 1));
			pStandardProfile->SetDiffuseColor(FMVector4(FMVector3((float*)pConversionMaterial->m_vecDiffuse), 1));
			pStandardProfile->SetSpecularColor(FMVector4(FMVector3((float*)pConversionMaterial->m_vecSpecular), 1));
			pStandardProfile->SetEmissionColor(FMVector4(FMVector3((float*)pConversionMaterial->m_vecEmissive), 1));
			pStandardProfile->SetShininess(pConversionMaterial->m_flShininess);
		}

		if (pConversionMaterial->GetDiffuseTexture().length())
		{
			FCDEffectParameter* pEffectParameterSampler = pEffectProfile->AddEffectParameter(FCDEffectParameter::SAMPLER);
			FCDEffectParameter* pEffectParameterSurface = pEffectProfile->AddEffectParameter(FCDEffectParameter::SURFACE);
			FCDEffectParameterSampler* pSampler = dynamic_cast<FCDEffectParameterSampler*>(pEffectParameterSampler);
			FCDEffectParameterSurface* pSurface = dynamic_cast<FCDEffectParameterSurface*>(pEffectParameterSurface);
			FCDImage* pSurfaceImage = pMatLib->GetDocument()->GetImageLibrary()->AddEntity();

			pSurfaceImage->SetFilename(convert_to_fstring(pConversionMaterial->GetDiffuseTexture()));

			pSurface->SetInitMethod(new FCDEffectParameterSurfaceInitFrom());
			pSurface->AddImage(pSurfaceImage);
			pSurface->SetReference((pConversionMaterial->GetName() + "-surface").c_str());

			pSampler->SetSurface(pSurface);
		}

		if (m_pWorkListener)
			m_pWorkListener->WorkProgress(iMaterial);
	}

	if (m_pWorkListener)
		m_pWorkListener->SetAction("Saving geometry", m_pScene->GetNumMeshes());

	FCDGeometryLibrary* pGeoLib = pDoc->GetGeometryLibrary();

	for (size_t i = 0; i < m_pScene->GetNumMeshes(); ++i)
	{
		CConversionMesh* pConversionMesh = m_pScene->GetMesh(i);

		FCDGeometry* pGeometry = pGeoLib->AddEntity();
		pGeometry->SetName(convert_to_fstring(pConversionMesh->GetName()));
		pGeometry->CreateMesh();
		FCDGeometryMesh* pMesh = pGeometry->GetMesh();

		FCDGeometrySource* pPositionSource = pMesh->AddSource(FUDaeGeometryInput::POSITION);
		pPositionSource->SetName(convert_to_fstring(pConversionMesh->GetName() + "-position"));
		pPositionSource->SetStride(3);
		pPositionSource->SetValueCount(pConversionMesh->GetNumVertices());
		for (size_t j = 0; j < pConversionMesh->GetNumVertices(); j++)
			pPositionSource->SetValue(j, pConversionMesh->GetVertex(j));

		pMesh->AddVertexSource(pPositionSource);

		FCDGeometrySource* pNormalSource = pMesh->AddSource(FUDaeGeometryInput::NORMAL);
		pNormalSource->SetName(convert_to_fstring(pConversionMesh->GetName() + "-normal"));
		pNormalSource->SetStride(3);
		pNormalSource->SetValueCount(pConversionMesh->GetNumNormals());
		for (size_t j = 0; j < pConversionMesh->GetNumNormals(); j++)
			pNormalSource->SetValue(j, pConversionMesh->GetNormal(j));

		FCDGeometrySource* pUVSource = NULL;
		if (pConversionMesh->GetNumUVs())
		{
			pUVSource = pMesh->AddSource(FUDaeGeometryInput::TEXCOORD);
			pUVSource->SetName(convert_to_fstring(pConversionMesh->GetName() + "-texcoord"));
			pUVSource->SetStride(2);
			pUVSource->SetValueCount(pConversionMesh->GetNumUVs());
			for (size_t j = 0; j < pConversionMesh->GetNumUVs(); j++)
				pUVSource->SetValue(j, pConversionMesh->GetUV(j));
		}

		for (size_t iMaterials = 0; iMaterials < pConversionMesh->GetNumMaterialStubs(); iMaterials++)
		{
			CConversionMaterialStub* pStub = pConversionMesh->GetMaterialStub(iMaterials);

			FCDGeometryPolygons* pPolygons = pMesh->AddPolygons();
			pPolygons->SetMaterialSemantic(convert_to_fstring(pStub->GetName()));
			pPolygons->AddInput(pPositionSource, 0);
			pPolygons->AddInput(pNormalSource, 1);
			if (pConversionMesh->GetNumUVs())
				pPolygons->AddInput(pUVSource, 2);

			FCDGeometryPolygonsInput* pPositionInput = pPolygons->FindInput(pPositionSource);
			FCDGeometryPolygonsInput* pNormalInput = pPolygons->FindInput(pNormalSource);
			FCDGeometryPolygonsInput* pUVInput = pPolygons->FindInput(pUVSource);

			for (size_t iFace = 0; iFace < pConversionMesh->GetNumFaces(); iFace++)
			{
				CConversionFace* pFace = pConversionMesh->GetFace(iFace);

				if (pFace->m != iMaterials)
					continue;

				pPolygons->AddFaceVertexCount(pFace->GetNumVertices());
				for (size_t iVertex = 0; iVertex < pFace->GetNumVertices(); iVertex++)
				{
					pPositionInput->AddIndex(pFace->GetVertex(iVertex)->v);
					pNormalInput->AddIndex(pFace->GetVertex(iVertex)->vn);
					if (pConversionMesh->GetNumUVs())
						pUVInput->AddIndex(pFace->GetVertex(iVertex)->vu);
				}
			}
		}

		if (m_pWorkListener)
			m_pWorkListener->WorkProgress(i);
	}

	if (m_pWorkListener)
		m_pWorkListener->SetAction("Saving scenes", m_pScene->GetNumScenes());

	FCDVisualSceneNodeLibrary* pVisualScenes = pDoc->GetVisualSceneLibrary();
	for (size_t i = 0; i < m_pScene->GetNumScenes(); ++i)
	{
		FCDSceneNode* pNode = pVisualScenes->AddEntity();

		SaveDAEScene(pNode, m_pScene->GetScene(i));

		if (m_pWorkListener)
			m_pWorkListener->WorkProgress(i);
	}

	if (m_pWorkListener)
		m_pWorkListener->SetAction("Writing to disk...", 0);

	FCollada::SaveDocument(pDoc, convert_to_fstring(sFilename));

	pDoc->Release();

	FCollada::Release();

	if (m_pWorkListener)
		m_pWorkListener->EndProgress();
}
Exemplo n.º 12
0
	void LinkAnimatedParamCommonVector(const fm::string& semantic, FCDEffectParameterColor4* param, FCDGeometryInstance* geometry, FCDMaterial* material, FCDEffect* effect, FCDEffectProfile* profile)
	{
		//Change this to account for the animated params in geometryInstance list.
		const fm::string& reference = param->GetReference();
		if (reference.empty()) return;
		FCDEffectParameter* geometryParam = FindEffectParameterBySemantic(geometry, semantic);
		FCDEffectParameter* materialParam = FindEffectParameterByReference(material, reference, true);
		FCDEffectParameter* effectParam = FindEffectParameterByReference(effect, reference, true);
		FCDEffectParameter* profileParam = FindEffectParameterByReference(profile, reference, false);
		if (!geometryParam && !materialParam && !effectParam && !profileParam) return;
		if (geometryParam && geometryParam->GetType() == FCDEffectParameter::FLOAT3)
		{
			FCDEffectParameterFloat3* animatedParam = (FCDEffectParameterFloat3*) geometryParam;
			if (materialParam && materialParam->GetType() == FCDEffectParameter::FLOAT3)
			{
				FCDEffectParameterFloat3* float3Param = (FCDEffectParameterFloat3*) materialParam;
				animatedParam->SetValue(float3Param->GetValue());
				return;
			}
			else if (materialParam && materialParam->GetType() == FCDEffectParameter::VECTOR)
			{
				FCDEffectParameterColor4* vectorParam = (FCDEffectParameterColor4*) materialParam;
				animatedParam->SetValue(FMVector3((const FMVector4&) vectorParam->GetValue()));
				return;
			}
			else if (effectParam && effectParam->GetType() == FCDEffectParameter::FLOAT3)
			{
				FCDEffectParameterFloat3* float3Param = (FCDEffectParameterFloat3*) effectParam;
				animatedParam->SetValue(float3Param->GetValue());
				return;
			}
			else if (effectParam && effectParam->GetType() == FCDEffectParameter::VECTOR)
			{
				FCDEffectParameterColor4* vectorParam = (FCDEffectParameterColor4*) effectParam;
				animatedParam->SetValue(FMVector3((const FMVector4&) vectorParam->GetValue()));
				return;
			}
			else if (profileParam && profileParam->GetType() == FCDEffectParameter::FLOAT3)
			{
				FCDEffectParameterFloat3* float3Param = (FCDEffectParameterFloat3*) profileParam;
				animatedParam->SetValue(float3Param->GetValue());
				return;
			}
			else if (profileParam && profileParam->GetType() == FCDEffectParameter::VECTOR)
			{
				FCDEffectParameterColor4* vectorParam = (FCDEffectParameterColor4*) profileParam;
				animatedParam->SetValue(FMVector3((const FMVector4&) vectorParam->GetValue()));
				return;
			}
		}
		else if (geometryParam && geometryParam->GetType() == FCDEffectParameter::VECTOR)
		{
			FCDEffectParameterColor4* animatedParam = (FCDEffectParameterColor4*) geometryParam;
			if (materialParam && materialParam->GetType() == FCDEffectParameter::FLOAT3)
			{
				FCDEffectParameterFloat3* float3Param = (FCDEffectParameterFloat3*) materialParam;
				animatedParam->SetValue(FMVector4(float3Param->GetValue(), 1.0f));
				return;
			}
			else if (materialParam && materialParam->GetType() == FCDEffectParameter::VECTOR)
			{
				FCDEffectParameterColor4* vectorParam = (FCDEffectParameterColor4*) materialParam;
				animatedParam->SetValue(vectorParam->GetValue());
				return;
			}
			else if (effectParam && effectParam->GetType() == FCDEffectParameter::FLOAT3)
			{
				FCDEffectParameterFloat3* float3Param = (FCDEffectParameterFloat3*) effectParam;
				animatedParam->SetValue(FMVector4(float3Param->GetValue(), 1.0f));
				return;
			}
			else if (effectParam && effectParam->GetType() == FCDEffectParameter::VECTOR)
			{
				FCDEffectParameterColor4* vectorParam = (FCDEffectParameterColor4*) effectParam;
				animatedParam->SetValue(vectorParam->GetValue());
				return;
			}
			else if (profileParam && profileParam->GetType() == FCDEffectParameter::FLOAT3)
			{
				FCDEffectParameterFloat3* float3Param = (FCDEffectParameterFloat3*) profileParam;
				animatedParam->SetValue(FMVector4(float3Param->GetValue(), 1.0f));
				return;
			}
			else if (profileParam && profileParam->GetType() == FCDEffectParameter::VECTOR)
			{
				FCDEffectParameterColor4* vectorParam = (FCDEffectParameterColor4*) profileParam;
				animatedParam->SetValue(vectorParam->GetValue());
				return;
			}
		}
	}
Exemplo n.º 13
0
	uint32 ReadSourceInterleaved(xmlNode* sourceNode, fm::pvector<FMVector3List>& arrays)
	{
		uint32 stride = 1;
		if (sourceNode != NULL)
		{
			// Get the accessor's count
			xmlNode* accessorNode = FindTechniqueAccessor(sourceNode);
			uint32 count = ReadNodeCount(accessorNode);
			for (fm::pvector<FMVector3List>::iterator it = arrays.begin(); it != arrays.end(); ++it)
			{
				(*it)->resize(count);
			}

			// Backward Compatibility: if the stride is exactly half the expected value,
			// then we have the old 1D tangents that we need to parse correctly.
			stride = ReadNodeStride(accessorNode);
			if (stride > 0 && stride == arrays.size())
			{
				// Read and parse the float array
				xmlNode* arrayNode = FindChildByType(sourceNode, DAE_FLOAT_ARRAY_ELEMENT);
				const char* value = ReadNodeContentDirect(arrayNode);
				for (size_t i = 0; i < count && *value != 0; ++i)
				{
					for (size_t j = 0; j < stride && *value != 0; ++j)
					{
						arrays[j]->at(i) = FMVector3(FUStringConversion::ToFloat(&value), 0.0f, 0.0f);
					}
				}

				while (*value != 0)
				{
					for (size_t i = 0; i < stride && *value != 0; ++i)
					{
						arrays[i]->push_back(FMVector3(FUStringConversion::ToFloat(&value), 0.0f, 0.0f));
					}
				}
			}
			else
			{
				// Use the stride to pad the interleaved float lists or remove extra elements
				while (stride < arrays.size() * 3) arrays.pop_back();
				while (stride > arrays.size() * 3) arrays.push_back(NULL);

				// Read and parse the float array
				xmlNode* arrayNode = FindChildByType(sourceNode, DAE_FLOAT_ARRAY_ELEMENT);
				const char* value = ReadNodeContentDirect(arrayNode);
				for (size_t i = 0; i < count && *value != 0; ++i)
				{
					for (size_t j = 0; 3 * j < stride && *value != 0; ++j)
					{
						if (arrays[j] != NULL)
						{
							arrays[j]->at(i).x = FUStringConversion::ToFloat(&value);
							arrays[j]->at(i).y = FUStringConversion::ToFloat(&value);
							arrays[j]->at(i).z = FUStringConversion::ToFloat(&value);
						}
						else
						{
							FUStringConversion::ToFloat(&value);
							FUStringConversion::ToFloat(&value);
							FUStringConversion::ToFloat(&value);
						}
					}
				}

				while (*value != 0)
				{
					for (size_t i = 0; 2 * i < stride && *value != 0; ++i)
					{
						if (arrays[i] != NULL)
						{
							FMVector3 v;
							v.x = FUStringConversion::ToFloat(&value);
							v.y = FUStringConversion::ToFloat(&value);
							v.z = FUStringConversion::ToFloat(&value);
							arrays[i]->push_back(v);
						}
						else
						{
							FUStringConversion::ToFloat(&value);
							FUStringConversion::ToFloat(&value);
							FUStringConversion::ToFloat(&value);
						}
					}
				}
			}
		}
		return stride;
	}