From 480c22ce9b50caad259e254d0127e99294b4c6ab Mon Sep 17 00:00:00 2001 From: sotech117 Date: Fri, 8 Dec 2023 15:03:20 -0500 Subject: rename src directory for vec4ops --- src/4dvecops/rotations4d.cpp | 74 -------------------------------------------- src/4dvecops/transform4d.cpp | 18 ----------- src/4dvecops/vec4ops.cpp | 64 -------------------------------------- src/4dvecops/vec4ops.h | 37 ---------------------- src/raytracer/raytracer.cpp | 48 +++++++++++++++------------- src/vec4ops/rotations4d.cpp | 74 ++++++++++++++++++++++++++++++++++++++++++++ src/vec4ops/transform4d.cpp | 18 +++++++++++ src/vec4ops/vec4ops.cpp | 64 ++++++++++++++++++++++++++++++++++++++ src/vec4ops/vec4ops.h | 37 ++++++++++++++++++++++ 9 files changed, 219 insertions(+), 215 deletions(-) delete mode 100644 src/4dvecops/rotations4d.cpp delete mode 100644 src/4dvecops/transform4d.cpp delete mode 100644 src/4dvecops/vec4ops.cpp delete mode 100644 src/4dvecops/vec4ops.h create mode 100644 src/vec4ops/rotations4d.cpp create mode 100644 src/vec4ops/transform4d.cpp create mode 100644 src/vec4ops/vec4ops.cpp create mode 100644 src/vec4ops/vec4ops.h (limited to 'src') diff --git a/src/4dvecops/rotations4d.cpp b/src/4dvecops/rotations4d.cpp deleted file mode 100644 index 4943c7f..0000000 --- a/src/4dvecops/rotations4d.cpp +++ /dev/null @@ -1,74 +0,0 @@ -#include "raytracer/raytracer.h" -#include "4dvecops/vec4ops.h" - -glm::mat4 Vec4Ops::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 Vec4Ops::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 Vec4Ops::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 Vec4Ops::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 Vec4Ops::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 Vec4Ops::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; -} \ No newline at end of file diff --git a/src/4dvecops/transform4d.cpp b/src/4dvecops/transform4d.cpp deleted file mode 100644 index 5cc51f3..0000000 --- a/src/4dvecops/transform4d.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "vec4ops.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 Vec4Ops::transformPoint4(glm::vec4 point4, glm::mat4 transformDirectionMatrix, glm::vec4 translationPointVector) { - // do the translation then direction - point4 = transformDirectionMatrix * point4; - point4 += translationPointVector; - return point4; -} - -glm::vec4 Vec4Ops::inverseTransformPoint4(glm::vec4 point4, glm::mat4 inverseTransformDirectionMatrix, glm::vec4 inverseTranslationPointVector) { - // do the direction then translation - point4 += inverseTranslationPointVector; - point4 = inverseTranslationPointVector * point4; - return point4; -} \ No newline at end of file diff --git a/src/4dvecops/vec4ops.cpp b/src/4dvecops/vec4ops.cpp deleted file mode 100644 index 80cebaf..0000000 --- a/src/4dvecops/vec4ops.cpp +++ /dev/null @@ -1,64 +0,0 @@ -#include -#include "vec4ops.h" - -// vector operations on 4d vectors, -// reference: https://hollasch.github.io/ray4/Four-Space_Visualization_of_4D_Objects.html#chapter5 - -glm::vec4 Vec4Ops::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 Vec4Ops::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 Vec4Ops::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.h b/src/4dvecops/vec4ops.h deleted file mode 100644 index d1c3ac8..0000000 --- a/src/4dvecops/vec4ops.h +++ /dev/null @@ -1,37 +0,0 @@ -// -// Created by Michael Foiani on 12/8/23. -// - -#ifndef PROJECTS_RAY_VEC4OPS_H -#define PROJECTS_RAY_VEC4OPS_H - -#include - -class Vec4Ops { -public: - - static glm::mat4 getRotationMatrix4XY(float angleRadians); - - static glm::mat4 getRotationMatrix4YZ(float angleRadians); - - static glm::mat4 getRotationMatrix4ZX(float angleRadians); - - static glm::mat4 getRotationMatrix4XW(float angleRadians); - - static glm::mat4 getRotationMatrix4YW(float angleRadians); - - static glm::mat4 getRotationMatrix4ZW(float angleRadians); - - static glm::vec4 transformPoint4(glm::vec4 point4, glm::mat4 transformDirectionMatrix, glm::vec4 translationPointVector); - - static glm::vec4 inverseTransformPoint4(glm::vec4 point4, glm::mat4 inverseTransformDirectionMatrix, - glm::vec4 inverseTranslationPointVector); - - static glm::vec4 cross4(glm::vec4 u, glm::vec4 v, glm::vec4 w); - - static glm::vec4 dot4(glm::vec4 u, glm::vec4 v); - - static glm::mat4 getViewMatrix4(glm::vec4 fromPoint, glm::vec4 toPoint, glm::vec4 upVector, glm::vec4 lookVector); -}; - -#endif //PROJECTS_RAY_VEC4OPS_H diff --git a/src/raytracer/raytracer.cpp b/src/raytracer/raytracer.cpp index be31b8a..6b33150 100644 --- a/src/raytracer/raytracer.cpp +++ b/src/raytracer/raytracer.cpp @@ -7,7 +7,7 @@ #include "mainwindow.h" #include #include - +#include "vec4ops/vec4ops.h" // RayTracer::RayTracer(const Config &config) : m_config(config) {} RayTracer::RayTracer(QWidget *parent) : QWidget(parent) { @@ -23,13 +23,8 @@ RayTracer::RayTracer(QWidget *parent) : QWidget(parent) { } +// updated to use 4D void RayTracer::render(RGBA *imageData, const RayTraceScene &scene) { - if(m_enableParallelism) - { - renderParallel(imageData, scene); - return; - } - // naive rendering Camera camera = scene.getCamera(); float cameraDepth = 1.f; @@ -39,21 +34,30 @@ void RayTracer::render(RGBA *imageData, const RayTraceScene &scene) { for (int imageRow = 0; imageRow < scene.height(); imageRow++) { for (int imageCol = 0; imageCol < scene.width(); imageCol++) { - float xCameraSpace = viewplaneWidth * - (-.5f + (imageCol + .5f) / scene.width()); - float yCameraSpace = viewplaneHeight * - (-.5f + (imageRow + .5f) / scene.height()); - - glm::vec4 pixelDirCamera{xCameraSpace, -yCameraSpace, -cameraDepth, 0.f}; //w=0 for dir - glm::vec4 eyeCamera{0.f, 0.f, 0.f, 1.f}; // w=1.f for point - - // convert to world space - glm::vec4 pWorld = camera.getInverseViewMatrix() * eyeCamera; - glm::vec4 dWorld = glm::normalize(camera.getInverseViewMatrix() * pixelDirCamera); - - // cast ray! - glm::vec4 pixel = getPixelFromRay(pWorld, dWorld, scene); - imageData[imageRow * scene.width() + imageCol] = toRGBA(pixel); + // FIXME: for now, use height as depth + for (int imageDepth = 0; imageDepth < scene.height(); imageDepth++) { + // compute the ray + float x = (imageCol - scene.width()/2.f) * viewplaneWidth / scene.width(); + float y = (imageRow - scene.height()/2.f) * viewplaneHeight / scene.height(); + float z = (imageDepth - scene.height()/2.f) * viewplaneHeight / scene.height(); + float camera4dDepth = 1; + + glm::vec4 pCamera = + glm::vec4 pWorld = Vec4Ops::transformPoint4(camera.getvec4(x, y, z, camera4dDepth); + glm::vec4 dWorld = glm::vec4(0.f, 0.f, -1.f, 0.f); + + // get the pixel color + glm::vec4 pixelColor = getPixelFromRay(pWorld, dWorld, scene, 0); + + // set the pixel color + int index = imageRow * scene.width() + imageCol; + imageData[index] = RGBA{ + (std::uint8_t) (pixelColor.r * 255.f), + (std::uint8_t) (pixelColor.g * 255.f), + (std::uint8_t) (pixelColor.b * 255.f), + (std::uint8_t) (pixelColor.a * 255.f) + }; + } } } } diff --git a/src/vec4ops/rotations4d.cpp b/src/vec4ops/rotations4d.cpp new file mode 100644 index 0000000..4943c7f --- /dev/null +++ b/src/vec4ops/rotations4d.cpp @@ -0,0 +1,74 @@ +#include "raytracer/raytracer.h" +#include "4dvecops/vec4ops.h" + +glm::mat4 Vec4Ops::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 Vec4Ops::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 Vec4Ops::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 Vec4Ops::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 Vec4Ops::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 Vec4Ops::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; +} \ No newline at end of file diff --git a/src/vec4ops/transform4d.cpp b/src/vec4ops/transform4d.cpp new file mode 100644 index 0000000..5cc51f3 --- /dev/null +++ b/src/vec4ops/transform4d.cpp @@ -0,0 +1,18 @@ +#include "vec4ops.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 Vec4Ops::transformPoint4(glm::vec4 point4, glm::mat4 transformDirectionMatrix, glm::vec4 translationPointVector) { + // do the translation then direction + point4 = transformDirectionMatrix * point4; + point4 += translationPointVector; + return point4; +} + +glm::vec4 Vec4Ops::inverseTransformPoint4(glm::vec4 point4, glm::mat4 inverseTransformDirectionMatrix, glm::vec4 inverseTranslationPointVector) { + // do the direction then translation + point4 += inverseTranslationPointVector; + point4 = inverseTranslationPointVector * point4; + return point4; +} \ No newline at end of file diff --git a/src/vec4ops/vec4ops.cpp b/src/vec4ops/vec4ops.cpp new file mode 100644 index 0000000..80cebaf --- /dev/null +++ b/src/vec4ops/vec4ops.cpp @@ -0,0 +1,64 @@ +#include +#include "vec4ops.h" + +// vector operations on 4d vectors, +// reference: https://hollasch.github.io/ray4/Four-Space_Visualization_of_4D_Objects.html#chapter5 + +glm::vec4 Vec4Ops::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 Vec4Ops::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 Vec4Ops::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/vec4ops/vec4ops.h b/src/vec4ops/vec4ops.h new file mode 100644 index 0000000..d1c3ac8 --- /dev/null +++ b/src/vec4ops/vec4ops.h @@ -0,0 +1,37 @@ +// +// Created by Michael Foiani on 12/8/23. +// + +#ifndef PROJECTS_RAY_VEC4OPS_H +#define PROJECTS_RAY_VEC4OPS_H + +#include + +class Vec4Ops { +public: + + static glm::mat4 getRotationMatrix4XY(float angleRadians); + + static glm::mat4 getRotationMatrix4YZ(float angleRadians); + + static glm::mat4 getRotationMatrix4ZX(float angleRadians); + + static glm::mat4 getRotationMatrix4XW(float angleRadians); + + static glm::mat4 getRotationMatrix4YW(float angleRadians); + + static glm::mat4 getRotationMatrix4ZW(float angleRadians); + + static glm::vec4 transformPoint4(glm::vec4 point4, glm::mat4 transformDirectionMatrix, glm::vec4 translationPointVector); + + static glm::vec4 inverseTransformPoint4(glm::vec4 point4, glm::mat4 inverseTransformDirectionMatrix, + glm::vec4 inverseTranslationPointVector); + + static glm::vec4 cross4(glm::vec4 u, glm::vec4 v, glm::vec4 w); + + static glm::vec4 dot4(glm::vec4 u, glm::vec4 v); + + static glm::mat4 getViewMatrix4(glm::vec4 fromPoint, glm::vec4 toPoint, glm::vec4 upVector, glm::vec4 lookVector); +}; + +#endif //PROJECTS_RAY_VEC4OPS_H -- cgit v1.2.3-70-g09d2