diff --git a/09_GeometryCreator/main.cpp b/09_GeometryCreator/main.cpp index cb3c21f4d..06521a6d2 100644 --- a/09_GeometryCreator/main.cpp +++ b/09_GeometryCreator/main.cpp @@ -2,6 +2,8 @@ // This file is part of the "Nabla Engine". // For conditions of distribution and use, see copyright notice in nabla.h +#include +#include #include "common.hpp" @@ -73,7 +75,7 @@ class GeometryCreatorApp final : public MonoWindowApplication, public BuiltinRes { core::vectorSIMDf cameraPosition(-5.81655884, 2.58630896, -4.23974705); core::vectorSIMDf cameraTarget(-0.349590302, -0.213266611, 0.317821503); - matrix4SIMD projectionMatrix = matrix4SIMD::buildProjectionMatrixPerspectiveFovLH(core::radians(60.0f), float(m_initialResolution.x)/float(m_initialResolution.y), 0.1, 10000); + float32_t4x4 projectionMatrix = hlsl::math::thin_lens::lhPerspectiveFovMatrix(core::radians(60.0f), float(m_initialResolution.x) / m_initialResolution.y, 0.1f, 10000.0f); camera = Camera(cameraPosition, cameraTarget, projectionMatrix, 1.069f, 0.4f); } @@ -139,13 +141,8 @@ class GeometryCreatorApp final : public MonoWindowApplication, public BuiltinRes cb->beginRenderPass(info, IGPUCommandBuffer::SUBPASS_CONTENTS::INLINE); } - float32_t3x4 viewMatrix; - float32_t4x4 viewProjMatrix; - // TODO: get rid of legacy matrices - { - memcpy(&viewMatrix,camera.getViewMatrix().pointer(),sizeof(viewMatrix)); - memcpy(&viewProjMatrix,camera.getConcatenatedMatrix().pointer(),sizeof(viewProjMatrix)); - } + float32_t3x4 viewMatrix = camera.getViewMatrix(); + float32_t4x4 viewProjMatrix = camera.getConcatenatedMatrix(); const auto viewParams = CSimpleDebugRenderer::SViewParams(viewMatrix,viewProjMatrix); // tear down scene every frame @@ -251,7 +248,7 @@ class GeometryCreatorApp final : public MonoWindowApplication, public BuiltinRes InputSystem::ChannelReader keyboard; // - Camera camera = Camera(core::vectorSIMDf(0, 0, 0), core::vectorSIMDf(0, 0, 0), core::matrix4SIMD()); + Camera camera = Camera(core::vectorSIMDf(0, 0, 0), core::vectorSIMDf(0, 0, 0), hlsl::float32_t4x4()); uint16_t gcIndex = {}; diff --git a/12_MeshLoaders/main.cpp b/12_MeshLoaders/main.cpp index d80fa8998..241fa5117 100644 --- a/12_MeshLoaders/main.cpp +++ b/12_MeshLoaders/main.cpp @@ -5,6 +5,7 @@ #include "common.hpp" #include "../3rdparty/portable-file-dialogs/portable-file-dialogs.h" +#include #ifdef NBL_BUILD_MITSUBA_LOADER #include "nbl/ext/MitsubaLoader/CSerializedLoader.h" @@ -161,13 +162,8 @@ class MeshLoadersApp final : public MonoWindowApplication, public BuiltinResourc } // draw scene { - float32_t3x4 viewMatrix; - float32_t4x4 viewProjMatrix; - // TODO: get rid of legacy matrices - { - memcpy(&viewMatrix, camera.getViewMatrix().pointer(), sizeof(viewMatrix)); - memcpy(&viewProjMatrix, camera.getConcatenatedMatrix().pointer(), sizeof(viewProjMatrix)); - } + float32_t3x4 viewMatrix = camera.getViewMatrix(); + float32_t4x4 viewProjMatrix = camera.getConcatenatedMatrix(); m_renderer->render(cb, CSimpleDebugRenderer::SViewParams(viewMatrix, viewProjMatrix)); } cb->endRenderPass(); @@ -456,7 +452,7 @@ class MeshLoadersApp final : public MonoWindowApplication, public BuiltinResourc { const auto measure = hlsl::length(diagonal); const auto aspectRatio = float(m_window->getWidth()) / float(m_window->getHeight()); - camera.setProjectionMatrix(core::matrix4SIMD::buildProjectionMatrixPerspectiveFovRH(1.2f, aspectRatio, distance * measure * 0.1, measure * 4.0)); + camera.setProjectionMatrix(hlsl::math::thin_lens::rhPerspectiveFovMatrix(1.2f, aspectRatio, distance * measure * 0.1, measure * 4.0)); camera.setMoveSpeed(measure * 0.04); } const auto pos = bound.maxVx + diagonal * distance; @@ -492,7 +488,7 @@ class MeshLoadersApp final : public MonoWindowApplication, public BuiltinResourc InputSystem::ChannelReader mouse; InputSystem::ChannelReader keyboard; // - Camera camera = Camera(core::vectorSIMDf(0, 0, 0), core::vectorSIMDf(0, 0, 0), core::matrix4SIMD()); + Camera camera = Camera(core::vectorSIMDf(0, 0, 0), core::vectorSIMDf(0, 0, 0), hlsl::float32_t4x4()); // mutables std::string m_modelPath; diff --git a/61_UI/main.cpp b/61_UI/main.cpp index 643cab079..503a2e421 100644 --- a/61_UI/main.cpp +++ b/61_UI/main.cpp @@ -3,6 +3,7 @@ // For conditions of distribution and use, see copyright notice in nabla.h #include "common.hpp" +#include /* Renders scene texture to an offscreen framebuffer whose color attachment is then sampled into a imgui window. @@ -252,14 +253,9 @@ class UISampleApp final : public MonoWindowApplication, public BuiltinResourcesA } // draw scene { - float32_t3x4 viewMatrix; - float32_t4x4 viewProjMatrix; - // TODO: get rid of legacy matrices - { - const auto& camera = interface.camera; - memcpy(&viewMatrix,camera.getViewMatrix().pointer(),sizeof(viewMatrix)); - memcpy(&viewProjMatrix,camera.getConcatenatedMatrix().pointer(),sizeof(viewProjMatrix)); - } + const auto& camera = interface.camera; + float32_t3x4 viewMatrix = camera.getViewMatrix(); + float32_t4x4 viewProjMatrix = camera.getConcatenatedMatrix(); const auto viewParams = CSimpleDebugRenderer::SViewParams(viewMatrix,viewProjMatrix); // tear down scene every frame @@ -570,21 +566,21 @@ class UISampleApp final : public MonoWindowApplication, public BuiltinResourcesA // TODO: why is this a lambda and not just an assignment in a scope ? camera.setProjectionMatrix([&]() { - matrix4SIMD projection; + hlsl::float32_t4x4 projection; if (isPerspective) if(isLH) - projection = matrix4SIMD::buildProjectionMatrixPerspectiveFovLH(core::radians(fov), io.DisplaySize.x / io.DisplaySize.y, zNear, zFar); + projection = hlsl::math::thin_lens::lhPerspectiveFovMatrix(core::radians(fov), io.DisplaySize.x / io.DisplaySize.y, zNear, zFar); else - projection = matrix4SIMD::buildProjectionMatrixPerspectiveFovRH(core::radians(fov), io.DisplaySize.x / io.DisplaySize.y, zNear, zFar); + projection = hlsl::math::thin_lens::rhPerspectiveFovMatrix(core::radians(fov), io.DisplaySize.x / io.DisplaySize.y, zNear, zFar); else { float viewHeight = viewWidth * io.DisplaySize.y / io.DisplaySize.x; if(isLH) - projection = matrix4SIMD::buildProjectionMatrixOrthoLH(viewWidth, viewHeight, zNear, zFar); + projection = hlsl::math::thin_lens::lhPerspectiveFovMatrix(viewWidth, viewHeight, zNear, zFar); else - projection = matrix4SIMD::buildProjectionMatrixOrthoRH(viewWidth, viewHeight, zNear, zFar); + projection = hlsl::math::thin_lens::rhPerspectiveFovMatrix(viewWidth, viewHeight, zNear, zFar); } return projection; @@ -720,33 +716,32 @@ class UISampleApp final : public MonoWindowApplication, public BuiltinResourcesA * note it also modifies input view matrix but projection matrix is immutable */ -// TODO: do all computation using `hlsl::matrix` and its `hlsl::float32_tNxM` aliases static struct { - core::matrix4SIMD view, projection, model; + hlsl::float32_t4x4 view, projection, model; } imguizmoM16InOut; ImGuizmo::SetID(0u); - imguizmoM16InOut.view = core::transpose(matrix4SIMD(camera.getViewMatrix())); - imguizmoM16InOut.projection = core::transpose(camera.getProjectionMatrix()); - imguizmoM16InOut.model = core::transpose(matrix4SIMD(model)); + imguizmoM16InOut.view = hlsl::transpose(hlsl::math::linalg::promote_affine<4,4,3,4>(camera.getViewMatrix())); + imguizmoM16InOut.projection = hlsl::transpose(camera.getProjectionMatrix()); + imguizmoM16InOut.model = hlsl::transpose(hlsl::math::linalg::promote_affine<4,4,3,4>(model)); { if (flipGizmoY) // note we allow to flip gizmo just to match our coordinates imguizmoM16InOut.projection[1][1] *= -1.f; // https://johannesugb.github.io/gpu-programming/why-do-opengl-proj-matrices-fail-in-vulkan/ - transformParams.editTransformDecomposition = true; - sceneResolution = EditTransform(imguizmoM16InOut.view.pointer(), imguizmoM16InOut.projection.pointer(), imguizmoM16InOut.model.pointer(), transformParams); + transformParams.editTransformDecomposition = true; + sceneResolution = EditTransform(&imguizmoM16InOut.view[0][0], &imguizmoM16InOut.projection[0][0], &imguizmoM16InOut.model[0][0], transformParams); } - model = core::transpose(imguizmoM16InOut.model).extractSub3x4(); + model = hlsl::math::linalg::truncate<3,4,4,4>(hlsl::transpose(imguizmoM16InOut.model)); // to Nabla + update camera & model matrices // TODO: make it more nicely, extract: // - Position by computing inverse of the view matrix and grabbing its translation // - Target from 3rd row without W component of view matrix multiplied by some arbitrary distance value (can be the length of position from origin) and adding the position // But then set the view matrix this way anyway, because up-vector may not be compatible const auto& view = camera.getViewMatrix(); - const_cast(view) = core::transpose(imguizmoM16InOut.view).extractSub3x4(); // a hack, correct way would be to use inverse matrix and get position + target because now it will bring you back to last position & target when switching from gizmo move to manual move (but from manual to gizmo is ok) + const_cast(view) = hlsl::math::linalg::truncate<3,4,4,4>(hlsl::transpose(imguizmoM16InOut.view)); // a hack, correct way would be to use inverse matrix and get position + target because now it will bring you back to last position & target when switching from gizmo move to manual move (but from manual to gizmo is ok) // update concatanated matrix const auto& projection = camera.getProjectionMatrix(); camera.setProjectionMatrix(projection); @@ -783,9 +778,9 @@ class UISampleApp final : public MonoWindowApplication, public BuiltinResourcesA ImGui::Separator(); }; - addMatrixTable("Model Matrix", "ModelMatrixTable", 3, 4, model.pointer()); - addMatrixTable("Camera View Matrix", "ViewMatrixTable", 3, 4, view.pointer()); - addMatrixTable("Camera View Projection Matrix", "ViewProjectionMatrixTable", 4, 4, projection.pointer(), false); + addMatrixTable("Model Matrix", "ModelMatrixTable", 3, 4, &model[0][0]); + addMatrixTable("Camera View Matrix", "ViewMatrixTable", 3, 4, &view[0][0]); + addMatrixTable("Camera View Projection Matrix", "ViewProjectionMatrixTable", 4, 4, &projection[0][0], false); ImGui::End(); } @@ -867,9 +862,9 @@ class UISampleApp final : public MonoWindowApplication, public BuiltinResourcesA smart_refctd_ptr subAllocDS; SubAllocatedDescriptorSet::value_type renderColorViewDescIndex = SubAllocatedDescriptorSet::invalid_value; // - Camera camera = Camera(core::vectorSIMDf(0, 0, 0), core::vectorSIMDf(0, 0, 0), core::matrix4SIMD()); + Camera camera = Camera(core::vectorSIMDf(0, 0, 0), core::vectorSIMDf(0, 0, 0), hlsl::float32_t4x4()); // mutables - core::matrix3x4SIMD model; + hlsl::float32_t3x4 model = hlsl::math::linalg::diagonal(1.0f); std::string_view objectName; TransformRequestParams transformParams; uint16_t2 sceneResolution = {1280,720}; diff --git a/67_RayQueryGeometry/include/common.hpp b/67_RayQueryGeometry/include/common.hpp index 84b0a3dcf..ac774b0df 100644 --- a/67_RayQueryGeometry/include/common.hpp +++ b/67_RayQueryGeometry/include/common.hpp @@ -23,9 +23,9 @@ using GeometryCollectionData = core::smart_refctd_ptr; using GeometryData = std::variant; struct ReferenceObjectCpu { - core::matrix3x4SIMD transform; + hlsl::float32_t3x4 transform; GeometryData data; - uint32_t instanceID; + uint32_t instanceID; }; } diff --git a/67_RayQueryGeometry/main.cpp b/67_RayQueryGeometry/main.cpp index 2783385f2..2463bac85 100644 --- a/67_RayQueryGeometry/main.cpp +++ b/67_RayQueryGeometry/main.cpp @@ -2,6 +2,7 @@ // This file is part of the "Nabla Engine". // For conditions of distribution and use, see copyright notice in nabla.h #include "common.hpp" +#include class RayQueryGeometryApp final : public SimpleWindowedApplication, public BuiltinResourcesApplication { @@ -197,7 +198,7 @@ class RayQueryGeometryApp final : public SimpleWindowedApplication, public Built { core::vectorSIMDf cameraPosition(-5.81655884, 2.58630896, -4.23974705); core::vectorSIMDf cameraTarget(-0.349590302, -0.213266611, 0.317821503); - matrix4SIMD projectionMatrix = matrix4SIMD::buildProjectionMatrixPerspectiveFovLH(core::radians(60.0f), float(WIN_W) / WIN_H, 0.1, 1000); + hlsl::float32_t4x4 projectionMatrix = hlsl::math::thin_lens::lhPerspectiveFovMatrix(core::radians(60.0f), float(WIN_W) / WIN_H, 0.1f, 1000.0f); camera = Camera(cameraPosition, cameraTarget, projectionMatrix, 1.069f, 0.4f); } @@ -266,13 +267,10 @@ class RayQueryGeometryApp final : public SimpleWindowedApplication, public Built const auto projectionMatrix = camera.getProjectionMatrix(); const auto viewProjectionMatrix = camera.getConcatenatedMatrix(); - core::matrix3x4SIMD modelMatrix; - modelMatrix.setTranslation(nbl::core::vectorSIMDf(0, 0, 0, 0)); - modelMatrix.setRotation(quaternion(0, 0, 0)); + hlsl::float32_t3x4 modelMatrix = hlsl::math::linalg::identity(); - core::matrix4SIMD modelViewProjectionMatrix = core::concatenateBFollowedByA(viewProjectionMatrix, modelMatrix); - core::matrix4SIMD invModelViewProjectionMatrix; - modelViewProjectionMatrix.getInverseTransform(invModelViewProjectionMatrix); + hlsl::float32_t4x4 modelViewProjectionMatrix = nbl::hlsl::math::linalg::promoted_mul(viewProjectionMatrix, modelMatrix); + hlsl::float32_t4x4 invModelViewProjectionMatrix = hlsl::inverse(modelViewProjectionMatrix); auto* queue = getGraphicsQueue(); @@ -305,7 +303,7 @@ class RayQueryGeometryApp final : public SimpleWindowedApplication, public Built const core::vector3df camPos = camera.getPosition().getAsVector3df(); pc.camPos = { camPos.X, camPos.Y, camPos.Z }; - memcpy(&pc.invMVP, invModelViewProjectionMatrix.pointer(), sizeof(pc.invMVP)); + pc.invMVP = invModelViewProjectionMatrix; pc.scaleNDC = { 2.f / WIN_W, -2.f / WIN_H }; pc.offsetNDC = { -1.f, 1.f }; @@ -494,8 +492,8 @@ class RayQueryGeometryApp final : public SimpleWindowedApplication, public Built auto transform_i = 0; auto nextTransform = [&transform_i]() { - core::matrix3x4SIMD transform; - transform.setTranslation(nbl::core::vectorSIMDf(5.f * transform_i, 0, 0, 0)); + hlsl::float32_t3x4 transform = hlsl::math::linalg::identity(); + hlsl::math::linalg::setTranslation(transform, hlsl::float32_t3(5.f * transform_i, 0.0f, 0.0f)); transform_i++; return transform; }; @@ -981,7 +979,7 @@ class RayQueryGeometryApp final : public SimpleWindowedApplication, public Built InputSystem::ChannelReader mouse; InputSystem::ChannelReader keyboard; - Camera camera = Camera(core::vectorSIMDf(0, 0, 0), core::vectorSIMDf(0, 0, 0), core::matrix4SIMD()); + Camera camera = Camera(core::vectorSIMDf(0, 0, 0), core::vectorSIMDf(0, 0, 0), hlsl::float32_t4x4()); video::CDumbPresentationOracle oracle; smart_refctd_ptr geometryInfoBuffer; diff --git a/70_FLIPFluids/main.cpp b/70_FLIPFluids/main.cpp index 899d00ba4..4e7a9aeae 100644 --- a/70_FLIPFluids/main.cpp +++ b/70_FLIPFluids/main.cpp @@ -6,6 +6,7 @@ #include "nbl/examples/examples.hpp" // TODO: why is it not in nabla.h ? #include "nbl/asset/metadata/CHLSLMetadata.h" +#include using namespace nbl; using namespace nbl::core; @@ -231,7 +232,7 @@ class FLIPFluidsApp final : public SimpleWindowedApplication, public BuiltinReso float zNear = 0.1f, zFar = 10000.f; core::vectorSIMDf cameraPosition(14, 8, 12); core::vectorSIMDf cameraTarget(0, 0, 0); - matrix4SIMD projectionMatrix = matrix4SIMD::buildProjectionMatrixPerspectiveFovLH(core::radians(60.0f), float(WIN_WIDTH) / WIN_HEIGHT, zNear, zFar); + hlsl::float32_t4x4 projectionMatrix = hlsl::math::thin_lens::lhPerspectiveFovMatrix(core::radians(60.0f), float(WIN_WIDTH) / WIN_HEIGHT, zNear, zFar); camera = Camera(cameraPosition, cameraTarget, projectionMatrix, 1.069f, 0.4f); m_pRenderParams.zNear = zNear; @@ -884,22 +885,20 @@ class FLIPFluidsApp final : public SimpleWindowedApplication, public BuiltinReso const auto projectionMatrix = camera.getProjectionMatrix(); const auto viewProjectionMatrix = camera.getConcatenatedMatrix(); - core::matrix3x4SIMD modelMatrix; - modelMatrix.setTranslation(nbl::core::vectorSIMDf(0, 0, 0, 0)); - modelMatrix.setRotation(quaternion(0, 0, 0)); + hlsl::float32_t3x4 modelMatrix = hlsl::math::linalg::identity(); - core::matrix3x4SIMD modelViewMatrix = core::concatenateBFollowedByA(viewMatrix, modelMatrix); - core::matrix4SIMD modelViewProjectionMatrix = core::concatenateBFollowedByA(viewProjectionMatrix, modelMatrix); + hlsl::float32_t3x4 modelViewMatrix = viewMatrix; + hlsl::float32_t4x4 modelViewProjectionMatrix = viewProjectionMatrix; - auto modelMat = core::concatenateBFollowedByA(core::matrix4SIMD(), modelMatrix); + auto modelMat = hlsl::math::linalg::promote_affine<4, 4, 3, 4>(modelMatrix); const core::vector3df camPos = camera.getPosition().getAsVector3df(); camPos.getAs4Values(camData.cameraPosition); - memcpy(camData.MVP, modelViewProjectionMatrix.pointer(), sizeof(camData.MVP)); - memcpy(camData.M, modelMat.pointer(), sizeof(camData.M)); - memcpy(camData.V, viewMatrix.pointer(), sizeof(camData.V)); - memcpy(camData.P, projectionMatrix.pointer(), sizeof(camData.P)); + memcpy(camData.MVP, &modelViewProjectionMatrix[0][0], sizeof(camData.MVP)); + memcpy(camData.M, &modelMat[0][0], sizeof(camData.M)); + memcpy(camData.V, &viewMatrix[0][0], sizeof(camData.V)); + memcpy(camData.P, &projectionMatrix[0][0], sizeof(camData.P)); { camDataRange.buffer = cameraBuffer; camDataRange.size = cameraBuffer->getSize(); @@ -1817,7 +1816,7 @@ class FLIPFluidsApp final : public SimpleWindowedApplication, public BuiltinReso InputSystem::ChannelReader mouse; InputSystem::ChannelReader keyboard; - Camera camera = Camera(core::vectorSIMDf(0,0,0), core::vectorSIMDf(0,0,0), core::matrix4SIMD()); + Camera camera = Camera(core::vectorSIMDf(0,0,0), core::vectorSIMDf(0,0,0), hlsl::float32_t4x4()); video::CDumbPresentationOracle oracle; bool m_shouldInitParticles = true; diff --git a/common/include/nbl/examples/cameras/CCamera.hpp b/common/include/nbl/examples/cameras/CCamera.hpp index 3b3cd38d8..2f4e2472a 100644 --- a/common/include/nbl/examples/cameras/CCamera.hpp +++ b/common/include/nbl/examples/cameras/CCamera.hpp @@ -12,12 +12,14 @@ #include #include +#include +#include class Camera { public: Camera() = default; - Camera(const nbl::core::vectorSIMDf& position, const nbl::core::vectorSIMDf& lookat, const nbl::core::matrix4SIMD& projection, float moveSpeed = 1.0f, float rotateSpeed = 1.0f, const nbl::core::vectorSIMDf& upVec = nbl::core::vectorSIMDf(0.0f, 1.0f, 0.0f), const nbl::core::vectorSIMDf& backupUpVec = nbl::core::vectorSIMDf(0.5f, 1.0f, 0.0f)) + Camera(const nbl::core::vectorSIMDf& position, const nbl::core::vectorSIMDf& lookat, const nbl::hlsl::float32_t4x4& projection, float moveSpeed = 1.0f, float rotateSpeed = 1.0f, const nbl::core::vectorSIMDf& upVec = nbl::core::vectorSIMDf(0.0f, 1.0f, 0.0f), const nbl::core::vectorSIMDf& backupUpVec = nbl::core::vectorSIMDf(0.5f, 1.0f, 0.0f)) : position(position) , initialPosition(position) , target(lookat) @@ -27,6 +29,7 @@ class Camera , rotateSpeed(rotateSpeed) , upVector(upVec) , backupUpVector(backupUpVec) + , viewMatrix(nbl::hlsl::math::linalg::diagonal(1.0f)) { initDefaultKeysMap(); allKeysUp(); @@ -63,19 +66,15 @@ class Camera inline void mapKeysCustom(std::array& map) { keysMap = map; } - inline const nbl::core::matrix4SIMD& getProjectionMatrix() const { return projMatrix; } - inline const nbl::core::matrix3x4SIMD& getViewMatrix() const { return viewMatrix; } - inline const nbl::core::matrix4SIMD& getConcatenatedMatrix() const { return concatMatrix; } + inline const nbl::hlsl::float32_t4x4& getProjectionMatrix() const { return projMatrix; } + inline const nbl::hlsl::float32_t3x4& getViewMatrix() const { return viewMatrix; } + inline const nbl::hlsl::float32_t4x4& getConcatenatedMatrix() const { return concatMatrix; } - inline void setProjectionMatrix(const nbl::core::matrix4SIMD& projection) + inline void setProjectionMatrix(const nbl::hlsl::float32_t4x4& projection) { projMatrix = projection; - - const auto hlslMatMap = *reinterpret_cast(&projMatrix); // TEMPORARY TILL THE CAMERA CLASS IS REFACTORED TO WORK WITH HLSL MATRICIES! - { - leftHanded = nbl::hlsl::determinant(hlslMatMap) < 0.f; - } - concatMatrix = nbl::core::matrix4SIMD::concatenateBFollowedByAPrecisely(projMatrix, nbl::core::matrix4SIMD(viewMatrix)); + leftHanded = nbl::hlsl::determinant(projMatrix) < 0.f; + concatMatrix = nbl::hlsl::math::linalg::promoted_mul(projMatrix, viewMatrix); } inline void setPosition(const nbl::core::vectorSIMDf& pos) @@ -112,22 +111,26 @@ class Camera inline void recomputeViewMatrix() { - nbl::core::vectorSIMDf pos = position; - nbl::core::vectorSIMDf localTarget = nbl::core::normalize(target - pos); + nbl::hlsl::float32_t3 pos = nbl::core::convertToHLSLVector(position).xyz; + nbl::hlsl::float32_t3 localTarget = nbl::hlsl::normalize(nbl::core::convertToHLSLVector(target).xyz - pos); + // TODO: remove completely when removing vectorSIMD + nbl::hlsl::float32_t3 _target = nbl::core::convertToHLSLVector(target).xyz; // if upvector and vector to the target are the same, we have a // problem. so solve this problem: - nbl::core::vectorSIMDf up = nbl::core::normalize(upVector); - nbl::core::vectorSIMDf cross = nbl::core::cross(localTarget, up); - bool upVectorNeedsChange = nbl::core::lengthsquared(cross)[0] == 0; + nbl::hlsl::float32_t3 up = nbl::core::convertToHLSLVector(nbl::core::normalize(upVector)).xyz; + nbl::hlsl::float32_t3 cross = nbl::hlsl::cross(localTarget, up); + const float squaredLength = dot(cross, cross); + const bool upVectorNeedsChange = squaredLength == 0; if (upVectorNeedsChange) - up = nbl::core::normalize(backupUpVector); + up = nbl::core::convertToHLSLVector(nbl::core::normalize(backupUpVector)); if (leftHanded) - viewMatrix = nbl::core::matrix3x4SIMD::buildCameraLookAtMatrixLH(pos, target, up); + viewMatrix = nbl::hlsl::math::linalg::lhLookAt(pos, _target, up); else - viewMatrix = nbl::core::matrix3x4SIMD::buildCameraLookAtMatrixRH(pos, target, up); - concatMatrix = nbl::core::matrix4SIMD::concatenateBFollowedByAPrecisely(projMatrix, nbl::core::matrix4SIMD(viewMatrix)); + viewMatrix = nbl::hlsl::math::linalg::rhLookAt(pos, _target, up); + + concatMatrix = nbl::hlsl::math::linalg::promoted_mul(projMatrix, viewMatrix); } inline bool getLeftHanded() const { return leftHanded; } @@ -148,14 +151,14 @@ class Camera if(ev.type == nbl::ui::SMouseEvent::EET_MOVEMENT && mouseDown) { - nbl::core::vectorSIMDf pos = getPosition(); - nbl::core::vectorSIMDf localTarget = getTarget() - pos; + nbl::hlsl::float32_t4 pos = nbl::core::convertToHLSLVector(getPosition()); + nbl::hlsl::float32_t4 localTarget = nbl::core::convertToHLSLVector(getTarget()) - pos; // Get Relative Rotation for localTarget in Radians float relativeRotationX, relativeRotationY; - relativeRotationY = atan2(localTarget.X, localTarget.Z); - const double z1 = nbl::core::sqrt(localTarget.X*localTarget.X + localTarget.Z*localTarget.Z); - relativeRotationX = atan2(z1, localTarget.Y) - nbl::core::PI()/2; + relativeRotationY = atan2(localTarget.x, localTarget.z); + const double z1 = nbl::core::sqrt(localTarget.x*localTarget.x + localTarget.z*localTarget.z); + relativeRotationX = atan2(z1, localTarget.y) - nbl::core::PI()/2; constexpr float RotateSpeedScale = 0.003f; relativeRotationX -= ev.movementEvent.relativeMovementY * rotateSpeed * RotateSpeedScale * -1.0f; @@ -174,13 +177,17 @@ class Camera if (relativeRotationX > MaxVerticalAngle && relativeRotationX < 2 * nbl::core::PI()-MaxVerticalAngle) relativeRotationX = MaxVerticalAngle; - localTarget.set(0,0, nbl::core::max(1.f, nbl::core::length(pos)[0]), 1.f); + pos.w = 0; + localTarget = nbl::hlsl::float32_t4(0, 0, nbl::core::max(1.f, nbl::hlsl::length(pos)), 1.0f); - nbl::core::matrix3x4SIMD mat; - mat.setRotation(nbl::core::quaternion(relativeRotationX, relativeRotationY, 0)); - mat.transformVect(localTarget); - - setTarget(localTarget + pos); + const nbl::hlsl::math::quaternion quat = nbl::hlsl::math::quaternion::create(relativeRotationX, relativeRotationY, 0); + nbl::hlsl::float32_t3x4 mat = nbl::hlsl::math::linalg::promote_affine<3, 4, 3, 3>(quat.constructMatrix()); + + localTarget = nbl::hlsl::float32_t4(nbl::hlsl::mul(mat, localTarget), 1.0f); + + nbl::core::vectorSIMDf finalTarget = nbl::core::constructVecorSIMDFromHLSLVector(localTarget + pos); + finalTarget.w = 1.0f; + setTarget(finalTarget); } } } @@ -311,8 +318,8 @@ class Camera private: nbl::core::vectorSIMDf initialPosition, initialTarget, position, target, upVector, backupUpVector; // TODO: make first 2 const + add default copy constructor - nbl::core::matrix3x4SIMD viewMatrix; - nbl::core::matrix4SIMD concatMatrix, projMatrix; + nbl::hlsl::float32_t3x4 viewMatrix; + nbl::hlsl::float32_t4x4 concatMatrix, projMatrix; float moveSpeed, rotateSpeed; bool leftHanded, firstUpdate = true, mouseDown = false;