demotool/camera.cc

99 lines
2.3 KiB
C++

#include "externals.hh"
#include "camera.hh"
#include "utilities.hh"
/*= T_Camera =================================================================*/
T_Camera::T_Camera( ) noexcept
{
cvtFov2Np( );
cvtAnglesToVectors( );
}
/*----------------------------------------------------------------------------*/
void T_Camera::cvtAnglesToVectors( ) noexcept
{
anglesToMatrix( &angles_.x , &rotMat_[ 0 ].x );
dir_ = glm::vec3( 0 , 0 , -distance_ ) * rotMat_;
up_ = glm::vec3( 0 , 1 , 0 ) * rotMat_;
pos_ = lookAt_ - dir_;
}
void T_Camera::cvtVectorsToAngles( ) noexcept
{
#warning implement the fuck
}
/*------------------------------------------------------------------------------*/
void T_Camera::handleDND(
ImVec2 const& move ,
const bool hasCtrl ,
const bool hasShift ,
const bool lmb ) noexcept
{
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_ += .1f * ( 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 );
}
cvtAnglesToVectors( );
}
void T_Camera::handleWheel(
const float wheel ,
const bool hasCtrl ,
const bool hasShift ) noexcept
{
const float delta( wheel * ( hasCtrl ? 1.f : .1f) );
if ( hasShift ) {
fov_ = std::max( 1.f , std::min( 179.f , fov_ + delta ) );
cvtFov2Np( );
} else {
distance_ = std::max( .01f , distance_ - delta );
cvtAnglesToVectors( );
}
}
/*----------------------------------------------------------------------------*/
void T_Camera::makeUI( ) noexcept
{
using namespace ImGui;
if ( !CollapsingHeader( "Camera" ) ) {
return;
}
const bool changed[] = {
DragFloat3( "Target" , &lookAt_.x ) ,
DragFloat( "Distance" , &distance_ , .1f ,
.1f , 1e8 , "%.1f" ) ,
DragFloat3( "Angles" , &angles_.x , .01f , -180 , 180 ) ,
DragFloat( "FoV" , &fov_ , .01f , .01f , 179.9f )
};
for ( unsigned i = 0 ; i < sizeof( changed ) / sizeof( bool ) ; i ++ ) {
if ( changed[ i ] ) {
#warning fix this shit
// FIXME update( );
break;
}
}
}