VR-Vantage 2.5 API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
oculusdevice.h
Go to the documentation of this file.
1 /*
2  * oculusdevice.h
3  *
4  * Created on: Jul 03, 2013
5  * Author: Bjorn Blissing
6  */
7 
8 #ifndef _OSG_OCULUSDEVICE_H_
9 #define _OSG_OCULUSDEVICE_H_
10 
11 #include <GL/glew.h>
12 
13 // Include the OculusVR SDK
14 #include <OVR_CAPI_GL.h>
15 
16 #include <osg/Geode>
17 #include <osg/Texture2D>
18 #include <osg/Version>
19 #include <osg/FrameBufferObject>
20 
21 
22 class OculusTextureBuffer : public osg::Referenced
23 {
24 public:
25  OculusTextureBuffer(const ovrSession& session, osg::ref_ptr<osg::State> state, const ovrSizei& size, int msaaSamples);
26  void destroy();
27  int textureWidth() const { return m_textureSize.x(); }
28  int textureHeight() const { return m_textureSize.y(); }
29  int samples() const { return m_samples; }
30  ovrTextureSwapChain textureSwapChain() const { return m_textureSwapChain; }
31  osg::ref_ptr<osg::Texture2D> colorBuffer() const { return m_colorBuffer; }
32  osg::ref_ptr<osg::Texture2D> depthBuffer() const { return m_depthBuffer; }
33  void onPreRender(osg::RenderInfo& renderInfo);
34  void onPostRender(osg::RenderInfo& renderInfo);
35 
36  void setEnableTextureBlit(bool set, int x, int y);
37 
38 protected:
39  virtual ~OculusTextureBuffer() {}
41 
42  void blitTexture(osg::State* state, int width, int height);
43 
44  const ovrSession m_session;
45  ovrTextureSwapChain m_textureSwapChain;
46  osg::ref_ptr<osg::Texture2D> m_colorBuffer;
47  osg::ref_ptr<osg::Texture2D> m_depthBuffer;
48  osg::Vec2i m_textureSize;
49 
50  void setup(osg::State& state);
51  void setupMSAA(osg::State& state);
52 
53  GLuint m_Oculus_FBO; // MSAA FBO is copied to this FBO after render.
54  GLuint m_MSAA_FBO; // framebuffer for MSAA texture
55  GLuint m_MSAA_ColorTex; // color texture for MSAA
56  GLuint m_MSAA_DepthTex; // depth texture for MSAA
57  int m_samples; // sample width for MSAA
58 
64 };
65 
66 class OculusMirrorTexture : public osg::Referenced
67 {
68 public:
69  OculusMirrorTexture(ovrSession& session, osg::ref_ptr<osg::State> state, int width, int height);
70  void destroy();
71  //GLuint id() const { return m_texture->OGL.TexId; }
72  GLint width() const { return m_width; }
73  GLint height() const { return m_height; }
74  void blitTexture(osg::GraphicsContext* gc, int width, int height);
75 protected:
76  virtual ~OculusMirrorTexture() {}
77 
78  const ovrSession m_session;
79  ovrMirrorTexture m_texture;
83 };
84 
85 
86 class OculusPreDrawCallback : public osg::Camera::DrawCallback
87 {
88 public:
89  OculusPreDrawCallback(osg::Camera* camera, OculusTextureBuffer* textureBuffer)
90  : m_camera(camera)
91  , m_textureBuffer(textureBuffer)
92  {
93  }
94 
95  virtual void operator()(osg::RenderInfo& renderInfo) const;
96 protected:
97  osg::observer_ptr<osg::Camera> m_camera;
98  osg::observer_ptr<OculusTextureBuffer> m_textureBuffer;
99 
100 };
101 
102 class OculusPostDrawCallback : public osg::Camera::DrawCallback
103 {
104 public:
105  OculusPostDrawCallback(osg::Camera* camera, OculusTextureBuffer* textureBuffer)
106  : m_camera(camera)
107  , m_textureBuffer(textureBuffer)
108  {
109  }
110 
111  virtual void operator()(osg::RenderInfo& renderInfo) const;
112 protected:
113  osg::observer_ptr<osg::Camera> m_camera;
114  osg::observer_ptr<OculusTextureBuffer> m_textureBuffer;
115 
116 };
117 
118 
119 class OculusDevice : public osg::Referenced
120 {
121 
122 public:
123  typedef enum Eye_
124  {
125  LEFT = 0,
126  RIGHT = 1,
127  COUNT = 2
128  } Eye;
129  OculusDevice(float nearClip, float farClip, const float pixelsPerDisplayPixel = 1.0f, const float worldUnitsPerMetre = 1.0f, const int samples = 0, unsigned int mirrorTextureWidth = 960);
130  void createRenderBuffers(osg::ref_ptr<osg::State> state);
131  void init();
132 
133  bool hmdPresent() const;
134 
135  unsigned int screenResolutionWidth() const;
136  unsigned int screenResolutionHeight() const;
137 
138  osg::Matrix projectionMatrixCenter() const;
139  osg::Matrix projectionMatrixLeft() const;
140  osg::Matrix projectionMatrixRight() const;
141 
142  osg::Matrix projectionOffsetMatrixLeft() const;
143  osg::Matrix projectionOffsetMatrixRight() const;
144 
145  osg::Matrix viewMatrixLeft() const;
146  osg::Matrix viewMatrixRight() const;
147 
148  float nearClip() const { return m_nearClip; }
149  float farClip() const { return m_farClip; }
150 
151  void resetSensorOrientation() const;
152  void updatePose(unsigned int frameIndex = 0);
153 
154  osg::Vec3 position() const { return m_position; }
155  osg::Quat orientation() const { return m_orientation; }
156 
157  osg::Camera* createRTTCamera(OculusDevice::Eye eye, osg::Transform::ReferenceFrame referenceFrame, const osg::Vec4& clearColor, osg::GraphicsContext* gc = 0) const;
158 
159  bool submitFrame(unsigned int frameIndex = 0);
160 
161  void setPerfHudMode(int mode);
162 
163  osg::GraphicsContext::Traits* graphicsContextTraits() const;
164 protected:
165  virtual ~OculusDevice(); // Since we inherit from osg::Referenced we must make destructor protected
166 
167  void printHMDDebugInfo();
168 
170  // Note: this function requires you to run the previous function first.
171  void calculateEyeAdjustment();
172  // Note: this function requires you to run the previous function first.
174 
175  void setupLayers();
176 
177  void trySetProcessAsHighPriority() const;
178 
179  ovrSession m_session;
180  ovrHmdDesc m_hmdDesc;
181 
183  const float m_worldUnitsPerMetre;
184 
185  osg::ref_ptr<OculusTextureBuffer> m_textureBuffer[2];
186  osg::ref_ptr<OculusMirrorTexture> m_mirrorTexture;
187 
188  unsigned int m_mirrorTextureWidth;
189 
190  ovrEyeRenderDesc m_eyeRenderDesc[2];
192  ovrPosef m_headPose[2];
193  ovrPosef m_eyeRenderPose[2];
194  ovrLayerEyeFov m_layerEyeFov;
195  ovrVector3f m_viewOffset[2];
198  osg::Vec3f m_leftEyeAdjust;
199  osg::Vec3f m_rightEyeAdjust;
200 
201  osg::Vec3 m_position;
202  osg::Quat m_orientation;
203 
204  float m_nearClip;
205  float m_farClip;
207 private:
208  OculusDevice(const OculusDevice&); // Do not allow copy
209  OculusDevice& operator=(const OculusDevice&); // Do not allow assignment operator.
210 };
211 
212 
213 class OculusInitialDrawCallback : public osg::Camera::DrawCallback
214 {
215 public:
216  virtual void operator()(osg::RenderInfo& renderInfo) const;
217 };
218 
219 #endif /* _OSG_OCULUSDEVICE_H_ */


Copyright © 2005-2019 VT MAK. All Rights Reserved (www.mak.com)