core_vr_simulator.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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-2024 Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #if defined(PLATFORM_DESKTOP)
  15. #define GLSL_VERSION 330
  16. #else // 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. .eyeToScreenDistance = 0.041f, // Distance between eye and display in meters
  38. .lensSeparationDistance = 0.07f, // Lens separation distance in meters
  39. .interpupillaryDistance = 0.07f, // IPD (distance between pupils) in meters
  40. // NOTE: CV1 uses fresnel-hybrid-asymmetric lenses with specific compute shaders
  41. // Following parameters are just an approximation to CV1 distortion stereo rendering
  42. .lensDistortionValues[0] = 1.0f, // Lens distortion constant parameter 0
  43. .lensDistortionValues[1] = 0.22f, // Lens distortion constant parameter 1
  44. .lensDistortionValues[2] = 0.24f, // Lens distortion constant parameter 2
  45. .lensDistortionValues[3] = 0.0f, // Lens distortion constant parameter 3
  46. .chromaAbCorrection[0] = 0.996f, // Chromatic aberration correction parameter 0
  47. .chromaAbCorrection[1] = -0.004f, // Chromatic aberration correction parameter 1
  48. .chromaAbCorrection[2] = 1.014f, // Chromatic aberration correction parameter 2
  49. .chromaAbCorrection[3] = 0.0f, // Chromatic aberration correction parameter 3
  50. };
  51. // Load VR stereo config for VR device parameteres (Oculus Rift CV1 parameters)
  52. VrStereoConfig config = LoadVrStereoConfig(device);
  53. // Distortion shader (uses device lens distortion and chroma)
  54. Shader distortion = LoadShader(0, TextFormat("resources/distortion%i.fs", GLSL_VERSION));
  55. // Update distortion shader with lens and distortion-scale parameters
  56. SetShaderValue(distortion, GetShaderLocation(distortion, "leftLensCenter"),
  57. config.leftLensCenter, SHADER_UNIFORM_VEC2);
  58. SetShaderValue(distortion, GetShaderLocation(distortion, "rightLensCenter"),
  59. config.rightLensCenter, SHADER_UNIFORM_VEC2);
  60. SetShaderValue(distortion, GetShaderLocation(distortion, "leftScreenCenter"),
  61. config.leftScreenCenter, SHADER_UNIFORM_VEC2);
  62. SetShaderValue(distortion, GetShaderLocation(distortion, "rightScreenCenter"),
  63. config.rightScreenCenter, SHADER_UNIFORM_VEC2);
  64. SetShaderValue(distortion, GetShaderLocation(distortion, "scale"),
  65. config.scale, SHADER_UNIFORM_VEC2);
  66. SetShaderValue(distortion, GetShaderLocation(distortion, "scaleIn"),
  67. config.scaleIn, SHADER_UNIFORM_VEC2);
  68. SetShaderValue(distortion, GetShaderLocation(distortion, "deviceWarpParam"),
  69. device.lensDistortionValues, SHADER_UNIFORM_VEC4);
  70. SetShaderValue(distortion, GetShaderLocation(distortion, "chromaAbParam"),
  71. device.chromaAbCorrection, SHADER_UNIFORM_VEC4);
  72. // Initialize framebuffer for stereo rendering
  73. // NOTE: Screen size should match HMD aspect ratio
  74. RenderTexture2D target = LoadRenderTexture(device.hResolution, device.vResolution);
  75. // The target's height is flipped (in the source Rectangle), due to OpenGL reasons
  76. Rectangle sourceRec = { 0.0f, 0.0f, (float)target.texture.width, -(float)target.texture.height };
  77. Rectangle destRec = { 0.0f, 0.0f, (float)GetScreenWidth(), (float)GetScreenHeight() };
  78. // Define the camera to look into our 3d world
  79. Camera camera = { 0 };
  80. camera.position = (Vector3){ 5.0f, 2.0f, 5.0f }; // Camera position
  81. camera.target = (Vector3){ 0.0f, 2.0f, 0.0f }; // Camera looking at point
  82. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector
  83. camera.fovy = 60.0f; // Camera field-of-view Y
  84. camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
  85. Vector3 cubePosition = { 0.0f, 0.0f, 0.0f };
  86. DisableCursor(); // Limit cursor to relative movement inside the window
  87. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  88. //--------------------------------------------------------------------------------------
  89. // Main game loop
  90. while (!WindowShouldClose()) // Detect window close button or ESC key
  91. {
  92. // Update
  93. //----------------------------------------------------------------------------------
  94. UpdateCamera(&camera, CAMERA_FIRST_PERSON);
  95. //----------------------------------------------------------------------------------
  96. // Draw
  97. //----------------------------------------------------------------------------------
  98. BeginTextureMode(target);
  99. ClearBackground(RAYWHITE);
  100. BeginVrStereoMode(config);
  101. BeginMode3D(camera);
  102. DrawCube(cubePosition, 2.0f, 2.0f, 2.0f, RED);
  103. DrawCubeWires(cubePosition, 2.0f, 2.0f, 2.0f, MAROON);
  104. DrawGrid(40, 1.0f);
  105. EndMode3D();
  106. EndVrStereoMode();
  107. EndTextureMode();
  108. BeginDrawing();
  109. ClearBackground(RAYWHITE);
  110. BeginShaderMode(distortion);
  111. DrawTexturePro(target.texture, sourceRec, destRec, (Vector2){ 0.0f, 0.0f }, 0.0f, WHITE);
  112. EndShaderMode();
  113. DrawFPS(10, 10);
  114. EndDrawing();
  115. //----------------------------------------------------------------------------------
  116. }
  117. // De-Initialization
  118. //--------------------------------------------------------------------------------------
  119. UnloadVrStereoConfig(config); // Unload stereo config
  120. UnloadRenderTexture(target); // Unload stereo render fbo
  121. UnloadShader(distortion); // Unload distortion shader
  122. CloseWindow(); // Close window and OpenGL context
  123. //--------------------------------------------------------------------------------------
  124. return 0;
  125. }