aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRay San <raysan5@gmail.com>2017-10-17 13:32:15 +0200
committerRay San <raysan5@gmail.com>2017-10-17 13:32:15 +0200
commit7057d083692f51a3d89734da81fa4db76705987f (patch)
tree5656f4ae7ed9aa06f21dd521f1b38228eb7f4cdb /src
parent8ace02c2ffd9dead6e67fb3faaaa8b8ad37ccd2a (diff)
downloadraylib-7057d083692f51a3d89734da81fa4db76705987f.tar.gz
raylib-7057d083692f51a3d89734da81fa4db76705987f.zip
Review VR functionality
To be more generic and configurable
Diffstat (limited to 'src')
-rw-r--r--src/raylib.h26
-rw-r--r--src/rlgl.c173
-rw-r--r--src/rlgl.h24
3 files changed, 132 insertions, 91 deletions
diff --git a/src/raylib.h b/src/raylib.h
index 288fd8d9..392e0a24 100644
--- a/src/raylib.h
+++ b/src/raylib.h
@@ -523,6 +523,20 @@ typedef struct RRESData {
// RRES type (pointer to RRESData array)
typedef struct RRESData *RRES;
+// Head-Mounted-Display device parameters
+typedef struct VrDeviceInfo {
+ int hResolution; // HMD horizontal resolution in pixels
+ int vResolution; // HMD vertical resolution in pixels
+ float hScreenSize; // HMD horizontal size in meters
+ float vScreenSize; // HMD vertical size in meters
+ float vScreenCenter; // HMD screen center in meters
+ float eyeToScreenDistance; // HMD distance between eye and display in meters
+ float lensSeparationDistance; // HMD lens separation distance in meters
+ float interpupillaryDistance; // HMD IPD (distance between pupils) in meters
+ float lensDistortionValues[4]; // HMD lens distortion constant parameters
+ float chromaAbCorrection[4]; // HMD chromatic aberration correction parameters
+} VrDeviceInfo;
+
//----------------------------------------------------------------------------------
// Enumerators Definition
//----------------------------------------------------------------------------------
@@ -665,13 +679,10 @@ typedef enum {
HMD_DEFAULT_DEVICE = 0,
HMD_OCULUS_RIFT_DK2,
HMD_OCULUS_RIFT_CV1,
+ HMD_OCULUS_GO,
HMD_VALVE_HTC_VIVE,
- HMD_SAMSUNG_GEAR_VR,
- HMD_GOOGLE_CARDBOARD,
- HMD_SONY_PLAYSTATION_VR,
- HMD_RAZER_OSVR,
- HMD_FOVE_VR,
-} VrDevice;
+ HMD_SONY_PSVR
+} VrDeviceType;
// RRESData type
typedef enum {
@@ -1083,7 +1094,8 @@ RLAPI void BeginBlendMode(int mode); // Beg
RLAPI void EndBlendMode(void); // End blending mode (reset to default: alpha blending)
// VR control functions
-RLAPI void InitVrSimulator(int vrDevice); // Init VR simulator for selected device
+VrDeviceInfo GetVrDeviceInfo(int vrDeviceType); // Get VR device information for some standard devices
+void InitVrSimulator(VrDeviceInfo info); // Init VR simulator for selected device parameters
RLAPI void CloseVrSimulator(void); // Close VR simulator for current device
RLAPI bool IsVrSimulatorReady(void); // Detect if VR simulator is ready
RLAPI void UpdateVrTracking(Camera *camera); // Update VR tracking (position and orientation) and camera
diff --git a/src/rlgl.c b/src/rlgl.c
index e1a4a5be..9e563ced 100644
--- a/src/rlgl.c
+++ b/src/rlgl.c
@@ -223,20 +223,6 @@ typedef struct DrawCall {
} DrawCall;
#if defined(SUPPORT_VR_SIMULATOR)
-// Head-Mounted-Display device parameters
-typedef struct VrDeviceInfo {
- int hResolution; // HMD horizontal resolution in pixels
- int vResolution; // HMD vertical resolution in pixels
- float hScreenSize; // HMD horizontal size in meters
- float vScreenSize; // HMD vertical size in meters
- float vScreenCenter; // HMD screen center in meters
- float eyeToScreenDistance; // HMD distance between eye and display in meters
- float lensSeparationDistance; // HMD lens separation distance in meters
- float interpupillaryDistance; // HMD IPD (distance between pupils) in meters
- float distortionK[4]; // HMD lens distortion constant parameters
- float chromaAbCorrection[4]; // HMD chromatic aberration correction parameters
-} VrDeviceInfo;
-
// VR Stereo rendering configuration for simulator
typedef struct VrStereoConfig {
RenderTexture2D stereoFbo; // VR stereo rendering framebuffer
@@ -2829,70 +2815,88 @@ void EndBlendMode(void)
}
#if defined(SUPPORT_VR_SIMULATOR)
-// Init VR simulator for selected device
-// NOTE: It modifies the global variable: VrStereoConfig vrConfig
-void InitVrSimulator(int vrDevice)
+// Get VR device information for some standard devices
+VrDeviceInfo GetVrDeviceInfo(int vrDeviceType)
{
-#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
- VrDeviceInfo hmd; // Current VR device info
+ VrDeviceInfo hmd = { 0 }; // Current VR device info
- if (vrDevice == HMD_OCULUS_RIFT_DK2)
- {
- // Oculus Rift DK2 parameters
- hmd.hResolution = 1280; // HMD horizontal resolution in pixels
- hmd.vResolution = 800; // HMD vertical resolution in pixels
- hmd.hScreenSize = 0.14976f; // HMD horizontal size in meters
- hmd.vScreenSize = 0.09356f; // HMD vertical size in meters
- hmd.vScreenCenter = 0.04678f; // HMD screen center in meters
- hmd.eyeToScreenDistance = 0.041f; // HMD distance between eye and display in meters
- hmd.lensSeparationDistance = 0.0635f; // HMD lens separation distance in meters
- hmd.interpupillaryDistance = 0.064f; // HMD IPD (distance between pupils) in meters
- hmd.distortionK[0] = 1.0f; // HMD lens distortion constant parameter 0
- hmd.distortionK[1] = 0.22f; // HMD lens distortion constant parameter 1
- hmd.distortionK[2] = 0.24f; // HMD lens distortion constant parameter 2
- hmd.distortionK[3] = 0.0f; // HMD lens distortion constant parameter 3
- hmd.chromaAbCorrection[0] = 0.996f; // HMD chromatic aberration correction parameter 0
- hmd.chromaAbCorrection[1] = -0.004f; // HMD chromatic aberration correction parameter 1
- hmd.chromaAbCorrection[2] = 1.014f; // HMD chromatic aberration correction parameter 2
- hmd.chromaAbCorrection[3] = 0.0f; // HMD chromatic aberration correction parameter 3
-
- TraceLog(LOG_INFO, "Initializing VR Simulator (Oculus Rift DK2)");
- }
- else if ((vrDevice == HMD_DEFAULT_DEVICE) || (vrDevice == HMD_OCULUS_RIFT_CV1))
+ switch (vrDeviceType)
{
- // Oculus Rift CV1 parameters
- // NOTE: CV1 represents a complete HMD redesign compared to previous versions,
- // new Fresnel-hybrid-asymmetric lenses have been added and, consequently,
- // previous parameters (DK2) and distortion shader (DK2) doesn't work any more.
- // I just defined a set of parameters for simulator that approximate to CV1 stereo rendering
- // but result is not the same obtained with Oculus PC SDK.
- hmd.hResolution = 2160; // HMD horizontal resolution in pixels
- hmd.vResolution = 1200; // HMD vertical resolution in pixels
- hmd.hScreenSize = 0.133793f; // HMD horizontal size in meters
- hmd.vScreenSize = 0.0669f; // HMD vertical size in meters
- hmd.vScreenCenter = 0.04678f; // HMD screen center in meters
- hmd.eyeToScreenDistance = 0.041f; // HMD distance between eye and display in meters
- hmd.lensSeparationDistance = 0.07f; // HMD lens separation distance in meters
- hmd.interpupillaryDistance = 0.07f; // HMD IPD (distance between pupils) in meters
- hmd.distortionK[0] = 1.0f; // HMD lens distortion constant parameter 0
- hmd.distortionK[1] = 0.22f; // HMD lens distortion constant parameter 1
- hmd.distortionK[2] = 0.24f; // HMD lens distortion constant parameter 2
- hmd.distortionK[3] = 0.0f; // HMD lens distortion constant parameter 3
- hmd.chromaAbCorrection[0] = 0.996f; // HMD chromatic aberration correction parameter 0
- hmd.chromaAbCorrection[1] = -0.004f; // HMD chromatic aberration correction parameter 1
- hmd.chromaAbCorrection[2] = 1.014f; // HMD chromatic aberration correction parameter 2
- hmd.chromaAbCorrection[3] = 0.0f; // HMD chromatic aberration correction parameter 3
-
- TraceLog(LOG_INFO, "Initializing VR Simulator (Oculus Rift CV1)");
- }
- else
- {
- TraceLog(LOG_WARNING, "VR Simulator doesn't support selected device parameters,");
- TraceLog(LOG_WARNING, "using default VR Simulator parameters");
+ case HMD_DEFAULT_DEVICE:
+ case HMD_OCULUS_RIFT_CV1:
+ {
+ // Oculus Rift CV1 parameters
+ // NOTE: CV1 represents a complete HMD redesign compared to previous versions,
+ // new Fresnel-hybrid-asymmetric lenses have been added and, consequently,
+ // previous parameters (DK2) and distortion shader (DK2) doesn't work any more.
+ // I just defined a set of parameters for simulator that approximate to CV1 stereo rendering
+ // but result is not the same obtained with Oculus PC SDK.
+ hmd.hResolution = 2160; // HMD horizontal resolution in pixels
+ hmd.vResolution = 1200; // HMD vertical resolution in pixels
+ hmd.hScreenSize = 0.133793f; // HMD horizontal size in meters
+ hmd.vScreenSize = 0.0669f; // HMD vertical size in meters
+ hmd.vScreenCenter = 0.04678f; // HMD screen center in meters
+ hmd.eyeToScreenDistance = 0.041f; // HMD distance between eye and display in meters
+ hmd.lensSeparationDistance = 0.07f; // HMD lens separation distance in meters
+ hmd.interpupillaryDistance = 0.07f; // HMD IPD (distance between pupils) in meters
+ hmd.lensDistortionValues[0] = 1.0f; // HMD lens distortion constant parameter 0
+ hmd.lensDistortionValues[1] = 0.22f; // HMD lens distortion constant parameter 1
+ hmd.lensDistortionValues[2] = 0.24f; // HMD lens distortion constant parameter 2
+ hmd.lensDistortionValues[3] = 0.0f; // HMD lens distortion constant parameter 3
+ hmd.chromaAbCorrection[0] = 0.996f; // HMD chromatic aberration correction parameter 0
+ hmd.chromaAbCorrection[1] = -0.004f; // HMD chromatic aberration correction parameter 1
+ hmd.chromaAbCorrection[2] = 1.014f; // HMD chromatic aberration correction parameter 2
+ hmd.chromaAbCorrection[3] = 0.0f; // HMD chromatic aberration correction parameter 3
+
+ TraceLog(LOG_INFO, "Initializing VR Simulator (Oculus Rift CV1)");
+ } break;
+ case HMD_OCULUS_RIFT_DK2:
+ {
+ // Oculus Rift DK2 parameters
+ hmd.hResolution = 1280; // HMD horizontal resolution in pixels
+ hmd.vResolution = 800; // HMD vertical resolution in pixels
+ hmd.hScreenSize = 0.14976f; // HMD horizontal size in meters
+ hmd.vScreenSize = 0.09356f; // HMD vertical size in meters
+ hmd.vScreenCenter = 0.04678f; // HMD screen center in meters
+ hmd.eyeToScreenDistance = 0.041f; // HMD distance between eye and display in meters
+ hmd.lensSeparationDistance = 0.0635f; // HMD lens separation distance in meters
+ hmd.interpupillaryDistance = 0.064f; // HMD IPD (distance between pupils) in meters
+ hmd.lensDistortionValues[0] = 1.0f; // HMD lens distortion constant parameter 0
+ hmd.lensDistortionValues[1] = 0.22f; // HMD lens distortion constant parameter 1
+ hmd.lensDistortionValues[2] = 0.24f; // HMD lens distortion constant parameter 2
+ hmd.lensDistortionValues[3] = 0.0f; // HMD lens distortion constant parameter 3
+ hmd.chromaAbCorrection[0] = 0.996f; // HMD chromatic aberration correction parameter 0
+ hmd.chromaAbCorrection[1] = -0.004f; // HMD chromatic aberration correction parameter 1
+ hmd.chromaAbCorrection[2] = 1.014f; // HMD chromatic aberration correction parameter 2
+ hmd.chromaAbCorrection[3] = 0.0f; // HMD chromatic aberration correction parameter 3
+
+ TraceLog(LOG_INFO, "Initializing VR Simulator (Oculus Rift DK2)");
+ } break;
+ case HMD_OCULUS_GO:
+ {
+ // TODO: Provide device display and lens parameters
+ }
+ case HMD_VALVE_HTC_VIVE:
+ {
+ // TODO: Provide device display and lens parameters
+ }
+ case HMD_SONY_PSVR:
+ {
+ // TODO: Provide device display and lens parameters
+ }
+ default: break;
}
+
+ return hmd;
+}
+// Init VR simulator for selected device parameters
+// NOTE: It modifies the global variable: VrStereoConfig vrConfig
+void InitVrSimulator(VrDeviceInfo info)
+{
+#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
// Initialize framebuffer and textures for stereo rendering
- // NOTE: screen size should match HMD aspect ratio
+ // NOTE: Screen size should match HMD aspect ratio
vrConfig.stereoFbo = rlLoadRenderTexture(screenWidth, screenHeight);
#if defined(SUPPORT_DISTORTION_SHADER)
@@ -2901,7 +2905,7 @@ void InitVrSimulator(int vrDevice)
if (vrConfig.distortionShader.id > 0) SetShaderDefaultLocations(&vrConfig.distortionShader);
#endif
- SetStereoConfig(hmd);
+ SetStereoConfig(info);
vrSimulatorReady = true;
#endif
@@ -2925,6 +2929,18 @@ void CloseVrSimulator(void)
#endif
}
+// TODO: Review VR system to be more flexible,
+// move distortion shader to user side,
+// SetStereoConfig() must be reviewed...
+/*
+// Set VR view distortion shader
+void SetVrDistortionShader(Shader shader)
+{
+ vrConfig.distortionShader = shader;
+ SetStereoConfig(info);
+}
+*/
+
// Detect if VR simulator is running
bool IsVrSimulatorReady(void)
{
@@ -3958,10 +3974,10 @@ static void SetStereoConfig(VrDeviceInfo hmd)
// NOTE: To get lens max radius, lensShift must be normalized to [-1..1]
float lensRadius = fabsf(-1.0f - 4.0f*lensShift);
float lensRadiusSq = lensRadius*lensRadius;
- float distortionScale = hmd.distortionK[0] +
- hmd.distortionK[1]*lensRadiusSq +
- hmd.distortionK[2]*lensRadiusSq*lensRadiusSq +
- hmd.distortionK[3]*lensRadiusSq*lensRadiusSq*lensRadiusSq;
+ float distortionScale = hmd.lensDistortionValues[0] +
+ hmd.lensDistortionValues[1]*lensRadiusSq +
+ hmd.lensDistortionValues[2]*lensRadiusSq*lensRadiusSq +
+ hmd.lensDistortionValues[3]*lensRadiusSq*lensRadiusSq*lensRadiusSq;
TraceLog(LOG_DEBUG, "VR: Distortion Scale: %f", distortionScale);
@@ -3984,7 +4000,7 @@ static void SetStereoConfig(VrDeviceInfo hmd)
SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "scale"), scale, 2);
SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "scaleIn"), scaleIn, 2);
- SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "hmdWarpParam"), hmd.distortionK, 4);
+ SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "hmdWarpParam"), hmd.lensDistortionValues, 4);
SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "chromaAbParam"), hmd.chromaAbCorrection, 4);
#endif
@@ -4023,6 +4039,7 @@ static void SetStereoView(int eye, Matrix matProjection, Matrix matModelView)
// Apply view offset to modelview matrix
eyeModelView = MatrixMultiply(matModelView, vrConfig.eyesViewOffset[eye]);
+ // Set current eye projection matrix
eyeProjection = vrConfig.eyesProjection[eye];
SetMatrixModelview(eyeModelView);
diff --git a/src/rlgl.h b/src/rlgl.h
index 6f1b6adc..3f06a697 100644
--- a/src/rlgl.h
+++ b/src/rlgl.h
@@ -269,6 +269,20 @@ typedef unsigned char byte;
float fovy; // Camera field-of-view apperture in Y (degrees)
} Camera;
+ // Head-Mounted-Display device parameters
+ typedef struct VrDeviceInfo {
+ int hResolution; // HMD horizontal resolution in pixels
+ int vResolution; // HMD vertical resolution in pixels
+ float hScreenSize; // HMD horizontal size in meters
+ float vScreenSize; // HMD vertical size in meters
+ float vScreenCenter; // HMD screen center in meters
+ float eyeToScreenDistance; // HMD distance between eye and display in meters
+ float lensSeparationDistance; // HMD lens separation distance in meters
+ float interpupillaryDistance; // HMD IPD (distance between pupils) in meters
+ float lensDistortionValues[4]; // HMD lens distortion constant parameters
+ float chromaAbCorrection[4]; // HMD chromatic aberration correction parameters
+ } VrDeviceInfo;
+
// TraceLog message types
typedef enum {
LOG_INFO = 0,
@@ -332,12 +346,9 @@ typedef unsigned char byte;
HMD_DEFAULT_DEVICE = 0,
HMD_OCULUS_RIFT_DK2,
HMD_OCULUS_RIFT_CV1,
+ HMD_OCULUS_GO,
HMD_VALVE_HTC_VIVE,
- HMD_SAMSUNG_GEAR_VR,
- HMD_GOOGLE_CARDBOARD,
- HMD_SONY_PLAYSTATION_VR,
- HMD_RAZER_OSVR,
- HMD_FOVE_VR,
+ HMD_SONY_PSVR
} VrDevice;
#endif
@@ -457,7 +468,8 @@ void BeginBlendMode(int mode); // Begin blending mode (
void EndBlendMode(void); // End blending mode (reset to default: alpha blending)
// VR simulator functionality
-void InitVrSimulator(int vrDevice); // Init VR simulator for selected device
+VrDeviceInfo GetVrDeviceInfo(int vrDeviceType); // Get VR device information for some standard devices
+void InitVrSimulator(VrDeviceInfo info); // Init VR simulator for selected device parameters
void CloseVrSimulator(void); // Close VR simulator for current device
void UpdateVrTracking(Camera *camera); // Update VR tracking (position and orientation) and camera
void ToggleVrMode(void); // Enable/Disable VR experience (device or simulator)