diff options
author | David Doan <daviddoan@Davids-MacBook-Pro-70.local> | 2023-12-08 13:28:05 -0500 |
---|---|---|
committer | David Doan <daviddoan@Davids-MacBook-Pro-70.local> | 2023-12-08 13:28:05 -0500 |
commit | 7e02d29375e9d6a1bbea98dc6dca600730ab8ec3 (patch) | |
tree | 66b2af48f645de2bb67f78edc7a784d7f304929c /src | |
parent | 31d28f945856ff4b3f5d55e61a747afd08d6f0f9 (diff) | |
parent | 3998e9509ffb511bb449965f1ca0695e2bec2c7d (diff) |
rotation and merge
Diffstat (limited to 'src')
-rw-r--r-- | src/.DS_Store | bin | 8196 -> 8196 bytes | |||
-rw-r--r-- | src/4dvecops/rotations4d.cpp | 88 | ||||
-rw-r--r-- | src/4dvecops/transform4d.cpp | 12 | ||||
-rw-r--r-- | src/4dvecops/vec4operations.cpp | 63 | ||||
-rw-r--r-- | src/4dvecops/vectoroperations.cpp | 30 | ||||
-rw-r--r-- | src/raytracer/raytracer.h | 5 |
6 files changed, 165 insertions, 33 deletions
diff --git a/src/.DS_Store b/src/.DS_Store Binary files differindex cef448c..663fe9e 100644 --- a/src/.DS_Store +++ b/src/.DS_Store diff --git a/src/4dvecops/rotations4d.cpp b/src/4dvecops/rotations4d.cpp new file mode 100644 index 0000000..62746eb --- /dev/null +++ b/src/4dvecops/rotations4d.cpp @@ -0,0 +1,88 @@ +#include "raytracer/raytracer.h" + +glm::mat4 getRotationMatrix4XY( + float angleRadians) { + glm::mat4 result; + result[0][0] = cos(angleRadians); + result[0][1] = -sin(angleRadians); + result[1][0] = sin(angleRadians); + result[1][1] = cos(angleRadians); + result[2][2] = 1; + result[3][3] = 1; + return result; +} + +glm::mat4 getRotationMatrix4YZ( + float angleRadians) { + glm::mat4 result; + result[1][1] = cos(angleRadians); + result[1][2] = -sin(angleRadians); + result[2][1] = sin(angleRadians); + result[2][2] = cos(angleRadians); + result[0][0] = 1; + result[3][3] = 1; + return result; +} + +glm::mat4 getRotationMatrix4ZX( + float angleRadians) { + glm::mat4 result; + result[2][2] = cos(angleRadians); + result[2][0] = -sin(angleRadians); + result[0][2] = sin(angleRadians); + result[0][0] = cos(angleRadians); + result[1][1] = 1; + result[3][3] = 1; + return result; +} + +glm::mat4 getRotationMatrix4XW( + float angleRadians) { + glm::mat4 result; + result[0][0] = cos(angleRadians); + result[0][3] = -sin(angleRadians); + result[3][0] = sin(angleRadians); + result[3][3] = cos(angleRadians); + result[1][1] = 1; + result[2][2] = 1; + return result; +} + +glm::mat4 getRotationMatrix4YW( + float angleRadians) { + glm::mat4 result; + result[1][1] = cos(angleRadians); + result[1][3] = -sin(angleRadians); + result[3][1] = sin(angleRadians); + result[3][3] = cos(angleRadians); + result[0][0] = 1; + result[2][2] = 1; + return result; +} + +glm::mat4 getRotationMatrix4ZW( + float angleRadians) { + glm::mat4 result; + result[2][2] = cos(angleRadians); + result[2][3] = -sin(angleRadians); + result[3][2] = sin(angleRadians); + result[3][3] = cos(angleRadians); + result[0][0] = 1; + result[1][1] = 1; + return result; +} + +glm::mat4 RayTracer::getRotationMatrix4( + float angleRadiansXY, + float angleRadiansYZ, + float angleRadiansZX, + float angleRadiansXW, + float angleRadiansYW, + float angleRadiansZW) { + return getRotationMatrix4XY(angleRadiansXY) * + getRotationMatrix4YZ(angleRadiansYZ) * + getRotationMatrix4ZX(angleRadiansZX) * + getRotationMatrix4XW(angleRadiansXW) * + getRotationMatrix4YW(angleRadiansYW) * + getRotationMatrix4ZW(angleRadiansZW); +}
\ No newline at end of file diff --git a/src/4dvecops/transform4d.cpp b/src/4dvecops/transform4d.cpp new file mode 100644 index 0000000..91f0d8c --- /dev/null +++ b/src/4dvecops/transform4d.cpp @@ -0,0 +1,12 @@ +#include "raytracer/raytracer.h" + +// this is used to transform a 4d point by a 4d matrix and its associated translation +// this is motivated by the fact that glm does not support 5d matrices, so we cannot define a mat5 to encapsulate both the rotation and translation in one matrix +// therefore, we break the 5d transformation into a 4d rotation and a 4d translation +glm::vec4 transformPoint4(glm::vec4 point4, glm::mat4 transformDirectionMatrix, glm::vec4 translationPointVector) { + // do the translation first + point4 -= translationPointVector; + // do the rotation and scaling + point4 = transformDirectionMatrix * point4; + return point4; +}
\ No newline at end of file diff --git a/src/4dvecops/vec4operations.cpp b/src/4dvecops/vec4operations.cpp new file mode 100644 index 0000000..1ffe673 --- /dev/null +++ b/src/4dvecops/vec4operations.cpp @@ -0,0 +1,63 @@ +#include "raytracer/raytracer.h" + +// vector operations on 4d vectors, +// reference: https://hollasch.github.io/ray4/Four-Space_Visualization_of_4D_Objects.html#chapter5 + +glm::vec4 cross4( + glm::vec4 u, + glm::vec4 v, + glm::vec4 w) { + float a = (v[0] * w[1]) - (v[1] * w[0]); + float b = (v[0] * w[2]) - (v[2] * w[0]); + float c = (v[0] * w[3]) - (v[3] * w[0]); + float d = (v[1] * w[2]) - (v[2] * w[1]); + float e = (v[1] * w[3]) - (v[3] * w[1]); + float f = (v[2] * w[3]) - (v[3] * w[2]); + + glm::vec4 result; + result[0] = (u[1] * f) - (u[2] * e) + (u[3] * d); + result[1] = -(u[0] * f) + (u[2] * c) - (u[3] * b); + result[2] = (u[0] * e) - (u[1] * c) + (u[3] * a); + result[3] = -(u[0] * d) + (u[1] * b) - (u[2] * a); + + return result; +} + +glm::vec4 dot4( + glm::vec4 u, + glm::vec4 v) { + return {u[0] * v[0], u[1] * v[1], u[2] * v[2], u[3] * v[3]}; +} + +glm::mat4 getViewMatrix4( + glm::vec4 fromPoint, + glm::vec4 toPoint, + glm::vec4 upVector, + glm::vec4 lookVector) { + + // calculate e3 basis vector, the transformation col of view matrix + if (glm::distance(fromPoint, toPoint) < 0.0001f) { + throw std::runtime_error("fromPoint and toPoint are the same"); + } + glm::vec4 e3 = glm::normalize(fromPoint - toPoint); + + // calculate e2 basis vector, from the combinatory cross of up and over with e3 + glm::vec4 e2 = cross4(upVector, lookVector, e3); + e2 = glm::normalize(e2); + if (glm::distance(e2, glm::vec4{0, 0, 0, 0}) < 0.0001f) { + throw std::runtime_error("invalid up vector"); + } + + // calculate e1 basis vector, from the cross of only the over vector + glm::vec4 e1 = cross4(lookVector, e3, e2); + e1 = glm::normalize(e1); + if (glm::distance(e1, glm::vec4{0, 0, 0, 0}) < 0.0001f) { + throw std::runtime_error("invalid over vector"); + } + + // calculate e0 basis vector, the 4d orthogonal vector to the other 3 bases + glm::vec4 e0 = cross4(e3, e2, e1); + e0 = glm::normalize(e0); + + return {e2, e1, e0, e3}; +}
\ No newline at end of file diff --git a/src/4dvecops/vectoroperations.cpp b/src/4dvecops/vectoroperations.cpp deleted file mode 100644 index d41dad3..0000000 --- a/src/4dvecops/vectoroperations.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include "raytracer/raytracer.h" - -// vector operations on 4d vectors, -// reference: https://hollasch.github.io/ray4/Four-Space_Visualization_of_4D_Objects.html#chapter5 - -glm::vec4 cross4( - glm::vec4 u, - glm::vec4 v, - glm::vec4 w) { - float a = (v[0] * w[1]) - (v[1] * w[0]); - float b = (v[0] * w[2]) - (v[2] * w[0]); - float c = (v[0] * w[3]) - (v[3] * w[0]); - float d = (v[1] * w[2]) - (v[2] * w[1]); - float e = (v[1] * w[3]) - (v[3] * w[1]); - float f = (v[2] * w[3]) - (v[3] * w[2]); - - glm::vec4 result; - result[0] = (u[1] * f) - (u[2] * e) + (u[3] * d); - result[1] = -(u[0] * f) + (u[2] * c) - (u[3] * b); - result[2] = (u[0] * e) - (u[1] * c) + (u[3] * a); - result[3] = -(u[0] * d) + (u[1] * b) - (u[2] * a); - - return result; -} - -glm::vec4 dot4( - glm::vec4 u, - glm::vec4 v) { - return {u[0] * v[0], u[1] * v[1], u[2] * v[2], u[3] * v[3]}; -}
\ No newline at end of file diff --git a/src/raytracer/raytracer.h b/src/raytracer/raytracer.h index a5450f7..4bdca6f 100644 --- a/src/raytracer/raytracer.h +++ b/src/raytracer/raytracer.h @@ -164,10 +164,9 @@ public: void keyPressEvent(QKeyEvent *event) override; void keyReleaseEvent(QKeyEvent *event) override; -// void timerEvent(QTimerEvent *event) override; - int m_timer; - void init(); + glm::mat4 getRotationMatrix4(float angleRadiansXY, float angleRadiansYZ, float angleRadiansZX, float angleRadiansXW, + float angleRadiansYW, float angleRadiansZW); signals: void xyRotationChanged(float value); void xzRotationChanged(float value); |