VR-Forces Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties 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; }
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() {}
40  GLuint createProgram(const std::string& name);
41 
42  void blitTexture(osg::State* state, int width, int height);
43 
44  const ovrSession m_session;
45  ovrTextureSwapChain m_textureSwapChain;
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 
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 
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);
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 
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 
214 {
215 public:
216  virtual void operator()(osg::RenderInfo& renderInfo) const;
217 };
218 
219 #endif /* _OSG_OCULUSDEVICE_H_ */
osg::Matrixf m_rightEyeProjectionMatrix
Definition: oculusdevice.h:197
enum OculusDevice::Eye_ Eye
OculusDevice(float nearClip, float farClip, const float pixelsPerDisplayPixel=1.0f, const float worldUnitsPerMetre=1.0f, const int samples=0, unsigned int mirrorTextureWidth=960)
void calculateProjectionMatrices()
void setPerfHudMode(int mode)
osg::ref_ptr< osg::Texture2D > depthBuffer() const
Definition: oculusdevice.h:32
GLint m_width
Definition: oculusdevice.h:80
virtual ~OculusMirrorTexture()
Definition: oculusdevice.h:76
Definition: oculusdevice.h:119
GLuint createProgram(const std::string &name)
int myBlitHeight
Definition: oculusdevice.h:63
OculusTextureBuffer(const ovrSession &session, osg::ref_ptr< osg::State > state, const ovrSizei &size, int msaaSamples)
void createRenderBuffers(osg::ref_ptr< osg::State > state)
void resetSensorOrientation() const
bool submitFrame(unsigned int frameIndex=0)
const ovrSession m_session
Definition: oculusdevice.h:44
int myBlitWidth
Definition: oculusdevice.h:62
Definition: oculusdevice.h:22
GLuint m_MSAA_ColorTex
Definition: oculusdevice.h:55
osg::Matrix projectionMatrixLeft() const
void trySetProcessAsHighPriority() const
void onPreRender(osg::RenderInfo &renderInfo)
voidpf void uLong size
Definition: ioapi.h:39
OculusPreDrawCallback(osg::Camera *camera, OculusTextureBuffer *textureBuffer)
Definition: oculusdevice.h:89
ovrPosef m_headPose[2]
Definition: oculusdevice.h:192
unsigned int GLuint
Definition: DtGlTextureBufferObject.h:18
float nearClip() const
Definition: oculusdevice.h:148
osg::observer_ptr< OculusTextureBuffer > m_textureBuffer
Definition: oculusdevice.h:98
void setEnableTextureBlit(bool set, int x, int y)
GLint height() const
Definition: oculusdevice.h:73
GLuint m_MSAA_DepthTex
Definition: oculusdevice.h:56
osg::GraphicsContext::Traits * graphicsContextTraits() const
virtual void operator()(osg::RenderInfo &renderInfo) const
int textureWidth() const
Definition: oculusdevice.h:27
osg::Quat orientation() const
Definition: oculusdevice.h:155
double m_frameTiming
Definition: oculusdevice.h:191
unsigned int screenResolutionHeight() const
ovrEyeRenderDesc m_eyeRenderDesc[2]
Definition: oculusdevice.h:190
osg::Matrix projectionOffsetMatrixLeft() const
osg::observer_ptr< osg::Camera > m_camera
Definition: oculusdevice.h:97
OculusDevice & operator=(const OculusDevice &)
OculusPostDrawCallback(osg::Camera *camera, OculusTextureBuffer *textureBuffer)
Definition: oculusdevice.h:105
int samples() const
Definition: oculusdevice.h:29
osg::Vec3f m_rightEyeAdjust
Definition: oculusdevice.h:199
GLuint myCurrentTextureId
Definition: oculusdevice.h:59
osg::ref_ptr< OculusMirrorTexture > m_mirrorTexture
Definition: oculusdevice.h:186
osg::Matrixf m_leftEyeProjectionMatrix
Definition: oculusdevice.h:196
osg::Matrix projectionOffsetMatrixRight() const
osg::Vec2i m_textureSize
Definition: oculusdevice.h:48
const float m_worldUnitsPerMetre
Definition: oculusdevice.h:183
ovrHmdDesc m_hmdDesc
Definition: oculusdevice.h:180
GLuint m_mirrorFBO
Definition: oculusdevice.h:82
osg::observer_ptr< OculusTextureBuffer > m_textureBuffer
Definition: oculusdevice.h:114
osg::ref_ptr< osg::Texture2D > m_depthBuffer
Definition: oculusdevice.h:47
osg::Matrix projectionMatrixCenter() const
unsigned int screenResolutionWidth() const
void setupLayers()
virtual ~OculusTextureBuffer()
Definition: oculusdevice.h:39
GLint width() const
Definition: oculusdevice.h:72
GLuint myDownsampleShader
Definition: oculusdevice.h:61
int textureHeight() const
Definition: oculusdevice.h:28
float m_nearClip
Definition: oculusdevice.h:204
Definition: oculusdevice.h:102
osg::Camera * createRTTCamera(OculusDevice::Eye eye, osg::Transform::ReferenceFrame referenceFrame, const osg::Vec4 &clearColor, osg::GraphicsContext *gc=0) const
osg::Vec3f m_leftEyeAdjust
Definition: oculusdevice.h:198
void printHMDDebugInfo()
osg::observer_ptr< osg::Camera > m_camera
Definition: oculusdevice.h:113
Definition: oculusdevice.h:127
void blitTexture(osg::State *state, int width, int height)
Definition: oculusdevice.h:86
ovrTextureSwapChain m_textureSwapChain
Definition: oculusdevice.h:45
ovrMirrorTexture m_texture
Definition: oculusdevice.h:79
void calculateEyeAdjustment()
osg::Matrix projectionMatrixRight() const
void onPostRender(osg::RenderInfo &renderInfo)
osg::Quat m_orientation
Definition: oculusdevice.h:202
osg::Matrix viewMatrixRight() const
Definition: oculusdevice.h:125
virtual ~OculusDevice()
unsigned int m_mirrorTextureWidth
Definition: oculusdevice.h:188
osg::Matrix viewMatrixLeft() const
GLuint m_MSAA_FBO
Definition: oculusdevice.h:54
ovrTextureSwapChain textureSwapChain() const
Definition: oculusdevice.h:30
const char int mode
Definition: ioapi.h:38
float m_farClip
Definition: oculusdevice.h:205
int m_samples
Definition: oculusdevice.h:206
osg::Vec3 position() const
Definition: oculusdevice.h:154
osg::ref_ptr< osg::Texture2D > colorBuffer() const
Definition: oculusdevice.h:31
ovrVector3f m_viewOffset[2]
Definition: oculusdevice.h:195
bool hmdPresent() const
virtual void operator()(osg::RenderInfo &renderInfo) const
ovrSession m_session
Definition: oculusdevice.h:179
osg::ref_ptr< osg::Texture2D > m_colorBuffer
Definition: oculusdevice.h:46
Definition: oculusdevice.h:213
ovrPosef m_eyeRenderPose[2]
Definition: oculusdevice.h:193
GLuint m_Oculus_FBO
Definition: oculusdevice.h:53
const float m_pixelsPerDisplayPixel
Definition: oculusdevice.h:182
OculusMirrorTexture(ovrSession &session, osg::ref_ptr< osg::State > state, int width, int height)
float farClip() const
Definition: oculusdevice.h:149
void blitTexture(osg::GraphicsContext *gc, int width, int height)
osg::ref_ptr< OculusTextureBuffer > m_textureBuffer[2]
Definition: oculusdevice.h:185
void setup(osg::State &state)
osg::Vec3 m_position
Definition: oculusdevice.h:201
ovrLayerEyeFov m_layerEyeFov
Definition: oculusdevice.h:194
void setupMSAA(osg::State &state)
int m_samples
Definition: oculusdevice.h:57
void updatePose(unsigned int frameIndex=0)
Definition: oculusdevice.h:126
bool myShouldBlitTexture
Definition: oculusdevice.h:60
const ovrSession m_session
Definition: oculusdevice.h:78
virtual void operator()(osg::RenderInfo &renderInfo) const
Eye_
Definition: oculusdevice.h:123
GLint m_height
Definition: oculusdevice.h:81
Definition: oculusdevice.h:66
void initializeEyeRenderDesc()

Document ID: Generated on Thu Oct 23 22:29:17 EDT 2025 from SVN revision 280951
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)