COculusDevice.h
Go to the documentation of this file.
1 //==============================================================================
2 /*
3  Software License Agreement (BSD License)
4  Copyright (c) 2003-2016, 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 3.2.0 $Rev: 2173 $
42 */
43 //==============================================================================
44 
45 //------------------------------------------------------------------------------
46 #ifndef COculusDeviceH
47 #define COculusDeviceH
48 //------------------------------------------------------------------------------
49 #include "system/CGlobals.h"
51 #include "math/CTransform.h"
52 #include "OVR_CAPI.h"
53 #include "OVR_CAPI_GL.h"
54 #include "Extras/OVR_Math.h"
55 //------------------------------------------------------------------------------
56 
57 //------------------------------------------------------------------------------
58 namespace chai3d {
59 //------------------------------------------------------------------------------
60 
61 //---------------------------------------------------------------------------
65 //---------------------------------------------------------------------------
66 
67 //==============================================================================
75 //==============================================================================
76 
78 
79 //==============================================================================
91 //==============================================================================
92 inline cTransform cOVRConvert(OVR::Matrix4f a_matrix)
93 {
94  cTransform result;
95  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];
96  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];
97  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];
98  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];
99  return (result);
100 }
101 
103 
104 //==============================================================================
115 //==============================================================================
117 {
118  //--------------------------------------------------------------------------
119  // CONSTRUCTOR & DESTRUCTOR:
120  //--------------------------------------------------------------------------
121 
122 public:
123 
125  cOVRDevice() : m_hmdSession(nullptr),
126  m_msaaEnabled(false)
127  {
128  m_msaaEnabled = true;
129  }
130 
132  ~cOVRDevice();
133 
134 
135  //--------------------------------------------------------------------------
136  // PUBLIC METHODS:
137  //--------------------------------------------------------------------------
138 
139 public:
140 
141  bool initVR();
142  bool initVRBuffers(int windowWidth, int windowHeight);
143  bool initNonDistortMirror(int windowWidth, int windowHeight); // create non-distorted mirror if necessary (debug purposes)
144  void destroyVR();
145  const ovrSizei getResolution() const;
146  void onRenderStart();
147  const OVR::Matrix4f onEyeRender(int eyeIndex);
148  void onEyeRender(int eyeIndex, cTransform& a_projectionMatrix, cTransform& a_modelViewMatrix);
149  void onEyeRenderFinish(int eyeIndex);
150  const OVR::Matrix4f getEyeMVPMatrix(int eyeIdx) const;
151  void submitFrame();
152 
153  void blitMirror(ovrEyeType numEyes=ovrEye_Count, int offset = 0); // regular OculusVR mirror view
154  void onNonDistortMirrorStart(); // non-distorted mirror rendering start (debug purposes)
155  void blitNonDistortMirror(int offset); // non-distorted mirror rendering (debug purposes)
156 
157  void renderTrackerFrustum();
158  bool isDebugHMD() const { return (m_hmdDesc.AvailableHmdCaps & ovrHmdCap_DebugDevice) != 0; }
159  bool isDK2() const { return m_hmdDesc.Type == ovrHmd_DK2; }
160  void showPerfStats(ovrPerfHudMode statsMode);
161  void setMSAA(bool val) { m_msaaEnabled = val; }
162  bool MSAAEnabled() const { return m_msaaEnabled; }
163  void recenterPose() { ovr_RecenterTrackingOrigin(m_hmdSession); }
164  ovrSizei getEyeTextureSize(int a_index) { return (m_eyeBuffers[a_index]->m_eyeTextureSize); }
165 
166  //--------------------------------------------------------------------------
167  // PROTECTED MEMBERS:
168  //--------------------------------------------------------------------------
169 
170 protected:
171 
172  // A buffer struct used to store eye textures and framebuffers.
173  // We create one instance for the left eye, one for the right eye.
174  // Final rendering is done via blitting two separate frame buffers into one render target.
175  struct OVRBuffer
176  {
177  OVRBuffer(const ovrSession &session, int eyeIdx);
178  /*
179  {
180  m_eyeFbo = 0;
181  m_depthBuffer = 0;
182  m_eyeTexId = 0;
183  m_msaaEyeFbo = 0; // framebuffer for MSAA texture
184  m_eyeTexMSAA = 0; // color texture for MSAA
185  m_depthTexMSAA = 0; // depth texture for MSAA
186  m_swapTextureChain = nullptr;
187  }
188  */
189 
190  void onRender();
191  void onRenderFinish();
192  void setupMSAA();
193  void onRenderMSAA();
194  void onRenderMSAAFinish();
195  void destroy(const ovrSession &session);
196 
198  GLuint m_eyeFbo;
199  GLuint m_eyeTexId;
201 
202  GLuint m_msaaEyeFbo; // framebuffer for MSAA texture
203  GLuint m_eyeTexMSAA; // color texture for MSAA
204  GLuint m_depthTexMSAA; // depth texture for MSAA
205 
206  ovrTextureSwapChain m_swapTextureChain;
207  };
208 
209  // data and buffers used to render to HMD
210  ovrSession m_hmdSession;
211  ovrHmdDesc m_hmdDesc;
212  ovrEyeRenderDesc m_eyeRenderDesc[ovrEye_Count];
213  ovrPosef m_eyeRenderPose[ovrEye_Count];
214  ovrVector3f m_hmdToEyeOffset[ovrEye_Count];
215  OVRBuffer *m_eyeBuffers[ovrEye_Count];
216 
217  OVR::Matrix4f m_projectionMatrix[ovrEye_Count];
218  OVR::Matrix4f m_eyeViewOffset[ovrEye_Count];
219  OVR::Matrix4f m_eyeOrientation[ovrEye_Count];
220  OVR::Matrix4f m_eyePose[ovrEye_Count];
221 
222  // frame timing data and tracking info
224  ovrTrackingState m_trackingState;
225 
226  // mirror texture used to render HMD view to OpenGL window
227  ovrMirrorTexture m_mirrorTexture;
228  ovrMirrorTextureDesc m_mirrorDesc;
229 
230  // debug non-distorted mirror texture data
233  GLuint m_mirrorFBO;
238  long long m_frameIndex;
240 };
241 
242 //------------------------------------------------------------------------------
243 } // namespace chai3d
244 //------------------------------------------------------------------------------
245 
246 //------------------------------------------------------------------------------
247 #endif
248 //------------------------------------------------------------------------------
GLuint m_eyeFbo
Definition: COculusDevice.h:198
GLuint m_eyeTexMSAA
Definition: COculusDevice.h:203
ovrSession m_hmdSession
Definition: COculusDevice.h:210
double m_frameTiming
Definition: COculusDevice.h:223
cOVRDevice()
Constructor of cOVRDevice.
Definition: COculusDevice.h:125
const OVR::Matrix4f onEyeRender(int eyeIndex)
Definition: COculusDevice.cpp:391
void destroy(const ovrSession &session)
Definition: COculusDevice.cpp:199
ovrMirrorTexture m_mirrorTexture
Definition: COculusDevice.h:227
ovrTrackingState m_trackingState
Definition: COculusDevice.h:224
ovrEyeRenderDesc m_eyeRenderDesc[ovrEye_Count]
Definition: COculusDevice.h:212
ovrSizei getEyeTextureSize(int a_index)
Definition: COculusDevice.h:164
bool initVR()
Definition: COculusDevice.cpp:230
ovrPosef m_eyeRenderPose[ovrEye_Count]
Definition: COculusDevice.h:213
void onRenderStart()
Definition: COculusDevice.cpp:376
bool m_msaaEnabled
Definition: COculusDevice.h:237
double m_sensorSampleTime
Definition: COculusDevice.h:239
void destroyVR()
Definition: COculusDevice.cpp:339
void recenterPose()
Definition: COculusDevice.h:163
bool MSAAEnabled() const
Definition: COculusDevice.h:162
const OVR::Matrix4f getEyeMVPMatrix(int eyeIdx) const
Definition: COculusDevice.cpp:448
GLuint m_nonDistortTexture
Definition: COculusDevice.h:231
void blitMirror(ovrEyeType numEyes=ovrEye_Count, int offset=0)
Definition: COculusDevice.cpp:486
const ovrSizei getResolution() const
Definition: COculusDevice.cpp:368
OVR::Matrix4f m_eyePose[ovrEye_Count]
Definition: COculusDevice.h:220
GLuint m_depthTexMSAA
Definition: COculusDevice.h:204
Definition: COculusDevice.h:175
ovrTextureSwapChain m_swapTextureChain
Definition: COculusDevice.h:206
OVR::Matrix4f m_eyeOrientation[ovrEye_Count]
Definition: COculusDevice.h:219
bool initVRBuffers(int windowWidth, int windowHeight)
Definition: COculusDevice.cpp:254
long long m_frameIndex
Definition: COculusDevice.h:238
void submitFrame()
Definition: COculusDevice.cpp:455
void onNonDistortMirrorStart()
Definition: COculusDevice.cpp:514
GLuint m_msaaEyeFbo
Definition: COculusDevice.h:202
void blitNonDistortMirror(int offset)
Definition: COculusDevice.cpp:526
void setMSAA(bool val)
Definition: COculusDevice.h:161
GLuint m_mirrorFBO
Definition: COculusDevice.h:233
OVR::Matrix4f m_projectionMatrix[ovrEye_Count]
Definition: COculusDevice.h:217
int m_nonDistortViewPortHeight
Definition: COculusDevice.h:236
void onRender()
Definition: COculusDevice.cpp:177
void onRenderFinish()
Definition: COculusDevice.cpp:190
void onEyeRenderFinish(int eyeIndex)
Definition: COculusDevice.cpp:436
bool isDebugHMD() const
Definition: COculusDevice.h:158
OVR::Matrix4f m_eyeViewOffset[ovrEye_Count]
Definition: COculusDevice.h:218
GLuint m_depthBuffer
Definition: COculusDevice.h:200
GLuint m_nonDistortFBO
Definition: COculusDevice.h:234
bool isDK2() const
Definition: COculusDevice.h:159
void setupMSAA()
Definition: COculusDevice.cpp:108
ovrSizei m_eyeTextureSize
Definition: COculusDevice.h:197
void showPerfStats(ovrPerfHudMode statsMode)
Definition: COculusDevice.cpp:545
cTransform cOVRConvert(OVR::Matrix4f a_matrix)
This function converts an oculus OVR matrix to a cTransform.
Definition: COculusDevice.h:92
ovrMirrorTextureDesc m_mirrorDesc
Definition: COculusDevice.h:228
GLuint m_eyeTexId
Definition: COculusDevice.h:199
~cOVRDevice()
Destructor of cOVRDevice.
Definition: COculusDevice.cpp:221
void onRenderMSAAFinish()
Definition: COculusDevice.cpp:158
void onRenderMSAA()
Definition: COculusDevice.cpp:146
ovrVector3f m_hmdToEyeOffset[ovrEye_Count]
Definition: COculusDevice.h:214
bool initNonDistortMirror(int windowWidth, int windowHeight)
Definition: COculusDevice.cpp:290
This class implements support for the Oculus Rift DK2.
Definition: COculusDevice.h:116
GLuint m_nonDistortDepthBuffer
Definition: COculusDevice.h:232
ovrHmdDesc m_hmdDesc
Definition: COculusDevice.h:211
OVRBuffer(const ovrSession &session, int eyeIdx)
Definition: COculusDevice.cpp:54
int m_nonDistortViewPortWidth
Definition: COculusDevice.h:235
void renderTrackerFrustum()
Definition: COculusDevice.cpp:539
OVRBuffer * m_eyeBuffers[ovrEye_Count]
Definition: COculusDevice.h:215