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 
37 enum class PROJECTION_TYPE
38 {
39  ORTHO,
41 };
42 
48 struct FRUSTUM
49 {
60  float nearD, farD, ratio, angle, tang;
61  float nw, nh, fw, fh;
62 };
63 
64 
66 {
67  LINEAR,
68  EASING_IN_OUT, // Quadratic
69  BEZIER,
70 };
71 
72 
79 class CCAMERA
80 {
81 
82  public:
83 
90  explicit CCAMERA( float aRangeScale );
91 
92 
98  const glm::mat4 GetRotationMatrix() const;
99 
100  const glm::mat4 &GetViewMatrix() const;
101  const glm::mat4 &GetViewMatrix_Inv() const;
102 
103  const glm::mat4 &GetProjectionMatrix() const;
104  const glm::mat4 &GetProjectionMatrixInv() const;
105 
106  const SFVEC3F &GetRight() const { return m_right; }
107  const SFVEC3F &GetUp() const { return m_up; }
108  const SFVEC3F &GetDir() const { return m_dir; }
109  const SFVEC3F &GetPos() const { return m_pos; }
110  const SFVEC2F &GetFocalLen() const { return m_focalLen; }
111  float GetNear() const { return m_frustum.nearD; }
112  float GetFar() const { return m_frustum.farD; }
113 
114  void SetBoardLookAtPos( const SFVEC3F &aBoardPos ) {
115  if( m_board_lookat_pos_init != aBoardPos )
116  {
117  m_board_lookat_pos_init = aBoardPos;
118  SetLookAtPos( aBoardPos );
119  }
120  }
121 
122  virtual void SetLookAtPos( const SFVEC3F &aLookAtPos ) = 0;
123 
124  void SetLookAtPos_T1( const SFVEC3F &aLookAtPos ) {
125  m_lookat_pos_t1 = aLookAtPos;
126  }
127 
128  const SFVEC3F &GetLookAtPos_T1() const { return m_lookat_pos_t1; }
129 
130  const SFVEC3F &GetCameraPos() const { return m_camera_pos; }
131 
135  virtual void Drag( const wxPoint &aNewMousePosition ) = 0;
136 
137  virtual void Pan( const wxPoint &aNewMousePosition ) = 0;
138 
139  virtual void Pan( const SFVEC3F &aDeltaOffsetInc ) = 0;
140 
141  virtual void Pan_T1( const SFVEC3F &aDeltaOffsetInc ) = 0;
142 
143 
147  virtual void Reset();
148  virtual void Reset_T1();
149 
150  void ResetXYpos();
151  void ResetXYpos_T1();
152 
157  void SetCurMousePosition( const wxPoint &aPosition );
158 
159  void SetProjection( PROJECTION_TYPE aProjectionType );
160 
161  void ToggleProjection();
162 
168  bool SetCurWindowSize( const wxSize &aSize );
169 
170  void ZoomReset();
171 
172  bool Zoom( float aFactor );
173 
174  bool Zoom_T1( float aFactor );
175 
176  float ZoomGet() const ;
177 
178  void RotateX( float aAngleInRadians );
179  void RotateY( float aAngleInRadians );
180  void RotateZ( float aAngleInRadians );
181 
182  void RotateX_T1( float aAngleInRadians );
183  void RotateY_T1( float aAngleInRadians );
184  void RotateZ_T1( float aAngleInRadians );
185 
189  virtual void SetT0_and_T1_current_T();
190 
195  virtual void Interpolate( float t );
196 
197  void SetInterpolateMode( CAMERA_INTERPOLATION aInterpolateMode )
198  {
199  m_interpolation_mode = aInterpolateMode;
200  }
201 
207  bool ParametersChanged();
208 
215 
222  void MakeRay( const SFVEC2I &aWindowPos, SFVEC3F &aOutOrigin, SFVEC3F &aOutDirection ) const;
223 
230  void MakeRay( const SFVEC2F &aWindowPos, SFVEC3F &aOutOrigin, SFVEC3F &aOutDirection ) const;
231 
237  void MakeRayAtCurrrentMousePosition( SFVEC3F &aOutOrigin, SFVEC3F &aOutDirection ) const;
238 
239  protected:
240 
241  void rebuildProjection();
242  void updateFrustum();
243  void updateViewMatrix();
244 
245  void updateRotationMatrix();
246 
252 
256  float m_zoom;
257  float m_zoom_t0;
258  float m_zoom_t1;
259 
264 
269 
270  glm::mat4 m_rotationMatrix;
272  glm::mat4 m_viewMatrix;
277 
279 
284 
286 
291 
296 
300 
302 
307  std::vector< float > m_scr_nX;
308  std::vector< float > m_scr_nY;
309 
314  std::vector< SFVEC3F > m_right_nX;
315  std::vector< SFVEC3F > m_up_nY;
316 
317 
322 
329  static const wxChar *m_logTrace;
330 };
331 
332 #endif // CCAMERA_H
SFVEC3F fc
Definition: ccamera.h:51
void SetLookAtPos_T1(const SFVEC3F &aLookAtPos)
Definition: ccamera.h:124
PROJECTION_TYPE
Definition: ccamera.h:37
void ResetXYpos()
Definition: ccamera.cpp:401
bool m_parametersChanged
Set to true if any of the parameters in the camera was changed.
Definition: ccamera.h:321
CAMERA_INTERPOLATION
Definition: ccamera.h:65
Frustum structure Frustum is a implementation based on a tutorial by http://www.lighthouse3d....
Definition: ccamera.h:48
SFVEC3F m_dir
Definition: ccamera.h:282
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:565
SFVEC3F ftl
Far Top Left.
Definition: ccamera.h:56
void ResetXYpos_T1()
Definition: ccamera.cpp:412
SFVEC3F ntl
Near Top Left.
Definition: ccamera.h:52
virtual void Reset()
Reset the camera to initial state.
Definition: ccamera.cpp:71
float farD
Definition: ccamera.h:60
const SFVEC3F & GetRight() const
Definition: ccamera.h:106
static const wxChar * m_logTrace
Trace mask used to enable or disable the trace output of this class.
Definition: ccamera.h:329
SFVEC3F m_board_lookat_pos_init
Default boardlookat position (the board center)
Definition: ccamera.h:295
SFVEC3F m_rotate_aux
Stores the rotation angle auxiliar.
Definition: ccamera.h:297
void ZoomReset()
Definition: ccamera.cpp:474
void RotateY(float aAngleInRadians)
Definition: ccamera.cpp:533
CCAMERA(float aRangeScale)
CCAMERA initialize a camera.
Definition: ccamera.cpp:56
Class CCAMERA is a virtual class used to derive CCAMERA objects from.
Definition: ccamera.h:79
void updateFrustum()
Definition: ccamera.cpp:263
std::vector< SFVEC3F > m_up_nY
Definition: ccamera.h:315
void SetProjection(PROJECTION_TYPE aProjectionType)
Definition: ccamera.cpp:437
float nearD
Definition: ccamera.h:60
glm::mat4 m_viewMatrixInverse
Definition: ccamera.h:273
float nw
Definition: ccamera.h:61
const SFVEC2F & GetFocalLen() const
Definition: ccamera.h:110
std::vector< float > m_scr_nY
Definition: ccamera.h:308
const SFVEC3F & GetDir() const
Definition: ccamera.h:108
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:307
const SFVEC3F & GetPos() const
Definition: ccamera.h:109
SFVEC3F fbr
Far Bottom Right.
Definition: ccamera.h:59
glm::ivec2 SFVEC2I
Definition: xv3d_types.h:42
float tang
Definition: ccamera.h:60
void updateViewMatrix()
Definition: ccamera.cpp:126
const SFVEC3F & GetLookAtPos_T1() const
Definition: ccamera.h:128
wxPoint m_lastPosition
The last mouse position in the screen.
Definition: ccamera.h:268
void SetCurMousePosition(const wxPoint &aPosition)
It updates the current mouse position without make any new recalculations on camera.
Definition: ccamera.cpp:431
bool Zoom(float aFactor)
Definition: ccamera.cpp:484
const glm::mat4 GetRotationMatrix() const
Function GetRotationMatrix Get the rotation matrix to be applied in a transformation camera.
Definition: ccamera.cpp:158
float fw
Definition: ccamera.h:61
void RotateZ(float aAngleInRadians)
Definition: ccamera.cpp:540
void RotateX(float aAngleInRadians)
Definition: ccamera.cpp:526
SFVEC2F m_focalLen
Definition: ccamera.h:285
SFVEC3F nbr
Near Bottom Right.
Definition: ccamera.h:55
SFVEC3F m_camera_pos_t1
Definition: ccamera.h:290
SFVEC3F fbl
Far Bottom Left.
Definition: ccamera.h:58
float m_zoom_t0
Definition: ccamera.h:257
float ZoomGet() const
Definition: ccamera.cpp:520
glm::vec2 SFVEC2F
Definition: xv3d_types.h:45
const SFVEC3F & GetUp() const
Definition: ccamera.h:107
SFVEC3F m_right
Definition: ccamera.h:280
SFVEC3F m_pos
Definition: ccamera.h:283
void ToggleProjection()
Definition: ccamera.cpp:447
float angle
Definition: ccamera.h:60
SFVEC3F m_up
Definition: ccamera.h:281
SFVEC3F m_lookat_pos_t1
Definition: ccamera.h:294
virtual void Interpolate(float t)
Interpolate - It will update the matrix to interpolate between T0 and T1 values.
Definition: ccamera.cpp:579
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:251
SFVEC3F m_lookat_pos_t0
Definition: ccamera.h:293
bool SetCurWindowSize(const wxSize &aSize)
SetCurWindowSize - update the windows size of the camera.
Definition: ccamera.cpp:458
glm::mat4 m_rotationMatrix
Definition: ccamera.h:270
SFVEC3F ntr
Near Top Right.
Definition: ccamera.h:53
FRUSTUM m_frustum
Definition: ccamera.h:278
glm::mat4 m_projectionMatrix
Definition: ccamera.h:274
bool Zoom_T1(float aFactor)
Definition: ccamera.cpp:503
virtual void Pan_T1(const SFVEC3F &aDeltaOffsetInc)=0
void RotateX_T1(float aAngleInRadians)
Definition: ccamera.cpp:547
virtual void Reset_T1()
Definition: ccamera.cpp:103
float m_zoom
3D zoom value (Min 0.0 ...
Definition: ccamera.h:256
SFVEC3F m_rotate_aux_t1
Definition: ccamera.h:299
void updateRotationMatrix()
Definition: ccamera.cpp:134
virtual void SetLookAtPos(const SFVEC3F &aLookAtPos)=0
SFVEC3F m_camera_pos_init
Definition: ccamera.h:287
SFVEC3F m_camera_pos
Definition: ccamera.h:288
SFVEC3F m_camera_pos_t0
Definition: ccamera.h:289
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:314
SFVEC3F nc
Definition: ccamera.h:50
void RotateY_T1(float aAngleInRadians)
Definition: ccamera.cpp:553
void MakeRayAtCurrrentMousePosition(SFVEC3F &aOutOrigin, SFVEC3F &aOutDirection) const
MakeRayAtCurrrentMousePosition - Make a ray based on the latest mouse position.
Definition: ccamera.cpp:380
const glm::mat4 & GetViewMatrix() const
Definition: ccamera.cpp:419
void rebuildProjection()
Definition: ccamera.cpp:164
virtual void Pan(const wxPoint &aNewMousePosition)=0
glm::vec3 SFVEC3F
Definition: xv3d_types.h:47
void SetBoardLookAtPos(const SFVEC3F &aBoardPos)
Definition: ccamera.h:114
SFVEC2I m_windowSize
The window size that this camera is working.
Definition: ccamera.h:263
SFVEC3F ftr
Far Top Right.
Definition: ccamera.h:57
const glm::mat4 & GetProjectionMatrix() const
Definition: ccamera.cpp:389
float m_zoom_t1
Definition: ccamera.h:258
CAMERA_INTERPOLATION m_interpolation_mode
Definition: ccamera.h:301
void MakeRay(const SFVEC2I &aWindowPos, SFVEC3F &aOutOrigin, SFVEC3F &aOutDirection) const
MakeRay - Make a ray based on a windows screen position.
Definition: ccamera.cpp:323
const SFVEC3F & GetCameraPos() const
Definition: ccamera.h:130
const glm::mat4 & GetProjectionMatrixInv() const
Definition: ccamera.cpp:395
SFVEC3F m_rotate_aux_t0
Definition: ccamera.h:298
glm::mat4 m_rotationMatrixAux
Definition: ccamera.h:271
bool ParametersChangedQuery() const
Function ParametersChangedQuery.
Definition: ccamera.h:214
const glm::mat4 & GetViewMatrix_Inv() const
Definition: ccamera.cpp:425
void RotateZ_T1(float aAngleInRadians)
Definition: ccamera.cpp:559
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:272
void SetInterpolateMode(CAMERA_INTERPOLATION aInterpolateMode)
Definition: ccamera.h:197
glm::mat4 m_projectionMatrixInv
Definition: ccamera.h:275
float GetFar() const
Definition: ccamera.h:112
bool ParametersChanged()
Function ParametersChanged.
Definition: ccamera.cpp:597
float GetNear() const
Definition: ccamera.h:111
SFVEC3F m_lookat_pos
Definition: ccamera.h:292
PROJECTION_TYPE m_projectionType
Definition: ccamera.h:276