diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/camera/camera.cpp | 34 | ||||
-rw-r--r-- | src/vec4ops/vec4ops.cpp | 52 | ||||
-rw-r--r-- | src/vec4ops/vec4ops.h | 4 |
3 files changed, 18 insertions, 72 deletions
diff --git a/src/camera/camera.cpp b/src/camera/camera.cpp index b2d4b12..1b37938 100644 --- a/src/camera/camera.cpp +++ b/src/camera/camera.cpp @@ -9,37 +9,11 @@ Camera::Camera(SceneCameraData cameraData) : m_focalLength(cameraData.focalLength), m_aperture(cameraData.aperture) { - // m_viewMatrix = Vec4Ops::getViewMatrix4(cameraData.pos, glm::vec4(0.f), cameraData.up, cameraData.look); - // m_translationVector = glm::vec4{-cameraData.pos.x, -cameraData.pos.y, -cameraData.pos.z, -cameraData.pos.w}; + m_viewMatrix = Vec4Ops::getViewMatrix4(cameraData.look, cameraData.up, cameraData.over); + m_translationVector = -cameraData.pos; - // m_inverseViewMatrix = glm::inverse(m_viewMatrix); - // m_inverseTranslationVector = -m_translationVector; - glm::vec3 look3{cameraData.look.x, cameraData.look.y, cameraData.look.z}; - glm::vec3 up3{cameraData.up.x, cameraData.up.y, cameraData.up.z}; - - // calculate new basis - glm::vec3 e0 = -glm::normalize(look3); - glm::vec3 e1 = glm::normalize(up3 - glm::dot(up3, e0) * e0); - glm::vec3 e2 = glm::cross(e1, e0); - - glm::mat4 alignment - { - e2.x, e1.x, e0.x, 0.f, - e2.y, e1.y, e0.y, 0.f, - e2.z, e1.z, e0.z, 0.f, - 0.f, 0.f, 0.f, 1.f - }; - glm::mat4 translation - { - 1.f, 0.f, 0.f, 0.f, - 0.f, 1.f, 0.f, 0.f, - 0.f, 0.f, 1.f, 0.f, - -cameraData.pos.x, -cameraData.pos.y, -cameraData.pos.z, 1.f - }; - - m_viewMatrix = alignment * translation; - - m_inverseViewMatrix = glm::inverse(m_viewMatrix); + m_inverseViewMatrix = glm::inverse(m_viewMatrix); + m_inverseTranslationVector = -m_translationVector; } diff --git a/src/vec4ops/vec4ops.cpp b/src/vec4ops/vec4ops.cpp index ecac3b1..9bd29e8 100644 --- a/src/vec4ops/vec4ops.cpp +++ b/src/vec4ops/vec4ops.cpp @@ -34,57 +34,29 @@ glm::vec4 Vec4Ops::dot4( } glm::mat4 Vec4Ops::getViewMatrix4( - glm::vec4 fromPoint, - glm::vec4 toPoint, glm::vec4 upVector, - glm::vec4 lookVector) { + glm::vec4 lookVector, + glm::vec4 overVector) { + // start with the e3 basis vector, the normalized look vector + glm::vec4 e3 = glm::normalize(lookVector); - // rotation matrices for each plane - glm::mat4 rotMatrixXY = getRotationMatrix4XY(glm::radians(settings.xy)); - glm::mat4 rotMatrixXZ = getRotationMatrix4ZX(glm::radians(settings.xz)); - glm::mat4 rotMatrixYZ = getRotationMatrix4YZ(glm::radians(settings.yz)); - glm::mat4 rotMatrixXW = getRotationMatrix4XW(glm::radians(settings.xw)); - glm::mat4 rotMatrixYW = getRotationMatrix4YW(glm::radians(settings.yw)); - glm::mat4 rotMatrixZW = getRotationMatrix4ZW(glm::radians(settings.zw)); - - glm::mat4 combinedRotationMatrix = rotMatrixXY * rotMatrixYZ * rotMatrixXZ * rotMatrixXW * rotMatrixYW * rotMatrixZW; - - // 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"); - std::cout << "warn: fromPoint and toPoint are the same" << std::endl; - } - 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) { + // calculate e0 basis vector, from the combinatory cross of up and over with e3 + glm::vec4 e0 = cross4(upVector, overVector, e3); + e0 = glm::normalize(e0); + if (glm::distance(e0, 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); + glm::vec4 e1 = cross4(overVector, e3, e0); 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); - - // Apply the combined rotation matrix to the view basis vectors - e0 = combinedRotationMatrix * e0; - e1 = combinedRotationMatrix * e1; - e2 = combinedRotationMatrix * e2; - e3 = combinedRotationMatrix * e3; - - // Normalizing might be necessary after applying the rotation - e0 = glm::normalize(e0); - e1 = glm::normalize(e1); + // calculate e2 basis vector, the 4d orthogonal vector to the other 3 bases + glm::vec4 e2 = cross4(e3, e0, e1); e2 = glm::normalize(e2); - e3 = glm::normalize(e3); - return {e2, e1, e0, e3}; + return {e0, e1, e2, e3}; }
\ No newline at end of file diff --git a/src/vec4ops/vec4ops.h b/src/vec4ops/vec4ops.h index a21bcc9..796722a 100644 --- a/src/vec4ops/vec4ops.h +++ b/src/vec4ops/vec4ops.h @@ -31,11 +31,11 @@ public: 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); - static glm::vec4 transformDir4(glm::vec4 dir4, glm::mat4 transformDirectionMatrix); static glm::vec4 inverseTransformDir4(glm::vec4 dir4, glm::mat4 inverseTransformDirectionMatrix); + + static glm::mat4 getViewMatrix4(glm::vec4 upVector, glm::vec4 lookVector, glm::vec4 overVector); }; #endif //PROJECTS_RAY_VEC4OPS_H |