core_vr_simulator.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - VR Simulator (Oculus Rift CV1 parameters)
  4. *
  5. * This example has been created using raylib 3.7 (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 fresnel-hybrid-asymmetric lenses with specific compute shaders
  37. // Following parameters are just an approximation to CV1 distortion stereo rendering
  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"),
  53. config.leftLensCenter, SHADER_UNIFORM_VEC2);
  54. SetShaderValue(distortion, GetShaderLocation(distortion, "rightLensCenter"),
  55. config.rightLensCenter, SHADER_UNIFORM_VEC2);
  56. SetShaderValue(distortion, GetShaderLocation(distortion, "leftScreenCenter"),
  57. config.leftScreenCenter, SHADER_UNIFORM_VEC2);
  58. SetShaderValue(distortion, GetShaderLocation(distortion, "rightScreenCenter"),
  59. config.rightScreenCenter, SHADER_UNIFORM_VEC2);
  60. SetShaderValue(distortion, GetShaderLocation(distortion, "scale"),
  61. config.scale, SHADER_UNIFORM_VEC2);
  62. SetShaderValue(distortion, GetShaderLocation(distortion, "scaleIn"),
  63. config.scaleIn, SHADER_UNIFORM_VEC2);
  64. SetShaderValue(distortion, GetShaderLocation(distortion, "deviceWarpParam"),
  65. device.lensDistortionValues, SHADER_UNIFORM_VEC4);
  66. SetShaderValue(distortion, GetShaderLocation(distortion, "chromaAbParam"),
  67. device.chromaAbCorrection, SHADER_UNIFORM_VEC4);
  68. // Initialize framebuffer for stereo rendering
  69. // NOTE: Screen size should match HMD aspect ratio
  70. RenderTexture2D target = LoadRenderTexture(GetScreenWidth(), GetScreenHeight());
  71. // Define the camera to look into our 3d world
  72. Camera camera = { 0 };
  73. camera.position = (Vector3){ 5.0f, 2.0f, 5.0f }; // Camera position
  74. camera.target = (Vector3){ 0.0f, 2.0f, 0.0f }; // Camera looking at point
  75. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector
  76. camera.fovy = 60.0f; // Camera field-of-view Y
  77. camera.projection = CAMERA_PERSPECTIVE; // Camera type
  78. Vector3 cubePosition = { 0.0f, 0.0f, 0.0f };
  79. SetCameraMode(camera, CAMERA_FIRST_PERSON); // Set first person camera mode
  80. SetTargetFPS(90); // Set our game to run at 90 frames-per-second
  81. //--------------------------------------------------------------------------------------
  82. // Main game loop
  83. while (!WindowShouldClose()) // Detect window close button or ESC key
  84. {
  85. // Update
  86. //----------------------------------------------------------------------------------
  87. UpdateCamera(&camera); // Update camera (simulator mode)
  88. //----------------------------------------------------------------------------------
  89. // Draw
  90. //----------------------------------------------------------------------------------
  91. BeginDrawing();
  92. ClearBackground(RAYWHITE);
  93. BeginTextureMode(target);
  94. ClearBackground(RAYWHITE);
  95. BeginVrStereoMode(config);
  96. BeginMode3D(camera);
  97. DrawCube(cubePosition, 2.0f, 2.0f, 2.0f, RED);
  98. DrawCubeWires(cubePosition, 2.0f, 2.0f, 2.0f, MAROON);
  99. DrawGrid(40, 1.0f);
  100. EndMode3D();
  101. EndVrStereoMode();
  102. EndTextureMode();
  103. BeginShaderMode(distortion);
  104. DrawTextureRec(target.texture, (Rectangle){ 0, 0, (float)target.texture.width,
  105. (float)-target.texture.height }, (Vector2){ 0.0f, 0.0f }, WHITE);
  106. EndShaderMode();
  107. DrawFPS(10, 10);
  108. EndDrawing();
  109. //----------------------------------------------------------------------------------
  110. }
  111. // De-Initialization
  112. //--------------------------------------------------------------------------------------
  113. UnloadVrStereoConfig(config); // Unload stereo config
  114. UnloadRenderTexture(target); // Unload stereo render fbo
  115. UnloadShader(distortion); // Unload distortion shader
  116. CloseWindow(); // Close window and OpenGL context
  117. //--------------------------------------------------------------------------------------
  118. return 0;
  119. }