core_vr_simulator.c 7.3 KB

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