From 3a3c06b9bcb6d98bd774d1fdb894a4aa63fc948a Mon Sep 17 00:00:00 2001 From: sotech117 Date: Fri, 8 Dec 2023 14:29:56 -0500 Subject: refactor vec4 ops --- CMakeLists.txt | 2 +- cmake-build-debug/.ninja_deps | Bin 183008 -> 190512 bytes cmake-build-debug/.ninja_log | 6 +++ cmake-build-debug/Testing/Temporary/LastTest.log | 4 +- cmake-build-debug/projects_ray | Bin 0 -> 1657145 bytes src/4dvecops/vec4operations.cpp | 63 ----------------------- src/4dvecops/vec4ops.cpp | 63 +++++++++++++++++++++++ 7 files changed, 72 insertions(+), 66 deletions(-) create mode 100755 cmake-build-debug/projects_ray delete mode 100644 src/4dvecops/vec4operations.cpp create mode 100644 src/4dvecops/vec4ops.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 3af6982..a0db553 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,7 +63,7 @@ add_executable(${PROJECT_NAME} src/accelerate/kdtree.h src/accelerate/bvh.cpp src/accelerate/bvh.h - src/4dvecops/vec4operations.cpp + src/4dvecops/vec4ops.cpp src/4dvecops/rotations4d.cpp src/4dvecops/transform4d.cpp src/4dvecops/vec4ops.h diff --git a/cmake-build-debug/.ninja_deps b/cmake-build-debug/.ninja_deps index ec03971..d347892 100644 Binary files a/cmake-build-debug/.ninja_deps and b/cmake-build-debug/.ninja_deps differ diff --git a/cmake-build-debug/.ninja_log b/cmake-build-debug/.ninja_log index fa84642..374ec09 100644 --- a/cmake-build-debug/.ninja_log +++ b/cmake-build-debug/.ninja_log @@ -36,3 +36,9 @@ 2277 3074 1702062978573573842 CMakeFiles/projects_ray.dir/projects_ray_autogen/mocs_compilation.cpp.o 75d01056e19b5cdd 1934 3161 1702062978659402142 CMakeFiles/projects_ray.dir/src/raytracer/raytracer.cpp.o 78383fd2d33d7c09 2190 3283 1702062978781768457 CMakeFiles/projects_ray.dir/src/main.cpp.o 18bcd3026e919fd3 +7 34 1702063494707727424 projects_ray_autogen/timestamp d580242d2b9854b3 +7 34 1702063494707727424 projects_ray_autogen/mocs_compilation.cpp d580242d2b9854b3 +7 34 1702063494707727424 /Users/sotech117/Documents/master/graphics/the-all-americans-in-cs1230/cmake-build-debug/projects_ray_autogen/timestamp d580242d2b9854b3 +7 34 1702063494707727424 /Users/sotech117/Documents/master/graphics/the-all-americans-in-cs1230/cmake-build-debug/projects_ray_autogen/mocs_compilation.cpp d580242d2b9854b3 +37 1002 1702063495670965252 CMakeFiles/projects_ray.dir/src/utils/scenefilereader.cpp.o 41959d435611c983 +1002 1148 1702063495820276807 projects_ray 69591c53d0e17f8e diff --git a/cmake-build-debug/Testing/Temporary/LastTest.log b/cmake-build-debug/Testing/Temporary/LastTest.log index 0c30d59..84b844b 100644 --- a/cmake-build-debug/Testing/Temporary/LastTest.log +++ b/cmake-build-debug/Testing/Temporary/LastTest.log @@ -1,3 +1,3 @@ -Start testing: Dec 08 13:39 EST +Start testing: Dec 08 14:24 EST ---------------------------------------------------------- -End testing: Dec 08 13:39 EST +End testing: Dec 08 14:24 EST diff --git a/cmake-build-debug/projects_ray b/cmake-build-debug/projects_ray new file mode 100755 index 0000000..9e19353 Binary files /dev/null and b/cmake-build-debug/projects_ray differ diff --git a/src/4dvecops/vec4operations.cpp b/src/4dvecops/vec4operations.cpp deleted file mode 100644 index 1ffe673..0000000 --- a/src/4dvecops/vec4operations.cpp +++ /dev/null @@ -1,63 +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]}; -} - -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/vec4ops.cpp b/src/4dvecops/vec4ops.cpp new file mode 100644 index 0000000..1ffe673 --- /dev/null +++ b/src/4dvecops/vec4ops.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 -- cgit v1.2.3-70-g09d2