From 0b41ae1b51195746100c2fe81896e07fc4d943d1 Mon Sep 17 00:00:00 2001 From: eduardodoria Date: Sun, 13 Oct 2024 23:15:06 -0300 Subject: [PATCH] Replaced camera view to camera target --- engine/core/component/CameraComponent.h | 8 +- engine/core/object/Camera.cpp | 116 ++++++++---------- engine/core/object/Camera.h | 37 +++--- .../core/script/binding/ObjectClassesLua.cpp | 22 ++-- engine/core/subsystem/AudioSystem.cpp | 2 +- engine/core/subsystem/RenderSystem.cpp | 15 ++- 6 files changed, 104 insertions(+), 96 deletions(-) diff --git a/engine/core/component/CameraComponent.h b/engine/core/component/CameraComponent.h index 8d035911..c68f8ee4 100644 --- a/engine/core/component/CameraComponent.h +++ b/engine/core/component/CameraComponent.h @@ -41,11 +41,15 @@ namespace Supernova{ Matrix4 viewMatrix; Matrix4 viewProjectionMatrix; - Vector3 view = Vector3(0, 0, 0); + Vector3 target = Vector3(0, 0, 0); + Vector3 worldTarget; + Vector3 up = Vector3(0, 1, 0); + Vector3 direction; + Vector3 right; - Vector3 worldView; Vector3 worldUp; + Vector3 worldDirection; Vector3 worldRight; float leftPlane = 0; diff --git a/engine/core/object/Camera.cpp b/engine/core/object/Camera.cpp index ed8922ec..464356ff 100644 --- a/engine/core/object/Camera.cpp +++ b/engine/core/object/Camera.cpp @@ -83,7 +83,7 @@ void Camera::setPerspective(float yfov, float aspect, float near, float far){ camera.needUpdate = true; } -void Camera::setNear(float near){ +void Camera::setNearPlane(float near){ CameraComponent& camera = getComponent(); if (camera.nearPlane != near){ @@ -93,13 +93,13 @@ void Camera::setNear(float near){ } } -float Camera::getNear() const{ +float Camera::getNearPlane() const{ CameraComponent& camera = getComponent(); return camera.nearPlane; } -void Camera::setFar(float far){ +void Camera::setFarPlane(float far){ CameraComponent& camera = getComponent(); if (camera.farPlane != far){ @@ -109,13 +109,13 @@ void Camera::setFar(float far){ } } -float Camera::getFar() const{ +float Camera::getFarPlane() const{ CameraComponent& camera = getComponent(); return camera.farPlane; } -void Camera::setLeft(float left){ +void Camera::setLeftPlane(float left){ CameraComponent& camera = getComponent(); if (camera.leftPlane != left){ @@ -127,13 +127,13 @@ void Camera::setLeft(float left){ } } -float Camera::getLeft() const{ +float Camera::getLeftPlane() const{ CameraComponent& camera = getComponent(); return camera.leftPlane; } -void Camera::setRight(float right){ +void Camera::setRightPlane(float right){ CameraComponent& camera = getComponent(); if (camera.rightPlane != right){ @@ -145,13 +145,13 @@ void Camera::setRight(float right){ } } -float Camera::getRight() const{ +float Camera::getRightPlane() const{ CameraComponent& camera = getComponent(); return camera.rightPlane; } -void Camera::setBottom(float bottom){ +void Camera::setBottomPlane(float bottom){ CameraComponent& camera = getComponent(); if (camera.bottomPlane != bottom){ @@ -163,13 +163,13 @@ void Camera::setBottom(float bottom){ } } -float Camera::getBottom() const{ +float Camera::getBottomPlane() const{ CameraComponent& camera = getComponent(); return camera.bottomPlane; } -void Camera::setTop(float top){ +void Camera::setTopPlane(float top){ CameraComponent& camera = getComponent(); if (camera.topPlane != top){ @@ -181,7 +181,7 @@ void Camera::setTop(float top){ } } -float Camera::getTop() const{ +float Camera::getTopPlane() const{ CameraComponent& camera = getComponent(); return camera.topPlane; @@ -243,23 +243,23 @@ CameraType Camera::getType() const{ return camera.type; } -void Camera::setView(Vector3 view){ +void Camera::setTarget(Vector3 target){ CameraComponent& camera = getComponent(); - if (camera.view != view){ - camera.view = view; + if (camera.target != target){ + camera.target = target; camera.needUpdate = true; } } -void Camera::setView(const float x, const float y, const float z){ - setView(Vector3(x,y,z)); +void Camera::setTarget(const float x, const float y, const float z){ + setTarget(Vector3(x,y,z)); } -Vector3 Camera::getView() const{ +Vector3 Camera::getTarget() const{ CameraComponent& camera = getComponent(); - return camera.view; + return camera.target; } void Camera::setUp(Vector3 up){ @@ -281,9 +281,24 @@ Vector3 Camera::getUp() const{ return camera.up; } -Vector3 Camera::getWorldView() const{ +Vector3 Camera::getDirection() const{ CameraComponent& camera = getComponent(); - return camera.worldView; + return camera.direction; +} + +Vector3 Camera::getRight() const{ + CameraComponent& camera = getComponent(); + return camera.right; +} + +Vector3 Camera::getWorldTarget() const{ + CameraComponent& camera = getComponent(); + return camera.worldTarget; +} + +Vector3 Camera::getWorldDirection() const{ + CameraComponent& camera = getComponent(); + return camera.worldDirection; } Vector3 Camera::getWorldUp() const{ @@ -301,13 +316,13 @@ void Camera::rotateView(float angle){ CameraComponent& camera = getComponent(); Transform& transf = getComponent(); - Vector3 viewCenter(camera.view.x - transf.position.x, camera.view.y - transf.position.y, camera.view.z - transf.position.z); + Vector3 viewCenter = camera.target - transf.position; Matrix4 rotation; rotation = Matrix4::rotateMatrix(angle, camera.up); viewCenter = rotation * viewCenter; - camera.view = Vector3(viewCenter.x + transf.position.x, viewCenter.y + transf.position.y, viewCenter.z + transf.position.z); + camera.target = viewCenter + transf.position; camera.needUpdate = true; } @@ -318,13 +333,13 @@ void Camera::rotatePosition(float angle){ CameraComponent& camera = getComponent(); Transform& transf = getComponent(); - Vector3 positionCenter(transf.position.x - camera.view.x, transf.position.y - camera.view.y, transf.position.z - camera.view.z); + Vector3 positionCenter = transf.position - camera.target; Matrix4 rotation; rotation = Matrix4::rotateMatrix(angle, camera.up); positionCenter = rotation * positionCenter; - transf.position = Vector3(positionCenter.x + camera.view.x, positionCenter.y + camera.view.y, positionCenter.z + camera.view.z); + transf.position = positionCenter + camera.target; transf.needUpdate = true; camera.needUpdate = true; @@ -336,13 +351,13 @@ void Camera::elevateView(float angle){ CameraComponent& camera = getComponent(); Transform& transf = getComponent(); - Vector3 viewCenter(camera.view.x - transf.position.x, camera.view.y - transf.position.y, camera.view.z - transf.position.z); + Vector3 viewCenter = camera.target - transf.position; Matrix4 rotation; rotation = Matrix4::rotateMatrix(angle, viewCenter.crossProduct(camera.up)); viewCenter = rotation * viewCenter; - camera.view = Vector3(viewCenter.x + transf.position.x, viewCenter.y + transf.position.y, viewCenter.z + transf.position.z); + camera.target = viewCenter + transf.position; camera.needUpdate = true; } @@ -353,13 +368,13 @@ void Camera::elevatePosition(float angle){ CameraComponent& camera = getComponent(); Transform& transf = getComponent(); - Vector3 positionCenter(transf.position.x - camera.view.x, transf.position.y - camera.view.y, transf.position.z - camera.view.z); + Vector3 positionCenter = transf.position - camera.target; Matrix4 rotation; rotation = Matrix4::rotateMatrix(angle, positionCenter.crossProduct(camera.up)); positionCenter = rotation * positionCenter; - transf.position = Vector3(positionCenter.x + camera.view.x, positionCenter.y + camera.view.y, positionCenter.z + camera.view.z); + transf.position = positionCenter + camera.target; transf.needUpdate = true; camera.needUpdate = true; @@ -371,15 +386,10 @@ void Camera::walkForward(float distance){ CameraComponent& camera = getComponent(); Transform& transf = getComponent(); - Vector3 viewCenter(camera.view.x - transf.position.x, camera.view.y - transf.position.y, camera.view.z - transf.position.z); - - Vector3 aux = viewCenter.dotProduct(camera.up) * camera.up / camera.up.squaredLength(); - - Vector3 walkVector = viewCenter - aux; - + Vector3 walkVector = camera.up.crossProduct(camera.right); walkVector.normalize(); - camera.view = camera.view + (walkVector * distance); + camera.target = camera.target + (walkVector * distance); transf.position = transf.position + (walkVector * distance); transf.needUpdate = true; @@ -392,11 +402,7 @@ void Camera::zoom(float distance){ CameraComponent& camera = getComponent(); Transform& transf = getComponent(); - Vector3 viewCenter(camera.view.x - transf.position.x, camera.view.y - transf.position.y, camera.view.z - transf.position.z); - - viewCenter.normalize(); - - transf.position = transf.position + (viewCenter * distance); + transf.position = transf.position + (camera.direction * distance); transf.needUpdate = true; } @@ -407,14 +413,8 @@ void Camera::slide(float distance){ CameraComponent& camera = getComponent(); Transform& transf = getComponent(); - Vector3 viewCenter(camera.view.x - transf.position.x, camera.view.y - transf.position.y, camera.view.z - transf.position.z); - - Vector3 slideVector = viewCenter.crossProduct(camera.up); - - slideVector.normalize(); - - camera.view = camera.view + (slideVector * distance); - transf.position = transf.position + (slideVector * distance); + camera.target = camera.target + (camera.right * distance); + transf.position = transf.position + (camera.right * distance); transf.needUpdate = true; camera.needUpdate = true; @@ -426,12 +426,8 @@ void Camera::slideForward(float distance){ CameraComponent& camera = getComponent(); Transform& transf = getComponent(); - Vector3 viewCenter(camera.view.x - transf.position.x, camera.view.y - transf.position.y, camera.view.z - transf.position.z); - - viewCenter.normalize(); - - camera.view = camera.view + (viewCenter * distance); - transf.position = transf.position + (viewCenter * distance); + camera.target = camera.target + (camera.direction * distance); + transf.position = transf.position + (camera.direction * distance); transf.needUpdate = true; camera.needUpdate = true; @@ -443,16 +439,10 @@ void Camera::slideUp(float distance){ CameraComponent& camera = getComponent(); Transform& transf = getComponent(); - Vector3 viewCenter(camera.view.x - transf.position.x, camera.view.y - transf.position.y, camera.view.z - transf.position.z); - - Vector3 view = viewCenter.normalize(); - Vector3 right = (view.crossProduct(camera.up)).normalize(); - - Vector3 slideVector = right.crossProduct(view); - + Vector3 slideVector = camera.right.crossProduct(camera.direction); slideVector.normalize(); - camera.view = camera.view + (slideVector * distance); + camera.target = camera.target + (slideVector * distance); transf.position = transf.position + (slideVector * distance); transf.needUpdate = true; diff --git a/engine/core/object/Camera.h b/engine/core/object/Camera.h index 9b92d3b0..b06a8c36 100644 --- a/engine/core/object/Camera.h +++ b/engine/core/object/Camera.h @@ -26,23 +26,23 @@ namespace Supernova{ void setOrtho(float left, float right, float bottom, float top, float near, float far); void setPerspective(float yfov, float aspect, float near, float far); - void setNear(float near); - float getNear() const; + void setNearPlane(float near); + float getNearPlane() const; - void setFar(float far); - float getFar() const; + void setFarPlane(float far); + float getFarPlane() const; - void setLeft(float left); - float getLeft() const; + void setLeftPlane(float left); + float getLeftPlane() const; - void setRight(float right); - float getRight() const; + void setRightPlane(float right); + float getRightPlane() const; - void setBottom(float bottom); - float getBottom() const; + void setBottomPlane(float bottom); + float getBottomPlane() const; - void setTop(float top); - float getTop() const; + void setTopPlane(float top); + float getTopPlane() const; void setAspect(float aspect); float getAspect() const; @@ -53,15 +53,20 @@ namespace Supernova{ void setType(CameraType type); CameraType getType() const; - void setView(Vector3 view); - void setView(const float x, const float y, const float z); - Vector3 getView() const; + void setTarget(Vector3 target); + void setTarget(const float x, const float y, const float z); + Vector3 getTarget() const; void setUp(Vector3 up); void setUp(const float x, const float y, const float z); Vector3 getUp() const; - Vector3 getWorldView() const; + Vector3 getDirection() const; + Vector3 getRight() const; + + Vector3 getWorldTarget() const; + + Vector3 getWorldDirection() const; Vector3 getWorldUp() const; Vector3 getWorldRight() const; diff --git a/engine/core/script/binding/ObjectClassesLua.cpp b/engine/core/script/binding/ObjectClassesLua.cpp index 2f14948b..2b0aeb4b 100644 --- a/engine/core/script/binding/ObjectClassesLua.cpp +++ b/engine/core/script/binding/ObjectClassesLua.cpp @@ -216,21 +216,25 @@ void LuaBinding::registerObjectClasses(lua_State *L){ .addFunction("activate", &Camera::activate) .addFunction("setOrtho", &Camera::setOrtho) .addFunction("setPerspective", &Camera::setPerspective) - .addProperty("near", &Camera::getNear, &Camera::setNear) - .addProperty("far", &Camera::getFar, &Camera::setFar) - .addProperty("left", &Camera::getLeft, &Camera::setLeft) - .addProperty("right", &Camera::getRight, &Camera::setRight) - .addProperty("bottom", &Camera::getBottom, &Camera::setBottom) - .addProperty("top", &Camera::getTop, &Camera::setTop) + .addProperty("nearPlane", &Camera::getNearPlane, &Camera::setNearPlane) + .addProperty("farPlane", &Camera::getFarPlane, &Camera::setFarPlane) + .addProperty("leftPlane", &Camera::getLeftPlane, &Camera::setLeftPlane) + .addProperty("rightPlane", &Camera::getRightPlane, &Camera::setRightPlane) + .addProperty("bottomPlane", &Camera::getBottomPlane, &Camera::setBottomPlane) + .addProperty("topPlane", &Camera::getTopPlane, &Camera::setTopPlane) .addProperty("aspect", &Camera::getAspect, &Camera::setAspect) .addProperty("yfov", &Camera::getYFov, &Camera::setYFov) .addProperty("type", &Camera::getType, &Camera::setType) .addFunction("setType", &Camera::setType) - .addProperty("view", &Camera::getView, (void(Camera::*)(Vector3))&Camera::setView) - .addFunction("setView", (void(Camera::*)(const float, const float, const float))&Camera::setView) + .addProperty("target", &Camera::getTarget, (void(Camera::*)(Vector3))&Camera::setTarget) + .addFunction("setTarget", (void(Camera::*)(const float, const float, const float))&Camera::setTarget) .addProperty("up", &Camera::getUp, (void(Camera::*)(Vector3))&Camera::setUp) .addFunction("setUp", (void(Camera::*)(const float, const float, const float))&Camera::setUp) - .addFunction("getWorldView", &Camera::getWorldView) + .addFunction("getUp", &Camera::getUp) + .addFunction("getDirection", &Camera::getDirection) + .addFunction("getRight", &Camera::getRight) + .addFunction("getWorldTarget", &Camera::getWorldTarget) + .addFunction("getWorldDirection", &Camera::getWorldDirection) .addFunction("getWorldUp", &Camera::getWorldUp) .addFunction("getWorldRight", &Camera::getWorldRight) .addFunction("rotateView", &Camera::rotateView) diff --git a/engine/core/subsystem/AudioSystem.cpp b/engine/core/subsystem/AudioSystem.cpp index 6e595ec8..7183d522 100644 --- a/engine/core/subsystem/AudioSystem.cpp +++ b/engine/core/subsystem/AudioSystem.cpp @@ -231,7 +231,7 @@ void AudioSystem::update(double dt){ Vector3 velocity = audio.lastPosition - worldPosition; Vector3 camWorldPos = cameraTransform.worldPosition; - Vector3 camWorldView = camera.worldView - camWorldPos; + Vector3 camWorldView = camera.worldTarget - camWorldPos; Vector3 camWorldUp = camera.worldUp; Vector3 camVelocity = cameraLastPosition - camWorldPos; diff --git a/engine/core/subsystem/RenderSystem.cpp b/engine/core/subsystem/RenderSystem.cpp index c41f37aa..af681d0f 100644 --- a/engine/core/subsystem/RenderSystem.cpp +++ b/engine/core/subsystem/RenderSystem.cpp @@ -1789,11 +1789,15 @@ void RenderSystem::updateCamera(CameraComponent& camera, Transform& transform){ camera.projectionMatrix = Matrix4::perspectiveMatrix(camera.yfov, camera.aspect, camera.nearPlane, camera.farPlane); } + camera.direction = (camera.target - transform.position).normalize(); + camera.right = (camera.direction.crossProduct(camera.up)).normalize(); + //camera.up = right.crossProduct(direction); // no need to align, keep "up" always same + if (transform.parent != NULL_ENTITY){ - camera.worldView = transform.modelMatrix * (camera.view - transform.position); + camera.worldTarget = transform.modelMatrix * (camera.target - transform.position); camera.worldUp = ((transform.modelMatrix * camera.up) - (transform.modelMatrix * Vector3(0,0,0))).normalize(); }else{ - camera.worldView = camera.view; + camera.worldTarget = camera.target; camera.worldUp = camera.up; } @@ -1801,9 +1805,10 @@ void RenderSystem::updateCamera(CameraComponent& camera, Transform& transform){ if (camera.type == CameraType::CAMERA_2D){ camera.viewMatrix.identity(); }else{ - camera.viewMatrix = Matrix4::lookAtMatrix(transform.worldPosition, camera.worldView, camera.worldUp); + camera.viewMatrix = Matrix4::lookAtMatrix(transform.worldPosition, camera.worldTarget, camera.worldUp); } + camera.worldDirection = Vector3(-camera.viewMatrix[0][2], -camera.viewMatrix[1][2], -camera.viewMatrix[2][2]); camera.worldRight = Vector3(camera.viewMatrix[0][0], camera.viewMatrix[1][0], camera.viewMatrix[2][0]); //Update ViewProjectionMatrix @@ -1864,7 +1869,7 @@ void RenderSystem::updatePoints(PointsComponent& points, Transform& transform, C } void RenderSystem::sortPoints(PointsComponent& points, Transform& transform, CameraComponent& camera, Transform& camTransform){ - Vector3 camDir = (camTransform.worldPosition - camera.worldView).normalize(); + Vector3 camDir = (camTransform.worldPosition - camera.worldTarget).normalize(); auto comparePoints = [&transform, &camDir](const PointRenderData& a, const PointRenderData& b) -> bool { return (transform.modelMatrix * a.position).dotProduct(camDir) < (transform.modelMatrix * b.position).dotProduct(camDir); @@ -2157,7 +2162,7 @@ void RenderSystem::updateInstancedMesh(InstancedMeshComponent& instmesh, MeshCom } void RenderSystem::sortInstancedMesh(InstancedMeshComponent& instmesh, MeshComponent& mesh, Transform& transform, CameraComponent& camera, Transform& camTransform){ - Vector3 camDir = (camTransform.worldPosition - camera.worldView).normalize(); + Vector3 camDir = (camTransform.worldPosition - camera.worldTarget).normalize(); auto comparePoints = [&transform, &camDir](const InstanceRenderData& a, const InstanceRenderData& b) -> bool { Vector3 positionA = Vector3(a.instanceMatrix[3][0], a.instanceMatrix[3][1], a.instanceMatrix[3][2]);