demotool/camera.cc

86 lines
2 KiB
C++
Raw Normal View History

2017-10-02 10:12:27 +02:00
#include "externals.hh"
#include "camera.hh"
#include "utilities.hh"
/*= T_Camera =================================================================*/
void T_Camera::handleDND(
2017-11-03 11:55:26 +01:00
ImVec2 const& move ,
const bool hasCtrl ,
const bool hasShift ,
const bool lmb // Left mouse button
2017-10-02 10:12:27 +02:00
)
{
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 ) ) );
2017-10-05 13:16:50 +02:00
lookAt += .1f * ( side * fdx + up * fdy );
2017-10-02 10:12:27 +02:00
} 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(
2017-11-03 11:55:26 +01:00
const float wheel ,
const bool hasCtrl ,
const bool hasShift
2017-10-02 10:12:27 +02:00
)
{
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. );
}