My Project
Loading...
Searching...
No Matches
COculusDevice.h
Go to the documentation of this file.
1//==============================================================================
2/*
3 Software License Agreement (BSD License)
4 Copyright (c) 2003-2024, CHAI3D
5 (www.chai3d.org)
6
7 All rights reserved.
8
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions
11 are met:
12
13 * Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
15
16 * Redistributions in binary form must reproduce the above
17 copyright notice, this list of conditions and the following
18 disclaimer in the documentation and/or other materials provided
19 with the distribution.
20
21 * Neither the name of CHAI3D nor the names of its contributors may
22 be used to endorse or promote products derived from this software
23 without specific prior written permission.
24
25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 POSSIBILITY OF SUCH DAMAGE.
37
38 \author <http://www.chai3d.org>
39 \author Krzysztof Kondrak
40 \author Francois Conti
41 \version 1.0.0
42*/
43//==============================================================================
44
45//------------------------------------------------------------------------------
46#ifndef COculusDeviceH
47#define COculusDeviceH
48//------------------------------------------------------------------------------
49#include "display/CCamera.h"
51#include "math/CTransform.h"
52#include "system/CGlobals.h"
53//------------------------------------------------------------------------------
54#include "OVR_CAPI.h"
55#include "OVR_CAPI_GL.h"
56#include "Extras/OVR_Math.h"
57//------------------------------------------------------------------------------
58
59//------------------------------------------------------------------------------
60namespace chai3d {
61//------------------------------------------------------------------------------
62
63//---------------------------------------------------------------------------
67//---------------------------------------------------------------------------
68
69//==============================================================================
77//==============================================================================
78
80
81//==============================================================================
93//==============================================================================
94inline cTransform cOculusConvertMatrix(OVR::Matrix4f a_matrix)
95{
96 cTransform result;
97 result(0, 0) = a_matrix.M[0][0]; result(0, 1) = a_matrix.M[0][1]; result(0, 2) = a_matrix.M[0][2]; result(0, 3) = a_matrix.M[0][3];
98 result(1, 0) = a_matrix.M[1][0]; result(1, 1) = a_matrix.M[1][1]; result(1, 2) = a_matrix.M[1][2]; result(1, 3) = a_matrix.M[1][3];
99 result(2, 0) = a_matrix.M[2][0]; result(2, 1) = a_matrix.M[2][1]; result(2, 2) = a_matrix.M[2][2]; result(2, 3) = a_matrix.M[2][3];
100 result(3, 0) = a_matrix.M[3][0]; result(3, 1) = a_matrix.M[3][1]; result(3, 2) = a_matrix.M[3][2]; result(3, 3) = a_matrix.M[3][3];
101 return (result);
102}
103
105
106//==============================================================================
117//==============================================================================
119{
120 //--------------------------------------------------------------------------
121 // CONSTRUCTOR & DESTRUCTOR:
122 //--------------------------------------------------------------------------
123
124public:
125
128
131
132
133 //--------------------------------------------------------------------------
134 // PUBLIC METHODS:
135 //--------------------------------------------------------------------------
136
137public:
138
140 bool initialize(cCamera* a_camera = NULL, bool a_useMirrorWindow = true, double a_mirrorDisplayScaleFactor = 1.0);
141
143 void setCamera(cCamera* a_camera) { m_camera = a_camera; }
144
146 cCamera* getCamera() { return (m_camera); }
147
149 void setUseMirror(bool a_useMirrorWindow) { m_useMirrorWindow = a_useMirrorWindow; }
150
152 bool getUseMirror() { return (m_useMirrorWindow); }
153
156
159
161 int getWindowWidth() { return (m_windowWidth); }
162
164 int getWindowHeight() { return (m_windowHeight); }
165
167 void setUseMSAA(bool a_useMSAA) { m_useMSAA = a_useMSAA; }
168
170 bool getUseMSAA() const { return (m_useMSAA); }
171
173 void render();
174
176 void recenterPose() { ovr_RecenterTrackingOrigin(m_hmdSession); }
177
178
179 //--------------------------------------------------------------------------
180 // PROTECTED METHODS:
181 //--------------------------------------------------------------------------
182
183protected:
184
185 bool initVR();
186 bool initVRBuffers(int windowWidth, int windowHeight);
187 bool initNonDistortMirror(int windowWidth, int windowHeight); // create non-distorted mirror if necessary (debug purposes)
188 void destroyVR();
189 const ovrSizei getResolution() const;
190 void onRenderStart();
191 const OVR::Matrix4f onEyeRender(int eyeIndex);
192 void onEyeRender(int eyeIndex, cTransform& a_projectionMatrix, cTransform& a_modelViewMatrix);
193 void onEyeRenderFinish(int eyeIndex);
194 const OVR::Matrix4f getEyeMVPMatrix(int eyeIdx) const;
195 void submitFrame();
196
197 void blitMirror(ovrEyeType numEyes=ovrEye_Count, int offset = 0); // regular OculusVR mirror view
198 void onNonDistortMirrorStart(); // non-distorted mirror rendering start (debug purposes)
199 void blitNonDistortMirror(int offset); // non-distorted mirror rendering (debug purposes)
200
202 bool isDebugHMD() const { return (m_hmdDesc.AvailableHmdCaps & ovrHmdCap_DebugDevice) != 0; }
203 bool isDK2() const { return m_hmdDesc.Type == ovrHmd_DK2; }
204 void showPerfStats(ovrPerfHudMode statsMode);
205
206 ovrSizei getEyeTextureSize(int a_index) { return (m_eyeBuffers[a_index]->m_eyeTextureSize); }
207
208 //--------------------------------------------------------------------------
209 // PROTECTED MEMBERS:
210 //--------------------------------------------------------------------------
211
212protected:
213
216
219
222
225
228
231
232
233 //--------------------------------------------------------------------------
234 // PROTECTED MEMBERS:
235 //--------------------------------------------------------------------------
236
237protected:
238
239 // Buffer structure used to store eye textures and framebuffers.
240 // We create one instance for the left eye, one for the right eye.
241 // Final rendering is done via blitting two separate frame buffers into one render target.
243 {
244 OVRBuffer(const ovrSession &session, int eyeIdx);
245
246 void onRender();
247 void onRenderFinish();
248 void setupMSAA();
249 void onRenderMSAA();
250 void onRenderMSAAFinish();
251 void destroy(const ovrSession &session);
252
254 GLuint m_eyeFbo;
257
258 GLuint m_msaaEyeFbo; // framebuffer for MSAA texture
259 GLuint m_eyeTexMSAA; // color texture for MSAA
260 GLuint m_depthTexMSAA; // depth texture for MSAA
261
262 ovrTextureSwapChain m_swapTextureChain;
263 };
264
265 // Data and buffers used to render to HMD
266 ovrSession m_hmdSession;
267 ovrHmdDesc m_hmdDesc;
268 ovrEyeRenderDesc m_eyeRenderDesc[ovrEye_Count];
269 ovrPosef m_eyeRenderPose[ovrEye_Count];
270 ovrPosef m_hmdToEyeOffset[ovrEye_Count];
271 OVRBuffer *m_eyeBuffers[ovrEye_Count];
272
273 OVR::Matrix4f m_projectionMatrix[ovrEye_Count];
274 OVR::Matrix4f m_eyeViewOffset[ovrEye_Count];
275 OVR::Matrix4f m_eyeOrientation[ovrEye_Count];
276 OVR::Matrix4f m_eyePose[ovrEye_Count];
277
278 // Frame timing data and tracking info
280 ovrTrackingState m_trackingState;
281
282 // Mirror texture used to render HMD view to OpenGL window
283 ovrMirrorTexture m_mirrorTexture;
284 ovrMirrorTextureDesc m_mirrorDesc;
285
286 // Debug non-distorted mirror texture data
294 long long m_frameIndex;
296};
297
298//------------------------------------------------------------------------------
299} // namespace chai3d
300//------------------------------------------------------------------------------
301
302//------------------------------------------------------------------------------
303#endif
304//------------------------------------------------------------------------------
This class implements support for the Oculus Rift DK2.
Definition COculusDevice.h:119
int m_nonDistortViewPortWidth
Definition COculusDevice.h:291
int getWindowHeight()
This method returns the mirrored window display height.
Definition COculusDevice.h:164
OVR::Matrix4f m_eyePose[ovrEye_Count]
Definition COculusDevice.h:276
bool m_useMirrorWindow
Flag which indicates if mirrored display should be rendered.
Definition COculusDevice.h:230
ovrPosef m_eyeRenderPose[ovrEye_Count]
Definition COculusDevice.h:269
void onEyeRenderFinish(int eyeIndex)
Definition COculusDevice.cpp:571
GLuint m_nonDistortFBO
Definition COculusDevice.h:290
void setCamera(cCamera *a_camera)
This method sets a camera.
Definition COculusDevice.h:143
const ovrSizei getResolution() const
Definition COculusDevice.cpp:503
ovrMirrorTextureDesc m_mirrorDesc
Definition COculusDevice.h:284
double m_frameTiming
Definition COculusDevice.h:279
ovrSizei getEyeTextureSize(int a_index)
Definition COculusDevice.h:206
int m_nonDistortViewPortHeight
Definition COculusDevice.h:292
bool m_useMSAA
Definition COculusDevice.h:293
bool initVRBuffers(int windowWidth, int windowHeight)
Definition COculusDevice.cpp:389
long long m_frameIndex
Definition COculusDevice.h:294
const OVR::Matrix4f onEyeRender(int eyeIndex)
Definition COculusDevice.cpp:526
double m_sensorSampleTime
Definition COculusDevice.h:295
GLuint m_nonDistortTexture
Definition COculusDevice.h:287
void setUseMSAA(bool a_useMSAA)
This method enables or disables the use of MSAA.
Definition COculusDevice.h:167
void renderTrackerFrustum()
Definition COculusDevice.cpp:673
int m_windowHeight
Height of mirrored display.
Definition COculusDevice.h:227
int m_resolutionHeight
Height resolution of oculus display.
Definition COculusDevice.h:221
const OVR::Matrix4f getEyeMVPMatrix(int eyeIdx) const
Definition COculusDevice.cpp:583
bool isDebugHMD() const
Definition COculusDevice.h:202
bool initialize(cCamera *a_camera=NULL, bool a_useMirrorWindow=true, double a_mirrorDisplayScaleFactor=1.0)
This method initializes the display.
Definition COculusDevice.cpp:97
void onNonDistortMirrorStart()
Definition COculusDevice.cpp:648
bool getUseMirror()
This method returns true if the mirrored window is rendered, false otherwise.
Definition COculusDevice.h:152
cOculusDevice()
Constructor of cOculusDevice.
Definition COculusDevice.cpp:60
void setUseMirror(bool a_useMirrorWindow)
This method enables or disables the rendering in a mirrored window.
Definition COculusDevice.h:149
ovrHmdDesc m_hmdDesc
Definition COculusDevice.h:267
cCamera * m_camera
CHAI3D camera.
Definition COculusDevice.h:215
ovrTrackingState m_trackingState
Definition COculusDevice.h:280
ovrMirrorTexture m_mirrorTexture
Definition COculusDevice.h:283
void blitNonDistortMirror(int offset)
Definition COculusDevice.cpp:660
cCamera * getCamera()
This method return a pointer to the camera.
Definition COculusDevice.h:146
void recenterPose()
This method recenters the position of user in the scene.
Definition COculusDevice.h:176
void render()
This method renders the camera scene to the Oculus display.
Definition COculusDevice.cpp:141
bool initVR()
Definition COculusDevice.cpp:365
bool isDK2() const
Definition COculusDevice.h:203
OVR::Matrix4f m_eyeViewOffset[ovrEye_Count]
Definition COculusDevice.h:274
int getResolutionHeight()
This method returns the resolution height of display.
Definition COculusDevice.h:158
int m_resolutionWidth
Width resolution of oculus display.
Definition COculusDevice.h:218
GLuint m_mirrorFBO
Definition COculusDevice.h:289
void blitMirror(ovrEyeType numEyes=ovrEye_Count, int offset=0)
Definition COculusDevice.cpp:620
void onRenderStart()
Definition COculusDevice.cpp:511
void submitFrame()
Definition COculusDevice.cpp:590
bool getUseMSAA() const
This method returns true if MSAA is enabled, false otherwise.
Definition COculusDevice.h:170
int getWindowWidth()
This method returns the mirrored window display width.
Definition COculusDevice.h:161
int m_windowWidth
Width of mirrored display.
Definition COculusDevice.h:224
GLuint m_nonDistortDepthBuffer
Definition COculusDevice.h:288
void showPerfStats(ovrPerfHudMode statsMode)
Definition COculusDevice.cpp:679
int getResolutionWidth()
This method returns the resolution width of the display.
Definition COculusDevice.h:155
bool initNonDistortMirror(int windowWidth, int windowHeight)
Definition COculusDevice.cpp:425
OVRBuffer * m_eyeBuffers[ovrEye_Count]
Definition COculusDevice.h:271
ovrEyeRenderDesc m_eyeRenderDesc[ovrEye_Count]
Definition COculusDevice.h:268
ovrSession m_hmdSession
Definition COculusDevice.h:266
~cOculusDevice()
Destructor of cOculusDevice.
Definition COculusDevice.cpp:77
ovrPosef m_hmdToEyeOffset[ovrEye_Count]
Definition COculusDevice.h:270
OVR::Matrix4f m_projectionMatrix[ovrEye_Count]
Definition COculusDevice.h:273
void destroyVR()
Definition COculusDevice.cpp:474
OVR::Matrix4f m_eyeOrientation[ovrEye_Count]
Definition COculusDevice.h:275
cTransform cOculusConvertMatrix(OVR::Matrix4f a_matrix)
This function converts an oculus OVR matrix to a cTransform.
Definition COculusDevice.h:94
Definition COculusDevice.h:243
GLuint m_depthBuffer
Definition COculusDevice.h:256
void destroy(const ovrSession &session)
Definition COculusDevice.cpp:342
ovrTextureSwapChain m_swapTextureChain
Definition COculusDevice.h:262
ovrSizei m_eyeTextureSize
Definition COculusDevice.h:253
void onRenderMSAAFinish()
Definition COculusDevice.cpp:301
GLuint m_eyeTexId
Definition COculusDevice.h:255
void onRenderMSAA()
Definition COculusDevice.cpp:289
GLuint m_eyeTexMSAA
Definition COculusDevice.h:259
GLuint m_depthTexMSAA
Definition COculusDevice.h:260
void onRenderFinish()
Definition COculusDevice.cpp:333
void onRender()
Definition COculusDevice.cpp:320
GLuint m_msaaEyeFbo
Definition COculusDevice.h:258
void setupMSAA()
Definition COculusDevice.cpp:251
GLuint m_eyeFbo
Definition COculusDevice.h:254