You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
5.4 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015-2016 Mario Luzeiro <mrluzeiro@ua.pt>
  5. * Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /**
  25. * @file ctrack_ball.cpp
  26. * @brief Implementation of a track ball camera. A track ball is placed in the
  27. * center of the screen and rotates the camera.
  28. */
  29. #include "ctrack_ball.h"
  30. #include "trackball.h"
  31. #include "../3d_math.h"
  32. #include <wx/log.h>
  33. CTRACK_BALL::CTRACK_BALL( float aRangeScale ) : CCAMERA( aRangeScale )
  34. {
  35. wxLogTrace( m_logTrace, wxT( "CTRACK_BALL::CTRACK_BALL" ) );
  36. memset( m_quat, 0, sizeof( m_quat ) );
  37. memset( m_quat_t0, 0, sizeof( m_quat_t0 ) );
  38. memset( m_quat_t1, 0, sizeof( m_quat_t1 ) );
  39. trackball( m_quat, 0.0, 0.0, 0.0, 0.0 );
  40. trackball( m_quat_t0, 0.0, 0.0, 0.0, 0.0 );
  41. trackball( m_quat_t1, 0.0, 0.0, 0.0, 0.0 );
  42. }
  43. void CTRACK_BALL::Drag( const wxPoint &aNewMousePosition )
  44. {
  45. m_parametersChanged = true;
  46. double spin_quat[4];
  47. // "Pass the x and y coordinates of the last and current positions of
  48. // the mouse, scaled so they are from (-1.0 ... 1.0)."
  49. const float zoom = 1.0f;
  50. trackball( spin_quat,
  51. zoom * (2.0 * m_lastPosition.x - m_windowSize.x) / m_windowSize.x,
  52. zoom * (m_windowSize.y - 2.0 * m_lastPosition.y) / m_windowSize.y,
  53. zoom * (2.0 * aNewMousePosition.x - m_windowSize.x) / m_windowSize.x,
  54. zoom * ( m_windowSize.y - 2.0 * aNewMousePosition.y ) / m_windowSize.y);
  55. add_quats( spin_quat, m_quat, m_quat );
  56. float rotationMatrix[4][4];
  57. build_rotmatrix( rotationMatrix, m_quat );
  58. m_rotationMatrix = glm::make_mat4( &rotationMatrix[0][0] );
  59. updateViewMatrix();
  60. updateFrustum();
  61. }
  62. void CTRACK_BALL::SetLookAtPos( const SFVEC3F &aLookAtPos )
  63. {
  64. if( m_lookat_pos != aLookAtPos )
  65. {
  66. m_lookat_pos = aLookAtPos;
  67. updateViewMatrix();
  68. updateFrustum();
  69. m_parametersChanged = true;
  70. }
  71. }
  72. void CTRACK_BALL::Pan( const wxPoint &aNewMousePosition )
  73. {
  74. m_parametersChanged = true;
  75. if( m_projectionType == PROJECTION_ORTHO )
  76. {
  77. // With the ortographic projection, there is just a zoom factor
  78. const float panFactor = m_zoom / 37.5f; // Magic number from CCAMERA::rebuildProjection
  79. m_camera_pos.x -= panFactor * ( m_lastPosition.x - aNewMousePosition.x );
  80. m_camera_pos.y -= panFactor * ( aNewMousePosition.y - m_lastPosition.y );
  81. }
  82. else // PROJECTION_PERSPECTIVE
  83. {
  84. // Unproject the coordinates using the precomputed frustum tangent (zoom level dependent)
  85. const float panFactor = -m_camera_pos.z * m_frustum.tang * 2;
  86. m_camera_pos.x -= panFactor * m_frustum.ratio * ( m_lastPosition.x - aNewMousePosition.x ) / m_windowSize.x;
  87. m_camera_pos.y -= panFactor * ( aNewMousePosition.y - m_lastPosition.y ) / m_windowSize.y;
  88. }
  89. updateViewMatrix();
  90. updateFrustum();
  91. }
  92. void CTRACK_BALL::Pan( const SFVEC3F &aDeltaOffsetInc )
  93. {
  94. m_parametersChanged = true;
  95. m_camera_pos += aDeltaOffsetInc;
  96. updateViewMatrix();
  97. updateFrustum();
  98. }
  99. void CTRACK_BALL::Pan_T1( const SFVEC3F &aDeltaOffsetInc )
  100. {
  101. m_camera_pos_t1 = m_camera_pos + aDeltaOffsetInc;
  102. }
  103. void CTRACK_BALL::Reset()
  104. {
  105. CCAMERA::Reset();
  106. memset( m_quat, 0, sizeof( m_quat ) );
  107. trackball( m_quat, 0.0, 0.0, 0.0, 0.0 );
  108. }
  109. void CTRACK_BALL::Reset_T1()
  110. {
  111. CCAMERA::Reset_T1();
  112. memset( m_quat_t1, 0, sizeof( m_quat_t1 ) );
  113. trackball( m_quat_t1, 0.0, 0.0, 0.0, 0.0 );
  114. }
  115. void CTRACK_BALL::SetT0_and_T1_current_T()
  116. {
  117. CCAMERA::SetT0_and_T1_current_T();
  118. memcpy( m_quat_t0, m_quat, sizeof( m_quat ) );
  119. memcpy( m_quat_t1, m_quat, sizeof( m_quat ) );
  120. }
  121. void CTRACK_BALL::Interpolate( float t )
  122. {
  123. wxASSERT( t >= 0.0f );
  124. // Limit t o 1.0
  125. t = (t > 1.0f)?1.0f:t;
  126. switch( m_interpolation_mode )
  127. {
  128. case INTERPOLATION_BEZIER:
  129. t = BezierBlend( t );
  130. break;
  131. case INTERPOLATION_EASING_IN_OUT:
  132. t = QuadricEasingInOut( t );
  133. break;
  134. case INTERPOLATION_LINEAR:
  135. default:
  136. break;
  137. }
  138. const float t0 = 1.0f - t;
  139. m_quat[0] = m_quat_t0[0] * t0 + m_quat_t1[0] * t;
  140. m_quat[1] = m_quat_t0[1] * t0 + m_quat_t1[1] * t;
  141. m_quat[2] = m_quat_t0[2] * t0 + m_quat_t1[2] * t;
  142. m_quat[3] = m_quat_t0[3] * t0 + m_quat_t1[3] * t;
  143. float rotationMatrix[4][4];
  144. build_rotmatrix( rotationMatrix, m_quat );
  145. m_rotationMatrix = glm::make_mat4( &rotationMatrix[0][0] );
  146. CCAMERA::Interpolate( t );
  147. }