2
0
Эх сурвалжийг харах

Skeleton animations now reference bones by bone name, not by index. (WARNING: Skeleton animation format has changed, animations need to be reimported). Fixed a bug in polyimport where the skeleton wouldn't get exported if a prefix wasn't specified

Ivan Safrin 12 жил өмнө
parent
commit
74e0c2c4e3

+ 77 - 75
Core/Contents/Source/PolySkeleton.cpp

@@ -250,86 +250,88 @@ void Skeleton::stopAnimation(SkeletonAnimation *animation) {
 }
 
 void Skeleton::addAnimation(const String& name, const String& fileName) {
-	OSFILE *inFile = OSBasics::open(fileName.c_str(), "rb");
-	if(!inFile) {
+
+    OSFILE *inFile = OSBasics::open(fileName.c_str(), "rb");
+    
+    if(!inFile) {
 		return;
 	}
 	
-		unsigned int activeBones,boneIndex,numPoints,numCurves, curveType;	
-		float length;
-		OSBasics::read(&length, 1, sizeof(float), inFile);
-    
-		SkeletonAnimation *newAnimation = new SkeletonAnimation(name, length);
-    
-        printf("LOADING %s\n", name.c_str());
+    unsigned int activeBones,numPoints,numCurves, curveType;
+    float length;
+    OSBasics::read(&length, 1, sizeof(float), inFile);
+
+    SkeletonAnimation *newAnimation = new SkeletonAnimation(name, length);
+    OSBasics::read(&activeBones, sizeof(unsigned int), 1, inFile);
     
-		OSBasics::read(&activeBones, sizeof(unsigned int), 1, inFile);
-		
-		//	Logger::log("activeBones: %d\n", activeBones);		
-		for(int j=0; j < activeBones; j++) {
-			OSBasics::read(&boneIndex, sizeof(unsigned int), 1, inFile);
-            if(boneIndex > bones.size()-1) {
-                printf("WARNING: INCORRECT BONE INDEX!\n");
-                continue;
-            }
-			BoneTrack *newTrack = new BoneTrack(bones[boneIndex], length);
-			
-            printf("TRACK FOR BONE %s\n", bones[boneIndex]->getName().c_str());
+    unsigned short boneNameLen;
+    char boneNameBuffer[1024];
+
+    for(int j=0; j < activeBones; j++) {
+        
+        OSBasics::read(&boneNameLen, sizeof(unsigned short), 1, inFile);
+        OSBasics::read(boneNameBuffer, 1, boneNameLen, inFile);
+        boneNameBuffer[boneNameLen] = '\0';
+        
+        Bone *trackBone = getBoneByName(boneNameBuffer);
+        if(!trackBone) {
+            printf("WARNING, INVALID BONE NAME: %s\n", boneNameBuffer);
+            continue;
+        }
+        
+        BoneTrack *newTrack = new BoneTrack(trackBone, length);
+        
+        BezierCurve *curve;
+        float vec1[2];
+        
+        OSBasics::read(&numCurves, sizeof(unsigned int), 1, inFile);
+        for(int l=0; l < numCurves; l++) {
+            curve = new BezierCurve();
+            OSBasics::read(&curveType, sizeof(unsigned int), 1, inFile);
+            OSBasics::read(&numPoints, sizeof(unsigned int), 1, inFile);
             
-			BezierCurve *curve;
-			float vec1[2]; //,vec2[2],vec3[2];
-			
-			OSBasics::read(&numCurves, sizeof(unsigned int), 1, inFile);
-			//			Logger::log("numCurves: %d\n", numCurves);
-            printf("numCurves: %d\n", numCurves);
-			for(int l=0; l < numCurves; l++) {
-				curve = new BezierCurve();
-				OSBasics::read(&curveType, sizeof(unsigned int), 1, inFile);
-				OSBasics::read(&numPoints, sizeof(unsigned int), 1, inFile);
-				for(int k=0; k < numPoints; k++) {
-					OSBasics::read(vec1, sizeof(float), 2, inFile);					
-					curve->addControlPoint2d(vec1[1], vec1[0]);
-					//					curve->addControlPoint(vec1[1]-10, vec1[0], 0, vec1[1], vec1[0], 0, vec1[1]+10, vec1[0], 0);
-				}
-				switch(curveType) {
-					case 0:
-						newTrack->scaleX = curve;
-						break;
-					case 1:
-						newTrack->scaleY = curve;
-						break;
-					case 2:
-						newTrack->scaleZ = curve;					
-						break;
-					case 3:
-						newTrack->QuatW = curve;					
-						break;
-					case 4:
-						newTrack->QuatX = curve;					
-						break;
-					case 5:
-						newTrack->QuatY = curve;					
-						break;
-					case 6:
-						newTrack->QuatZ = curve;					
-						break;
-					case 7:;
-						newTrack->LocX = curve;					
-						break;
-					case 8:
-						newTrack->LocY = curve;					
-						break;
-					case 9:
-						newTrack->LocZ = curve;					
-						break;
-				}
-			}
-			newTrack->initTweens();
-			newAnimation->addBoneTrack(newTrack);
-		}
-		animations.push_back(newAnimation);
-	
-	
+            for(int k=0; k < numPoints; k++) {
+                OSBasics::read(vec1, sizeof(float), 2, inFile);					
+                curve->addControlPoint2d(vec1[1], vec1[0]);
+            }
+            switch(curveType) {
+                case 0:
+                    newTrack->scaleX = curve;
+                    break;
+                case 1:
+                    newTrack->scaleY = curve;
+                    break;
+                case 2:
+                    newTrack->scaleZ = curve;					
+                    break;
+                case 3:
+                    newTrack->QuatW = curve;					
+                    break;
+                case 4:
+                    newTrack->QuatX = curve;					
+                    break;
+                case 5:
+                    newTrack->QuatY = curve;					
+                    break;
+                case 6:
+                    newTrack->QuatZ = curve;					
+                    break;
+                case 7:;
+                    newTrack->LocX = curve;					
+                    break;
+                case 8:
+                    newTrack->LocY = curve;					
+                    break;
+                case 9:
+                    newTrack->LocZ = curve;					
+                    break;
+            }
+        }
+        newTrack->initTweens();
+        newAnimation->addBoneTrack(newTrack);
+    }
+    
+    animations.push_back(newAnimation);
 	OSBasics::close(inFile);	
 }
 

+ 15 - 10
Tools/Contents/polyimport/Include/polyimport.h

@@ -30,11 +30,9 @@ class IBone {
 
 class ITrack {
 	public:
-		ITrack(){ boneID = 666; }
-		unsigned int boneID;
-
-	aiNodeAnim *nodeAnim;
-		
+		ITrack(){}
+        aiNodeAnim *nodeAnim;
+        Polycode::String boneName;
 };
 
 class IAnimation {
@@ -76,7 +74,7 @@ class ISkeleton {
 
 	void saveToFile(const char *fileName, bool swapZY) {
 		using Polycode::String;
-		String fileNameSkel = String(fileName)+".skeleton";
+		String fileNameSkel = String(fileName);
 		FILE *file = fopen(fileNameSkel.c_str(), "wb");
 		unsigned int numBones = bones.size();
 		
@@ -90,7 +88,7 @@ class ISkeleton {
 		for(int i=0; i < animations.size(); i++) {
 			IAnimation *anim = animations[i];
 			for(int j=0; j < anim->numTracks; j++) {
-				anim->tracks[j]->boneID = this->getBoneID(anim->tracks[j]->nodeAnim->mNodeName);
+				anim->tracks[j]->boneName = anim->tracks[j]->nodeAnim->mNodeName.data;
 			}
 		}
 
@@ -184,7 +182,8 @@ class ISkeleton {
 
 	//	unsigned int numAnimations = animations.size();
 	//	fwrite(&numAnimations, sizeof(unsigned int), 1, file);
-
+        
+        
 		for(int i=0; i < animations.size(); i++) {
 
 			char anim_s[2];
@@ -205,8 +204,14 @@ class ISkeleton {
             
 			for(int j=0; j < anim->numTracks; j++) {
 				ITrack *track = anim->tracks[j];
-				fwrite(&track->boneID, sizeof(unsigned int), 1, file);
-				
+                
+                unsigned short boneNameLen = track->boneName.length();
+				fwrite(&boneNameLen, sizeof(unsigned short), 1, file);
+                
+                char boneNameBuffer[1024];
+                memcpy(boneNameBuffer, track->boneName.c_str(), boneNameLen);
+                fwrite(boneNameBuffer, 1, boneNameLen, file);
+                
 				aiNodeAnim *nodeAnim = track->nodeAnim;
 
 				unsigned int curveType,numPoints;

+ 8 - 3
Tools/Contents/polyimport/Source/polyimport.cpp

@@ -193,7 +193,12 @@ int exportToFile(String prefix, bool swapZY, bool addSubmeshes, bool listOnly) {
 			printf("Mesh has weights, exporting skeleton...\n");
 		}	
 		
-		String fileNameSkel = prefix+".skeleton";
+		String fileNameSkel;
+        if(prefix != "") {
+            fileNameSkel = prefix+".skeleton";
+        } else {
+            fileNameSkel = "out.skeleton";
+        }
 		ISkeleton *skeleton = new ISkeleton();
 	
 		for (int n = 0; n < scene->mRootNode->mNumChildren; ++n) {
@@ -237,7 +242,7 @@ int exportToFile(String prefix, bool swapZY, bool addSubmeshes, bool listOnly) {
 		}
 
 		if(!listOnly) {
-			skeleton->saveToFile(prefix.c_str(), swapZY);
+			skeleton->saveToFile(fileNameSkel.c_str(), swapZY);
 		}
 	} else {
 		if(!listOnly) {
@@ -373,7 +378,7 @@ int main(int argc, char **argv) {
         
 		exportToFile(prefix, swapZYAxis, addSubmeshes, listOnly);
 	} else {
-		printf("Error opening scene...\n");
+		printf("Error opening scene (%s)\n", aiGetErrorString());
 	}
 
 	aiReleaseImport(scene);