Skip to content

Commit

Permalink
Fix Quaternion from Euler and ToMatrix
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianensis committed Sep 24, 2020
1 parent 3e67cd3 commit ef1e6ae
Show file tree
Hide file tree
Showing 18 changed files with 20,164 additions and 3,390 deletions.
8 changes: 2 additions & 6 deletions code/include/Graphics/Renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,7 @@ class Renderer: public Component {

const Animation* getCurrentAnimation() const;

/**
* Update de material with information about the region, the alphacolor and the animation.
* \param Material material The material.
*/
void updateAnimation(Material *material);
void updateAnimation();

void setColor(const Vector4 &color);
const Array<f32>* getColor() const { return mColor; };
Expand Down Expand Up @@ -177,7 +173,7 @@ class Renderer: public Component {

const Matrix4& getRendererModelMatrix();

Array<Vector2>* getVertices();
const Array<Vector2>* getVertices();
};

} /* namespace DE */
Expand Down
4 changes: 2 additions & 2 deletions code/include/Math/Quaternion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ class Quaternion: public DE_Class /*16 bytes alignment*/{
Quaternion& squad(); // TODO: implement
void fromEuler(f32 roll, f32 pitch, f32 yaw);
void fromEuler(const Vector3 &v);
Vector3 toEuler() const;
//Vector3 toEuler() const;
void toMatrix(Matrix4 *outMatrix) const;
void fromMatrix(const Matrix4 &m);
//void fromMatrix(const Matrix4 &m);

//-------------------------------------------------------------------
// OPERATORS
Expand Down
2 changes: 1 addition & 1 deletion code/include/Test/Test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ using namespace DE;
#define DE_test_expected_float(x,exp) DE_test_expected_any(__test__float,x,exp)
#define DE_test_expected_sint(x,exp) DE_test_expected_any(__test__sint,x,exp)
#define DE_test_expected_uint(x,exp) DE_test_expected_any(__test__uint,x,exp)
#define DE_test_expected_float_eps(x,exp,eps) DE_test_tick(); __test__float_precision = (x); DE_test_tock(); __test__total++; __test__b = ( std::max(0.0f,fabsf(__test__float_precision)-fabsf(exp)) < fabsf(eps)); if (__test__b){ __test__ok++; } __test__output << (__test__b ? "[ OK ]" : "[ FAIL ]") << "\t" << #x << std::endl << "\t" << "result: " << __PRECISION << __test__float << std::endl << "\t" << "DE_test_expected: " << __PRECISION << (exp) << std::endl << "\t" << "epsilon: " << __PRECISION << (eps) << std::endl << std::endl
#define DE_test_expected_float_eps(x,exp,eps) DE_test_tick(); __test__float_precision = (x); DE_test_tock(); __test__total++; __test__b = ( std::max(0.0f,fabsf(fabsf(__test__float_precision)-fabsf(exp))) < fabsf(eps)); if (__test__b){ __test__ok++; } __test__output << (__test__b ? "[ OK ]" : "[ FAIL ]") << "\t" << #x << std::endl << "\t" << "result: " << __PRECISION << __test__float_precision << std::endl << "\t" << "DE_test_expected: " << __PRECISION << (exp) << std::endl << "\t" << "epsilon: " << __PRECISION << (eps) << std::endl << std::endl
#define DE_test_expected(x,exp) DE_test_expected_any(__test__any,x,exp)

#define summary() __test__output << std::endl << std::setprecision(4) << (isnan(__test__totaltime) ? 0 : __test__totaltime) << "ms " << __test__ok << " " <<(__test__total-__test__ok) << std::endl; std::cout << __test__output.str();
Expand Down
6 changes: 2 additions & 4 deletions code/source/Graphics/Batch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,9 @@ u32 Batch::render(u32 layer) {

shader->addBool(renderer->isAffectedByProjection(), "isAffectedByProjection");

renderer->updateAnimation(mMaterial);

bool lineMode = it.get()->isLineMode();

renderer->updateAnimation();
addToVertexBuffer(renderer);

drawCallCounter++;
Expand Down Expand Up @@ -443,8 +442,7 @@ void Batch::addToVertexBuffer(Renderer* renderer) {

Transform* t = renderer->getGameObject()->getTransform();


Array<Vector2>* vertexPositions = renderer->getVertices();
const Array<Vector2>* vertexPositions = renderer->getVertices();

FOR_RANGE(i,0,mVerticesPerMesh) {

Expand Down
8 changes: 4 additions & 4 deletions code/source/Graphics/Chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ void Chunk::set(const Vector3 &leftTop, f32 size) {

void Chunk::update(BatchesMap *batchesMap) {

// RenderEngine::getInstance()->drawLine(Vector3(mLeftTop.x, mLeftTop.y,0), Vector3(mLeftTop.x, mLeftTop.y - mSize,0));
// RenderEngine::getInstance()->drawLine(Vector3(mLeftTop.x, mLeftTop.y - mSize,0), Vector3(mLeftTop.x + mSize, mLeftTop.y - mSize,0));
// RenderEngine::getInstance()->drawLine(Vector3(mLeftTop.x + mSize, mLeftTop.y - mSize,0), Vector3(mLeftTop.x + mSize, mLeftTop.y,0));
// RenderEngine::getInstance()->drawLine(Vector3(mLeftTop.x + mSize, mLeftTop.y,0), Vector3(mLeftTop.x, mLeftTop.y,0));
// RenderEngine::getInstance()->drawLine(Vector3(mLeftTop.x, mLeftTop.y,0), Vector3(mLeftTop.x, mLeftTop.y - mSize,0));
// RenderEngine::getInstance()->drawLine(Vector3(mLeftTop.x, mLeftTop.y - mSize,0), Vector3(mLeftTop.x + mSize, mLeftTop.y - mSize,0));
// RenderEngine::getInstance()->drawLine(Vector3(mLeftTop.x + mSize, mLeftTop.y - mSize,0), Vector3(mLeftTop.x + mSize, mLeftTop.y,0));
// RenderEngine::getInstance()->drawLine(Vector3(mLeftTop.x + mSize, mLeftTop.y,0), Vector3(mLeftTop.x, mLeftTop.y,0));

FOR_LIST(it, mRenderers) {
Renderer* renderer = it.get();
Expand Down
7 changes: 2 additions & 5 deletions code/source/Graphics/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,8 @@ void Renderer::addAnimation(const std::string &name, Animation *animation) {

// ---------------------------------------------------------------------------

void Renderer::updateAnimation(Material *material) {
void Renderer::updateAnimation() {
if (mMaterial) {

Shader* shader = mMaterial->getShader();

if (hasAnimations()) {
const AnimationFrame* frame = mCurrentAnimation->getNextFrame();
mRegionPosition = frame->getPosition();
Expand Down Expand Up @@ -212,7 +209,7 @@ const Matrix4& Renderer::getRendererModelMatrix() {
return mRenderereModelMatrix;
};

Array<Vector2>* Renderer::getVertices() {
const Array<Vector2>* Renderer::getVertices() {

if(mPositionOffsetDirty || !isStatic()){
FOR_ARRAY(i, mVertices) {
Expand Down
8 changes: 6 additions & 2 deletions code/source/Math/Matrix4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "MathUtils.hpp"
#include "Vector4.hpp"
#include "Vector3.hpp"
#include "Quaternion.hpp"
#include "Array.hpp"

#include <cstring>
Expand Down Expand Up @@ -275,7 +276,10 @@ void Matrix4::translation(const Vector3 &vector) {

void Matrix4::rotation(const Vector3 &vector) {

this->identity();
Quaternion q(vector);
q.toMatrix(this);

/*this->identity();
f32 radians, cos, sin;
Expand Down Expand Up @@ -303,7 +307,7 @@ void Matrix4::rotation(const Vector3 &vector) {
this->set(0, 1, -sin);
this->set(1, 0, sin);
this->set(1, 1, cos);
}
}*/

};

Expand Down
78 changes: 40 additions & 38 deletions code/source/Math/Quaternion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,30 +177,29 @@ Quaternion& Quaternion::slerp(const Quaternion &target, f32 t) {
return *this;
}

void Quaternion::fromEuler(f32 roll, f32 pitch, f32 yaw) {
void Quaternion::fromEuler(f32 roll, f32 pitch, f32 yaw) { // pitch attitude, yaw heading, or roll bank

// https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles

f32 roll2 = MathUtils::rad(roll) * 0.5f; // x
f32 pitch2 = MathUtils::rad(pitch) * 0.5f; // y
f32 yaw2 = MathUtils::rad(yaw) * 0.5f; // z

f32 c3 = cosf(yaw2);
f32 c2 = cosf(pitch2);
f32 c1 = cosf(roll2);

f32 s3 = sinf(yaw2);
f32 s2 = sinf(pitch2);
f32 s1 = sinf(roll2);
f32 cy = cos(yaw2);
f32 sy = sin(yaw2);
f32 cp = cos(pitch2);
f32 sp = sin(pitch2);
f32 cr = cos(roll2);
f32 sr = sin(roll2);

// original
// w = c3*c2*c1 + s3*s2*s1;
// v.x = c3*c2*s1 - s3*s2*c1;
// v.y = c3*s2*c1 + s3*c2*s1;
// v.z = s3*c2*c1 - c3*s2*s1;
w = cr * cp * cy + sr * sp * sy;
v.x = sr * cp * cy - cr * sp * sy;
v.y = cr * sp * cy + sr * cp * sy;
v.z = cr * cp * sy - sr * sp * cy;

// SIMD-optimized
f32 c3c2 = c3 * c2;
/*f32 c3c2 = c3 * c2;
f32 s3s2 = s3 * s2;
f32 c3s2 = c3 * s2;
f32 s3c2 = s3 * c2;
Expand All @@ -213,19 +212,19 @@ void Quaternion::fromEuler(f32 roll, f32 pitch, f32 yaw) {
f32 aux4 = s3s2 * s1;
f32 aux5 = s3s2 * c1;
f32 aux6 = s3c2 * s1;
f32 aux7 = c3s2 * s1;
f32 aux7 = c3s2 * s1;*/

w = aux0 + aux4;
/*w = aux0 + aux4;
v.x = aux1 - aux5;
v.y = aux2 + aux6;
v.z = aux3 - aux7;
v.z = aux3 - aux7;*/
}

void Quaternion::fromEuler(const Vector3 &v) {
fromEuler(v.x, v.y, v.z);
}

Vector3 Quaternion::toEuler() const {
/*Vector3 Quaternion::toEuler() const {
// https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles
Expand Down Expand Up @@ -256,9 +255,9 @@ Vector3 Quaternion::toEuler() const {
f32 yaw = atan2f(t3, t4);
return Vector3(MathUtils::deg(roll), MathUtils::deg(pitch), MathUtils::deg(yaw));
}
}*/

void Quaternion::fromMatrix(const Matrix4 &m) {
/*void Quaternion::fromMatrix(const Matrix4 &m) {
// https://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/
Expand Down Expand Up @@ -303,35 +302,38 @@ void Quaternion::fromMatrix(const Matrix4 &m) {
}
}
}*/

void Quaternion::toMatrix(Matrix4 *outMatrix) const {

f32 xx2 = 2 * v.x * v.x;
f32 yy2 = 2 * v.y * v.y;
f32 zz2 = 2 * v.z * v.z;
Quaternion copy((*this));
copy.nor();

f32 xx2 = 2 * copy.v.x * copy.v.x;
f32 yy2 = 2 * copy.v.y * copy.v.y;
f32 zz2 = 2 * copy.v.z * copy.v.z;

f32 xy2 = 2 * v.x * v.y;
f32 xz2 = 2 * v.x * v.z;
f32 yz2 = 2 * v.y * v.z;
f32 xy2 = 2 * copy.v.x * copy.v.y;
f32 xz2 = 2 * copy.v.x * copy.v.z;
f32 yz2 = 2 * copy.v.y * copy.v.z;

f32 wx2 = 2 * w * v.x;
f32 wy2 = 2 * w * v.y;
f32 wz2 = 2 * w * v.z;
f32 wx2 = 2 * copy.w * copy.v.x;
f32 wy2 = 2 * copy.w * copy.v.y;
f32 wz2 = 2 * copy.w * copy.v.z;

outMatrix->identity();

outMatrix->set(0, 0, 1 - yy2 - zz2);
outMatrix->set(0, 1, xy2 + wz2);
outMatrix->set(0, 2, xz2 - wy2);
outMatrix->set(0, 0, 1 - (yy2 + zz2));
outMatrix->set(0, 1, xy2 - wz2);
outMatrix->set(0, 2, xz2 + wy2);

outMatrix->set(1, 0, xy2 - wz2);
outMatrix->set(1, 1, 1 - xx2 - zz2);
outMatrix->set(1, 2, yz2 + wx2);
outMatrix->set(1, 0, xy2 + wz2);
outMatrix->set(1, 1, 1 - (xx2 + zz2));
outMatrix->set(1, 2, yz2 - wx2);

outMatrix->set(2, 0, xz2 + wy2);
outMatrix->set(2, 1, yz2 - wx2);
outMatrix->set(2, 2, 1 - xx2 - yy2);
outMatrix->set(2, 0, xz2 - wy2);
outMatrix->set(2, 1, yz2 + wx2);
outMatrix->set(2, 2, 1 - (xx2 + yy2));

}

Expand Down
4 changes: 2 additions & 2 deletions code/source/Scene/Transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void Transform::rotate(const Vector3 &vector) {

void Transform::lookAt(const Vector3 &targetPosition) {

mIsDirtyRotation = true;
/*mIsDirtyRotation = true;
Vector3 target(targetPosition);
Expand All @@ -120,7 +120,7 @@ void Transform::lookAt(const Vector3 &targetPosition) {
q.fromMatrix(lookAtMatrix);
mRotation = q.toEuler();
mRotation = q.toEuler();*/
}

// ---------------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions config/engine.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ scene.maxNewObjectsToSpawn=20
scene.sortByYCoordinate=true
line.renderers.count=1000
scene.maxLayers=10
#scenes.length=0
scenes.length=1
scenes[0]=config/sceneTmp.conf
scenes.length=0
#scenes.length=1
#scenes[0]=config/sceneTmp.conf
Loading

0 comments on commit ef1e6ae

Please sign in to comment.