KiCad PCB EDA Suite
ccamera.h
Go to the documentation of this file.
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 
30 #ifndef CCAMERA_H
31 #define CCAMERA_H
32 
33 #include "../3d_rendering/3d_render_raytracing/ray.h"
34 #include <wx/gdicmn.h> // for wxSize
35 #include <vector>
36 
38 {
41 };
42 
48 struct FRUSTUM
49 {
60  float nearD, farD, ratio, angle, tang;
61  float nw, nh, fw, fh;
62 };
63 
64 
66 {
70 
71 };
72 
73 
80 class CCAMERA
81 {
82 
83  public:
84 
91  explicit CCAMERA( float aRangeScale );
92 
93 
99  const glm::mat4 GetRotationMatrix() const;
100 
101  const glm::mat4 &GetViewMatrix() const;
102  const glm::mat4 &GetViewMatrix_Inv() const;
103 
104  const glm::mat4 &GetProjectionMatrix() const;
105  const glm::mat4 &GetProjectionMatrixInv() const;
106 
107  const SFVEC3F &GetRight() const { return m_right; }
108  const SFVEC3F &GetUp() const { return m_up; }
109  const SFVEC3F &GetDir() const { return m_dir; }
110  const SFVEC3F &GetPos() const { return m_pos; }
111  const SFVEC2F &GetFocalLen() const { return m_focalLen; }
112  float GetNear() const { return m_frustum.nearD; }
113  float GetFar() const { return m_frustum.farD; }
114 
115  void SetBoardLookAtPos( const SFVEC3F &aBoardPos ) {
116  if( m_board_lookat_pos_init != aBoardPos )
117  {
118  m_board_lookat_pos_init = aBoardPos;
119  SetLookAtPos( aBoardPos );
120  }
121  }
122 
123  virtual void SetLookAtPos( const SFVEC3F &aLookAtPos ) = 0;
124 
125  void SetLookAtPos_T1( const SFVEC3F &aLookAtPos ) {
126  m_lookat_pos_t1 = aLookAtPos;
127  }
128 
129  const SFVEC3F &GetLookAtPos_T1() const { return m_lookat_pos_t1; }
130 
131  const SFVEC3F &GetCameraPos() const { return m_camera_pos; }
132 
136  virtual void Drag( const wxPoint &aNewMousePosition ) = 0;
137 
138  virtual void Pan( const wxPoint &aNewMousePosition ) = 0;
139 
140  virtual void Pan( const SFVEC3F &aDeltaOffsetInc ) = 0;
141 
142  virtual void Pan_T1( const SFVEC3F &aDeltaOffsetInc ) = 0;
143 
144 
148  virtual void Reset();
149  virtual void Reset_T1();
150 
151  void ResetXYpos();
152  void ResetXYpos_T1();
153 
158  void SetCurMousePosition( const wxPoint &aPosition );
159 
160  void SetProjection( PROJECTION_TYPE aProjectionType );
161 
162  void ToggleProjection();
163 
169  bool SetCurWindowSize( const wxSize &aSize );
170 
171  void ZoomReset();
172 
173  bool Zoom( float aFactor );
174 
175  bool Zoom_T1( float aFactor );
176 
177  float ZoomGet() const ;
178 
179  void RotateX( float aAngleInRadians );
180  void RotateY( float aAngleInRadians );
181  void RotateZ( float aAngleInRadians );
182 
183  void RotateX_T1( float aAngleInRadians );
184  void RotateY_T1( float aAngleInRadians );
185  void RotateZ_T1( float aAngleInRadians );
186 
190  virtual void SetT0_and_T1_current_T();
191 
196  virtual void Interpolate( float t );
197 
198  void SetInterpolateMode( CAMERA_INTERPOLATION aInterpolateMode )
199  {
200  m_interpolation_mode = aInterpolateMode;
201  }
202 
208  bool ParametersChanged();
209 
216 
223  void MakeRay( const SFVEC2I &aWindowPos, SFVEC3F &aOutOrigin, SFVEC3F &aOutDirection ) const;
224 
231  void MakeRay( const SFVEC2F &aWindowPos, SFVEC3F &aOutOrigin, SFVEC3F &aOutDirection ) const;
232 
238  void MakeRayAtCurrrentMousePosition( SFVEC3F &aOutOrigin, SFVEC3F &aOutDirection ) const;
239 
240  protected:
241 
242  void rebuildProjection();
243  void updateFrustum();
244  void updateViewMatrix();
245 
246  void updateRotationMatrix();
247 
253 
257  float m_zoom;
258  float m_zoom_t0;
259  float m_zoom_t1;
260 
265 
270 
271  glm::mat4 m_rotationMatrix;
273  glm::mat4 m_viewMatrix;
278 
280 
285 
287 
292 
297 
301 
303 
308  std::vector< float > m_scr_nX;
309  std::vector< float > m_scr_nY;
310 
315  std::vector< SFVEC3F > m_right_nX;
316  std::vector< SFVEC3F > m_up_nY;
317 
318 
323 
330  static const wxChar *m_logTrace;
331 };
332 
333 #endif // CCAMERA_H
SFVEC3F fc
Definition: ccamera.h:51
void SetLookAtPos_T1(const SFVEC3F &aLookAtPos)
Definition: ccamera.h:125
float ZoomGet() const
Definition: ccamera.cpp:490
PROJECTION_TYPE
Definition: ccamera.h:37
void ResetXYpos()
Definition: ccamera.cpp:371
bool m_parametersChanged
Set to true if any of the parameters in the camera was changed.
Definition: ccamera.h:322
CAMERA_INTERPOLATION
Definition: ccamera.h:65
Frustum structure Frustum is a implementation based on a tutorial by http://www.lighthouse3d.com/tutorials/view-frustum-culling/.
Definition: ccamera.h:48
SFVEC3F m_dir
Definition: ccamera.h:283
virtual void SetT0_and_T1_current_T()
SetT0_and_T1_current_T - This will set T0 and T1 with the current values.
Definition: ccamera.cpp:535
SFVEC3F ftl
Far Top Left.
Definition: ccamera.h:56
void ResetXYpos_T1()
Definition: ccamera.cpp:382
SFVEC3F ntl
Near Top Left.
Definition: ccamera.h:52
virtual void Reset()
Reset the camera to initial state.
Definition: ccamera.cpp:60
float farD
Definition: ccamera.h:60
static const wxChar * m_logTrace
Trace mask used to enable or disable the trace output of this class.
Definition: ccamera.h:330
SFVEC3F m_board_lookat_pos_init
Default boardlookat position (the board center)
Definition: ccamera.h:296
SFVEC3F m_rotate_aux
Stores the rotation angle auxiliar.
Definition: ccamera.h:298
void ZoomReset()
Definition: ccamera.cpp:444
void RotateY(float aAngleInRadians)
Definition: ccamera.cpp:503
CCAMERA(float aRangeScale)
CCAMERA initialize a camera.
Definition: ccamera.cpp:45
Class CCAMERA is a virtual class used to derive CCAMERA objects from.
Definition: ccamera.h:80
void updateFrustum()
Definition: ccamera.cpp:233
const glm::mat4 & GetViewMatrix_Inv() const
Definition: ccamera.cpp:395
std::vector< SFVEC3F > m_up_nY
Definition: ccamera.h:316
const glm::mat4 & GetProjectionMatrix() const
Definition: ccamera.cpp:359
void SetProjection(PROJECTION_TYPE aProjectionType)
Definition: ccamera.cpp:407
float nearD
Definition: ccamera.h:60
float GetFar() const
Definition: ccamera.h:113
glm::mat4 m_viewMatrixInverse
Definition: ccamera.h:274
float nw
Definition: ccamera.h:61
const glm::mat4 & GetViewMatrix() const
Definition: ccamera.cpp:389
std::vector< float > m_scr_nY
Definition: ccamera.h:309
std::vector< float > m_scr_nX
Precalc values array used to calc ray for each pixel (constant for the same window size) ...
Definition: ccamera.h:308
SFVEC3F fbr
Far Bottom Right.
Definition: ccamera.h:59
const SFVEC3F & GetDir() const
Definition: ccamera.h:109
glm::ivec2 SFVEC2I
Definition: xv3d_types.h:42
float tang
Definition: ccamera.h:60
const SFVEC2F & GetFocalLen() const
Definition: ccamera.h:111
void updateViewMatrix()
Definition: ccamera.cpp:101
wxPoint m_lastPosition
The last mouse position in the screen.
Definition: ccamera.h:269
void SetCurMousePosition(const wxPoint &aPosition)
It updates the current mouse position without make any new recalculations on camera.
Definition: ccamera.cpp:401
bool Zoom(float aFactor)
Definition: ccamera.cpp:454
float fw
Definition: ccamera.h:61
void RotateZ(float aAngleInRadians)
Definition: ccamera.cpp:510
void RotateX(float aAngleInRadians)
Definition: ccamera.cpp:496
SFVEC2F m_focalLen
Definition: ccamera.h:286
SFVEC3F nbr
Near Bottom Right.
Definition: ccamera.h:55
SFVEC3F m_camera_pos_t1
Definition: ccamera.h:291
SFVEC3F fbl
Far Bottom Left.
Definition: ccamera.h:58
float m_zoom_t0
Definition: ccamera.h:258
glm::vec2 SFVEC2F
Definition: xv3d_types.h:45
SFVEC3F m_right
Definition: ccamera.h:281
SFVEC3F m_pos
Definition: ccamera.h:284
const glm::mat4 & GetProjectionMatrixInv() const
Definition: ccamera.cpp:365
void ToggleProjection()
Definition: ccamera.cpp:417
float angle
Definition: ccamera.h:60
SFVEC3F m_up
Definition: ccamera.h:282
SFVEC3F m_lookat_pos_t1
Definition: ccamera.h:295
virtual void Interpolate(float t)
Interpolate - It will update the matrix to interpolate between T0 and T1 values.
Definition: ccamera.cpp:549
virtual void Drag(const wxPoint &aNewMousePosition)=0
Calculate a new mouse drag position.
SFVEC3F nbl
Near Bottom Left.
Definition: ccamera.h:54
float m_range_scale
m_range_scale - the nominal range expected to be used in the camera.
Definition: ccamera.h:252
SFVEC3F m_lookat_pos_t0
Definition: ccamera.h:294
bool ParametersChangedQuery() const
Function ParametersChangedQuery.
Definition: ccamera.h:215
void MakeRay(const SFVEC2I &aWindowPos, SFVEC3F &aOutOrigin, SFVEC3F &aOutDirection) const
MakeRay - Make a ray based on a windows screen position.
Definition: ccamera.cpp:293
const SFVEC3F & GetCameraPos() const
Definition: ccamera.h:131
bool SetCurWindowSize(const wxSize &aSize)
SetCurWindowSize - update the windows size of the camera.
Definition: ccamera.cpp:428
glm::mat4 m_rotationMatrix
Definition: ccamera.h:271
SFVEC3F ntr
Near Top Right.
Definition: ccamera.h:53
FRUSTUM m_frustum
Definition: ccamera.h:279
glm::mat4 m_projectionMatrix
Definition: ccamera.h:275
bool Zoom_T1(float aFactor)
Definition: ccamera.cpp:473
virtual void Pan_T1(const SFVEC3F &aDeltaOffsetInc)=0
void RotateX_T1(float aAngleInRadians)
Definition: ccamera.cpp:517
virtual void Reset_T1()
Definition: ccamera.cpp:92
float m_zoom
3D zoom value (Min 0.0 ...
Definition: ccamera.h:257
SFVEC3F m_rotate_aux_t1
Definition: ccamera.h:300
void updateRotationMatrix()
Definition: ccamera.cpp:109
virtual void SetLookAtPos(const SFVEC3F &aLookAtPos)=0
SFVEC3F m_camera_pos_init
Definition: ccamera.h:288
SFVEC3F m_camera_pos
Definition: ccamera.h:289
const SFVEC3F & GetLookAtPos_T1() const
Definition: ccamera.h:129
SFVEC3F m_camera_pos_t0
Definition: ccamera.h:290
std::vector< SFVEC3F > m_right_nX
Precalc values array used to calc ray for each pixel, for X and Y axis of each new camera position...
Definition: ccamera.h:315
float GetNear() const
Definition: ccamera.h:112
SFVEC3F nc
Definition: ccamera.h:50
void MakeRayAtCurrrentMousePosition(SFVEC3F &aOutOrigin, SFVEC3F &aOutDirection) const
MakeRayAtCurrrentMousePosition - Make a ray based on the latest mouse position.
Definition: ccamera.cpp:350
void RotateY_T1(float aAngleInRadians)
Definition: ccamera.cpp:523
void rebuildProjection()
Definition: ccamera.cpp:136
virtual void Pan(const wxPoint &aNewMousePosition)=0
glm::vec3 SFVEC3F
Definition: xv3d_types.h:47
const glm::mat4 GetRotationMatrix() const
Function GetRotationMatrix Get the rotation matrix to be applied in a transformation camera...
Definition: ccamera.cpp:130
void SetBoardLookAtPos(const SFVEC3F &aBoardPos)
Definition: ccamera.h:115
SFVEC2I m_windowSize
The window size that this camera is working.
Definition: ccamera.h:264
SFVEC3F ftr
Far Top Right.
Definition: ccamera.h:57
const SFVEC3F & GetRight() const
Definition: ccamera.h:107
float m_zoom_t1
Definition: ccamera.h:259
CAMERA_INTERPOLATION m_interpolation_mode
Definition: ccamera.h:302
SFVEC3F m_rotate_aux_t0
Definition: ccamera.h:299
glm::mat4 m_rotationMatrixAux
Definition: ccamera.h:272
const SFVEC3F & GetUp() const
Definition: ccamera.h:108
const SFVEC3F & GetPos() const
Definition: ccamera.h:110
void RotateZ_T1(float aAngleInRadians)
Definition: ccamera.cpp:529
float fh
Definition: ccamera.h:61
float nh
Definition: ccamera.h:61
float ratio
Definition: ccamera.h:60
glm::mat4 m_viewMatrix
Definition: ccamera.h:273
void SetInterpolateMode(CAMERA_INTERPOLATION aInterpolateMode)
Definition: ccamera.h:198
glm::mat4 m_projectionMatrixInv
Definition: ccamera.h:276
bool ParametersChanged()
Function ParametersChanged.
Definition: ccamera.cpp:567
SFVEC3F m_lookat_pos
Definition: ccamera.h:293
PROJECTION_TYPE m_projectionType
Definition: ccamera.h:277