[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [21182] branches/soc-2009-chingachgook/ source/blender/collada: Object rotation read/write.

Arystanbek Dyussenov arystan.d at gmail.com
Sat Jun 27 11:29:30 CEST 2009


Revision: 21182
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=21182
Author:   kazanbas
Date:     2009-06-27 11:29:30 +0200 (Sat, 27 Jun 2009)

Log Message:
-----------
Object rotation read/write.
The new QuatToAxisAngle function could go to BLI_arithb.h.

Modified Paths:
--------------
    branches/soc-2009-chingachgook/source/blender/collada/DocumentExporter.cpp
    branches/soc-2009-chingachgook/source/blender/collada/DocumentImporter.cpp

Modified: branches/soc-2009-chingachgook/source/blender/collada/DocumentExporter.cpp
===================================================================
--- branches/soc-2009-chingachgook/source/blender/collada/DocumentExporter.cpp	2009-06-27 08:42:47 UTC (rev 21181)
+++ branches/soc-2009-chingachgook/source/blender/collada/DocumentExporter.cpp	2009-06-27 09:29:30 UTC (rev 21182)
@@ -15,6 +15,8 @@
 #include "BKE_main.h"
 #include "BKE_material.h"
 
+#include "BLI_arithb.h"
+
 #include "DocumentExporter.h"
 
 #include <COLLADASWAsset.h>
@@ -44,10 +46,36 @@
 
 #include <vector>
 #include <algorithm> // std::find
+#include <math.h>
 
-// utilities to avoid code duplication
-// definition of these is difficult to read, but they should be useful
+// TODO: this can handy in BLI_arith.b
+// This function assumes that quat is normalized.
+// The following document was used as reference:
+// http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm
+void QuatToAxisAngle(float *q, float *axis, float *angle)
+{
+	// quat to axis angle
+	*angle = 2 * acos(q[0]);
+	float divisor = sqrt(1 - q[0] * q[0]);
 
+	// test to avoid divide by zero, divisor is always positive
+	if (divisor < 0.001f ) {
+		axis[0] = 1.0f;
+		axis[1] = 0.0f;
+		axis[2] = 0.0f;
+	}
+	else {
+		axis[0] = q[1] / divisor;
+		axis[1] = q[2] / divisor;
+		axis[2] = q[3] / divisor;
+	}
+}
+
+/*
+  Utilities to avoid code duplication.
+  Definition can take some time to understand, but they should be useful.
+*/
+
 // f should have
 // void operator()(Object* ob)
 template<class Functor>
@@ -455,12 +483,20 @@
 		node.start();
 
 		node.addTranslate(ob->loc[0], ob->loc[1], ob->loc[2]);
-		// XXX for rotation we need to convert ob->rot (euler, I guess) to axis/angle
-		// see http://www.euclideanspace.com/maths/geometry/rotations/conversions/eulerToAngle/index.htm
-		// add it to BLI_arithb.h
-		// node.addRotate();
+
+		// when animation time comes, replace a single <rotate> with 3, one for each axis
+		float quat[4];
+		float axis[3];
+		float angle;
+		double angle_deg;
+		EulToQuat(ob->rot, quat);
+		NormalQuat(quat);
+		QuatToAxisAngle(quat, axis, &angle);
+		angle_deg = angle * 180.0f / M_PI;
+		node.addRotate(axis[0], axis[1], axis[2], angle_deg);
+
 		node.addScale(ob->size[0], ob->size[1], ob->size[2]);
-				
+
 		COLLADASW::InstanceGeometry instGeom(mSW);
 		std::string ob_name(ob->id.name);
 		instGeom.setUrl(COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, ob_name));

Modified: branches/soc-2009-chingachgook/source/blender/collada/DocumentImporter.cpp
===================================================================
--- branches/soc-2009-chingachgook/source/blender/collada/DocumentImporter.cpp	2009-06-27 08:42:47 UTC (rev 21181)
+++ branches/soc-2009-chingachgook/source/blender/collada/DocumentImporter.cpp	2009-06-27 09:29:30 UTC (rev 21182)
@@ -35,6 +35,8 @@
 #include "BKE_library.h"
 }
 
+#include "BLI_arithb.h"
+
 #include "DNA_object_types.h"
 #include "DNA_meshdata_types.h"
 #include "DNA_mesh_types.h"
@@ -216,7 +218,7 @@
 		Scene *sce = CTX_data_scene(mContext);
 // 		Scene *sce = add_scene(visualScene->getName());
 		int i = 0;
-		
+
 		for (i = 0; i < visualScene->getRootNodes().getCount(); i++) {
 			COLLADAFW::Node *node = visualScene->getRootNodes()[i];
 
@@ -255,6 +257,10 @@
 
 			set_mesh(ob, uid_mesh_map[uid]);
 
+			float rot[3][3];
+			Mat3One(rot);
+			
+			// transform Object
 			for (int k = 0; k < node->getTransformations().getCount(); k ++) {
 				COLLADAFW::Transformation *transform = node->getTransformations()[k];
 				COLLADAFW::Transformation::TransformationType type = transform->getTransformationType();
@@ -272,6 +278,20 @@
 					}
 					break;
 				case COLLADAFW::Transformation::ROTATE:
+					{
+						COLLADAFW::Rotate *ro = (COLLADAFW::Rotate*)transform;
+						COLLADABU::Math::Vector3& raxis = ro->getRotationAxis();
+						float angle = (float)(ro->getRotationAngle() * M_PI / 180.0f);
+						float axis[] = {raxis[0], raxis[1], raxis[2]};
+						float quat[4];
+						float rot_copy[3][3];
+						float mat[3][3];
+						AxisAngleToQuat(quat, axis, angle);
+
+						QuatToMat3(quat, mat);
+						Mat3CpyMat3(rot_copy, rot);
+						Mat3MulMat3(rot, rot_copy, mat);
+					}
 					break;
 				case COLLADAFW::Transformation::SCALE:
 					{
@@ -293,6 +313,8 @@
 				}
 			}
 
+			Mat3ToEul(rot, ob->rot);
+
 		}
 
 		mVisualScenes.push_back(*visualScene);





More information about the Bf-blender-cvs mailing list