VR-Vantage
Loading...
Searching...
No Matches
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
22class OculusTextureBuffer : public osg::Referenced
23{
24public:
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
38protected:
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;
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
66class OculusMirrorTexture : public osg::Referenced
67{
68public:
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);
75protected:
77
78 const ovrSession m_session;
79 ovrMirrorTexture m_texture;
83};
84
85
87{
88public:
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;
96protected:
97 osg::observer_ptr<osg::Camera> m_camera;
98 osg::observer_ptr<OculusTextureBuffer> m_textureBuffer;
99
100};
101
103{
104public:
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;
112protected:
113 osg::observer_ptr<osg::Camera> m_camera;
114 osg::observer_ptr<OculusTextureBuffer> m_textureBuffer;
115
116};
117
118
119class OculusDevice : public osg::Referenced
120{
121
122public:
123 typedef enum Eye_
124 {
125 LEFT = 0,
126 RIGHT = 1,
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
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;
164protected:
165 virtual ~OculusDevice(); // Since we inherit from osg::Referenced we must make destructor protected
166
168
170 // Note: this function requires you to run the previous function first.
172 // Note: this function requires you to run the previous function first.
174
176
178
179 ovrSession m_session;
180 ovrHmdDesc m_hmdDesc;
181
184
185 osg::ref_ptr<OculusTextureBuffer> m_textureBuffer[2];
186 osg::ref_ptr<OculusMirrorTexture> m_mirrorTexture;
187
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;
200
201 osg::Vec3 m_position;
202 osg::Quat m_orientation;
203
207private:
208 OculusDevice(const OculusDevice&); // Do not allow copy
209 OculusDevice& operator=(const OculusDevice&); // Do not allow assignment operator.
210};
211
212
214{
215public:
216 virtual void operator()(osg::RenderInfo& renderInfo) const;
217};
218
219#endif /* _OSG_OCULUSDEVICE_H_ */
unsigned int GLuint
Definition DtGlTextureBufferObject.h:18
int GLint
Definition DtTritonShaderUniformManager.h:27
osg::Vec3f m_rightEyeAdjust
Definition oculusdevice.h:199
void calculateEyeAdjustment()
float m_farClip
Definition oculusdevice.h:205
float farClip() const
Definition oculusdevice.h:149
osg::Quat orientation() const
Definition oculusdevice.h:155
const float m_worldUnitsPerMetre
Definition oculusdevice.h:183
osg::Vec3 m_position
Definition oculusdevice.h:201
void printHMDDebugInfo()
ovrEyeRenderDesc m_eyeRenderDesc[2]
Definition oculusdevice.h:190
unsigned int screenResolutionWidth() const
void resetSensorOrientation() const
virtual ~OculusDevice()
double m_frameTiming
Definition oculusdevice.h:191
osg::Matrix projectionMatrixLeft() const
ovrVector3f m_viewOffset[2]
Definition oculusdevice.h:195
osg::ref_ptr< OculusTextureBuffer > m_textureBuffer[2]
Definition oculusdevice.h:185
osg::Vec3f m_leftEyeAdjust
Definition oculusdevice.h:198
float m_nearClip
Definition oculusdevice.h:204
osg::Matrix viewMatrixRight() const
osg::Camera * createRTTCamera(OculusDevice::Eye eye, osg::Transform::ReferenceFrame referenceFrame, const osg::Vec4 &clearColor, osg::GraphicsContext *gc=0) const
void updatePose(unsigned int frameIndex=0)
ovrPosef m_headPose[2]
Definition oculusdevice.h:192
osg::Matrix viewMatrixLeft() const
enum OculusDevice::Eye_ Eye
unsigned int screenResolutionHeight() const
ovrHmdDesc m_hmdDesc
Definition oculusdevice.h:180
void initializeEyeRenderDesc()
bool submitFrame(unsigned int frameIndex=0)
Eye_
Definition oculusdevice.h:124
@ COUNT
Definition oculusdevice.h:127
@ RIGHT
Definition oculusdevice.h:126
@ LEFT
Definition oculusdevice.h:125
float nearClip() const
Definition oculusdevice.h:148
void setPerfHudMode(int mode)
osg::Vec3 position() const
Definition oculusdevice.h:154
osg::Matrixf m_leftEyeProjectionMatrix
Definition oculusdevice.h:196
int m_samples
Definition oculusdevice.h:206
const float m_pixelsPerDisplayPixel
Definition oculusdevice.h:182
osg::Matrix projectionOffsetMatrixRight() const
osg::Matrix projectionMatrixCenter() const
osg::Matrix projectionOffsetMatrixLeft() const
void calculateProjectionMatrices()
osg::Matrix projectionMatrixRight() const
osg::Matrixf m_rightEyeProjectionMatrix
Definition oculusdevice.h:197
osg::ref_ptr< OculusMirrorTexture > m_mirrorTexture
Definition oculusdevice.h:186
unsigned int m_mirrorTextureWidth
Definition oculusdevice.h:188
OculusDevice & operator=(const OculusDevice &)
void setupLayers()
void createRenderBuffers(osg::ref_ptr< osg::State > state)
ovrLayerEyeFov m_layerEyeFov
Definition oculusdevice.h:194
bool hmdPresent() const
OculusDevice(const OculusDevice &)
void trySetProcessAsHighPriority() const
osg::GraphicsContext::Traits * graphicsContextTraits() const
osg::Quat m_orientation
Definition oculusdevice.h:202
OculusDevice(float nearClip, float farClip, const float pixelsPerDisplayPixel=1.0f, const float worldUnitsPerMetre=1.0f, const int samples=0, unsigned int mirrorTextureWidth=960)
ovrSession m_session
Definition oculusdevice.h:179
ovrPosef m_eyeRenderPose[2]
Definition oculusdevice.h:193
Definition oculusdevice.h:214
virtual void operator()(osg::RenderInfo &renderInfo) const
GLuint m_mirrorFBO
Definition oculusdevice.h:82
const ovrSession m_session
Definition oculusdevice.h:78
GLint m_width
Definition oculusdevice.h:80
ovrMirrorTexture m_texture
Definition oculusdevice.h:79
void blitTexture(osg::GraphicsContext *gc, int width, int height)
GLint m_height
Definition oculusdevice.h:81
GLint height() const
Definition oculusdevice.h:73
GLint width() const
Definition oculusdevice.h:72
OculusMirrorTexture(ovrSession &session, osg::ref_ptr< osg::State > state, int width, int height)
virtual ~OculusMirrorTexture()
Definition oculusdevice.h:76
OculusPostDrawCallback(osg::Camera *camera, OculusTextureBuffer *textureBuffer)
Definition oculusdevice.h:105
virtual void operator()(osg::RenderInfo &renderInfo) const
osg::observer_ptr< osg::Camera > m_camera
Definition oculusdevice.h:113
osg::observer_ptr< OculusTextureBuffer > m_textureBuffer
Definition oculusdevice.h:114
osg::observer_ptr< osg::Camera > m_camera
Definition oculusdevice.h:97
osg::observer_ptr< OculusTextureBuffer > m_textureBuffer
Definition oculusdevice.h:98
virtual void operator()(osg::RenderInfo &renderInfo) const
OculusPreDrawCallback(osg::Camera *camera, OculusTextureBuffer *textureBuffer)
Definition oculusdevice.h:89
Definition oculusdevice.h:23
GLuint m_Oculus_FBO
Definition oculusdevice.h:53
GLuint m_MSAA_ColorTex
Definition oculusdevice.h:55
virtual ~OculusTextureBuffer()
Definition oculusdevice.h:39
GLuint myDownsampleShader
Definition oculusdevice.h:61
GLuint myCurrentTextureId
Definition oculusdevice.h:59
int textureWidth() const
Definition oculusdevice.h:27
GLuint createProgram(const std::string &name)
void setEnableTextureBlit(bool set, int x, int y)
ovrTextureSwapChain m_textureSwapChain
Definition oculusdevice.h:45
bool myShouldBlitTexture
Definition oculusdevice.h:60
int samples() const
Definition oculusdevice.h:29
osg::Vec2i m_textureSize
Definition oculusdevice.h:48
OculusTextureBuffer(const ovrSession &session, osg::ref_ptr< osg::State > state, const ovrSizei &size, int msaaSamples)
ovrTextureSwapChain textureSwapChain() const
Definition oculusdevice.h:30
int m_samples
Definition oculusdevice.h:57
void onPostRender(osg::RenderInfo &renderInfo)
void blitTexture(osg::State *state, int width, int height)
void onPreRender(osg::RenderInfo &renderInfo)
int myBlitHeight
Definition oculusdevice.h:63
osg::ref_ptr< osg::Texture2D > depthBuffer() const
Definition oculusdevice.h:32
void setup(osg::State &state)
osg::ref_ptr< osg::Texture2D > m_colorBuffer
Definition oculusdevice.h:46
int myBlitWidth
Definition oculusdevice.h:62
osg::ref_ptr< osg::Texture2D > m_depthBuffer
Definition oculusdevice.h:47
void setupMSAA(osg::State &state)
GLuint m_MSAA_FBO
Definition oculusdevice.h:54
int textureHeight() const
Definition oculusdevice.h:28
const ovrSession m_session
Definition oculusdevice.h:44
osg::ref_ptr< osg::Texture2D > colorBuffer() const
Definition oculusdevice.h:31
GLuint m_MSAA_DepthTex
Definition oculusdevice.h:56