/// output: a new motion primitive
void BVHMotionDatabaseCompiler::CreateMotionPrimitive( MotionPrimitiveDesc& desc,
												        const MotionPrimitiveDescGroup& desc_group,
												        BVHPlayer& bvh_player )
{
	vector<Keyframe> vecKeyframe;
	vecKeyframe.reserve( 64 );

	// add a new motion primitive and get the reference to it
	shared_ptr<MotionPrimitive> pMotion( new MotionPrimitive( desc.m_Name ) );
	m_pvecpMotionPrimitive->push_back( pMotion );
	desc.m_pMotionPrimitive = pMotion;
	MotionPrimitive& motion = *(m_pvecpMotionPrimitive->back());

	motion.SetSkeleton( desc_group.m_Skeleton );

	motion.SetLoopedMotion( desc.m_bIsLoopMotion );
	motion.SetStartsBoneName( desc.m_StartBoneName );

	vecKeyframe.resize(0);

	int i = 0;
	int frame = 0;
	int start = desc.m_StartFrame;
	int end   = desc.m_EndFrame;
	float time_per_frame = bvh_player.GetFrameTime();

	// create keyframes
	for( frame = start, i=0;
		 frame <= end;
		 frame++,       i++ )
	{
		float frame_time = (float)frame * time_per_frame;

		bvh_player.SetWorldTransformation( frame_time );

		// extract world transforms

		BVHBone *pRootBone = bvh_player.GetRootBone();

		// create keyframe from frametime and transformation hierarchy
		vecKeyframe.push_back( Keyframe( (float)i * time_per_frame, TransformNode( *pRootBone ) ) );
	}

	// modify root position
	if( 0 < vecKeyframe.size() && desc.m_bResetHorizontalRootPos )
	{
		Matrix34 root_pose = vecKeyframe[0].GetRootPose();
		Vector3 vBasePosH = root_pose.vPosition;
		vBasePosH.y = 0;

		BOOST_FOREACH( Keyframe& keyframe, vecKeyframe )
		{
			root_pose = keyframe.GetRootPose();
			root_pose.vPosition = root_pose.vPosition - vBasePosH;
			keyframe.SetRootPose( root_pose );
		}
Ejemplo n.º 2
0
void CCharShape::AdjustOrientation (CControl *ctrl, double dtime,
		 double dist_from_surface, TVector3 surf_nml) {
    TVector3 new_x, new_y, new_z; 
    TMatrix cob_mat, inv_cob_mat;
    TMatrix rot_mat;
    TQuaternion new_orient;
    double time_constant;
    static TVector3 minus_z_vec = { 0, 0, -1};
    static TVector3 y_vec = { 0, 1, 0 };

    if  (dist_from_surface > 0) {
		new_y = ScaleVector (1, ctrl->cvel);
		NormVector (&new_y);
		new_z = ProjectToPlane (new_y, MakeVector(0, -1, 0));
		NormVector (&new_z);
		new_z = AdjustRollvector (ctrl, ctrl->cvel, new_z);
    } else { 
		new_z = ScaleVector (-1, surf_nml);
		new_z = AdjustRollvector (ctrl, ctrl->cvel, new_z);
		new_y = ProjectToPlane (surf_nml, ScaleVector (1, ctrl->cvel));
		NormVector(&new_y);
    }

    new_x = CrossProduct (new_y, new_z);
    MakeBasismatrix_Inv (cob_mat, inv_cob_mat, new_x, new_y, new_z);
    new_orient = MakeQuaternionFromMatrix (cob_mat);

    if (!ctrl->orientation_initialized) {
		ctrl->orientation_initialized = true;
		ctrl->corientation = new_orient;
    }

    time_constant = dist_from_surface > 0 ? TO_AIR_TIME : TO_TIME;

    ctrl->corientation = InterpolateQuaternions (
			ctrl->corientation, new_orient, 
			min (dtime / time_constant, 1.0));

    ctrl->plane_nml = RotateVector (ctrl->corientation, minus_z_vec);
    ctrl->cdirection = RotateVector (ctrl->corientation, y_vec);
    MakeMatrixFromQuaternion (cob_mat, ctrl->corientation);

    // Trick rotations 
    new_y = MakeVector (cob_mat[1][0], cob_mat[1][1], cob_mat[1][2]); 
    RotateAboutVectorMatrix (rot_mat, new_y, (ctrl->roll_factor * 360));
    MultiplyMatrices (cob_mat, rot_mat, cob_mat);
    new_x = MakeVector (cob_mat[0][0], cob_mat[0][1], cob_mat[0][2]); 
    RotateAboutVectorMatrix (rot_mat, new_x, ctrl->flip_factor * 360);
    MultiplyMatrices (cob_mat, rot_mat, cob_mat);

    TransposeMatrix (cob_mat, inv_cob_mat);
    TransformNode (0, cob_mat, inv_cob_mat); 
}
Ejemplo n.º 3
0
void CCharShape::AdjustOrientation (CControl *ctrl, bool eps,
                                    ETR_DOUBLE dist_from_surface, const TVector3d& surf_nml) {
	TVector3d new_y, new_z;
	static const TVector3d minus_z_vec(0, 0, -1);
	static const TVector3d y_vec(0, 1, 0);

	if (dist_from_surface > 0) {
		new_y = ctrl->cvel;
		new_y.Norm();
		new_z = ProjectToPlane (new_y, TVector3d(0, -1, 0));
		new_z.Norm();
		new_z = AdjustRollvector (ctrl, ctrl->cvel, new_z);
	} else {
		new_z = -1.0 * surf_nml;
		new_z = AdjustRollvector (ctrl, ctrl->cvel, new_z);
		new_y = ProjectToPlane (surf_nml, ctrl->cvel);
		new_y.Norm();
	}

	TVector3d new_x = CrossProduct (new_y, new_z);
	TMatrix<4, 4> cob_mat(new_x, new_y, new_z);
	TQuaternion new_orient = MakeQuaternionFromMatrix (cob_mat);

	if (!ctrl->orientation_initialized) {
		ctrl->orientation_initialized = true;
		ctrl->corientation = new_orient;
	}

	ETR_DOUBLE time_constant = dist_from_surface > 0 ? TO_AIR_TIME : TO_TIME;

	float dtime = eps ? EPS : g_game.time_step;
	ctrl->corientation = InterpolateQuaternions (
	                         ctrl->corientation, new_orient,
	                         min (dtime / time_constant, 1.0));

	ctrl->plane_nml = RotateVector (ctrl->corientation, minus_z_vec);
	ctrl->cdirection = RotateVector (ctrl->corientation, y_vec);
	cob_mat = MakeMatrixFromQuaternion(ctrl->corientation);

	// Trick rotations
	new_y = TVector3d (cob_mat[1][0], cob_mat[1][1], cob_mat[1][2]);
	TMatrix<4, 4> rot_mat = RotateAboutVectorMatrix(new_y, (ctrl->roll_factor * 360));
	cob_mat = rot_mat * cob_mat;
	new_x = TVector3d (cob_mat[0][0], cob_mat[0][1], cob_mat[0][2]);
	rot_mat = RotateAboutVectorMatrix (new_x, ctrl->flip_factor * 360);
	cob_mat = rot_mat * cob_mat;

	TransformNode (0, cob_mat, cob_mat.GetTransposed());
}
DisplayItemTransformTree::DisplayItemTransformTree()
{
    // There is always a root node.
    m_nodes.append(TransformNode(kInvalidIndex, SkMatrix44::kIdentity_Constructor));
    ASSERT(m_nodes[0].isRoot());
}
Ejemplo n.º 5
0
/*
int MshExp::ExportMeshTiles(Interface* ip,INode* node,TimeValue t_start,TimeValue t_end,int tiles_count)
{
	// Get animation range
	Interval animRange = ip->GetAnimRange();

	TimeValue curtime;

	curtime = animRange.Start();
}
*/
int MshExp::ExportMesh(const TCHAR *filename,Interface* i,INode* node,TimeValue curtime)
{
	char buf[4096];
	void* cur=buf;
	void* end=buf+sizeof(buf);

	// Verifie les arguments
	if(node==0){
		NP_LOG("ExportMesh: !node");
		return 0;
	}
	
	/*-----------------------------------------------------
		Obtient l'objet Mesh
	-----------------------------------------------------*/

	// Frame a exporter
	if(curtime<0)
		curtime = i->GetTime();
	
	// Obtient la matrice de transformation
	Matrix3 tm = TransformNode(node,curtime,this->exportTransform);

	// Evalue l'objet a partir du noeud
	ObjectState os = node->EvalWorldState(curtime);
	if(!os.obj || os.obj->SuperClassID()!=GEOMOBJECT_CLASS_ID){
		NP_LOG("ExportMesh: !ObjectState");
		return 0;
	}
	Interval objRange = os.obj->ObjectValidity(curtime);

	// convertie l'objet en Tiangles
	BOOL needDel;
	TriObject* tri = GetTriObjectFromNode(node, curtime, needDel);
	if (!tri) {
		NP_LOG("ExportMesh: !TriObject");
		return 0;
	}
	// obtient le maillage
	Mesh* mesh = &tri->GetMesh();

	// obtient le nombre de points
	uint npoint=(uint)mesh->getNumVerts();

	// construit les normals
	mesh->buildNormals();

	// obtient le nombre de faces
	uint nfaces=(uint)mesh->getNumFaces();
	
	//Supprime l'objet TriObject, si necessaire
	if(needDel) {
		delete tri;
	}

	/*-----------------------------------------------------
		Exporte les triangles
	-----------------------------------------------------*/
	
	//Obtient la matrice de transformation
	tm = TransformNode(node,curtime,this->exportTransform);

	// Evalue l'objet a partir du node
	os = node->EvalWorldState(curtime);
	objRange = os.obj->ObjectValidity(curtime);

	// convertie l'objet en Tiangles
	tri = GetTriObjectFromNode(node, curtime, needDel);
	if (!tri) {
		NP_LOG("ExportMesh: !TriObject");
		return 0;
	}
	// obtient le maillage
	mesh = &tri->GetMesh();
	// construit les normals
	mesh->buildNormals();

	// copie les faces
	for(uint y=0; y<nfaces; y++)
	{
		Face* face;
		uint facenumber=0;//numero de la face dans l'index du maillage 3DS

		//obtient la face en cours
		face = &mesh->faces[y];
		facenumber = y;
		
		//////////////////////////////////////////////
		// Definit les 3 points de la face
		//////////////////////////////////////////////
		for(uint w=0;w<3;w++)
		{
			if((cur = descWriteText(cur,end,"point")==0)
				return 0;

			Point3 pos;
			Point3 nml;
			Point3 clr;
			UVVert tex;
			uint nvertex;

			// obtient le point
			nvertex = face->v[w];
			pos = tm * mesh->verts[nvertex];
			float point[3]={pos.x,pos.y,pos.z};
			if((cur = descWriteStruct(cur,end,Point3_t,sizeof(Point3_t)/sizeof(Point3_t[0]),&point))==0)
				return 0;

			// obtient les normals
			nvertex = face->getVert(w);
			nml = GetVertexNormal(mesh, face, mesh->getRVertPtr(nvertex));
			float normal[3]={nml.x,nml.y,nml.z};
			if((cur = descWriteStruct(cur,end,Normal_t,sizeof(Normal_t)/sizeof(Normal_t[0]),&normal))==0)
				return 0;

			// obtient les coordonnees de texture
			if(mesh->tvFace){
				nvertex = mesh->tvFace[facenumber].t[w];
				tex = mesh->tVerts[nvertex];
				float textureMap[3]={tex.x,tex.y,tex.z};
				if((cur = descWriteStruct(cur,end,TextureMap_t,sizeof(TextureMap_t)/sizeof(TextureMap_t[0]),&textureMap))==0)
					return 0;
			}

			// obtient la couleur
			if(mesh->vcFace){
				nvertex = mesh->vcFace[facenumber].t[w];
				clr = mesh->vertCol[nvertex];
				float color[3]={clr.x,clr.y,clr.z};
				if((cur = descWriteStruct(cur,end,Color_t,sizeof(Color_t)/sizeof(Color_t[0]),&color))==0)
					return 0;
			}
		}
	}

	//Supprime l'objet TriObject, si necessaire
	if(needDel) {
		delete tri;
	}
	
	/* ecrit les donnees de la structure */
	if(!fileWrite(filename,0,buf,(char*)cur))
		return 0;
	
	NP_LOG("done");

	return 1;
}