SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLGLOculus Class Reference

Wrapper around Oculus Rift Devkit 2. More...

#include <SLGLOculus.h>

Public Member Functions

 SLGLOculus (SLstring shaderDir)
 
 ~SLGLOculus ()
 
void init ()
 
const SLQuat4forientation (SLEyeType eye)
 
const SLVec3fposition (SLEyeType eye)
 
const SLVec3fviewAdjust (SLEyeType eye)
 
const SLMat4fprojection (SLEyeType eye)
 
const SLMat4forthoProjection (SLEyeType eye)
 
SLfloat resolutionScale ()
 
void renderResolution (SLint width, SLint height)
 
void beginFrame ()
 
void renderDistortion (SLint width, SLint height, SLuint tex, const SLCol4f &background)
 
void lowPersistance (SLbool val)
 
void timeWarp (SLbool val)
 
void positionTracking (SLbool val)
 
void displaySleep (SLbool val)
 
SLbool isConnected ()
 
SLbool isCameraConnected ()
 
SLbool isPositionTracked ()
 
SLbool isPositionTrackingEnabled ()
 
SLbool isLowPersistanceEnabled ()
 
SLbool isTimeWarpEnabled ()
 

Private Member Functions

void dispose ()
 
void calculateHmdValues ()
 recalculate HMD settings changed More...
 

Private Attributes

SLVec2i _outputRes
 output resolution used for ortho projection More...
 
SLQuat4f _orientation [2]
 eye orientation More...
 
SLVec3f _position [2]
 eye position More...
 
SLMat4f _projection [2]
 projection matrices for left and right eye More...
 
SLMat4f _orthoProjection [2]
 projection for 2d elements More...
 
SLVec3f _viewAdjust [2]
 view adjust vector More...
 
SLGLVertexArray _distortionMeshVAO [2]
 distortion meshes for left and right eye More...
 
SLfloat _resolutionScale
 required resolution scale for a 1.0 min pixel density More...
 
SLbool _usingDebugHmd
 we're using a debug HMD More...
 
SLbool _positionTrackingEnabled
 is position tracking enabled More...
 
SLbool _lowPersistanceEnabled
 low persistence rendering enabled More...
 
SLbool _timeWarpEnabled
 time warp correction enabled More...
 
SLbool _displaySleep
 is the display of the rift currently off More...
 
SLbool _isConnected
 is HMD connected More...
 
SLbool _isCameraConnected
 is position tracker camera connected More...
 
SLbool _isPositionTracked
 is the position tracked (false if out of range) More...
 
SLVec2i _resolution
 Resolution of the HMD. More...
 
SLVec2i _rtSize
 Required resolution for the render target. More...
 
SLbool _hmdSettingsChanged
 settings need to be updated flag More...
 
SLGLProgram_stereoOculusDistProgram = nullptr
 
SLstring _shaderFileDir
 

Detailed Description

Wrapper around Oculus Rift Devkit 2.

This class is depricated since the lib_ovr from Oculus completely changed The lib_ovr that connects the Oculus hardware was removed since it only worked for devkit2 under windows.

Definition at line 36 of file SLGLOculus.h.

Constructor & Destructor Documentation

◆ SLGLOculus()

SLGLOculus::SLGLOculus ( SLstring  shaderDir)

Constructor initializing with default values

Definition at line 19 of file SLGLOculus.cpp.

20  : _usingDebugHmd(false),
23  _timeWarpEnabled(false),
24  _displaySleep(false),
25  _isConnected(false),
26  _isCameraConnected(false),
27  _isPositionTracked(false),
28  _shaderFileDir(shaderDir)
29 {
30 }
SLstring _shaderFileDir
Definition: SLGLOculus.h:107
SLbool _usingDebugHmd
we're using a debug HMD
Definition: SLGLOculus.h:90
SLbool _isConnected
is HMD connected
Definition: SLGLOculus.h:96
SLbool _displaySleep
is the display of the rift currently off
Definition: SLGLOculus.h:94
SLbool _timeWarpEnabled
time warp correction enabled
Definition: SLGLOculus.h:93
SLbool _lowPersistanceEnabled
low persistence rendering enabled
Definition: SLGLOculus.h:92
SLbool _isPositionTracked
is the position tracked (false if out of range)
Definition: SLGLOculus.h:98
SLbool _isCameraConnected
is position tracker camera connected
Definition: SLGLOculus.h:97
SLbool _positionTrackingEnabled
is position tracking enabled
Definition: SLGLOculus.h:91

◆ ~SLGLOculus()

SLGLOculus::~SLGLOculus ( )

Destructor calling dispose

Definition at line 34 of file SLGLOculus.cpp.

35 {
36  dispose();
37 
39  {
41  _stereoOculusDistProgram = nullptr;
42  }
43 }
void dispose()
Definition: SLGLOculus.cpp:47
SLGLProgram * _stereoOculusDistProgram
Definition: SLGLOculus.h:105

Member Function Documentation

◆ beginFrame()

void SLGLOculus::beginFrame ( )

Updates rift status and collects data for timewarp

Definition at line 195 of file SLGLOculus.cpp.

196 {
197  // update changed settings
200 }
SLbool _hmdSettingsChanged
settings need to be updated flag
Definition: SLGLOculus.h:103
void calculateHmdValues()
recalculate HMD settings changed
Definition: SLGLOculus.cpp:151

◆ calculateHmdValues()

void SLGLOculus::calculateHmdValues ( )
private

recalculate HMD settings changed

Recalculates values such as projection or render target size This function gets called whenever some settings changed.

Definition at line 151 of file SLGLOculus.cpp.

152 {
153  for (SLint i = 0; i < 2; ++i)
154  {
155  _position[i].set(0, 0, 0);
156  _orientation[i].set(0, 0, 0, 1);
157  _viewAdjust[i].set((i * 2 - 1) * 0.03f, 0, 0); //[-0.03, 0.03]m
158 
159  ovrFovPort fov;
160  fov.DownTan = 1.329f;
161  fov.UpTan = 1.329f;
162  fov.LeftTan = 1.058f;
163  fov.RightTan = 1.092f;
164  _projection[i] = CreateProjection(true, fov, 0.01f, 10000.0f);
165 
167  SLVec2f(1.0f / (549.618286f * ((SLfloat)_outputRes.x / _resolution.x)),
168  1.0f / (549.618286f * ((SLfloat)_outputRes.x / _resolution.x))),
169  0.8f,
170  _viewAdjust[i].x);
171 
172  SLMat4f flipY(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
173  _orthoProjection[i] = flipY * _orthoProjection[i];
174  }
175 
176  // done
177  _hmdSettingsChanged = false;
178 }
float SLfloat
Definition: SL.h:173
int SLint
Definition: SL.h:170
SLMat4f ovrMatrix4f_OrthoSubProjection(SLMat4f projection, SLVec2f orthoScale, float orthoDistance, float eyeViewAdjustX)
SLMat4f CreateProjection(bool rightHanded, ovrFovPort tanHalfFov, float zNear, float zFar)
SLVec2< SLfloat > SLVec2f
Definition: SLVec2.h:141
SLVec2i _resolution
Resolution of the HMD.
Definition: SLGLOculus.h:100
SLVec3f _viewAdjust[2]
view adjust vector
Definition: SLGLOculus.h:84
SLVec2i _outputRes
output resolution used for ortho projection
Definition: SLGLOculus.h:78
SLVec3f _position[2]
eye position
Definition: SLGLOculus.h:81
SLMat4f _projection[2]
projection matrices for left and right eye
Definition: SLGLOculus.h:82
SLQuat4f _orientation[2]
eye orientation
Definition: SLGLOculus.h:80
SLMat4f _orthoProjection[2]
projection for 2d elements
Definition: SLGLOculus.h:83
void set(T x, T y, T z, T w)
Definition: SLQuat4.h:140
T x
Definition: SLVec2.h:30
T x
Definition: SLVec3.h:43
void set(const T X, const T Y, const T Z)
Definition: SLVec3.h:59
float DownTan
The tangent of the angle between the viewing vector and the bottom edge of the field of view.
float UpTan
The tangent of the angle between the viewing vector and the top edge of the field of view.
float RightTan
The tangent of the angle between the viewing vector and the right edge of the field of view.
float LeftTan
The tangent of the angle between the viewing vector and the left edge of the field of view.

◆ displaySleep()

void SLGLOculus::displaySleep ( SLbool  val)

enable or disable position tracking

Definition at line 258 of file SLGLOculus.cpp.

259 {
260  if (val == _displaySleep)
261  return;
262 
263  _displaySleep = val;
264  _hmdSettingsChanged = true;
265 }

◆ dispose()

void SLGLOculus::dispose ( )
private

Deletes the buffer object

Definition at line 47 of file SLGLOculus.cpp.

48 {
49 }

◆ init()

void SLGLOculus::init ( )

Initialization of the Oculus Rift SDK and the device recognition.

Definition at line 53 of file SLGLOculus.cpp.

54 {
56  _shaderFileDir + "StereoOculusDistortionMesh.vert",
57  _shaderFileDir + "StereoOculusDistortionMesh.frag");
58  _resolutionScale = 1.25f;
59  _resolution.set(1920, 1080);
60  renderResolution(1920, 1080);
61 
62  for (SLint i = 0; i < 2; ++i)
63  {
64  _position[i].set(0, 0, 0);
65  _orientation[i].set(0, 0, 0, 1);
66  _viewAdjust[i].set((i * 2 - 1) * 0.03f, 0, 0); //[-0.03, 0.03]m
67 
68  // not 100% correct projections but it just has to look somewhat right
69  _projection[i].perspective(125.0f, 0.88f, 0.1f, 1000.0f);
71  }
72 
75 }
@ ET_right
Definition: SLEnums.h:155
@ ET_left
Definition: SLEnums.h:153
void createSLDistortionMesh(DistortionMeshVertexData **ppVertices, uint16_t **ppTriangleListIndices, SLuint *pNumVertices, SLuint *pNumTriangles, bool rightEye, const HmdRenderInfo &hmdRenderInfo, const DistortionRenderDesc &distortion, const ScaleAndOffset2D &eyeToSourceNDC)
SLGLVertexArray _distortionMeshVAO[2]
distortion meshes for left and right eye
Definition: SLGLOculus.h:85
SLfloat _resolutionScale
required resolution scale for a 1.0 min pixel density
Definition: SLGLOculus.h:87
void renderResolution(SLint width, SLint height)
Definition: SLGLOculus.cpp:182
Generic Shader Program class inherited from SLGLProgram.
void perspective(T fov, T aspect, T n, T f)
Defines a perspective projection matrix with a field of view angle.
Definition: SLMat4.h:874
void translate(T tx, T ty, T tz=0)
Definition: SLMat4.h:601
void set(const T X, const T Y)
Definition: SLVec2.h:40

◆ isCameraConnected()

SLbool SLGLOculus::isCameraConnected ( )
inline

Definition at line 66 of file SLGLOculus.h.

66 { return _isCameraConnected; }

◆ isConnected()

SLbool SLGLOculus::isConnected ( )
inline

Definition at line 65 of file SLGLOculus.h.

65 { return _isConnected; }

◆ isLowPersistanceEnabled()

SLbool SLGLOculus::isLowPersistanceEnabled ( )
inline

Definition at line 70 of file SLGLOculus.h.

70 { return _lowPersistanceEnabled; }

◆ isPositionTracked()

SLbool SLGLOculus::isPositionTracked ( )
inline

Definition at line 67 of file SLGLOculus.h.

67 { return _isPositionTracked; }

◆ isPositionTrackingEnabled()

SLbool SLGLOculus::isPositionTrackingEnabled ( )
inline

Definition at line 69 of file SLGLOculus.h.

69 { return _positionTrackingEnabled; }

◆ isTimeWarpEnabled()

SLbool SLGLOculus::isTimeWarpEnabled ( )
inline

Definition at line 71 of file SLGLOculus.h.

71 { return _timeWarpEnabled; }

◆ lowPersistance()

void SLGLOculus::lowPersistance ( SLbool  val)

enable or disable low persistance

Definition at line 225 of file SLGLOculus.cpp.

226 {
227  if (val == _lowPersistanceEnabled)
228  return;
229 
231  _hmdSettingsChanged = true;
232 }

◆ orientation()

const SLQuat4f & SLGLOculus::orientation ( SLEyeType  eye)

Returns the Oculus orientation as quaternion. If no Oculus Rift is recognized it returns a unit quaternion.

Definition at line 205 of file SLGLOculus.cpp.

206 {
207  if (eye == ET_left)
208  return _orientation[0];
209  else
210  return _orientation[1];
211 }

◆ orthoProjection()

const SLMat4f & SLGLOculus::orthoProjection ( SLEyeType  eye)

Returns an orthogonal projection matrix for the specified eye

Definition at line 140 of file SLGLOculus.cpp.

141 {
142  if (eye == ET_left)
143  return _orthoProjection[0];
144  else
145  return _orthoProjection[1];
146 }

◆ position()

const SLVec3f & SLGLOculus::position ( SLEyeType  eye)

Returns the Oculus position.

Definition at line 215 of file SLGLOculus.cpp.

216 {
217  if (eye == ET_left)
218  return _position[0];
219  else
220  return _position[1];
221 }

◆ positionTracking()

void SLGLOculus::positionTracking ( SLbool  val)

enable or disable position tracking

Definition at line 247 of file SLGLOculus.cpp.

248 {
249  if (val == _positionTrackingEnabled)
250  return;
251 
253  _hmdSettingsChanged = true;
254 }

◆ projection()

const SLMat4f & SLGLOculus::projection ( SLEyeType  eye)

Returns an perspective projection matrix for the specified eye

Definition at line 129 of file SLGLOculus.cpp.

130 {
131  if (eye == ET_left)
132  return _projection[0];
133  else
134  return _projection[1];
135 }

◆ renderDistortion()

void SLGLOculus::renderDistortion ( SLint  width,
SLint  height,
SLuint  tex,
const SLCol4f background 
)

Renders the distortion mesh with time warp and chromatic abberation

Definition at line 79 of file SLGLOculus.cpp.

83 {
84  assert(_stereoOculusDistProgram && "SLGLOculus::renderDistortion: shader program not set");
86 
87  glViewport(0, 0, width, height);
88  glBindFramebuffer(GL_FRAMEBUFFER, 0);
89  glClearColor(0, 0, 0, 1);
90  glClear(GL_COLOR_BUFFER_BIT);
91  glDisable(GL_DEPTH_TEST);
92  glDisable(GL_CULL_FACE);
93  glActiveTexture(GL_TEXTURE0);
94  glBindTexture(GL_TEXTURE_2D, tex);
95 
96  sp->beginUse(nullptr, nullptr, nullptr);
97 
98  for (auto& eye : _distortionMeshVAO)
99  {
100  sp->uniform1i("u_texture", 0);
101  sp->uniform2f("u_eyeToSourceUVScale", 0.232f, -0.376f);
102  sp->uniform2f("u_eyeToSourceUVOffset", 0.246f, 0.5f);
103  SLMat4f identity;
104 
105  sp->uniformMatrix4fv("u_eyeRotationStart", 1, identity.m());
106  sp->uniformMatrix4fv("u_eyeRotationEnd", 1, identity.m());
107  eye.drawElementsAs(PT_triangles);
108  }
109 
110  sp->endUse();
111 
112  glEnable(GL_DEPTH_TEST);
113  glEnable(GL_CULL_FACE);
114 }
@ PT_triangles
Definition: SLGLEnums.h:35
Encapsulation of an OpenGL shader program object.
Definition: SLGLProgram.h:56
SLint uniformMatrix4fv(const SLchar *name, SLsizei count, const SLfloat *value, GLboolean transpose=false) const
Passes a 4x4 float matrix values py pointer to the uniform variable "name".
void beginUse(SLCamera *cam, SLMaterial *mat, SLVLight *lights)
SLint uniform2f(const SLchar *name, SLfloat v0, SLfloat v1) const
Passes the float values v0 & v1 to the uniform variable "name".
SLint uniform1i(const SLchar *name, SLint v0) const
Passes the int values v0 to the uniform variable "name".
void endUse()
SLGLProgram::endUse stops the shader program.
void m(int i, T val)
Definition: SLMat4.h:93

◆ renderResolution()

void SLGLOculus::renderResolution ( SLint  width,
SLint  height 
)

Specify the final output resolution for this rift

Definition at line 182 of file SLGLOculus.cpp.

183 {
184  if (width == _outputRes.x && height == _outputRes.y)
185  return;
186 
187  _outputRes.x = width;
188  _outputRes.y = height;
189 
190  _hmdSettingsChanged = true;
191 }
T y
Definition: SLVec2.h:30

◆ resolutionScale()

SLfloat SLGLOculus::resolutionScale ( )
inline

Definition at line 51 of file SLGLOculus.h.

51 { return _resolutionScale; }

◆ timeWarp()

void SLGLOculus::timeWarp ( SLbool  val)

enable or disable timewarp

Definition at line 236 of file SLGLOculus.cpp.

237 {
238  if (val == _timeWarpEnabled)
239  return;
240 
241  _timeWarpEnabled = val;
242  _hmdSettingsChanged = true;
243 }

◆ viewAdjust()

const SLVec3f & SLGLOculus::viewAdjust ( SLEyeType  eye)

Returns the view adjust vector as reported by the HMD for the specified eye

Definition at line 118 of file SLGLOculus.cpp.

119 {
120  if (eye == ET_left)
121  return _viewAdjust[0];
122  else
123  return _viewAdjust[1];
124 }

Member Data Documentation

◆ _displaySleep

SLbool SLGLOculus::_displaySleep
private

is the display of the rift currently off

Definition at line 94 of file SLGLOculus.h.

◆ _distortionMeshVAO

SLGLVertexArray SLGLOculus::_distortionMeshVAO[2]
private

distortion meshes for left and right eye

Definition at line 85 of file SLGLOculus.h.

◆ _hmdSettingsChanged

SLbool SLGLOculus::_hmdSettingsChanged
private

settings need to be updated flag

Definition at line 103 of file SLGLOculus.h.

◆ _isCameraConnected

SLbool SLGLOculus::_isCameraConnected
private

is position tracker camera connected

Definition at line 97 of file SLGLOculus.h.

◆ _isConnected

SLbool SLGLOculus::_isConnected
private

is HMD connected

Definition at line 96 of file SLGLOculus.h.

◆ _isPositionTracked

SLbool SLGLOculus::_isPositionTracked
private

is the position tracked (false if out of range)

Definition at line 98 of file SLGLOculus.h.

◆ _lowPersistanceEnabled

SLbool SLGLOculus::_lowPersistanceEnabled
private

low persistence rendering enabled

Definition at line 92 of file SLGLOculus.h.

◆ _orientation

SLQuat4f SLGLOculus::_orientation[2]
private

eye orientation

Definition at line 80 of file SLGLOculus.h.

◆ _orthoProjection

SLMat4f SLGLOculus::_orthoProjection[2]
private

projection for 2d elements

Definition at line 83 of file SLGLOculus.h.

◆ _outputRes

SLVec2i SLGLOculus::_outputRes
private

output resolution used for ortho projection

Definition at line 78 of file SLGLOculus.h.

◆ _position

SLVec3f SLGLOculus::_position[2]
private

eye position

Definition at line 81 of file SLGLOculus.h.

◆ _positionTrackingEnabled

SLbool SLGLOculus::_positionTrackingEnabled
private

is position tracking enabled

Definition at line 91 of file SLGLOculus.h.

◆ _projection

SLMat4f SLGLOculus::_projection[2]
private

projection matrices for left and right eye

Definition at line 82 of file SLGLOculus.h.

◆ _resolution

SLVec2i SLGLOculus::_resolution
private

Resolution of the HMD.

Definition at line 100 of file SLGLOculus.h.

◆ _resolutionScale

SLfloat SLGLOculus::_resolutionScale
private

required resolution scale for a 1.0 min pixel density

Definition at line 87 of file SLGLOculus.h.

◆ _rtSize

SLVec2i SLGLOculus::_rtSize
private

Required resolution for the render target.

Definition at line 101 of file SLGLOculus.h.

◆ _shaderFileDir

SLstring SLGLOculus::_shaderFileDir
private

Definition at line 107 of file SLGLOculus.h.

◆ _stereoOculusDistProgram

SLGLProgram* SLGLOculus::_stereoOculusDistProgram = nullptr
private

Definition at line 105 of file SLGLOculus.h.

◆ _timeWarpEnabled

SLbool SLGLOculus::_timeWarpEnabled
private

time warp correction enabled

Definition at line 93 of file SLGLOculus.h.

◆ _usingDebugHmd

SLbool SLGLOculus::_usingDebugHmd
private

we're using a debug HMD

Definition at line 90 of file SLGLOculus.h.

◆ _viewAdjust

SLVec3f SLGLOculus::_viewAdjust[2]
private

view adjust vector

Definition at line 84 of file SLGLOculus.h.


The documentation for this class was generated from the following files: