core_vr_simulator.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - VR Simulator (Oculus Rift CV1 parameters)
  4. *
  5. * This example has been created using raylib 3.6-dev (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2017-2021 Ramon Santamaria (@raysan5)
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. #if defined(PLATFORM_DESKTOP)
  13. #define GLSL_VERSION 330
  14. #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
  15. #define GLSL_VERSION 100
  16. #endif
  17. int main(void)
  18. {
  19. // Initialization
  20. //--------------------------------------------------------------------------------------
  21. const int screenWidth = 800;
  22. const int screenHeight = 450;
  23. // NOTE: screenWidth/screenHeight should match VR device aspect ratio
  24. InitWindow(screenWidth, screenHeight, "raylib [core] example - vr simulator");
  25. // VR device parameters definition
  26. VrDeviceInfo device = {
  27. // Oculus Rift CV1 parameters for simulator
  28. .hResolution = 2160, // Horizontal resolution in pixels
  29. .vResolution = 1200, // Vertical resolution in pixels
  30. .hScreenSize = 0.133793f, // Horizontal size in meters
  31. .vScreenSize = 0.0669f, // Vertical size in meters
  32. .vScreenCenter = 0.04678f, // Screen center in meters
  33. .eyeToScreenDistance = 0.041f, // Distance between eye and display in meters
  34. .lensSeparationDistance = 0.07f, // Lens separation distance in meters
  35. .interpupillaryDistance = 0.07f, // IPD (distance between pupils) in meters
  36. // NOTE: CV1 uses a Fresnel-hybrid-asymmetric lenses with specific distortion compute shaders
  37. // Following parameters are an approximation to distortion stereo rendering but results differ from actual device
  38. .lensDistortionValues[0] = 1.0f, // Lens distortion constant parameter 0
  39. .lensDistortionValues[1] = 0.22f, // Lens distortion constant parameter 1
  40. .lensDistortionValues[2] = 0.24f, // Lens distortion constant parameter 2
  41. .lensDistortionValues[3] = 0.0f, // Lens distortion constant parameter 3
  42. .chromaAbCorrection[0] = 0.996f, // Chromatic aberration correction parameter 0
  43. .chromaAbCorrection[1] = -0.004f, // Chromatic aberration correction parameter 1
  44. .chromaAbCorrection[2] = 1.014f, // Chromatic aberration correction parameter 2
  45. .chromaAbCorrection[3] = 0.0f, // Chromatic aberration correction parameter 3
  46. };
  47. // Load VR stereo config for VR device parameteres (Oculus Rift CV1 parameters)
  48. VrStereoConfig config = LoadVrStereoConfig(device);
  49. // Distortion shader (uses device lens distortion and chroma)
  50. Shader distortion = LoadShader(0, TextFormat("resources/distortion%i.fs", GLSL_VERSION));
  51. // Update distortion shader with lens and distortion-scale parameters
  52. SetShaderValue(distortion, GetShaderLocation(distortion, "leftLensCenter"), config.leftLensCenter, SHADER_UNIFORM_VEC2);
  53. SetShaderValue(distortion, GetShaderLocation(distortion, "rightLensCenter"), config.rightLensCenter, SHADER_UNIFORM_VEC2);
  54. SetShaderValue(distortion, GetShaderLocation(distortion, "leftScreenCenter"), config.leftScreenCenter, SHADER_UNIFORM_VEC2);
  55. SetShaderValue(distortion, GetShaderLocation(distortion, "rightScreenCenter"), config.rightScreenCenter, SHADER_UNIFORM_VEC2);
  56. SetShaderValue(distortion, GetShaderLocation(distortion, "scale"), config.scale, SHADER_UNIFORM_VEC2);
  57. SetShaderValue(distortion, GetShaderLocation(distortion, "scaleIn"), config.scaleIn, SHADER_UNIFORM_VEC2);
  58. SetShaderValue(distortion, GetShaderLocation(distortion, "deviceWarpParam"), device.lensDistortionValues, SHADER_UNIFORM_VEC4);
  59. SetShaderValue(distortion, GetShaderLocation(distortion, "chromaAbParam"), device.chromaAbCorrection, SHADER_UNIFORM_VEC4);
  60. // Initialize framebuffer for stereo rendering
  61. // NOTE: Screen size should match HMD aspect ratio
  62. RenderTexture2D target = LoadRenderTexture(GetScreenWidth(), GetScreenHeight());
  63. // Define the camera to look into our 3d world
  64. Camera camera = { 0 };
  65. camera.position = (Vector3){ 5.0f, 2.0f, 5.0f }; // Camera position
  66. camera.target = (Vector3){ 0.0f, 2.0f, 0.0f }; // Camera looking at point
  67. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  68. camera.fovy = 60.0f; // Camera field-of-view Y
  69. camera.projection = CAMERA_PERSPECTIVE; // Camera type
  70. Vector3 cubePosition = { 0.0f, 0.0f, 0.0f };
  71. SetCameraMode(camera, CAMERA_FIRST_PERSON); // Set first person camera mode
  72. SetTargetFPS(90); // Set our game to run at 90 frames-per-second
  73. //--------------------------------------------------------------------------------------
  74. // Main game loop
  75. while (!WindowShouldClose()) // Detect window close button or ESC key
  76. {
  77. // Update
  78. //----------------------------------------------------------------------------------
  79. UpdateCamera(&camera); // Update camera (simulator mode)
  80. //----------------------------------------------------------------------------------
  81. // Draw
  82. //----------------------------------------------------------------------------------
  83. BeginDrawing();
  84. ClearBackground(RAYWHITE);
  85. BeginTextureMode(target);
  86. ClearBackground(RAYWHITE);
  87. BeginVrStereoMode(config);
  88. BeginMode3D(camera);
  89. DrawCube(cubePosition, 2.0f, 2.0f, 2.0f, RED);
  90. DrawCubeWires(cubePosition, 2.0f, 2.0f, 2.0f, MAROON);
  91. DrawGrid(40, 1.0f);
  92. EndMode3D();
  93. EndVrStereoMode();
  94. EndTextureMode();
  95. BeginShaderMode(distortion);
  96. DrawTextureRec(target.texture, (Rectangle){ 0, 0, (float)target.texture.width, (float)-target.texture.height }, (Vector2){ 0.0f, 0.0f }, WHITE);
  97. EndShaderMode();
  98. DrawFPS(10, 10);
  99. EndDrawing();
  100. //----------------------------------------------------------------------------------
  101. }
  102. // De-Initialization
  103. //--------------------------------------------------------------------------------------
  104. UnloadVrStereoConfig(config); // Unload stereo config
  105. UnloadRenderTexture(target); // Unload stereo render fbo
  106. UnloadShader(distortion); // Unload distortion shader
  107. CloseWindow(); // Close window and OpenGL context
  108. //--------------------------------------------------------------------------------------
  109. return 0;
  110. }