Skip to content

Commit

Permalink
Review VR simulator mechanism
Browse files Browse the repository at this point in the history
- No default VR device parameteres inside raylib
- VR device parameter should be provided by user
- VR distortion shader should be provided by user
  • Loading branch information
raysan5 committed Apr 11, 2019
1 parent aa8d10c commit 5bfa675
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 326 deletions.
38 changes: 34 additions & 4 deletions examples/core/core_vr_simulator.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,35 @@ int main()
InitWindow(screenWidth, screenHeight, "raylib [core] example - vr simulator");

// Init VR simulator (Oculus Rift CV1 parameters)
InitVrSimulator(GetVrDeviceInfo(HMD_OCULUS_RIFT_CV1));
InitVrSimulator();

VrDeviceInfo hmd = { 0 }; // VR device parameters (head-mounted-device)

// Oculus Rift CV1 parameters for simulator
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

// NOTE: CV1 uses a Fresnel-hybrid-asymmetric lenses with specific distortion compute shaders.
// Following parameters are an approximation to distortion stereo rendering but results differ from actual device.
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

// Distortion shader (uses device lens distortion and chroma)
Shader distortion = LoadShader(0, "resources/distortion.fs");

SetVrConfiguration(hmd, distortion); // Set Vr device parameters for stereo rendering

// Define the camera to look into our 3d world
Camera camera;
Expand Down Expand Up @@ -77,9 +105,11 @@ int main()

// De-Initialization
//--------------------------------------------------------------------------------------
CloseVrSimulator(); // Close VR simulator

CloseWindow(); // Close window and OpenGL context
UnloadShader(distortion); // Unload distortion shader

CloseVrSimulator(); // Close VR simulator

CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------

return 0;
Expand Down
43 changes: 43 additions & 0 deletions examples/core/resources/distortion.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#version 330

in vec2 fragTexCoord;
in vec4 fragColor;
out vec4 finalColor;

uniform sampler2D texture0;
uniform vec2 leftLensCenter = vec2(0.288, 0.5);
uniform vec2 rightLensCenter = vec2(0.712, 0.5);
uniform vec2 leftScreenCenter = vec2(0.25, 0.5);
uniform vec2 rightScreenCenter = vec2(0.75, 0.5);
uniform vec2 scale = vec2(0.25, 0.45);
uniform vec2 scaleIn = vec2(4, 2.2222);
uniform vec4 hmdWarpParam = vec4(1, 0.22, 0.24, 0);
uniform vec4 chromaAbParam = vec4(0.996, -0.004, 1.014, 0.0);

void main()
{
vec2 lensCenter = fragTexCoord.x < 0.5? leftLensCenter : rightLensCenter;
vec2 screenCenter = fragTexCoord.x < 0.5? leftScreenCenter : rightScreenCenter;
vec2 theta = (fragTexCoord - lensCenter)*scaleIn;
float rSq = theta.x*theta.x + theta.y*theta.y;
vec2 theta1 = theta*(hmdWarpParam.x + hmdWarpParam.y*rSq + hmdWarpParam.z*rSq*rSq + hmdWarpParam.w*rSq*rSq*rSq);
vec2 thetaBlue = theta1*(chromaAbParam.z + chromaAbParam.w*rSq);
vec2 tcBlue = lensCenter + scale*thetaBlue;

if (any(bvec2(clamp(tcBlue, screenCenter - vec2(0.25, 0.5), screenCenter + vec2(0.25, 0.5)) - tcBlue)))
{
finalColor = vec4(0.0, 0.0, 0.0, 1.0);
}
else
{
float blue = texture(texture0, tcBlue).b;
vec2 tcGreen = lensCenter + scale*theta1;
float green = texture(texture0, tcGreen).g;

vec2 thetaRed = theta1*(chromaAbParam.x + chromaAbParam.y*rSq);
vec2 tcRed = lensCenter + scale*thetaRed;

float red = texture(texture0, tcRed).r;
finalColor = vec4(red, green, blue, 1.0);
}
};
2 changes: 0 additions & 2 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@
//------------------------------------------------------------------------------------
// Support VR simulation functionality (stereo rendering)
#define SUPPORT_VR_SIMULATOR 1
// Include stereo rendering distortion shader (shader_distortion.h)
#define SUPPORT_DISTORTION_SHADER 1


//------------------------------------------------------------------------------------
Expand Down
30 changes: 5 additions & 25 deletions src/raylib.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,16 +443,6 @@ typedef struct VrDeviceInfo {
float chromaAbCorrection[4]; // HMD chromatic aberration correction parameters
} VrDeviceInfo;

// VR Stereo rendering configuration for simulator
typedef struct VrStereoConfig {
RenderTexture2D stereoFbo; // VR stereo rendering framebuffer
Shader distortionShader; // VR stereo rendering distortion shader
Matrix eyesProjection[2]; // VR stereo rendering eyes projection matrices
Matrix eyesViewOffset[2]; // VR stereo rendering eyes view offset matrices
int eyeViewportRight[4]; // VR stereo rendering right eye viewport [x, y, w, h]
int eyeViewportLeft[4]; // VR stereo rendering left eye viewport [x, y, w, h]
} VrStereoConfig;

//----------------------------------------------------------------------------------
// Enumerators Definition
//----------------------------------------------------------------------------------
Expand Down Expand Up @@ -860,16 +850,6 @@ typedef enum {
CAMERA_ORTHOGRAPHIC
} CameraType;

// Head Mounted Display devices
typedef enum {
HMD_DEFAULT_DEVICE = 0,
HMD_OCULUS_RIFT_DK2,
HMD_OCULUS_RIFT_CV1,
HMD_OCULUS_GO,
HMD_VALVE_HTC_VIVE,
HMD_SONY_PSVR
} VrDeviceType;

// Type of n-patch
typedef enum {
NPT_9PATCH = 0, // Npatch defined by 3x3 tiles
Expand Down Expand Up @@ -1330,7 +1310,7 @@ RLAPI Shader GetShaderDefault(void); // Get
RLAPI Texture2D GetTextureDefault(void); // Get default texture

// Shader configuration functions
RLAPI int GetShaderLocation(Shader shader, const char *uniformName); // Get shader uniform location
RLAPI int GetShaderLocation(Shader shader, const char *uniformName); // Get shader uniform location
RLAPI void SetShaderValue(Shader shader, int uniformLoc, const void *value, int uniformType); // Set shader uniform value
RLAPI void SetShaderValueV(Shader shader, int uniformLoc, const void *value, int uniformType, int count); // Set shader uniform value vector
RLAPI void SetShaderValueMatrix(Shader shader, int uniformLoc, Matrix mat); // Set shader uniform value (matrix 4x4)
Expand All @@ -1344,7 +1324,7 @@ RLAPI Matrix GetMatrixModelview(); // Get
RLAPI Texture2D GenTextureCubemap(Shader shader, Texture2D skyHDR, int size); // Generate cubemap texture from HDR texture
RLAPI Texture2D GenTextureIrradiance(Shader shader, Texture2D cubemap, int size); // Generate irradiance texture using cubemap data
RLAPI Texture2D GenTexturePrefilter(Shader shader, Texture2D cubemap, int size); // Generate prefilter texture using cubemap data
RLAPI Texture2D GenTextureBRDF(Shader shader, int size); // Generate BRDF texture using cubemap data
RLAPI Texture2D GenTextureBRDF(Shader shader, int size); // Generate BRDF texture

// Shading begin/end functions
RLAPI void BeginShaderMode(Shader shader); // Begin custom shader drawing
Expand All @@ -1355,10 +1335,10 @@ RLAPI void BeginScissorMode(int x, int y, int width, int height); // Beg
RLAPI void EndScissorMode(void); // End scissor mode

// VR control functions
RLAPI VrDeviceInfo GetVrDeviceInfo(int vrDeviceType); // Get VR device information for some standard devices
RLAPI void InitVrSimulator(VrDeviceInfo info); // Init VR simulator for selected device parameters
RLAPI void UpdateVrTracking(Camera *camera); // Update VR tracking (position and orientation) and camera
RLAPI void InitVrSimulator(void); // Init VR simulator for selected device parameters
RLAPI void CloseVrSimulator(void); // Close VR simulator for current device
RLAPI void UpdateVrTracking(Camera *camera); // Update VR tracking (position and orientation) and camera
RLAPI void SetVrConfiguration(VrDeviceInfo info, Shader distortion); // Set stereo rendering configuration parameters
RLAPI bool IsVrSimulatorReady(void); // Detect if VR simulator is ready
RLAPI void ToggleVrMode(void); // Enable/Disable VR experience
RLAPI void BeginVrDrawing(void); // Begin VR simulator stereo rendering
Expand Down
Loading

0 comments on commit 5bfa675

Please sign in to comment.