Review VR functionality

To be more generic and configurable
This commit is contained in:
Ray San
2017-10-17 13:32:15 +02:00
parent 8ace02c2ff
commit 7057d08369
5 changed files with 1302 additions and 93 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -523,6 +523,20 @@ typedef struct RRESData {
// RRES type (pointer to RRESData array) // RRES type (pointer to RRESData array)
typedef struct RRESData *RRES; 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 // Enumerators Definition
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@@ -665,13 +679,10 @@ typedef enum {
HMD_DEFAULT_DEVICE = 0, HMD_DEFAULT_DEVICE = 0,
HMD_OCULUS_RIFT_DK2, HMD_OCULUS_RIFT_DK2,
HMD_OCULUS_RIFT_CV1, HMD_OCULUS_RIFT_CV1,
HMD_OCULUS_GO,
HMD_VALVE_HTC_VIVE, HMD_VALVE_HTC_VIVE,
HMD_SAMSUNG_GEAR_VR, HMD_SONY_PSVR
HMD_GOOGLE_CARDBOARD, } VrDeviceType;
HMD_SONY_PLAYSTATION_VR,
HMD_RAZER_OSVR,
HMD_FOVE_VR,
} VrDevice;
// RRESData type // RRESData type
typedef enum { typedef enum {
@@ -1083,7 +1094,8 @@ RLAPI void BeginBlendMode(int mode); // Beg
RLAPI void EndBlendMode(void); // End blending mode (reset to default: alpha blending) RLAPI void EndBlendMode(void); // End blending mode (reset to default: alpha blending)
// VR control functions // 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 void CloseVrSimulator(void); // Close VR simulator for current device
RLAPI bool IsVrSimulatorReady(void); // Detect if VR simulator is ready RLAPI bool IsVrSimulatorReady(void); // Detect if VR simulator is ready
RLAPI void UpdateVrTracking(Camera *camera); // Update VR tracking (position and orientation) and camera RLAPI void UpdateVrTracking(Camera *camera); // Update VR tracking (position and orientation) and camera

View File

@@ -223,20 +223,6 @@ typedef struct DrawCall {
} DrawCall; } DrawCall;
#if defined(SUPPORT_VR_SIMULATOR) #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 // VR Stereo rendering configuration for simulator
typedef struct VrStereoConfig { typedef struct VrStereoConfig {
RenderTexture2D stereoFbo; // VR stereo rendering framebuffer RenderTexture2D stereoFbo; // VR stereo rendering framebuffer
@@ -2829,36 +2815,15 @@ void EndBlendMode(void)
} }
#if defined(SUPPORT_VR_SIMULATOR) #if defined(SUPPORT_VR_SIMULATOR)
// Init VR simulator for selected device // Get VR device information for some standard devices
// NOTE: It modifies the global variable: VrStereoConfig vrConfig VrDeviceInfo GetVrDeviceInfo(int vrDeviceType)
void InitVrSimulator(int vrDevice)
{ {
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) VrDeviceInfo hmd = { 0 }; // Current VR device info
VrDeviceInfo hmd; // Current VR device info
if (vrDevice == HMD_OCULUS_RIFT_DK2) switch (vrDeviceType)
{ {
// Oculus Rift DK2 parameters case HMD_DEFAULT_DEVICE:
hmd.hResolution = 1280; // HMD horizontal resolution in pixels case HMD_OCULUS_RIFT_CV1:
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))
{ {
// Oculus Rift CV1 parameters // Oculus Rift CV1 parameters
// NOTE: CV1 represents a complete HMD redesign compared to previous versions, // NOTE: CV1 represents a complete HMD redesign compared to previous versions,
@@ -2874,25 +2839,64 @@ void InitVrSimulator(int vrDevice)
hmd.eyeToScreenDistance = 0.041f; // HMD distance between eye and display 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.lensSeparationDistance = 0.07f; // HMD lens separation distance in meters
hmd.interpupillaryDistance = 0.07f; // HMD IPD (distance between pupils) 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.lensDistortionValues[0] = 1.0f; // HMD lens distortion constant parameter 0
hmd.distortionK[1] = 0.22f; // HMD lens distortion constant parameter 1 hmd.lensDistortionValues[1] = 0.22f; // HMD lens distortion constant parameter 1
hmd.distortionK[2] = 0.24f; // HMD lens distortion constant parameter 2 hmd.lensDistortionValues[2] = 0.24f; // HMD lens distortion constant parameter 2
hmd.distortionK[3] = 0.0f; // HMD lens distortion constant parameter 3 hmd.lensDistortionValues[3] = 0.0f; // HMD lens distortion constant parameter 3
hmd.chromaAbCorrection[0] = 0.996f; // HMD chromatic aberration correction parameter 0 hmd.chromaAbCorrection[0] = 0.996f; // HMD chromatic aberration correction parameter 0
hmd.chromaAbCorrection[1] = -0.004f; // HMD chromatic aberration correction parameter 1 hmd.chromaAbCorrection[1] = -0.004f; // HMD chromatic aberration correction parameter 1
hmd.chromaAbCorrection[2] = 1.014f; // HMD chromatic aberration correction parameter 2 hmd.chromaAbCorrection[2] = 1.014f; // HMD chromatic aberration correction parameter 2
hmd.chromaAbCorrection[3] = 0.0f; // HMD chromatic aberration correction parameter 3 hmd.chromaAbCorrection[3] = 0.0f; // HMD chromatic aberration correction parameter 3
TraceLog(LOG_INFO, "Initializing VR Simulator (Oculus Rift CV1)"); TraceLog(LOG_INFO, "Initializing VR Simulator (Oculus Rift CV1)");
} } break;
else case HMD_OCULUS_RIFT_DK2:
{ {
TraceLog(LOG_WARNING, "VR Simulator doesn't support selected device parameters,"); // Oculus Rift DK2 parameters
TraceLog(LOG_WARNING, "using default VR Simulator 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 // 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); vrConfig.stereoFbo = rlLoadRenderTexture(screenWidth, screenHeight);
#if defined(SUPPORT_DISTORTION_SHADER) #if defined(SUPPORT_DISTORTION_SHADER)
@@ -2901,7 +2905,7 @@ void InitVrSimulator(int vrDevice)
if (vrConfig.distortionShader.id > 0) SetShaderDefaultLocations(&vrConfig.distortionShader); if (vrConfig.distortionShader.id > 0) SetShaderDefaultLocations(&vrConfig.distortionShader);
#endif #endif
SetStereoConfig(hmd); SetStereoConfig(info);
vrSimulatorReady = true; vrSimulatorReady = true;
#endif #endif
@@ -2925,6 +2929,18 @@ void CloseVrSimulator(void)
#endif #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 // Detect if VR simulator is running
bool IsVrSimulatorReady(void) 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] // NOTE: To get lens max radius, lensShift must be normalized to [-1..1]
float lensRadius = fabsf(-1.0f - 4.0f*lensShift); float lensRadius = fabsf(-1.0f - 4.0f*lensShift);
float lensRadiusSq = lensRadius*lensRadius; float lensRadiusSq = lensRadius*lensRadius;
float distortionScale = hmd.distortionK[0] + float distortionScale = hmd.lensDistortionValues[0] +
hmd.distortionK[1]*lensRadiusSq + hmd.lensDistortionValues[1]*lensRadiusSq +
hmd.distortionK[2]*lensRadiusSq*lensRadiusSq + hmd.lensDistortionValues[2]*lensRadiusSq*lensRadiusSq +
hmd.distortionK[3]*lensRadiusSq*lensRadiusSq*lensRadiusSq; hmd.lensDistortionValues[3]*lensRadiusSq*lensRadiusSq*lensRadiusSq;
TraceLog(LOG_DEBUG, "VR: Distortion Scale: %f", distortionScale); 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, "scale"), scale, 2);
SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "scaleIn"), scaleIn, 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); SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "chromaAbParam"), hmd.chromaAbCorrection, 4);
#endif #endif
@@ -4023,6 +4039,7 @@ static void SetStereoView(int eye, Matrix matProjection, Matrix matModelView)
// Apply view offset to modelview matrix // Apply view offset to modelview matrix
eyeModelView = MatrixMultiply(matModelView, vrConfig.eyesViewOffset[eye]); eyeModelView = MatrixMultiply(matModelView, vrConfig.eyesViewOffset[eye]);
// Set current eye projection matrix
eyeProjection = vrConfig.eyesProjection[eye]; eyeProjection = vrConfig.eyesProjection[eye];
SetMatrixModelview(eyeModelView); SetMatrixModelview(eyeModelView);

View File

@@ -269,6 +269,20 @@ typedef unsigned char byte;
float fovy; // Camera field-of-view apperture in Y (degrees) float fovy; // Camera field-of-view apperture in Y (degrees)
} Camera; } 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 // TraceLog message types
typedef enum { typedef enum {
LOG_INFO = 0, LOG_INFO = 0,
@@ -332,12 +346,9 @@ typedef unsigned char byte;
HMD_DEFAULT_DEVICE = 0, HMD_DEFAULT_DEVICE = 0,
HMD_OCULUS_RIFT_DK2, HMD_OCULUS_RIFT_DK2,
HMD_OCULUS_RIFT_CV1, HMD_OCULUS_RIFT_CV1,
HMD_OCULUS_GO,
HMD_VALVE_HTC_VIVE, HMD_VALVE_HTC_VIVE,
HMD_SAMSUNG_GEAR_VR, HMD_SONY_PSVR
HMD_GOOGLE_CARDBOARD,
HMD_SONY_PLAYSTATION_VR,
HMD_RAZER_OSVR,
HMD_FOVE_VR,
} VrDevice; } VrDevice;
#endif #endif
@@ -457,7 +468,8 @@ void BeginBlendMode(int mode); // Begin blending mode (
void EndBlendMode(void); // End blending mode (reset to default: alpha blending) void EndBlendMode(void); // End blending mode (reset to default: alpha blending)
// VR simulator functionality // 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 CloseVrSimulator(void); // Close VR simulator for current device
void UpdateVrTracking(Camera *camera); // Update VR tracking (position and orientation) and camera void UpdateVrTracking(Camera *camera); // Update VR tracking (position and orientation) and camera
void ToggleVrMode(void); // Enable/Disable VR experience (device or simulator) void ToggleVrMode(void); // Enable/Disable VR experience (device or simulator)