From 080eb3b29b07615cad95263f96bb606953faaa9e Mon Sep 17 00:00:00 2001 From: Emmanuel Benoit Date: Mon, 2 Oct 2017 10:12:27 +0200 Subject: [PATCH] Moved camera to separate file --- Makefile | 1 + camera.cc | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++ camera.hh | 43 ++++++++++++++++++++++++++ main.cc | 4 --- raymarcher.hh | 1 + utilities.cc | 82 ------------------------------------------------- utilities.hh | 39 ----------------------- 7 files changed, 130 insertions(+), 125 deletions(-) create mode 100644 camera.cc create mode 100644 camera.hh diff --git a/Makefile b/Makefile index ea87301..8c3aae1 100644 --- a/Makefile +++ b/Makefile @@ -17,6 +17,7 @@ DEMO = \ texture.cc \ rendertarget.cc \ programs.cc \ + camera.cc \ \ raymarcher.cc \ \ diff --git a/camera.cc b/camera.cc new file mode 100644 index 0000000..0874379 --- /dev/null +++ b/camera.cc @@ -0,0 +1,85 @@ +#include "externals.hh" +#include "camera.hh" +#include "utilities.hh" + + +/*= T_Camera =================================================================*/ + +void T_Camera::handleDND( + __rd__ ImVec2 const& move , + __rd__ const bool hasCtrl , + __rd__ const bool hasShift , + __rd__ const bool lmb // Left mouse button + ) +{ + if ( move.x == 0 || move.y == 0 ) { + return; + } + + const float fdx( move.x * .1f * ( hasCtrl ? 1.f : .1f ) ); + const float fdy( move.y * .1f * ( hasCtrl ? 1.f : .1f ) ); + + if ( lmb && hasShift ) { + // Left mouse button, shift - move camera + const auto side( glm::normalize( glm::cross( up , dir ) ) ); + lookAt += .01f * ( side * fdx + up * fdy ); + } else if ( lmb ) { + // Left mouse button, no shift - change yaw/pitch + updateAngle( angles.y , fdx ); + updateAngle( angles.x , fdy ); + } else { + // Right mouse button - change roll + updateAngle( angles.z , fdx ); + } + update( ); +} + +void T_Camera::handleWheel( + __rd__ const float wheel , + __rd__ const bool hasCtrl , + __rd__ const bool hasShift + ) +{ + const float delta( wheel * ( hasCtrl ? 1.f : .1f) ); + if ( hasShift ) { + fov = std::max( 1.f , std::min( 179.f , fov + delta ) ); + } else { + distance = std::max( .01f , distance - delta ); + } + update( ); +} + +/*----------------------------------------------------------------------------*/ + +void T_Camera::makeUI( ) +{ + if ( !ImGui::CollapsingHeader( "Camera" ) ) { + return; + } + + const bool changed[] = { + ImGui::DragFloat3( "Look at" , &lookAt.x ) , + ImGui::DragFloat( "Distance" , &distance , .1f , + .1f , 1e8 , "%.1f" ) , + ImGui::DragFloat3( "Angles" , &angles.x , .01f , -180 , 180 ) , + ImGui::DragFloat( "FoV" , &fov , .01f , .01f , 179.9f ) + }; + + for ( unsigned i = 0 ; i < sizeof( changed ) / sizeof( bool ) ; i ++ ) { + if ( changed[ i ] ) { + update( ); + break; + } + } +} + +/*----------------------------------------------------------------------------*/ + +void T_Camera::update( ) +{ + anglesToMatrix( &angles.x , &rotMat[ 0 ].x ); + dir = glm::vec3( 0 , 0 , -distance ) * rotMat; + up = glm::vec3( 0 , 1 , 0 ) * rotMat; + pos = lookAt - dir; + np = 2 * tan( M_PI * ( 180. - fov ) / 360. ); +} diff --git a/camera.hh b/camera.hh new file mode 100644 index 0000000..87730ff --- /dev/null +++ b/camera.hh @@ -0,0 +1,43 @@ +#pragma once +#ifndef REAL_BUILD +# include "externals.hh" +#endif + + +/*= T_Camera =================================================================*/ + +struct T_Camera +{ + glm::vec3 lookAt; + glm::vec3 angles; + float distance = 10; + float fov = 90; + + // Everything below is updated by update() + glm::vec3 dir; + glm::vec3 up; + glm::vec3 pos; + float np; + + T_Camera() + { update( ); } + + void handleDND( + __rd__ ImVec2 const& move , + __rd__ const bool hasCtrl , + __rd__ const bool hasShift , + __rd__ const bool lmb // Left mouse button + ); + void handleWheel( + __rd__ const float wheel , + __rd__ const bool hasCtrl , + __rd__ const bool hasShift + ); + + void makeUI( ); + + private: + glm::mat3x3 rotMat; + float rotationMatrix[ 9 ]; + void update( ); +}; diff --git a/main.cc b/main.cc index f58eba0..b8efce0 100644 --- a/main.cc +++ b/main.cc @@ -1,10 +1,6 @@ #include "externals.hh" #include "imgui_impl_sdl.h" -#include "utilities.hh" -#include "programs.hh" -#include "texture.hh" -#include "rendertarget.hh" #include "raymarcher.hh" #include "dof.hh" #include "bloom.hh" diff --git a/raymarcher.hh b/raymarcher.hh index 997bbba..c24a26e 100644 --- a/raymarcher.hh +++ b/raymarcher.hh @@ -2,6 +2,7 @@ #include "rendertarget.hh" #include "programs.hh" +#include "camera.hh" struct T_Raymarcher diff --git a/utilities.cc b/utilities.cc index f4e1a16..cecf429 100644 --- a/utilities.cc +++ b/utilities.cc @@ -155,85 +155,3 @@ bool T_WatchedFiles::watch( } return false; } - - -/*= T_Camera =================================================================*/ - -void T_Camera::handleDND( - __rd__ ImVec2 const& move , - __rd__ const bool hasCtrl , - __rd__ const bool hasShift , - __rd__ const bool lmb // Left mouse button - ) -{ - if ( move.x == 0 || move.y == 0 ) { - return; - } - - const float fdx( move.x * .1f * ( hasCtrl ? 1.f : .1f ) ); - const float fdy( move.y * .1f * ( hasCtrl ? 1.f : .1f ) ); - - if ( lmb && hasShift ) { - // Left mouse button, shift - move camera - const auto side( glm::normalize( glm::cross( up , dir ) ) ); - lookAt += .01f * ( side * fdx + up * fdy ); - } else if ( lmb ) { - // Left mouse button, no shift - change yaw/pitch - updateAngle( angles.y , fdx ); - updateAngle( angles.x , fdy ); - } else { - // Right mouse button - change roll - updateAngle( angles.z , fdx ); - } - update( ); -} - -void T_Camera::handleWheel( - __rd__ const float wheel , - __rd__ const bool hasCtrl , - __rd__ const bool hasShift - ) -{ - const float delta( wheel * ( hasCtrl ? 1.f : .1f) ); - if ( hasShift ) { - fov = std::max( 1.f , std::min( 179.f , fov + delta ) ); - } else { - distance = std::max( .01f , distance - delta ); - } - update( ); -} - -/*----------------------------------------------------------------------------*/ - -void T_Camera::makeUI( ) -{ - if ( !ImGui::CollapsingHeader( "Camera" ) ) { - return; - } - - const bool changed[] = { - ImGui::DragFloat3( "Look at" , &lookAt.x ) , - ImGui::DragFloat( "Distance" , &distance , .1f , - .1f , 1e8 , "%.1f" ) , - ImGui::DragFloat3( "Angles" , &angles.x , .01f , -180 , 180 ) , - ImGui::DragFloat( "FoV" , &fov , .01f , .01f , 179.9f ) - }; - - for ( unsigned i = 0 ; i < sizeof( changed ) / sizeof( bool ) ; i ++ ) { - if ( changed[ i ] ) { - update( ); - break; - } - } -} - -/*----------------------------------------------------------------------------*/ - -void T_Camera::update( ) -{ - anglesToMatrix( &angles.x , &rotMat[ 0 ].x ); - dir = glm::vec3( 0 , 0 , -distance ) * rotMat; - up = glm::vec3( 0 , 1 , 0 ) * rotMat; - pos = lookAt - dir; - np = 2 * tan( M_PI * ( 180. - fov ) / 360. ); -} diff --git a/utilities.hh b/utilities.hh index 4d33282..182d5c7 100644 --- a/utilities.hh +++ b/utilities.hh @@ -105,42 +105,3 @@ struct T_WatchedFiles bool triggered; std::vector< int > identifiers; }; - - -/*= T_Camera =================================================================*/ - -struct T_Camera -{ - glm::vec3 lookAt; - glm::vec3 angles; - float distance = 10; - float fov = 90; - - // Everything below is updated by update() - glm::vec3 dir; - glm::vec3 up; - glm::vec3 pos; - float np; - - T_Camera() - { update( ); } - - void handleDND( - __rd__ ImVec2 const& move , - __rd__ const bool hasCtrl , - __rd__ const bool hasShift , - __rd__ const bool lmb // Left mouse button - ); - void handleWheel( - __rd__ const float wheel , - __rd__ const bool hasCtrl , - __rd__ const bool hasShift - ); - - void makeUI( ); - - private: - glm::mat3x3 rotMat; - float rotationMatrix[ 9 ]; - void update( ); -};