Skip to content
Snippets Groups Projects
Coll.cpp 42.5 KiB
Newer Older
Tazpn's avatar
Tazpn committed
				Mesh& mesh = triObj->GetMesh();
				switch (bvType)
				{
				case bv_type_box:
Tazpn's avatar
Tazpn committed
					shape = makeProxyBoxShape(node, obj, mesh, tm);
Tazpn's avatar
Tazpn committed
					break;

Tazpn's avatar
Tazpn committed
				//case bv_type_sphere:
				//	shape = makeProxySphereShape(node, obj, mesh, tm);
				//	break;

Tazpn's avatar
Tazpn committed
				case bv_type_shapes:
Tazpn's avatar
Tazpn committed
					shape = makeProxyTriStripShape(node, obj, mesh, tm);
					break;
Tazpn's avatar
Tazpn committed

Tazpn's avatar
Tazpn committed
				case bv_type_packed:
Tazpn's avatar
Tazpn committed
					shape = makeProxyPackedTriStripShape(node, obj, mesh, tm);
Tazpn's avatar
Tazpn committed
					break;

				case bv_type_convex: 
Tazpn's avatar
Tazpn committed
					shape = makeProxyConvexShape(node, obj, mesh, tm);
Tazpn's avatar
Tazpn committed
					break;
				}
			}
		}
	}
	return shape;
}

Tazpn's avatar
Tazpn committed
bhkShapeRef	Exporter::makeProxyBoxShape(INode *node, Object *obj, Mesh& mesh, Matrix3& tm)
{
	enum { list_params, bv_mesh, };  // pblock2 ID
	enum { PB_MATERIAL, PB_MESHLIST, PB_BOUND_TYPE, PB_CENTER, };
	enum { bv_type_none, bv_type_box, bv_type_shapes, bv_type_packed, bv_type_convex, };  // pblock ID

	bhkShapeRef retval;
	if (IParamBlock2* pblock2 = obj->GetParamBlockByID(list_params))
	{
		Box3 box; box.Init();
		CalcAxisAlignedBox(mesh, box, NULL);

		int mtl = 0;
		float length = 0, width = 0, height = 0;
		pblock2->GetValue(PB_MATERIAL, 0, mtl, FOREVER, 0);

		bhkBoxShapeRef shape = new bhkBoxShape();
		Vector3 dim(box.Max().x-box.Min().x, box.Max().y-box.Min().y, box.Max().z-box.Min().z);
		dim /= (Exporter::bhkScaleFactor * 2);

		shape->SetMaterial(HavokMaterial(mtl));
		shape->SetDimensions(dim);

Tazpn's avatar
Tazpn committed
		Matrix3 ltm = /*GetLocalTM(node) * */TransMatrix(box.Center()) * tm;
		if (ltm.IsIdentity())
Tazpn's avatar
Tazpn committed
		{
			retval = StaticCast<bhkShape>(shape);
		}
		else
		{
Tazpn's avatar
Tazpn committed
			ltm.SetTrans(ltm.GetTrans() / Exporter::bhkScaleFactor);

Tazpn's avatar
Tazpn committed
			bhkTransformShapeRef transform = new bhkTransformShape();
Tazpn's avatar
Tazpn committed
			transform->SetTransform(TOMATRIX4(ltm).Transpose());
Tazpn's avatar
Tazpn committed
			transform->SetShape(shape);
			transform->SetMaterial(HavokMaterial(mtl));
			retval = StaticCast<bhkShape>(transform);
		}
	}
	return retval;
}

bhkShapeRef	Exporter::makeProxySphereShape(INode *node, Object *obj, Mesh& mesh, Matrix3& tm)
{
	enum { list_params, bv_mesh, };  // pblock2 ID
	enum { PB_MATERIAL, PB_MESHLIST, PB_BOUND_TYPE, PB_CENTER, };
	enum { bv_type_none, bv_type_box, bv_type_shapes, bv_type_packed, bv_type_convex, };  // pblock ID

	bhkShapeRef shape;
	if (IParamBlock2* pblock2 = obj->GetParamBlockByID(list_params))
	{
		//Matrix3 tm = GetLocalTM(node) * TransMatrix(box.Center());

	}
	return shape;
}

bhkShapeRef	Exporter::makeProxyConvexShape(INode *node, Object *obj, Mesh& mesh, Matrix3& tm)
{
	enum { list_params, bv_mesh, };  // pblock2 ID
	enum { PB_MATERIAL, PB_MESHLIST, PB_BOUND_TYPE, PB_CENTER, };
	enum { bv_type_none, bv_type_box, bv_type_shapes, bv_type_packed, bv_type_convex, };  // pblock ID

	bhkShapeRef shape;
	if (IParamBlock2* pblock2 = obj->GetParamBlockByID(list_params))
	{
		if (bhkConvexVerticesShapeRef convShape = makeConvexShape(mesh, tm))
		{
			int mtl = pblock2->GetInt(PB_MATERIAL, 0, 0);
			convShape->SetMaterial(HavokMaterial(mtl));
			shape = StaticCast<bhkShape>(convShape);
		}
	}
	return shape;
}

bhkShapeRef	Exporter::makeProxyTriStripShape(INode *node, Object *obj, Mesh& mesh, Matrix3& tm)
{
	enum { list_params, bv_mesh, };  // pblock2 ID
	enum { PB_MATERIAL, PB_MESHLIST, PB_BOUND_TYPE, PB_CENTER, };
	enum { bv_type_none, bv_type_box, bv_type_shapes, bv_type_packed, bv_type_convex, };  // pblock ID

	bhkShapeRef shape;
	if (IParamBlock2* pblock2 = obj->GetParamBlockByID(list_params))
	{
		int mtl = pblock2->GetInt(PB_MATERIAL, 0, 0);

Tazpn's avatar
Tazpn committed
		// Transform location
		Mesh localmesh(mesh);
		MNMesh tmpMesh(localmesh);
		tmpMesh.Transform(tm);
		tmpMesh.buildNormals();
		tmpMesh.OutToTri(localmesh);
		localmesh.buildNormals();

Tazpn's avatar
Tazpn committed
		Matrix3 ident(true);
Tazpn's avatar
Tazpn committed
		bhkNiTriStripsShapeRef trishape = makeTriStripsShape(localmesh, ident);
Tazpn's avatar
Tazpn committed
		trishape->SetMaterial(HavokMaterial(mtl));

		shape = StaticCast<bhkShape>(trishape);
	}
	return shape;
}

bhkShapeRef	Exporter::makeProxyPackedTriStripShape(INode *node, Object *obj, Mesh& mesh, Matrix3& tm)
{
	enum { list_params, bv_mesh, };  // pblock2 ID
	enum { PB_MATERIAL, PB_MESHLIST, PB_BOUND_TYPE, PB_CENTER, };
	enum { bv_type_none, bv_type_box, bv_type_shapes, bv_type_packed, bv_type_convex, };  // pblock ID

	bhkShapeRef shape;
	if (IParamBlock2* pblock2 = obj->GetParamBlockByID(list_params))
	{
		int mtl = pblock2->GetInt(PB_MATERIAL, 0, 0);

Tazpn's avatar
Tazpn committed
		// Transform location
		Mesh localmesh(mesh);
		MNMesh tmpMesh(localmesh);
		tmpMesh.Transform(tm);
		tmpMesh.buildNormals();
		tmpMesh.OutToTri(localmesh);
		localmesh.buildNormals();

Tazpn's avatar
Tazpn committed
		Matrix3 ident(true);
Tazpn's avatar
Tazpn committed
		bhkPackedNiTriStripsShapeRef trishape = makePackedTriStripsShape(localmesh, ident);
		if ( TheHavokCode.Initialize() )
			shape = StaticCast<bhkShape>( makeTreeShape(trishape) );
		else
			shape = StaticCast<bhkShape>(trishape);
Tazpn's avatar
Tazpn committed
	}
	return shape;
}

Tazpn's avatar
Tazpn committed
bhkShapeRef	Exporter::makeModifierShape(INode *node, Object* obj, Modifier* mod, Matrix3& tm)
{
	enum { havok_params };
	enum { PB_BOUND_TYPE, PB_MATERIAL, };
Tazpn's avatar
Tazpn committed
	enum { bv_type_none, bv_type_box, bv_type_sphere, bv_type_capsule, bv_type_shapes, bv_type_convex, bv_type_packed, };  // pblock ID
Tazpn's avatar
Tazpn committed

	bhkShapeRef shape;

	const Mesh* mesh = NULL;
	int material = NP_DEFAULT_HVK_MATERIAL;
	int type = bv_type_none;

Tazpn's avatar
Tazpn committed
	node->EvalWorldState(0);
Tazpn's avatar
Tazpn committed
	if (bhkHelperInterface* bhkHelp = (bhkHelperInterface*)mod->GetInterface(BHKHELPERINTERFACE_DESC))
	{
		mesh = bhkHelp->GetMesh();
	}
	else
	{
		if (TriObject *tri = (TriObject *)obj->ConvertToType(0, Class_ID(TRIOBJ_CLASS_ID, 0)))
		{
			mesh = &tri->GetMesh();
		}
	}
	if (IParamBlock2* pblock2 = mod->GetParamBlockByID(havok_params))
	{
		pblock2->GetValue(PB_BOUND_TYPE, 0, type, FOREVER, 0);
		pblock2->GetValue(PB_MATERIAL, 0, material, FOREVER, 0);
	}
	switch (type)
	{
	default:
	case bv_type_none:
		break;

	case bv_type_box:
Tazpn's avatar
Tazpn committed
		shape = makeModBoxShape(node, mod, const_cast<Mesh&>(*mesh), tm);
Tazpn's avatar
Tazpn committed
		break;

	case bv_type_sphere:
Tazpn's avatar
Tazpn committed
		shape = makeModSphereShape(node, mod, const_cast<Mesh&>(*mesh), tm);
Tazpn's avatar
Tazpn committed
		break;

	case bv_type_capsule:
Tazpn's avatar
Tazpn committed
		shape = makeModCapsuleShape(node, mod, const_cast<Mesh&>(*mesh), tm);
Tazpn's avatar
Tazpn committed
		break;

	case bv_type_shapes:
Tazpn's avatar
Tazpn committed
		shape = makeModTriStripShape(node, mod, const_cast<Mesh&>(*mesh), tm);
Tazpn's avatar
Tazpn committed
		break;

	case bv_type_convex:
Tazpn's avatar
Tazpn committed
		shape = makeModConvexShape(node, mod, const_cast<Mesh&>(*mesh), tm);
		break;
Tazpn's avatar
Tazpn committed

	case bv_type_packed:
		shape = makeModPackedTriStripShape(node, mod, const_cast<Mesh&>(*mesh), tm);
		break;
Tazpn's avatar
Tazpn committed
	}
	return shape;
}

bhkShapeRef	Exporter::makeModBoxShape(INode *node, Modifier* mod, Mesh& mesh, Matrix3& tm)
{
	enum { havok_params };
	enum { PB_BOUND_TYPE, PB_MATERIAL, };
Tazpn's avatar
Tazpn committed
	enum { bv_type_none, bv_type_box, bv_type_sphere, bv_type_capsule, bv_type_shapes, bv_type_convex, bv_type_packed, };  // pblock ID
Tazpn's avatar
Tazpn committed
	int material = NP_DEFAULT_HVK_MATERIAL;

	if (IParamBlock2* pblock2 = mod->GetParamBlockByID(havok_params))
	{
		pblock2->GetValue(PB_MATERIAL, 0, material, FOREVER, 0);
	}

	bhkShapeRef retval;
	if (bhkBoxShapeRef shape = new bhkBoxShape())
	{
		Box3 box; box.Init();
		CalcAxisAlignedBox(mesh, box, NULL);

		Vector3 dim(box.Max().x-box.Min().x, box.Max().y-box.Min().y, box.Max().z-box.Min().z);
		dim /= (Exporter::bhkScaleFactor * 2);
		shape->SetDimensions(dim);
		shape->SetMaterial(HavokMaterial(material));

Tazpn's avatar
Tazpn committed
		Matrix3 ltm = TransMatrix(box.Center()) * node->GetNodeTM(0) * tm;
		if (ltm.IsIdentity())
Tazpn's avatar
Tazpn committed
		{
Tazpn's avatar
Tazpn committed
			retval = StaticCast<bhkShape>(shape);
		}
		else
		{
Tazpn's avatar
Tazpn committed
			ltm.SetTrans(ltm.GetTrans() / Exporter::bhkScaleFactor);

Tazpn's avatar
Tazpn committed
			bhkTransformShapeRef transform = new bhkTransformShape();
Tazpn's avatar
Tazpn committed
			transform->SetTransform(TOMATRIX4(ltm).Transpose());
Tazpn's avatar
Tazpn committed
			transform->SetShape(shape);
			transform->SetMaterial(HavokMaterial(material));
			retval = StaticCast<bhkShape>(transform);
		}
	}
	return retval;
}

bhkShapeRef	Exporter::makeModSphereShape(INode *node, Modifier* mod, Mesh& mesh, Matrix3& tm)
{
	enum { havok_params };
	enum { PB_BOUND_TYPE, PB_MATERIAL, };
Tazpn's avatar
Tazpn committed
	enum { bv_type_none, bv_type_box, bv_type_sphere, bv_type_capsule, bv_type_shapes, bv_type_convex, bv_type_packed, };  // pblock ID
Tazpn's avatar
Tazpn committed
	int material = NP_DEFAULT_HVK_MATERIAL;

	if (IParamBlock2* pblock2 = mod->GetParamBlockByID(havok_params))
	{
		pblock2->GetValue(PB_MATERIAL, 0, material, FOREVER, 0);
	}

	bhkShapeRef retval;

	Point3 center = Point3::Origin;
	float radius = 0.0f;
	CalcCenteredSphere(mesh, center, radius);

	if (bhkSphereShapeRef shape = new bhkSphereShape())
	{
		shape->SetRadius(radius / Exporter::bhkScaleFactor);
		shape->SetMaterial(HavokMaterial(material));

Tazpn's avatar
Tazpn committed
		Matrix3 ltm = TransMatrix(center) * node->GetObjTMAfterWSM(0) * tm;
		if (ltm.IsIdentity())
Tazpn's avatar
Tazpn committed
		{
			retval = StaticCast<bhkShape>(shape);
		}
		else
		{
Tazpn's avatar
Tazpn committed
			ltm.SetTrans(ltm.GetTrans() / Exporter::bhkScaleFactor);

Tazpn's avatar
Tazpn committed
			bhkTransformShapeRef transform = new bhkTransformShape();
Tazpn's avatar
Tazpn committed
			transform->SetTransform(TOMATRIX4(ltm).Transpose());
Tazpn's avatar
Tazpn committed
			transform->SetShape(shape);
			transform->SetMaterial(HavokMaterial(material));
			retval = StaticCast<bhkShape>(transform);
Tazpn's avatar
Tazpn committed
		}
Tazpn's avatar
Tazpn committed
	}
	return retval;
}

bhkShapeRef	Exporter::makeModCapsuleShape(INode *node, Modifier* mod, Mesh& mesh, Matrix3& tm)
{
	enum { havok_params };
	enum { PB_BOUND_TYPE, PB_MATERIAL, };
Tazpn's avatar
Tazpn committed
	enum { bv_type_none, bv_type_box, bv_type_sphere, bv_type_capsule, bv_type_shapes, bv_type_convex, bv_type_packed, };  // pblock ID
Tazpn's avatar
Tazpn committed
	int material = NP_DEFAULT_HVK_MATERIAL;

Tazpn's avatar
Tazpn committed
	node->EvalWorldState(0);
Tazpn's avatar
Tazpn committed
	if (IParamBlock2* pblock2 = mod->GetParamBlockByID(havok_params))
	{
		pblock2->GetValue(PB_MATERIAL, 0, material, FOREVER, 0);
	}

	bhkShapeRef retval;

	Point3 center = Point3::Origin;
	float radius = 0.0f;
	CalcCenteredSphere(mesh, center, radius);

	if (bhkCapsuleShapeRef shape = new bhkCapsuleShape())
	{
		shape->SetRadius(radius / Exporter::bhkScaleFactor);
		shape->SetRadius1(radius / Exporter::bhkScaleFactor);
		shape->SetRadius2(radius / Exporter::bhkScaleFactor);
		shape->SetFirstPoint(TOVECTOR3(center/Exporter::bhkScaleFactor));
		shape->SetSecondPoint(TOVECTOR3(center/Exporter::bhkScaleFactor));
		shape->SetMaterial(HavokMaterial(material));

Tazpn's avatar
Tazpn committed
		Matrix3 ltm = TransMatrix(center) * node->GetObjTMAfterWSM(0) * tm;
		if (ltm.IsIdentity())
Tazpn's avatar
Tazpn committed
		{
			retval = StaticCast<bhkShape>(shape);
		}
		else
		{
Tazpn's avatar
Tazpn committed
			ltm.SetTrans(ltm.GetTrans() / Exporter::bhkScaleFactor);

Tazpn's avatar
Tazpn committed
			bhkTransformShapeRef transform = new bhkTransformShape();
Tazpn's avatar
Tazpn committed
			transform->SetTransform(TOMATRIX4(ltm).Transpose());
Tazpn's avatar
Tazpn committed
			transform->SetShape(shape);
			transform->SetMaterial(HavokMaterial(material));
			retval = StaticCast<bhkShape>(transform);
		}
	}
	return retval;
}

bhkShapeRef	Exporter::makeModConvexShape(INode *node, Modifier* mod, Mesh& mesh, Matrix3& tm)
{
	enum { havok_params };
	enum { PB_BOUND_TYPE, PB_MATERIAL, };
Tazpn's avatar
Tazpn committed
	enum { bv_type_none, bv_type_box, bv_type_sphere, bv_type_capsule, bv_type_shapes, bv_type_convex, bv_type_packed, };  // pblock ID
Tazpn's avatar
Tazpn committed
	int material = NP_DEFAULT_HVK_MATERIAL;

Tazpn's avatar
Tazpn committed
	Matrix3 ltm = node->GetObjTMAfterWSM(0) * tm;

Tazpn's avatar
Tazpn committed
	bhkShapeRef shape;
Tazpn's avatar
Tazpn committed
	if (bhkConvexVerticesShapeRef convShape = makeConvexShape(mesh, ltm))
Tazpn's avatar
Tazpn committed
	{
		if (IParamBlock2* pblock2 = mod->GetParamBlockByID(havok_params))
		{
			pblock2->GetValue(PB_MATERIAL, 0, material, FOREVER, 0);
		}
		convShape->SetMaterial(HavokMaterial(material));
		shape = StaticCast<bhkShape>(convShape);
	}
	return shape;
}

bhkShapeRef	Exporter::makeModTriStripShape(INode *node, Modifier* mod, Mesh& mesh, Matrix3& tm)
{
	enum { havok_params };
	enum { PB_BOUND_TYPE, PB_MATERIAL, };
Tazpn's avatar
Tazpn committed
	enum { bv_type_none, bv_type_box, bv_type_sphere, bv_type_capsule, bv_type_shapes, bv_type_convex, bv_type_packed, };  // pblock ID
Tazpn's avatar
Tazpn committed
	int material = NP_DEFAULT_HVK_MATERIAL;

Tazpn's avatar
Tazpn committed
	Matrix3 ltm = node->GetObjTMAfterWSM(0) * tm;

Tazpn's avatar
Tazpn committed
	bhkShapeRef shape;
Tazpn's avatar
Tazpn committed
	if (bhkNiTriStripsShapeRef trishape = makeTriStripsShape(mesh, ltm))
Tazpn's avatar
Tazpn committed
	{
		trishape->SetMaterial(HavokMaterial(material));
		if (IParamBlock2* pblock2 = mod->GetParamBlockByID(havok_params))
		{
			pblock2->GetValue(PB_MATERIAL, 0, material, FOREVER, 0);
		}
		shape = StaticCast<bhkShape>(trishape);
Tazpn's avatar
Tazpn committed
	}
	return shape;
Tazpn's avatar
Tazpn committed
}

bhkShapeRef	Exporter::makeModPackedTriStripShape(INode *node, Modifier* mod, Mesh& mesh, Matrix3& tm)
{
Tazpn's avatar
Tazpn committed
	enum { havok_params };
	enum { PB_BOUND_TYPE, PB_MATERIAL, };
	enum { bv_type_none, bv_type_box, bv_type_sphere, bv_type_capsule, bv_type_shapes, bv_type_convex, bv_type_packed, };  // pblock ID
	int material = NP_DEFAULT_HVK_MATERIAL;

	Matrix3 ltm = node->GetObjTMAfterWSM(0) * tm;

	bhkShapeRef shape;
	if (bhkPackedNiTriStripsShapeRef trishape = makePackedTriStripsShape(mesh, ltm))
	{
		//trishape->SetMaterial(HavokMaterial(material));
		if (IParamBlock2* pblock2 = mod->GetParamBlockByID(havok_params))
		{
			pblock2->GetValue(PB_MATERIAL, 0, material, FOREVER, 0);
		}

		if ( TheHavokCode.Initialize() )
			shape = StaticCast<bhkShape>( makeTreeShape(trishape) );
		else
			shape = StaticCast<bhkShape>(trishape);
Tazpn's avatar
Tazpn committed
	}
	return shape;
Tazpn's avatar
Tazpn committed
}