OVRDevice.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.1.1 $Rev: 1639 $
42 */
43 //==============================================================================
44 
45 //------------------------------------------------------------------------------
46 #ifndef OVRDeviceH
47 #define OVRDeviceH
48 //------------------------------------------------------------------------------
49 #include "system/CGlobals.h"
51 #include "math/CTransform.h"
52 #include "Extras/OVR_Math.h"
53 #include "OVR_CAPI_0_8_0.h"
54 #include "OVR_CAPI_GL.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  }
129 
131  ~cOVRDevice();
132 
133 
134  //--------------------------------------------------------------------------
135  // PUBLIC METHODS:
136  //--------------------------------------------------------------------------
137 
138 public:
139 
140  bool initVR();
141  bool initVRBuffers(int windowWidth, int windowHeight);
142  bool initNonDistortMirror(int windowWidth, int windowHeight); // create non-distorted mirror if necessary (debug purposes)
143  void destroyVR();
144  const ovrSizei getResolution() const;
145  void onRenderStart();
146  const OVR::Matrix4f onEyeRender(int eyeIndex);
147  void onEyeRender(int eyeIndex, cTransform& a_projectionMatrix, cTransform& a_modelViewMatrix);
148  void onEyeRenderFinish(int eyeIndex);
149  const OVR::Matrix4f getEyeMVPMatrix(int eyeIdx) const;
150  void submitFrame();
151 
152  void blitMirror(ovrEyeType numEyes=ovrEye_Count, int offset = 0); // regular OculusVR mirror view
153  void onNonDistortMirrorStart(); // non-distorted mirror rendering start (debug purposes)
154  void blitNonDistortMirror(int offset); // non-distorted mirror rendering (debug purposes)
155 
156  void createDebug();
157  void updateDebug();
158  void renderDebug();
159  void renderTrackerFrustum();
160  bool isDebugHMD() const { return (m_hmdDesc.AvailableHmdCaps & ovrHmdCap_DebugDevice) != 0; }
161  bool isDK2() const { return m_hmdDesc.Type == ovrHmd_DK2; }
162  void showPerfStats(ovrPerfHudMode statsMode);
163  void setMSAA(bool val) { m_msaaEnabled = val; }
164  bool MSAAEnabled() const { return m_msaaEnabled; }
165  void recenterPose() { ovr_RecenterPose(m_hmdSession); }
166  ovrSizei getEyeTextureSize(int a_index) { return (m_eyeBuffers[a_index]->m_eyeTextureSize); }
167 
168  //--------------------------------------------------------------------------
169  // PROTECTED MEMBERS:
170  //--------------------------------------------------------------------------
171 
172 protected:
173 
174  // A buffer struct used to store eye textures and framebuffers.
175  // We create one instance for the left eye, one for the right eye.
176  // Final rendering is done via blitting two separate frame buffers into one render target.
177  struct OVRBuffer
178  {
179  OVRBuffer(const ovrSession &session, int eyeIdx);
180  /*
181  {
182  m_eyeFbo = 0;
183  m_depthBuffer = 0;
184  m_msaaEyeFbo = 0; // framebuffer for MSAA texture
185  m_eyeTexMSAA = 0; // color texture for MSAA
186  m_depthTexMSAA = 0; // depth texture for MSAA
187  m_swapTextureSet = nullptr;
188  }
189 */
190  void onRender();
191  void onRenderFinish();
192  void setupMSAA();
193  void onRenderMSAA();
194  void onRenderMSAAFinish();
195  void destroy(const ovrSession &session);
196 
197  ovrTexture m_eyeTexture;
199  GLuint m_eyeFbo;
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  ovrSwapTextureSet *m_swapTextureSet;
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_hmdToEyeViewOffset[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  ovrGLTexture *m_mirrorTexture;
228 
229  // debug non-distorted mirror texture data
232  GLuint m_mirrorFBO;
237 };
238 
239 //------------------------------------------------------------------------------
240 } // namespace chai3d
241 //------------------------------------------------------------------------------
242 
243 //------------------------------------------------------------------------------
244 #endif
245 //------------------------------------------------------------------------------
GLuint m_eyeFbo
Definition: OVRDevice.h:199
GLuint m_eyeTexMSAA
Definition: OVRDevice.h:203
ovrSwapTextureSet * m_swapTextureSet
Definition: OVRDevice.h:206
ovrSession m_hmdSession
Definition: OVRDevice.h:210
double m_frameTiming
Definition: OVRDevice.h:223
ovrGLTexture * m_mirrorTexture
Definition: OVRDevice.h:227
cOVRDevice()
Constructor of cOVRDevice.
Definition: OVRDevice.h:125
const OVR::Matrix4f onEyeRender(int eyeIndex)
Definition: OVRDevice.cpp:374
void destroy(const ovrSession &session)
Definition: OVRDevice.cpp:191
ovrTrackingState m_trackingState
Definition: OVRDevice.h:224
ovrEyeRenderDesc m_eyeRenderDesc[ovrEye_Count]
Definition: OVRDevice.h:212
ovrSizei getEyeTextureSize(int a_index)
Definition: OVRDevice.h:166
bool initVR()
Definition: OVRDevice.cpp:222
ovrPosef m_eyeRenderPose[ovrEye_Count]
Definition: OVRDevice.h:213
void onRenderStart()
Definition: OVRDevice.cpp:360
bool m_msaaEnabled
Definition: OVRDevice.h:236
void destroyVR()
Definition: OVRDevice.cpp:323
void recenterPose()
Definition: OVRDevice.h:165
bool MSAAEnabled() const
Definition: OVRDevice.h:164
const OVR::Matrix4f getEyeMVPMatrix(int eyeIdx) const
Definition: OVRDevice.cpp:419
GLuint m_nonDistortTexture
Definition: OVRDevice.h:230
void blitMirror(ovrEyeType numEyes=ovrEye_Count, int offset=0)
Definition: OVRDevice.cpp:456
const ovrSizei getResolution() const
Definition: OVRDevice.cpp:352
OVR::Matrix4f m_eyePose[ovrEye_Count]
Definition: OVRDevice.h:220
GLuint m_depthTexMSAA
Definition: OVRDevice.h:204
Definition: OVRDevice.h:177
OVR::Matrix4f m_eyeOrientation[ovrEye_Count]
Definition: OVRDevice.h:219
bool initVRBuffers(int windowWidth, int windowHeight)
Definition: OVRDevice.cpp:245
void submitFrame()
Definition: OVRDevice.cpp:426
void onNonDistortMirrorStart()
Definition: OVRDevice.cpp:484
ovrTexture m_eyeTexture
Definition: OVRDevice.h:197
GLuint m_msaaEyeFbo
Definition: OVRDevice.h:202
void blitNonDistortMirror(int offset)
Definition: OVRDevice.cpp:496
void setMSAA(bool val)
Definition: OVRDevice.h:163
GLuint m_mirrorFBO
Definition: OVRDevice.h:232
OVR::Matrix4f m_projectionMatrix[ovrEye_Count]
Definition: OVRDevice.h:217
int m_nonDistortViewPortHeight
Definition: OVRDevice.h:235
void onRender()
Definition: OVRDevice.cpp:164
void onRenderFinish()
Definition: OVRDevice.cpp:182
void onEyeRenderFinish(int eyeIndex)
Definition: OVRDevice.cpp:409
bool isDebugHMD() const
Definition: OVRDevice.h:160
OVR::Matrix4f m_eyeViewOffset[ovrEye_Count]
Definition: OVRDevice.h:218
GLuint m_depthBuffer
Definition: OVRDevice.h:200
GLuint m_nonDistortFBO
Definition: OVRDevice.h:233
bool isDK2() const
Definition: OVRDevice.h:161
void setupMSAA()
Definition: OVRDevice.cpp:90
ovrSizei m_eyeTextureSize
Definition: OVRDevice.h:198
void showPerfStats(ovrPerfHudMode statsMode)
Definition: OVRDevice.cpp:515
ovrVector3f m_hmdToEyeViewOffset[ovrEye_Count]
Definition: OVRDevice.h:214
~cOVRDevice()
Destructor of cOVRDevice.
Definition: OVRDevice.cpp:213
void onRenderMSAAFinish()
Definition: OVRDevice.cpp:143
void onRenderMSAA()
Definition: OVRDevice.cpp:128
bool initNonDistortMirror(int windowWidth, int windowHeight)
Definition: OVRDevice.cpp:274
This class implements support for the Oculus Rift DK2.
Definition: OVRDevice.h:116
GLuint m_nonDistortDepthBuffer
Definition: OVRDevice.h:231
ovrHmdDesc m_hmdDesc
Definition: OVRDevice.h:211
OVRBuffer(const ovrSession &session, int eyeIdx)
Definition: OVRDevice.cpp:53
int m_nonDistortViewPortWidth
Definition: OVRDevice.h:234
void renderTrackerFrustum()
Definition: OVRDevice.cpp:509
OVRBuffer * m_eyeBuffers[ovrEye_Count]
Definition: OVRDevice.h:215