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  virtual ~CCAMERA()
93  {
94  }
95 
96 
102  const glm::mat4 GetRotationMatrix() const;
103 
104  const glm::mat4 &GetViewMatrix() const;
105  const glm::mat4 &GetViewMatrix_Inv() const;
106 
107  const glm::mat4 &GetProjectionMatrix() const;
108  const glm::mat4 &GetProjectionMatrixInv() const;
109 
110  const SFVEC3F &GetRight() const { return m_right; }
111  const SFVEC3F &GetUp() const { return m_up; }
112  const SFVEC3F &GetDir() const { return m_dir; }
113  const SFVEC3F &GetPos() const { return m_pos; }
114  const SFVEC2F &GetFocalLen() const { return m_focalLen; }
115  float GetNear() const { return m_frustum.nearD; }
116  float GetFar() const { return m_frustum.farD; }
117 
118  void SetBoardLookAtPos( const SFVEC3F &aBoardPos ) {
119  if( m_board_lookat_pos_init != aBoardPos )
120  {
121  m_board_lookat_pos_init = aBoardPos;
122  SetLookAtPos( aBoardPos );
123  }
124  }
125 
126  virtual void SetLookAtPos( const SFVEC3F &aLookAtPos ) = 0;
127 
128  void SetLookAtPos_T1( const SFVEC3F &aLookAtPos ) {
129  m_lookat_pos_t1 = aLookAtPos;
130  }
131 
132  const SFVEC3F &GetLookAtPos_T1() const { return m_lookat_pos_t1; }
133 
134  const SFVEC3F &GetCameraPos() const { return m_camera_pos; }
135 
139  virtual void Drag( const wxPoint &aNewMousePosition ) = 0;
140 
141  virtual void Pan( const wxPoint &aNewMousePosition ) = 0;
142 
143  virtual void Pan( const SFVEC3F &aDeltaOffsetInc ) = 0;
144 
145  virtual void Pan_T1( const SFVEC3F &aDeltaOffsetInc ) = 0;
146 
147 
151  virtual void Reset();
152  virtual void Reset_T1();
153 
154  void ResetXYpos();
155  void ResetXYpos_T1();
156 
161  void SetCurMousePosition( const wxPoint &aPosition );
162 
163  void ToggleProjection();
165 
171  bool SetCurWindowSize( const wxSize &aSize );
172 
173  void ZoomReset();
174 
175  bool Zoom( float aFactor );
176 
177  bool Zoom_T1( float aFactor );
178 
179  float ZoomGet() const ;
180 
181  void RotateX( float aAngleInRadians );
182  void RotateY( float aAngleInRadians );
183  void RotateZ( float aAngleInRadians );
184 
185  void RotateX_T1( float aAngleInRadians );
186  void RotateY_T1( float aAngleInRadians );
187  void RotateZ_T1( float aAngleInRadians );
188 
192  virtual void SetT0_and_T1_current_T();
193 
198  virtual void Interpolate( float t );
199 
200  void SetInterpolateMode( CAMERA_INTERPOLATION aInterpolateMode )
201  {
202  m_interpolation_mode = aInterpolateMode;
203  }
204 
210  bool ParametersChanged();
211 
218 
225  void MakeRay( const SFVEC2I &aWindowPos, SFVEC3F &aOutOrigin, SFVEC3F &aOutDirection ) const;
226 
233  void MakeRay( const SFVEC2F &aWindowPos, SFVEC3F &aOutOrigin, SFVEC3F &aOutDirection ) const;
234 
240  void MakeRayAtCurrrentMousePosition( SFVEC3F &aOutOrigin, SFVEC3F &aOutDirection ) const;
241 
242  protected:
243 
244  void rebuildProjection();
245  void updateFrustum();
246  void updateViewMatrix();
247 
248  void updateRotationMatrix();
249 
255 
259  float m_zoom;
260  float m_zoom_t0;
261  float m_zoom_t1;
262 
267 
272 
273  glm::mat4 m_rotationMatrix;
275  glm::mat4 m_viewMatrix;
280 
282 
287 
289 
294 
299 
303 
305 
310  std::vector< float > m_scr_nX;
311  std::vector< float > m_scr_nY;
312 
317  std::vector< SFVEC3F > m_right_nX;
318  std::vector< SFVEC3F > m_up_nY;
319 
320 
325 
332  static const wxChar *m_logTrace;
333 };
334 
335 #endif // CCAMERA_H
SFVEC3F fc
Definition: ccamera.h:51
void SetLookAtPos_T1(const SFVEC3F &aLookAtPos)
Definition: ccamera.h:128
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:324
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:285
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:555
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:110
static const wxChar * m_logTrace
Trace mask used to enable or disable the trace output of this class.
Definition: ccamera.h:332
SFVEC3F m_board_lookat_pos_init
Default boardlookat position (the board center)
Definition: ccamera.h:298
SFVEC3F m_rotate_aux
Stores the rotation angle auxiliar.
Definition: ccamera.h:300
void ZoomReset()
Definition: ccamera.cpp:464
void RotateY(float aAngleInRadians)
Definition: ccamera.cpp:523
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:318
float nearD
Definition: ccamera.h:60
glm::mat4 m_viewMatrixInverse
Definition: ccamera.h:276
float nw
Definition: ccamera.h:61
const SFVEC2F & GetFocalLen() const
Definition: ccamera.h:114
std::vector< float > m_scr_nY
Definition: ccamera.h:311
const SFVEC3F & GetDir() const
Definition: ccamera.h:112
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:310
const SFVEC3F & GetPos() const
Definition: ccamera.h:113
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:132
wxPoint m_lastPosition
The last mouse position in the screen.
Definition: ccamera.h:271
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:474
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:530
void RotateX(float aAngleInRadians)
Definition: ccamera.cpp:516
SFVEC2F m_focalLen
Definition: ccamera.h:288
virtual ~CCAMERA()
Definition: ccamera.h:92
SFVEC3F nbr
Near Bottom Right.
Definition: ccamera.h:55
SFVEC3F m_camera_pos_t1
Definition: ccamera.h:293
SFVEC3F fbl
Far Bottom Left.
Definition: ccamera.h:58
float m_zoom_t0
Definition: ccamera.h:260
float ZoomGet() const
Definition: ccamera.cpp:510
PROJECTION_TYPE GetProjection()
Definition: ccamera.h:164
glm::vec2 SFVEC2F
Definition: xv3d_types.h:45
const SFVEC3F & GetUp() const
Definition: ccamera.h:111
SFVEC3F m_right
Definition: ccamera.h:283
SFVEC3F m_pos
Definition: ccamera.h:286
void ToggleProjection()
Definition: ccamera.cpp:437
float angle
Definition: ccamera.h:60
SFVEC3F m_up
Definition: ccamera.h:284
SFVEC3F m_lookat_pos_t1
Definition: ccamera.h:297
virtual void Interpolate(float t)
Interpolate - It will update the matrix to interpolate between T0 and T1 values.
Definition: ccamera.cpp:569
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:254
SFVEC3F m_lookat_pos_t0
Definition: ccamera.h:296
bool SetCurWindowSize(const wxSize &aSize)
SetCurWindowSize - update the windows size of the camera.
Definition: ccamera.cpp:448
glm::mat4 m_rotationMatrix
Definition: ccamera.h:273
SFVEC3F ntr
Near Top Right.
Definition: ccamera.h:53
FRUSTUM m_frustum
Definition: ccamera.h:281
glm::mat4 m_projectionMatrix
Definition: ccamera.h:277
bool Zoom_T1(float aFactor)
Definition: ccamera.cpp:493
virtual void Pan_T1(const SFVEC3F &aDeltaOffsetInc)=0
void RotateX_T1(float aAngleInRadians)
Definition: ccamera.cpp:537
virtual void Reset_T1()
Definition: ccamera.cpp:103
float m_zoom
3D zoom value (Min 0.0 ...
Definition: ccamera.h:259
SFVEC3F m_rotate_aux_t1
Definition: ccamera.h:302
void updateRotationMatrix()
Definition: ccamera.cpp:134
virtual void SetLookAtPos(const SFVEC3F &aLookAtPos)=0
SFVEC3F m_camera_pos_init
Definition: ccamera.h:290
SFVEC3F m_camera_pos
Definition: ccamera.h:291
SFVEC3F m_camera_pos_t0
Definition: ccamera.h:292
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:317
SFVEC3F nc
Definition: ccamera.h:50
void RotateY_T1(float aAngleInRadians)
Definition: ccamera.cpp:543
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:118
SFVEC2I m_windowSize
The window size that this camera is working.
Definition: ccamera.h:266
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:261
CAMERA_INTERPOLATION m_interpolation_mode
Definition: ccamera.h:304
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:134
const glm::mat4 & GetProjectionMatrixInv() const
Definition: ccamera.cpp:395
SFVEC3F m_rotate_aux_t0
Definition: ccamera.h:301
glm::mat4 m_rotationMatrixAux
Definition: ccamera.h:274
bool ParametersChangedQuery() const
Function ParametersChangedQuery.
Definition: ccamera.h:217
const glm::mat4 & GetViewMatrix_Inv() const
Definition: ccamera.cpp:425
void RotateZ_T1(float aAngleInRadians)
Definition: ccamera.cpp:549
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:275
void SetInterpolateMode(CAMERA_INTERPOLATION aInterpolateMode)
Definition: ccamera.h:200
glm::mat4 m_projectionMatrixInv
Definition: ccamera.h:278
float GetFar() const
Definition: ccamera.h:116
bool ParametersChanged()
Function ParametersChanged.
Definition: ccamera.cpp:587
float GetNear() const
Definition: ccamera.h:115
SFVEC3F m_lookat_pos
Definition: ccamera.h:295
PROJECTION_TYPE m_projectionType
Definition: ccamera.h:279