H3D API  2.4.1
OculusRiftHandler.h
Go to the documentation of this file.
1 // Copyright 2016-2019, SenseGraphics AB (modifications)
3 // Copyright(c) 2016 Krzysztof Kondrak (original code)
4 //
5 // This file is part of H3D API.
6 //
7 // H3D API is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // H3D API is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with H3D API; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 //
21 // A commercial license is also available. Please contact us at
22 // www.sensegraphics.com for more information.
23 //
24 //
28 //
30 
31 #ifndef __H3D_OCULUSRIFTHANDLER_H__
32 #define __H3D_OCULUSRIFTHANDLER_H__
33 
34 #include <H3D/Sphere.h>
35 
36 #ifdef HAVE_LIBOVR
37 #include <OVR_CAPI_GL.h>
38 #include <Extras/OVR_Math.h>
39 
49 class H3DAPI_API OculusRiftHandler {
50 public:
55 
57  inline bool isInitialized() { return m_isInitialized; }
58 
61  bool initVR();
62 
67  bool initVRBuffers(int window_width, int window_height);
68 
71  void onRenderStart();
72 
79  void onEyeRender(int eye_index, H3D::Matrix4f &view_matrix, H3D::Matrix4f &projection_matrix );
80 
83  void onEyeRenderFinish(int eye_index);
84 
87  void submitFrame();
88 
91  void blitMirror(ovrEyeType num_eyes = ovrEye_Count, int offset = 0); // regular OculusRiftHandler mirror view
92 
94  void destroyVR();
95 
97  void recenterTracking();
98 
99  void setControllerVibration( ovrControllerType type, float frequency, float amplitude );
100 
101  void showPerfStats(ovrPerfHudMode statsMode);
102 
104  const ovrSizei getResolution() const;
105 
106  inline const ovrHmdDesc &getHMDDesc() { return m_hmdDesc; }
107  inline const ovrEyeRenderDesc &getLeftEyeRenderDesc() { return m_eyeRenderDesc[0]; }
108  inline const ovrEyeRenderDesc &getRightEyeRenderDesc() { return m_eyeRenderDesc[1]; }
109  inline const ovrTrackingState &getTrackingState() { return m_trackingState; }
110  inline double getFrameTiming() { return m_frameTiming; }
111  inline const ovrInputState &getInputState() { return m_inputState; }
112 
113 
115  H3D::Matrix4f getEyeMVPMatrix(int eye_index) const;
116 
118  inline void setMSAA(bool val) { m_msaaEnabled = val; }
119 
122  inline bool MSAAEnabled() const { return m_msaaEnabled; }
123 
124 private:
125 
126  // A buffer struct used to store eye textures and framebuffers.
127  // We create one instance for the left eye, one for the right eye.
128  // Final rendering is done via blitting two separate frame buffers into one render target.
129  struct OVRBuffer
130  {
131  OVRBuffer(const ovrSession &session, int eyeIdx);
132  void onRender();
133  void onRenderFinish();
134  void setupMSAA();
135  void onRenderMSAA();
136  void onRenderMSAAFinish();
137  void destroy(const ovrSession &session);
138 
139  ovrSizei m_eyeTextureSize;
140  GLuint m_eyeFbo = 0;
141  GLuint m_eyeTexId = 0;
142  GLuint m_depthBuffer = 0;
143 
144  GLuint m_msaaEyeFbo = 0; // framebuffer for MSAA texture
145  GLuint m_eyeTexMSAA = 0; // color texture for MSAA
146  GLuint m_depthTexMSAA = 0; // depth texture for MSAA
147 
148  ovrTextureSwapChain m_swapTextureChain = nullptr;
149  };
150 
151  // data and buffers used to render to HMD
152  ovrSession m_hmdSession;
153  ovrHmdDesc m_hmdDesc;
154  ovrEyeRenderDesc m_eyeRenderDesc[ovrEye_Count];
155  ovrPosef m_eyeRenderPose[ovrEye_Count];
156  ovrVector3f m_hmdToEyeOffset[ovrEye_Count];
157  OVRBuffer *m_eyeBuffers[ovrEye_Count];
158 
159  OVR::Matrix4f m_projectionMatrix[ovrEye_Count];
160  OVR::Matrix4f m_eyeOrientation[ovrEye_Count];
161  OVR::Matrix4f m_eyePose[ovrEye_Count];
162 
163 
164  // frame timing data and tracking info
165  double m_frameTiming;
166  ovrTrackingState m_trackingState;
167  ovrInputState m_inputState;
168 
169  // mirror texture used to render HMD view to OpenGL window
170  ovrMirrorTexture m_mirrorTexture;
171  ovrMirrorTextureDesc m_mirrorDesc;
172 
173  GLuint m_mirrorFBO;
174  bool m_msaaEnabled;
175  long long m_frameIndex;
176  double m_sensorSampleTime;
177  bool m_isInitialized;
178 
179 };
180 
181 #endif
182 #endif
Header file for Sphere, X3D scene-graph node.
The OculusRiftHandler class provides support for the Oculus Rift VR device.
Definition: OculusRiftHandler.h:49
bool MSAAEnabled() const
Get current status of multi-sample anti-aliasing.
Definition: OculusRiftHandler.h:122
bool isInitialized()
Definition: OculusRiftHandler.h:57
void setMSAA(bool val)
Toggle multi-sample anti-aliasing.
Definition: OculusRiftHandler.h:118