demotool/camera.cc

85 lines
2.1 KiB
C++

#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. );
}