Cafu Engine
Generic3DWindow.hpp
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 #ifndef CAFU_GENERIC_3D_WINDOW_HPP_INCLUDED
8 #define CAFU_GENERIC_3D_WINDOW_HPP_INCLUDED
9 
10 #include "Math3D/Plane3.hpp"
11 #include "wx/glcanvas.h"
12 
13 
14 class AxesInfoT;
15 class CameraT;
16 
17 
18 /// This class implements a generic 3D window.
19 /// It provides basic, common 3D window functionality that is independent from any editor.
20 class Generic3DWindowT : public wxGLCanvas
21 {
22  public:
23 
24  /// This class defines if and how the camera of the associated window is currently being controlled with the mouse.
26  {
27  public:
28 
29  enum StateT { NOT_ACTIVE, ACTIVE_NORMAL, ACTIVE_ORBIT };
30 
32 
33  /// Activates the mouse control in the given state.
34  /// @param NewState The state in which the mouse control should be activated.
35  /// @param RefPt The position of the reference point in window coordinates.
36  /// Note that if the mouse control is already active, it can not be reactivated in a different ACTIVE_* state but must be deactivated first.
37  void Activate(StateT NewState, const wxPoint& RefPt=wxDefaultPosition);
38 
39  /// Deactivates the mouse control.
40  void Deactivate();
41 
42  /// Returns the state that the mouse control is currently in.
43  StateT GetState() const { return m_State; }
44 
45  /// Returns whether the mouse control is active; a shortcut for GetState()!=NOT_ACTIVE.
46  bool IsActive() const { return m_State!=NOT_ACTIVE; }
47 
48  /// Return the position of the reference point in window coordinates as set when the mouse control was activated.
49  const wxPoint& GetRefPtWin() const { return m_RefPtWin; }
50 
51  /// Return the position of the reference point in world coordinates as set when the mouse control was activated.
52  const Vector3fT& GetRefPtWorld() const { return m_RefPtWorld; }
53 
54 
55  private:
56 
57  Generic3DWindowT& m_Win; ///< The 3D window that this is the mouse control for.
58  StateT m_State; ///< The current state of the mouse control.
59  wxPoint m_RefPtWin; ///< The position of the reference point in window coordinates as set when the mouse control was activated.
60  Vector3fT m_RefPtWorld; ///< The position of the reference point in world coordinates as set when the mouse control was activated.
61  };
62 
63 
64  /// The constructor.
65  Generic3DWindowT(wxWindow* Parent, CameraT* InitialCamera);
66 
67  /// The destructor.
69 
70  /// Returns the set of axes that the camera orientation is currently the closest to.
71  AxesInfoT GetAxesInfo() const;
72 
73  /// Returns the camera that is currently associated with this window.
74  const CameraT& GetCamera() const { return *m_Camera; }
75 
76  /// Sets Camera as the new camera to use for this window.
77  void SetCamera(CameraT* Camera);
78 
79  /// Moves the camera that is currently associated with this window to the given new position.
80  void MoveCamera(const Vector3fT& NewPos);
81 
82  /// Processes the user input for the (last) frame with the given duration and updates the camera accordingly.
83  void ProcessInput(float FrameTime);
84 
85  /// Returns the mouse control instance of this window.
86  const MouseControlT& GetMouseControl() const { return m_MouseControl; }
87 
88  /// Returns the view frustum for this window, based on its current window dimensions and camera setting.
89  void GetViewFrustum(Plane3fT* Planes, unsigned int NumPlanes=6) const;
90 
91  /// Transforms (unprojects) the given pixel from window space to the related point in world space.
92  /// The returned point is located on the near clipping plane of this windows view pyramid/frustum.
93  /// Therefore, "camera rays" are conveniently built through the two points GetCamera().Pos and WindowToWorld(Pixel).
94  /// This method is "roughly" the inverse of WorldToWindow().
95  /// @param Pixel The pixel to convert from window to world space.
96  /// @returns the pixel in world space coordinates.
97  Vector3fT WindowToWorld(const wxPoint& Pixel) const;
98 
99  /// Transforms (projects) the given point from world space to the related pixel in the 3D window.
100  /// The transformation is not always reasonably possible, and therefore, if CheckFrustum is true (recommended),
101  /// the method checks whether v is inside the view frustum first and returns (-1, -1) otherwise.
102  /// If CheckFrustum is false, no view-frustum check is performed, with the potential negative consequences
103  /// that can occur when performing the transformation stubbornly (divisions-by-zero, point behind the
104  /// viewer yields valid window coordinates, etc.). This method is "roughly" the inverse of WindowToWorld().
105  /// @param v The point to be transformed from world to window coordinates.
106  /// @param CheckFrustum Whether v should be tested against the current view frustum before the transformation.
107  /// @returns the transformed point, or (-1, -1) if CheckFrustum was true and the v failed the test.
108  wxPoint WorldToWindow(const Vector3fT& v, bool CheckFrustum) const;
109 
110 
111  private:
112 
113  // Methods that derived classes (cannot call, but) must implement.
114  virtual Vector3fT GetRefPtWorld(const wxPoint& RefPtWin) const=0;
115  virtual void InfoCameraChanged()=0;
116  virtual void InfoRightMouseClick(wxMouseEvent& ME)=0;
117 
118  enum RightMBStateT { RMB_UP_IDLE, RMB_DOWN_UNDECIDED, RMB_DOWN_DRAGGING }; ///< This enumeration describes the states that the right mouse button can take.
119 
120  CameraT* m_Camera; ///< Pointer to the camera that is currently used for this 3D window. The actual instance of the camera is kept in and owned by the caller (i.e. the owner of the window instance).
121  Vector3fT m_CameraVel; ///< The cameras current velocity, in camera space. Positive values for m_CameraVel.y mean forward movement, etc.
122  MouseControlT m_MouseControl; ///< If and how the camera of this window is currently being controlled with the mouse.
123  RightMBStateT m_RightMBState; ///< The state of the right mouse button. This is required because the RMB has a dual function: a click can bring up the context menu, or initiate mouse-looking for the 3D window.
124  wxPoint m_RDownPosWin; ///< The point where the RMB went down, in window coordinates.
125 
126  // Event handlers.
127  void OnKeyDown (wxKeyEvent& ME);
128  void OnKeyUp (wxKeyEvent& ME);
129  void OnMouseMiddleDown (wxMouseEvent& ME); ///< We also handle "double-click" events in this method (use ME.ButtonDClick() for distinction).
130  void OnMouseMiddleUp (wxMouseEvent& ME);
131  void OnMouseRightDown (wxMouseEvent& ME); ///< We also handle "double-click" events in this method (use ME.ButtonDClick() for distinction).
132  void OnMouseRightUp (wxMouseEvent& ME);
133  void OnMouseWheel (wxMouseEvent& ME);
134  void OnMouseMove (wxMouseEvent& ME);
135  void OnKillFocus (wxFocusEvent& FE);
136  void OnMouseCaptureLost(wxMouseCaptureLostEvent& ME);
137 
138  DECLARE_EVENT_TABLE()
139 };
140 
141 #endif
bool IsActive() const
Returns whether the mouse control is active; a shortcut for GetState()!=NOT_ACTIVE.
Definition: Generic3DWindow.hpp:46
const Vector3fT & GetRefPtWorld() const
Return the position of the reference point in world coordinates as set when the mouse control was act...
Definition: Generic3DWindow.hpp:52
Vector3fT WindowToWorld(const wxPoint &Pixel) const
Transforms (unprojects) the given pixel from window space to the related point in world space...
Definition: Generic3DWindow.cpp:415
const CameraT & GetCamera() const
Returns the camera that is currently associated with this window.
Definition: Generic3DWindow.hpp:74
const wxPoint & GetRefPtWin() const
Return the position of the reference point in window coordinates as set when the mouse control was ac...
Definition: Generic3DWindow.hpp:49
void GetViewFrustum(Plane3fT *Planes, unsigned int NumPlanes=6) const
Returns the view frustum for this window, based on its current window dimensions and camera setting...
Definition: Generic3DWindow.cpp:372
This class implements a camera.
Definition: Camera.hpp:17
wxPoint WorldToWindow(const Vector3fT &v, bool CheckFrustum) const
Transforms (projects) the given point from world space to the related pixel in the 3D window...
Definition: Generic3DWindow.cpp:443
Generic3DWindowT(wxWindow *Parent, CameraT *InitialCamera)
The constructor.
Definition: Generic3DWindow.cpp:80
This class defines if and how the camera of the associated window is currently being controlled with ...
Definition: Generic3DWindow.hpp:25
StateT GetState() const
Returns the state that the mouse control is currently in.
Definition: Generic3DWindow.hpp:43
void MoveCamera(const Vector3fT &NewPos)
Moves the camera that is currently associated with this window to the given new position.
Definition: Generic3DWindow.cpp:150
AxesInfoT GetAxesInfo() const
Returns the set of axes that the camera orientation is currently the closest to.
Definition: Generic3DWindow.cpp:134
void SetCamera(CameraT *Camera)
Sets Camera as the new camera to use for this window.
Definition: Generic3DWindow.cpp:143
This class describes how the three world-space axes are mapped to the two screen- or window-space axe...
Definition: AxesInfo.hpp:15
void Deactivate()
Deactivates the mouse control.
Definition: Generic3DWindow.cpp:49
const MouseControlT & GetMouseControl() const
Returns the mouse control instance of this window.
Definition: Generic3DWindow.hpp:86
This class implements a generic 3D window.
Definition: Generic3DWindow.hpp:20
void Activate(StateT NewState, const wxPoint &RefPt=wxDefaultPosition)
Activates the mouse control in the given state.
Definition: Generic3DWindow.cpp:30
~Generic3DWindowT()
The destructor.
Definition: Generic3DWindow.cpp:92
void ProcessInput(float FrameTime)
Processes the user input for the (last) frame with the given duration and updates the camera accordin...
Definition: Generic3DWindow.cpp:157