Просмотр исходного кода

Added Node::LookAt().
Added Quaternion::FromAxes().

Lasse Öörni 14 лет назад
Родитель
Сommit
d5c42e8c2c

+ 3 - 1
Docs/ScriptAPI.dox

@@ -335,6 +335,7 @@ Methods:<br>
 - void FromAngleAxis(float, const Vector3&)
 - void FromAngleAxis(float, const Vector3&)
 - void FromEulerAngles(float, float, float)
 - void FromEulerAngles(float, float, float)
 - void FromRotationTo(const Vector3&, const Vector3&)
 - void FromRotationTo(const Vector3&, const Vector3&)
+- void FromAxes(const Vector3&, const Vector3&, const Vector3&)
 - void Normalize()
 - void Normalize()
 - Quaternion Normalized() const
 - Quaternion Normalized() const
 - Quaternion Inverse() const
 - Quaternion Inverse() const
@@ -1093,6 +1094,7 @@ Methods:<br>
 - void Pitch(float, bool arg1 = false)
 - void Pitch(float, bool arg1 = false)
 - void Yaw(float, bool arg1 = false)
 - void Yaw(float, bool arg1 = false)
 - void Roll(float, bool arg1 = false)
 - void Roll(float, bool arg1 = false)
+- void LookAt(const Vector3&, const Vector3& arg1 = Vector3 ( 0 , 1 , 0 ), bool arg2 = false)
 - void Scale(float)
 - void Scale(float)
 - void Scale(const Vector3&)
 - void Scale(const Vector3&)
 - Node@ CreateChild(const String& arg0 = "", CreateMode arg1 = REPLICATED)
 - Node@ CreateChild(const String& arg0 = "", CreateMode arg1 = REPLICATED)
@@ -1176,6 +1178,7 @@ Methods:<br>
 - void Pitch(float, bool arg1 = false)
 - void Pitch(float, bool arg1 = false)
 - void Yaw(float, bool arg1 = false)
 - void Yaw(float, bool arg1 = false)
 - void Roll(float, bool arg1 = false)
 - void Roll(float, bool arg1 = false)
+- void LookAt(const Vector3&, const Vector3& arg1 = Vector3 ( 0 , 1 , 0 ), bool arg2 = false)
 - void Scale(float)
 - void Scale(float)
 - void Scale(const Vector3&)
 - void Scale(const Vector3&)
 - Node@ CreateChild(const String& arg0 = "", CreateMode arg1 = REPLICATED)
 - Node@ CreateChild(const String& arg0 = "", CreateMode arg1 = REPLICATED)
@@ -2103,7 +2106,6 @@ Properties:<br>
 - bool reuseShadowMaps
 - bool reuseShadowMaps
 - bool lightStencilMasking
 - bool lightStencilMasking
 - bool dynamicInstancing
 - bool dynamicInstancing
-- int minInstanceGroupSize
 - int maxInstanceTriangles
 - int maxInstanceTriangles
 - int maxOccluderTriangles
 - int maxOccluderTriangles
 - int occlusionBufferSize
 - int occlusionBufferSize

+ 1 - 0
Engine/Engine/APITemplates.h

@@ -505,6 +505,7 @@ template <class T> void RegisterNode(asIScriptEngine* engine, const char* classN
     engine->RegisterObjectMethod(className, "void Pitch(float, bool fixedAxis = false)", asMETHOD(T, Pitch), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "void Pitch(float, bool fixedAxis = false)", asMETHOD(T, Pitch), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "void Yaw(float, bool fixedAxis = false)", asMETHOD(T, Yaw), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "void Yaw(float, bool fixedAxis = false)", asMETHOD(T, Yaw), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "void Roll(float, bool fixedAxis = false)", asMETHOD(T, Roll), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "void Roll(float, bool fixedAxis = false)", asMETHOD(T, Roll), asCALL_THISCALL);
+    engine->RegisterObjectMethod(className, "void LookAt(const Vector3&in, const Vector3&in upAxis = Vector3(0, 1, 0), bool worldSpace = false)", asMETHOD(T, LookAt), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "void Scale(float)", asMETHODPR(T, Scale, (float), void), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "void Scale(float)", asMETHODPR(T, Scale, (float), void), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "void Scale(const Vector3&in)", asMETHODPR(T, Scale, (const Vector3&), void), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "void Scale(const Vector3&in)", asMETHODPR(T, Scale, (const Vector3&), void), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "Node@+ CreateChild(const String&in name = \"\", CreateMode mode = REPLICATED)", asMETHODPR(T, CreateChild, (const String&, CreateMode), Node*), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "Node@+ CreateChild(const String&in name = \"\", CreateMode mode = REPLICATED)", asMETHODPR(T, CreateChild, (const String&, CreateMode), Node*), asCALL_THISCALL);

+ 7 - 0
Engine/Engine/MathAPI.cpp

@@ -352,6 +352,11 @@ static void ConstructQuaternionRotation(const Vector3& start, const Vector3& end
     new(ptr) Quaternion(start, end);
     new(ptr) Quaternion(start, end);
 }
 }
 
 
+static void ConstructQuaternionAxes(const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis, Quaternion* ptr)
+{
+    new(ptr) Quaternion(xAxis, yAxis, zAxis);
+}
+
 static void RegisterQuaternion(asIScriptEngine* engine)
 static void RegisterQuaternion(asIScriptEngine* engine)
 {
 {
     engine->RegisterObjectType("Quaternion", sizeof(Quaternion), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_CAK);
     engine->RegisterObjectType("Quaternion", sizeof(Quaternion), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_CAK);
@@ -362,6 +367,7 @@ static void RegisterQuaternion(asIScriptEngine* engine)
     engine->RegisterObjectBehaviour("Quaternion", asBEHAVE_CONSTRUCT, "void f(float, float, float)", asFUNCTION(ConstructQuaternionEuler), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectBehaviour("Quaternion", asBEHAVE_CONSTRUCT, "void f(float, float, float)", asFUNCTION(ConstructQuaternionEuler), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectBehaviour("Quaternion", asBEHAVE_CONSTRUCT, "void f(const Vector3&in)", asFUNCTION(ConstructQuaternionEulerVector), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectBehaviour("Quaternion", asBEHAVE_CONSTRUCT, "void f(const Vector3&in)", asFUNCTION(ConstructQuaternionEulerVector), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectBehaviour("Quaternion", asBEHAVE_CONSTRUCT, "void f(const Vector3&in, const Vector3&in)", asFUNCTION(ConstructQuaternionRotation), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectBehaviour("Quaternion", asBEHAVE_CONSTRUCT, "void f(const Vector3&in, const Vector3&in)", asFUNCTION(ConstructQuaternionRotation), asCALL_CDECL_OBJLAST);
+    engine->RegisterObjectBehaviour("Quaternion", asBEHAVE_CONSTRUCT, "void f(const Vector3&in, const Vector3&in, const Vector3&in)", asFUNCTION(ConstructQuaternionAxes), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod("Quaternion", "Quaternion& opAssign(const Quaternion&in)", asMETHOD(Quaternion, operator =), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "Quaternion& opAssign(const Quaternion&in)", asMETHOD(Quaternion, operator =), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "Quaternion& opAddAssign(const Quaternion&in)", asMETHOD(Quaternion, operator +=), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "Quaternion& opAddAssign(const Quaternion&in)", asMETHOD(Quaternion, operator +=), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "bool opEquals(const Quaternion&in) const", asMETHOD(Quaternion, operator ==), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "bool opEquals(const Quaternion&in) const", asMETHOD(Quaternion, operator ==), asCALL_THISCALL);
@@ -374,6 +380,7 @@ static void RegisterQuaternion(asIScriptEngine* engine)
     engine->RegisterObjectMethod("Quaternion", "void FromAngleAxis(float, const Vector3&in)", asMETHOD(Quaternion, FromAngleAxis), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "void FromAngleAxis(float, const Vector3&in)", asMETHOD(Quaternion, FromAngleAxis), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "void FromEulerAngles(float, float, float)", asMETHOD(Quaternion, FromEulerAngles), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "void FromEulerAngles(float, float, float)", asMETHOD(Quaternion, FromEulerAngles), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "void FromRotationTo(const Vector3&in, const Vector3&in)", asMETHOD(Quaternion, FromRotationTo), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "void FromRotationTo(const Vector3&in, const Vector3&in)", asMETHOD(Quaternion, FromRotationTo), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Quaternion", "void FromAxes(const Vector3&in, const Vector3&in, const Vector3&in)", asMETHOD(Quaternion, FromAxes), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "void Normalize()", asMETHOD(Quaternion, Normalize), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "void Normalize()", asMETHOD(Quaternion, Normalize), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "Quaternion Normalized() const", asMETHOD(Quaternion, Normalized), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "Quaternion Normalized() const", asMETHOD(Quaternion, Normalized), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "Quaternion Inverse() const", asMETHOD(Quaternion, Inverse), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "Quaternion Inverse() const", asMETHOD(Quaternion, Inverse), asCALL_THISCALL);

+ 11 - 0
Engine/Math/Quaternion.cpp

@@ -91,6 +91,17 @@ void Quaternion::FromRotationTo(const Vector3& start, const Vector3& end)
     }
     }
 }
 }
 
 
+void Quaternion::FromAxes(const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis)
+{
+    Matrix3 matrix(
+        xAxis.x_, yAxis.x_, zAxis.x_,
+        xAxis.y_, yAxis.y_, zAxis.y_,
+        xAxis.z_, yAxis.z_, zAxis.z_
+    );
+    
+    FromRotationMatrix(matrix);
+}
+
 void Quaternion::FromRotationMatrix(const Matrix3& matrix)
 void Quaternion::FromRotationMatrix(const Matrix3& matrix)
 {
 {
     float t = matrix.m00_ + matrix.m11_ + matrix.m22_;
     float t = matrix.m00_ + matrix.m11_ + matrix.m22_;

+ 8 - 0
Engine/Math/Quaternion.h

@@ -83,6 +83,12 @@ public:
         FromRotationTo(start, end);
         FromRotationTo(start, end);
     }
     }
     
     
+    /// Construct from orthonormal axes.
+    Quaternion(const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis)
+    {
+        FromAxes(xAxis, yAxis, zAxis);
+    }
+
     /// Construct from a rotation matrix.
     /// Construct from a rotation matrix.
     Quaternion(const Matrix3& matrix)
     Quaternion(const Matrix3& matrix)
     {
     {
@@ -149,6 +155,8 @@ public:
     void FromEulerAngles(float x, float y, float z);
     void FromEulerAngles(float x, float y, float z);
     /// Define from the rotation difference between two vectors.
     /// Define from the rotation difference between two vectors.
     void FromRotationTo(const Vector3& start, const Vector3& end);
     void FromRotationTo(const Vector3& start, const Vector3& end);
+    /// Define from orthonormal axes.
+    void FromAxes(const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis);
     /// Define from a rotation matrix.
     /// Define from a rotation matrix.
     void FromRotationMatrix(const Matrix3& matrix);
     void FromRotationMatrix(const Matrix3& matrix);
     
     

+ 17 - 0
Engine/Scene/Node.cpp

@@ -384,6 +384,23 @@ void Node::Roll(float angle, bool fixedAxis)
     Rotate(Quaternion(angle, Vector3::FORWARD), fixedAxis);
     Rotate(Quaternion(angle, Vector3::FORWARD), fixedAxis);
 }
 }
 
 
+void Node::LookAt(const Vector3& target, const Vector3& upAxis, bool worldSpace)
+{
+    Vector3 targetZ;
+    if (worldSpace)
+        targetZ = (target - GetWorldPosition()).Normalized();
+    else
+        targetZ = (target - position_).Normalized();
+    
+    Vector3 targetX = upAxis.CrossProduct(targetZ).Normalized();
+    Vector3 targetY = targetZ.CrossProduct(targetX).Normalized();
+    
+    if (!worldSpace || !parent_)
+        SetRotation(Quaternion(targetX, targetY, targetZ));
+    else
+        SetRotation(parent_->GetWorldRotation().Inverse() * Quaternion(targetX, targetY, targetZ));
+}
+
 void Node::Scale(float scale)
 void Node::Scale(float scale)
 {
 {
     scale_ *= scale;
     scale_ *= scale;

+ 2 - 0
Engine/Scene/Node.h

@@ -107,6 +107,8 @@ public:
     void Yaw(float angle, bool fixedAxis = false);
     void Yaw(float angle, bool fixedAxis = false);
     /// Rotate around the Z axis.
     /// Rotate around the Z axis.
     void Roll(float angle, bool fixedAxis = false);
     void Roll(float angle, bool fixedAxis = false);
+    /// Look at a target position.
+    void LookAt(const Vector3& target, const Vector3& upAxis = Vector3::UP, bool worldSpace = false);
     /// Modify scale uniformly.
     /// Modify scale uniformly.
     void Scale(float scale);
     void Scale(float scale);
     /// Modify scale.
     /// Modify scale.