2015-12-08 07:31:57 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2016-07-19 17:35:25 +00:00
|
|
|
* Copyright (C) 2015-2016 Mario Luzeiro <mrluzeiro@ua.pt>
|
2020-12-13 23:02:57 +00:00
|
|
|
* Copyright (C) 2015-2020 KiCad Developers, see AUTHORS.txt for contributors.
|
2015-12-08 07:31:57 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2023-04-19 11:51:26 +00:00
|
|
|
* @file track_ball.cpp
|
2016-07-19 17:35:25 +00:00
|
|
|
* @brief Implementation of a track ball camera. A track ball is placed in the
|
|
|
|
* center of the screen and rotates the camera.
|
2015-12-08 07:31:57 +00:00
|
|
|
*/
|
|
|
|
|
2021-01-03 22:23:00 +00:00
|
|
|
#include "track_ball.h"
|
2015-12-08 07:31:57 +00:00
|
|
|
#include "trackball.h"
|
2016-07-19 17:35:25 +00:00
|
|
|
#include "../3d_math.h"
|
2015-12-08 07:31:57 +00:00
|
|
|
#include <wx/log.h>
|
|
|
|
|
2021-06-11 13:10:36 +00:00
|
|
|
#include <glm/gtc/quaternion.hpp>
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2021-06-23 13:06:20 +00:00
|
|
|
// stdlib
|
|
|
|
#include <algorithm>
|
|
|
|
|
2021-09-15 22:01:24 +00:00
|
|
|
TRACK_BALL::TRACK_BALL( float aInitialDistance ) :
|
|
|
|
CAMERA( aInitialDistance )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
wxLogTrace( m_logTrace, wxT( "TRACK_BALL::TRACK_BALL" ) );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2016-07-19 17:35:25 +00:00
|
|
|
memset( m_quat_t0, 0, sizeof( m_quat_t0 ) );
|
|
|
|
memset( m_quat_t1, 0, sizeof( m_quat_t1 ) );
|
|
|
|
|
|
|
|
trackball( m_quat_t0, 0.0, 0.0, 0.0, 0.0 );
|
|
|
|
trackball( m_quat_t1, 0.0, 0.0, 0.0, 0.0 );
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void TRACK_BALL::Drag( const wxPoint& aNewMousePosition )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
m_parametersChanged = true;
|
|
|
|
|
|
|
|
double spin_quat[4];
|
|
|
|
|
|
|
|
// "Pass the x and y coordinates of the last and current positions of
|
|
|
|
// the mouse, scaled so they are from (-1.0 ... 1.0)."
|
2016-07-19 17:35:25 +00:00
|
|
|
const float zoom = 1.0f;
|
|
|
|
|
2020-12-13 23:02:57 +00:00
|
|
|
trackball( spin_quat, zoom * ( 2.0 * m_lastPosition.x - m_windowSize.x ) / m_windowSize.x,
|
|
|
|
zoom * ( m_windowSize.y - 2.0 * m_lastPosition.y ) / m_windowSize.y,
|
|
|
|
zoom * ( 2.0 * aNewMousePosition.x - m_windowSize.x ) / m_windowSize.x,
|
|
|
|
zoom * ( m_windowSize.y - 2.0 * aNewMousePosition.y ) / m_windowSize.y );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2021-06-11 13:10:36 +00:00
|
|
|
float spin_matrix[4][4];
|
|
|
|
build_rotmatrix( spin_matrix, spin_quat );
|
|
|
|
m_rotationMatrix = glm::make_mat4( &spin_matrix[0][0] ) * m_rotationMatrix;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
updateViewMatrix();
|
|
|
|
|
|
|
|
updateFrustum();
|
|
|
|
}
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void TRACK_BALL::Pan( const wxPoint& aNewMousePosition )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
m_parametersChanged = true;
|
|
|
|
|
2019-12-30 13:01:06 +00:00
|
|
|
if( m_projectionType == PROJECTION_TYPE::ORTHO )
|
2016-09-06 23:43:15 +00:00
|
|
|
{
|
2021-04-10 16:28:49 +00:00
|
|
|
m_camera_pos.x -= m_frustum.nw *
|
|
|
|
( m_lastPosition.x - aNewMousePosition.x ) / m_windowSize.x;
|
|
|
|
m_camera_pos.y -= m_frustum.nh *
|
|
|
|
( aNewMousePosition.y - m_lastPosition.y ) / m_windowSize.y;
|
2016-09-06 23:43:15 +00:00
|
|
|
}
|
2019-12-30 13:01:06 +00:00
|
|
|
else // PROJECTION_TYPE::PERSPECTIVE
|
2016-09-06 23:43:15 +00:00
|
|
|
{
|
|
|
|
// Unproject the coordinates using the precomputed frustum tangent (zoom level dependent)
|
2021-06-11 13:10:36 +00:00
|
|
|
const float panFactor = -m_camera_pos.z * m_frustum.tang * 2;
|
2020-12-13 23:02:57 +00:00
|
|
|
m_camera_pos.x -= panFactor * m_frustum.ratio *
|
|
|
|
( m_lastPosition.x - aNewMousePosition.x ) / m_windowSize.x;
|
2016-09-06 23:43:15 +00:00
|
|
|
m_camera_pos.y -= panFactor * ( aNewMousePosition.y - m_lastPosition.y ) / m_windowSize.y;
|
|
|
|
}
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
updateViewMatrix();
|
|
|
|
updateFrustum();
|
|
|
|
}
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void TRACK_BALL::Pan( const SFVEC3F& aDeltaOffsetInc )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
m_parametersChanged = true;
|
|
|
|
|
|
|
|
m_camera_pos += aDeltaOffsetInc;
|
|
|
|
|
|
|
|
updateViewMatrix();
|
|
|
|
updateFrustum();
|
|
|
|
}
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void TRACK_BALL::Pan_T1( const SFVEC3F& aDeltaOffsetInc )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
m_camera_pos_t1 = m_camera_pos + aDeltaOffsetInc;
|
|
|
|
}
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void TRACK_BALL::Reset_T1()
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
CAMERA::Reset_T1();
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
memset( m_quat_t1, 0, sizeof( m_quat_t1 ) );
|
|
|
|
trackball( m_quat_t1, 0.0, 0.0, 0.0, 0.0 );
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void TRACK_BALL::SetT0_and_T1_current_T()
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
CAMERA::SetT0_and_T1_current_T();
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-06-11 13:10:36 +00:00
|
|
|
double quat[4];
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-06-11 13:10:36 +00:00
|
|
|
// Charge the quaternions with the current rotation matrix to allow dual input.
|
|
|
|
std::copy_n( glm::value_ptr( glm::conjugate( glm::quat_cast( m_rotationMatrix ) ) ),
|
|
|
|
sizeof( quat ) / sizeof( quat[0] ), quat );
|
|
|
|
|
|
|
|
memcpy( m_quat_t0, quat, sizeof( quat ) );
|
|
|
|
memcpy( m_quat_t1, quat, sizeof( quat ) );
|
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void TRACK_BALL::Interpolate( float t )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
wxASSERT( t >= 0.0f );
|
|
|
|
|
|
|
|
// Limit t o 1.0
|
2020-12-13 23:02:57 +00:00
|
|
|
t = ( t > 1.0f ) ? 1.0f : t;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
switch( m_interpolation_mode )
|
|
|
|
{
|
2019-12-30 13:01:06 +00:00
|
|
|
case CAMERA_INTERPOLATION::BEZIER:
|
2016-07-19 17:35:25 +00:00
|
|
|
t = BezierBlend( t );
|
|
|
|
break;
|
|
|
|
|
2019-12-30 13:01:06 +00:00
|
|
|
case CAMERA_INTERPOLATION::EASING_IN_OUT:
|
2016-07-19 17:35:25 +00:00
|
|
|
t = QuadricEasingInOut( t );
|
|
|
|
break;
|
|
|
|
|
2019-12-30 13:01:06 +00:00
|
|
|
case CAMERA_INTERPOLATION::LINEAR:
|
2016-07-19 17:35:25 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const float t0 = 1.0f - t;
|
2021-06-11 13:10:36 +00:00
|
|
|
double quat[4];
|
|
|
|
quat[0] = m_quat_t0[0] * t0 + m_quat_t1[0] * t;
|
|
|
|
quat[1] = m_quat_t0[1] * t0 + m_quat_t1[1] * t;
|
|
|
|
quat[2] = m_quat_t0[2] * t0 + m_quat_t1[2] * t;
|
|
|
|
quat[3] = m_quat_t0[3] * t0 + m_quat_t1[3] * t;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
float rotationMatrix[4][4];
|
|
|
|
|
2021-06-11 13:10:36 +00:00
|
|
|
build_rotmatrix( rotationMatrix, quat );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
m_rotationMatrix = glm::make_mat4( &rotationMatrix[0][0] );
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
CAMERA::Interpolate( t );
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|