diff --git a/libraries/script-engine/src/Quat.cpp b/libraries/script-engine/src/Quat.cpp index 9cd659dd5d..14a6573415 100644 --- a/libraries/script-engine/src/Quat.cpp +++ b/libraries/script-engine/src/Quat.cpp @@ -18,6 +18,26 @@ #include "ScriptEngineLogging.h" #include "Quat.h" +quat Quat::normalize(const glm::quat& q) { + return glm::normalize(q); +} + +glm::quat Quat::rotationBetween(const glm::vec3& v1, const glm::vec3& v2) { + return ::rotationBetween(v1, v2); +} + +glm::quat Quat::lookAt(const glm::vec3& eye, const glm::vec3& center, const glm::vec3& up) { + return glm::quat_cast(glm::lookAt(eye, center, up)); +} + +glm::quat Quat::lookAtSimple(const glm::vec3& eye, const glm::vec3& center) { + auto dir = glm::normalize(center - eye); + // if the direction is nearly aligned with the Y axis, then use the X axis for 'up' + if (dir.x < 0.001f && dir.z < 0.001f) { + return lookAt(eye, center, Vectors::UNIT_X); + } + return lookAt(eye, center, Vectors::UNIT_Y); +} glm::quat Quat::multiply(const glm::quat& q1, const glm::quat& q2) { return q1 * q2; diff --git a/libraries/script-engine/src/Quat.h b/libraries/script-engine/src/Quat.h index 1f130c57c7..543c93401f 100644 --- a/libraries/script-engine/src/Quat.h +++ b/libraries/script-engine/src/Quat.h @@ -25,6 +25,10 @@ class Quat : public QObject { public slots: glm::quat multiply(const glm::quat& q1, const glm::quat& q2); + glm::quat normalize(const glm::quat& q); + glm::quat lookAt(const glm::vec3& eye, const glm::vec3& center, const glm::vec3& up); + glm::quat lookAtSimple(const glm::vec3& eye, const glm::vec3& center); + glm::quat rotationBetween(const glm::vec3& v1, const glm::vec3& v2); glm::quat fromVec3Degrees(const glm::vec3& vec3); // degrees glm::quat fromVec3Radians(const glm::vec3& vec3); // radians glm::quat fromPitchYawRollDegrees(float pitch, float yaw, float roll); // degrees diff --git a/libraries/script-engine/src/Vec3.cpp b/libraries/script-engine/src/Vec3.cpp index 4ed16b2ef0..012663073c 100644 --- a/libraries/script-engine/src/Vec3.cpp +++ b/libraries/script-engine/src/Vec3.cpp @@ -123,3 +123,70 @@ glm::vec3 Vec3::fromPolar(float elevation, float azimuth) { return fromPolar(v); } +const vec3& Vec3::UNIT_X() { + return Vectors::UNIT_X; +} + +const vec3& Vec3::UNIT_Y() { + return Vectors::UNIT_Y; +} + +const vec3& Vec3::UNIT_Z() { + return Vectors::UNIT_Z; +} + +const vec3& Vec3::UNIT_NEG_X() { + return Vectors::UNIT_NEG_X; +} + +const vec3& Vec3::UNIT_NEG_Y() { + return Vectors::UNIT_NEG_Y; +} + +const vec3& Vec3::UNIT_NEG_Z() { + return Vectors::UNIT_NEG_Z; +} + +const vec3& Vec3::UNIT_XY() { + return Vectors::UNIT_XY; +} + +const vec3& Vec3::UNIT_XZ() { + return Vectors::UNIT_XZ; +} + +const vec3& Vec3::UNIT_YZ() { + return Vectors::UNIT_YZ; +} + +const vec3& Vec3::UNIT_XYZ() { + return Vectors::UNIT_XYZ; +} + +const vec3& Vec3::FLOAT_MAX() { + return Vectors::MAX; +} + +const vec3& Vec3::FLOAT_MIN() { + return Vectors::MIN; +} + +const vec3& Vec3::ZERO() { + return Vectors::ZERO; +} + +const vec3& Vec3::ONE() { + return Vectors::ONE; +} + +const vec3& Vec3::RIGHT() { + return Vectors::RIGHT; +} + +const vec3& Vec3::UP() { + return Vectors::UNIT_X; +} + +const vec3& Vec3::FRONT() { + return Vectors::FRONT; +} diff --git a/libraries/script-engine/src/Vec3.h b/libraries/script-engine/src/Vec3.h index 82062ca80d..492345b7fe 100644 --- a/libraries/script-engine/src/Vec3.h +++ b/libraries/script-engine/src/Vec3.h @@ -13,36 +13,52 @@ #ifndef hifi_Vec3_h #define hifi_Vec3_h - -#include -#include - + #include #include +#include "GLMHelpers.h" + /// Scriptable interface a Vec3ernion helper class object. Used exclusively in the JavaScript API class Vec3 : public QObject { Q_OBJECT public slots: - glm::vec3 reflect(const glm::vec3& v1, const glm::vec3& v2); - glm::vec3 cross(const glm::vec3& v1, const glm::vec3& v2); - float dot(const glm::vec3& v1, const glm::vec3& v2); - glm::vec3 multiply(const glm::vec3& v1, float f); - glm::vec3 multiply(float, const glm::vec3& v1); - glm::vec3 multiplyQbyV(const glm::quat& q, const glm::vec3& v); - glm::vec3 sum(const glm::vec3& v1, const glm::vec3& v2); - glm::vec3 subtract(const glm::vec3& v1, const glm::vec3& v2); - float length(const glm::vec3& v); - float distance(const glm::vec3& v1, const glm::vec3& v2); - float orientedAngle(const glm::vec3& v1, const glm::vec3& v2, const glm::vec3& v3); - glm::vec3 normalize(const glm::vec3& v); - glm::vec3 mix(const glm::vec3& v1, const glm::vec3& v2, float m); - void print(const QString& lable, const glm::vec3& v); - bool equal(const glm::vec3& v1, const glm::vec3& v2); - glm::vec3 toPolar(const glm::vec3& v); - glm::vec3 fromPolar(const glm::vec3& polar); - glm::vec3 fromPolar(float elevation, float azimuth); + vec3 reflect(const vec3& v1, const vec3& v2); + vec3 cross(const vec3& v1, const vec3& v2); + float dot(const vec3& v1, const vec3& v2); + vec3 multiply(const vec3& v1, float f); + vec3 multiply(float, const vec3& v1); + vec3 multiplyQbyV(const glm::quat& q, const vec3& v); + vec3 sum(const vec3& v1, const vec3& v2); + vec3 subtract(const vec3& v1, const vec3& v2); + float length(const vec3& v); + float distance(const vec3& v1, const vec3& v2); + float orientedAngle(const vec3& v1, const vec3& v2, const vec3& v3); + vec3 normalize(const vec3& v); + vec3 mix(const vec3& v1, const vec3& v2, float m); + void print(const QString& lable, const vec3& v); + bool equal(const vec3& v1, const vec3& v2); + vec3 toPolar(const vec3& v); + vec3 fromPolar(const vec3& polar); + vec3 fromPolar(float elevation, float azimuth); + const vec3& UNIT_X(); + const vec3& UNIT_Y(); + const vec3& UNIT_Z(); + const vec3& UNIT_NEG_X(); + const vec3& UNIT_NEG_Y(); + const vec3& UNIT_NEG_Z(); + const vec3& UNIT_XY(); + const vec3& UNIT_XZ(); + const vec3& UNIT_YZ(); + const vec3& UNIT_XYZ(); + const vec3& FLOAT_MAX(); + const vec3& FLOAT_MIN(); + const vec3& ZERO(); + const vec3& ONE(); + const vec3& RIGHT(); + const vec3& UP(); + const vec3& FRONT(); }; diff --git a/libraries/shared/src/GLMHelpers.h b/libraries/shared/src/GLMHelpers.h index 4b03ed2525..8e3cadac74 100644 --- a/libraries/shared/src/GLMHelpers.h +++ b/libraries/shared/src/GLMHelpers.h @@ -64,7 +64,6 @@ public: static const vec3 UNIT_XY; static const vec3 UNIT_XZ; static const vec3 UNIT_YZ; - static const vec3 UNIT_ZX; static const vec3 UNIT_XYZ; static const vec3 MAX; static const vec3 MIN;