From b0c4280492041159202e0a478e86f218edd67eba Mon Sep 17 00:00:00 2001 From: Joseph Walton-Rivers <joseph@walton-rivers.uk> Date: Tue, 7 Sep 2021 21:12:07 +0100 Subject: [PATCH] fix arcball with normalised coords --- demo/main.cpp | 49 ++++++++++++++++++++++++------------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/demo/main.cpp b/demo/main.cpp index 60d340f..7c3b7f1 100644 --- a/demo/main.cpp +++ b/demo/main.cpp @@ -106,33 +106,32 @@ void CameraArcball(fggl::ecs2::impl::iter& it, fggl::components::Transform* t, f auto& camTransform = t[0]; auto& camComp = c[0]; - auto finalPos = camTransform.origin(); - // see https://asliceofrendering.com/camera/2019/11/30/ArcballCamera/ - glm::vec4 position(camTransform.origin(), 1.0f); - glm::vec4 pivot(camComp.target, 1.0f); - glm::mat4 view = glm::lookAt( camTransform.origin(), camComp.target, camTransform.up() ); - glm::vec3 viewDir = -glm::transpose(view)[2]; - glm::vec3 rightDir = glm::transpose(view)[0]; - - float deltaAngleX = ( 2 * M_PI / window.width() ); - float deltaAngleY = ( M_PI / window.height() ); - float xAngle = ( input.cursorDeltaX() ) * deltaAngleX; - float yAngle = ( input.cursorDeltaY() ) * deltaAngleY; - - auto cosAngle = glm::dot( viewDir, fggl::math::UP ); - if ( cosAngle * sgn(deltaAngleY) > 0.99f ) { - deltaAngleY = 0; - } + // see https://asliceofrendering.com/camera/2019/11/30/ArcballCamera/ + glm::vec4 position(camTransform.origin(), 1.0f); + glm::vec4 pivot(camComp.target, 1.0f); + glm::mat4 view = glm::lookAt( camTransform.origin(), camComp.target, camTransform.up() ); + glm::vec3 viewDir = -glm::transpose(view)[2]; + glm::vec3 rightDir = glm::transpose(view)[0]; + + float deltaAngleX = ( 2 * M_PI / window.width() ); + float deltaAngleY = ( M_PI / window.height() ); + float xAngle = ( input.cursorDeltaX() * window.width() ) * deltaAngleX; + float yAngle = ( input.cursorDeltaY() * window.height() ) * deltaAngleY; + + auto cosAngle = glm::dot( viewDir, fggl::math::UP ); + if ( cosAngle * sgn(deltaAngleY) > 0.99f ) { + deltaAngleY = 0; + } - // rotate the camera around the pivot on the first axis - glm::mat4x4 rotationMatrixX(1.0f); - rotationMatrixX = glm::rotate( rotationMatrixX, xAngle, fggl::math::UP ); - position = ( rotationMatrixX * ( position - pivot ) ) + pivot; + // rotate the camera around the pivot on the first axis + glm::mat4x4 rotationMatrixX(1.0f); + rotationMatrixX = glm::rotate( rotationMatrixX, xAngle, fggl::math::UP ); + position = ( rotationMatrixX * ( position - pivot ) ) + pivot; - // rotate the camera aroud the pivot on the second axis - glm::mat4x4 rotationMatrixY(1.0f); - rotationMatrixY = glm::rotate(rotationMatrixY, yAngle, rightDir ); - finalPos = ( rotationMatrixY * ( position - pivot ) ) + pivot; + // rotate the camera aroud the pivot on the second axis + glm::mat4x4 rotationMatrixY(1.0f); + rotationMatrixY = glm::rotate(rotationMatrixY, yAngle, rightDir ); + auto finalPos = ( rotationMatrixY * ( position - pivot ) ) + pivot; camTransform.origin( finalPos ); } -- GitLab