Overrides - Started rework of camera

This commit is contained in:
Emmanuel BENOîT 2017-11-19 23:47:26 +01:00
parent 7a4595e8e8
commit bebf54cfee
3 changed files with 126 additions and 50 deletions

4
TODO
View file

@ -14,6 +14,7 @@ Scripting:
* Re-execute init if shaders are changed * Re-execute init if shaders are changed
(better yet, only execute the parts that are actually needed) (better yet, only execute the parts that are actually needed)
* Overrides * Overrides
* Check for overrides on the same inputs when setting things up
* Aliases * Aliases
* Display errors in UI * Display errors in UI
* Optimizers: * Optimizers:
@ -26,6 +27,9 @@ Scripting:
Sync / inputs: Sync / inputs:
* Support for overrides * Support for overrides
* Camera controls
* FoV vs near plane
* Target/angles/distance vs dir/up/left
* Curve / timeline display * Curve / timeline display
* Display selected input values * Display selected input values
* Display selected overrides * Display selected overrides

View file

@ -5,12 +5,34 @@
/*= T_Camera =================================================================*/ /*= 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( void T_Camera::handleDND(
ImVec2 const& move , ImVec2 const& move ,
const bool hasCtrl , const bool hasCtrl ,
const bool hasShift , const bool hasShift ,
const bool lmb // Left mouse button const bool lmb ) noexcept
)
{ {
if ( move.x == 0 || move.y == 0 ) { if ( move.x == 0 || move.y == 0 ) {
return; return;
@ -21,65 +43,57 @@ void T_Camera::handleDND(
if ( lmb && hasShift ) { if ( lmb && hasShift ) {
// Left mouse button, shift - move camera // Left mouse button, shift - move camera
const auto side( glm::normalize( glm::cross( up , dir ) ) ); const auto side( glm::normalize( glm::cross( up_ , dir_ ) ) );
lookAt += .1f * ( side * fdx + up * fdy ); lookAt_ += .1f * ( side * fdx + up_ * fdy );
} else if ( lmb ) { } else if ( lmb ) {
// Left mouse button, no shift - change yaw/pitch // Left mouse button, no shift - change yaw/pitch
updateAngle( angles.y , fdx ); updateAngle( angles_.y , fdx );
updateAngle( angles.x , fdy ); updateAngle( angles_.x , fdy );
} else { } else {
// Right mouse button - change roll // Right mouse button - change roll
updateAngle( angles.z , fdx ); updateAngle( angles_.z , fdx );
} }
update( ); cvtAnglesToVectors( );
} }
void T_Camera::handleWheel( void T_Camera::handleWheel(
const float wheel , const float wheel ,
const bool hasCtrl , const bool hasCtrl ,
const bool hasShift const bool hasShift ) noexcept
)
{ {
const float delta( wheel * ( hasCtrl ? 1.f : .1f) ); const float delta( wheel * ( hasCtrl ? 1.f : .1f) );
if ( hasShift ) { if ( hasShift ) {
fov = std::max( 1.f , std::min( 179.f , fov + delta ) ); fov_ = std::max( 1.f , std::min( 179.f , fov_ + delta ) );
cvtFov2Np( );
} else { } else {
distance = std::max( .01f , distance - delta ); distance_ = std::max( .01f , distance_ - delta );
cvtAnglesToVectors( );
} }
update( );
} }
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
void T_Camera::makeUI( ) void T_Camera::makeUI( ) noexcept
{ {
if ( !ImGui::CollapsingHeader( "Camera" ) ) { using namespace ImGui;
if ( !CollapsingHeader( "Camera" ) ) {
return; return;
} }
const bool changed[] = { const bool changed[] = {
ImGui::DragFloat3( "Look at" , &lookAt.x ) , DragFloat3( "Target" , &lookAt_.x ) ,
ImGui::DragFloat( "Distance" , &distance , .1f , DragFloat( "Distance" , &distance_ , .1f ,
.1f , 1e8 , "%.1f" ) , .1f , 1e8 , "%.1f" ) ,
ImGui::DragFloat3( "Angles" , &angles.x , .01f , -180 , 180 ) , DragFloat3( "Angles" , &angles_.x , .01f , -180 , 180 ) ,
ImGui::DragFloat( "FoV" , &fov , .01f , .01f , 179.9f )
DragFloat( "FoV" , &fov_ , .01f , .01f , 179.9f )
}; };
for ( unsigned i = 0 ; i < sizeof( changed ) / sizeof( bool ) ; i ++ ) { for ( unsigned i = 0 ; i < sizeof( changed ) / sizeof( bool ) ; i ++ ) {
if ( changed[ i ] ) { if ( changed[ i ] ) {
update( ); #warning fix this shit
// FIXME update( );
break; 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. );
}

View file

@ -6,38 +6,96 @@
/*= T_Camera =================================================================*/ /*= T_Camera =================================================================*/
/* Data for a camera.
*
* For the field of view, both the near plane distance and the angle are
* stored. Updating one causes the other to be updated.
*
* For the camera position and orientation, the data is stored as two different
* sets:
* - target, angles, distance
* - position, direction and up vectors
* Modifying one of the sets updates the other.
*/
struct T_Camera struct T_Camera
{ {
glm::vec3 lookAt = glm::vec3( 0 ); T_Camera( ) noexcept;
glm::vec3 angles = glm::vec3( 0 );
float distance = 10;
float fov = 90;
// Everything below is updated by update() DEF_COPY( T_Camera );
glm::vec3 dir; DEF_MOVE( T_Camera );
glm::vec3 up;
glm::vec3 pos;
float np;
T_Camera() // ---------------------------------------------------------------------
{ update( ); } // FIELD OF VIEW / NEAR PLANE
public:
void fieldOfView( const float angle ) noexcept
{
fov_ = std::min( 179.f , std::max( 1.f , angle ) );
cvtFov2Np( );
}
void nearPlane( const float distance ) noexcept
{
np_ = std::max( .001f , distance );
cvtNp2Fov( );
}
float fieldOfView( ) const noexcept
{ return fov_; }
float nearPlane( ) const noexcept
{ return np_; }
private:
float fov_ = 90.f;
float np_;
void cvtFov2Np( ) noexcept
{ np_ = 2 * tanf( M_PI * ( 180.f - fov_ ) / 360.f ); }
void cvtNp2Fov( ) noexcept
{ fov_ = 180.f - atanf( .5 * np_ ) * 360.f / M_PI; }
// ---------------------------------------------------------------------
// POSITION AND ORIENTATION
public:
void camera( glm::vec3 const& lookAt ,
glm::vec3 const& angles ,
float distance ) noexcept;
void camera( glm::vec3 const& position ,
glm::vec3 const& direction ,
glm::vec3 const& up ) noexcept;
private:
glm::vec3 lookAt_ = glm::vec3( 0 );
glm::vec3 angles_ = glm::vec3( 0 );
float distance_ = 10;
glm::vec3 dir_;
glm::vec3 up_;
glm::vec3 pos_;
glm::mat3x3 rotMat_;
void cvtAnglesToVectors( ) noexcept;
void cvtVectorsToAngles( ) noexcept;
// ---------------------------------------------------------------------
// UI & MOUSE CONTROLS
public:
void makeUI( ) noexcept;
void handleDND( void handleDND(
ImVec2 const& move , ImVec2 const& move ,
const bool hasCtrl , const bool hasCtrl ,
const bool hasShift , const bool hasShift ,
const bool lmb // Left mouse button const bool lmb // Left mouse button
); ) noexcept;
void handleWheel( void handleWheel(
const float wheel , const float wheel ,
const bool hasCtrl , const bool hasCtrl ,
const bool hasShift const bool hasShift
); ) noexcept;
void makeUI( );
private:
glm::mat3x3 rotMat;
float rotationMatrix[ 9 ];
void update( );
}; };