core_vr_simulator.c 7.4 KB

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