diff --git a/demo/main.cpp b/demo/main.cpp index 60d340f5f8fea1c7c46b3801cfb4877470073c6e..7c3b7f13161b1b808f87f0345d8bc4733e8cee3b 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 ); }