NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
CameraControls.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2011, NVIDIA Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA Corporation nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #pragma once
29 #include "gui/CommonControls.hpp"
30 #include "base/Timer.hpp"
31 
32 namespace FW
33 {
34 //------------------------------------------------------------------------
35 
36 class MeshBase;
37 
38 //------------------------------------------------------------------------
39 
41 {
42 public:
43 
44  //------------------------------------------------------------------------
45 
46  enum Feature
47  {
56 
58  Feature_All = (1 << 8) - 1,
60  };
61 
62  //------------------------------------------------------------------------
63 
64 public:
65  CameraControls (CommonControls* commonControls = NULL, U32 features = Feature_Default);
66  virtual ~CameraControls (void);
67 
68  virtual bool handleEvent (const Window::Event& ev); // must be before the listener that queries things
69  virtual void readState (StateDump& d);
70  virtual void writeState (StateDump& d) const;
71 
72  const Vec3f& getPosition (void) const { return m_position; }
73  void setPosition (const Vec3f& v) { m_position = v; repaint(); }
74  const Vec3f& getForward (void) const { return m_forward; }
75  void setForward (const Vec3f& v) { m_forward = v; repaint(); }
76  const Vec3f& getUp (void) const { return m_up; }
77  void setUp (const Vec3f& v) { m_up = v; repaint(); }
78  bool getKeepAligned (void) const { return m_keepAligned; }
79  void setKeepAligned (bool v) { m_keepAligned = v; }
80  F32 getSpeed (void) const { return m_speed; }
81  void setSpeed (F32 v) { m_speed = v; }
82  F32 getFOV (void) const { return m_fov; }
83  void setFOV (F32 v) { m_fov = v; repaint(); }
84  F32 getNear (void) const { return m_near; }
85  void setNear (F32 v) { m_near = v; repaint(); }
86  F32 getFar (void) const { return m_far; }
87  void setFar (F32 v) { m_far = v; repaint(); }
88 
89  Mat3f getOrientation (void) const;
90  Mat4f getCameraToWorld (void) const;
91  Mat4f getWorldToCamera (void) const;
92  Mat4f getCameraToClip (void) const { return Mat4f::perspective(m_fov, m_near, m_far); }
93  Mat4f getWorldToClip (void) const { return getCameraToClip() * getWorldToCamera(); }
94  Mat4f getCameraToLeftEye (void) const { Mat4f m; m.m02 = m_stereoConvergence; m.m03 = m_stereoSeparation; return m; }
96 
97  void setCameraToWorld (const Mat4f& m); // Sets position & forward, and up if !keepAligned.
98  void setWorldToCamera (const Mat4f& m) { setCameraToWorld(invert(m)); }
99 
100  void initDefaults (void);
101  void initForMesh (const MeshBase* mesh);
102 
103  String encodeSignature (void) const;
104  void decodeSignature (const String& sig);
105  void print (void);
106 
107  void addGUIControls (void); // done automatically on Window.addListener(this)
108  void removeGUIControls (void);
109  void setEnableMovement (bool enable) { m_enableMovement = enable; }
110 
111 private:
112  bool hasFeature (Feature feature) { return ((m_features & feature) != 0); }
113  void repaint (void) { if (m_window) m_window->repaint(); }
114 
115  static void encodeBits (String& dst, U32 v);
116  static U32 decodeBits (const char*& src);
117 
118  static void encodeFloat (String& dst, F32 v);
119  static F32 decodeFloat (const char*& src);
120 
121  static void encodeDirection (String& dst, const Vec3f& v);
122  static Vec3f decodeDirection (const char*& src);
123 
124 private:
125  CameraControls (const CameraControls&); // forbidden
126  CameraControls& operator= (const CameraControls&); // forbidden
127 
128 private:
129  CommonControls* m_commonControls;
130  U32 m_features;
131  Window* m_window;
132  Timer m_timer;
133  bool m_enableMovement;
134 
135  Vec3f m_position;
136  Vec3f m_forward;
137  Vec3f m_up;
138 
139  bool m_keepAligned;
140  F32 m_speed;
141  F32 m_fov;
142  F32 m_near;
143  F32 m_far;
144 
145  bool m_dragLeft;
146  bool m_dragMiddle;
147  bool m_dragRight;
148  bool m_alignY;
149  bool m_alignZ;
150 
151  bool m_enableStereo;
152  F32 m_stereoSeparation;
153  F32 m_stereoConvergence;
154 };
155 
156 //------------------------------------------------------------------------
157 }
void setEnableMovement(bool enable)
#define NULL
Definition: Defs.hpp:39
Mat4f getWorldToCamera(void) const
void removeGUIControls(void)
void setUp(const Vec3f &v)
CameraControls(CommonControls *commonControls=NULL, U32 features=Feature_Default)
F32 getSpeed(void) const
void setPosition(const Vec3f &v)
void setKeepAligned(bool v)
Mat4f getWorldToClip(void) const
const Vec3f & getUp(void) const
F32 m02
Definition: Math.hpp:698
void repaint(void)
Definition: Window.cpp:216
Mat4f getCameraToWorld(void) const
float F32
Definition: Defs.hpp:89
FW_CUDA_FUNC S invert(const MatrixBase< T, L, S > &v)
Definition: Math.hpp:784
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid GLenum usage GLuint shader GLenum type GLsizei const GLuint framebuffers GLsizei const GLuint renderbuffers GLuint v
Definition: DLLImports.inl:329
virtual void writeState(StateDump &d) const
F32 getFar(void) const
Mat4f getCameraToClip(void) const
static Mat4f perspective(F32 fov, F32 nearDist, F32 farDist)
Definition: Math.cpp:79
Mat4f getCameraToLeftEye(void) const
F32 getNear(void) const
Mat4f getCameraToRightEye(void) const
String encodeSignature(void) const
F32 m03
Definition: Math.hpp:699
FW_CUDA_FUNC S inverted(void) const
Definition: Math.hpp:964
Mat3f getOrientation(void) const
void setForward(const Vec3f &v)
void setCameraToWorld(const Mat4f &m)
unsigned int U32
Definition: Defs.hpp:85
const Vec3f & getForward(void) const
void initForMesh(const MeshBase *mesh)
bool getKeepAligned(void) const
virtual void readState(StateDump &d)
F32 getFOV(void) const
void decodeSignature(const String &sig)
const Vec3f & getPosition(void) const
void setWorldToCamera(const Mat4f &m)
virtual bool handleEvent(const Window::Event &ev)
virtual ~CameraControls(void)