core_vr_simulator.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - VR Simulator (Oculus Rift CV1 parameters)
  4. *
  5. * This example has been created using raylib 1.7 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2017 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. SetConfigFlags(FLAG_MSAA_4X_HINT);
  25. InitWindow(screenWidth, screenHeight, "raylib [core] example - vr simulator");
  26. // Init VR simulator (Oculus Rift CV1 parameters)
  27. InitVrSimulator();
  28. VrDeviceInfo hmd = { 0 }; // VR device parameters (head-mounted-device)
  29. // Oculus Rift CV1 parameters for simulator
  30. hmd.hResolution = 2160; // HMD horizontal resolution in pixels
  31. hmd.vResolution = 1200; // HMD vertical resolution in pixels
  32. hmd.hScreenSize = 0.133793f; // HMD horizontal size in meters
  33. hmd.vScreenSize = 0.0669f; // HMD vertical size in meters
  34. hmd.vScreenCenter = 0.04678f; // HMD screen center in meters
  35. hmd.eyeToScreenDistance = 0.041f; // HMD distance between eye and display in meters
  36. hmd.lensSeparationDistance = 0.07f; // HMD lens separation distance in meters
  37. hmd.interpupillaryDistance = 0.07f; // HMD IPD (distance between pupils) in meters
  38. // NOTE: CV1 uses a Fresnel-hybrid-asymmetric lenses with specific distortion compute shaders.
  39. // Following parameters are an approximation to distortion stereo rendering but results differ from actual device.
  40. hmd.lensDistortionValues[0] = 1.0f; // HMD lens distortion constant parameter 0
  41. hmd.lensDistortionValues[1] = 0.22f; // HMD lens distortion constant parameter 1
  42. hmd.lensDistortionValues[2] = 0.24f; // HMD lens distortion constant parameter 2
  43. hmd.lensDistortionValues[3] = 0.0f; // HMD lens distortion constant parameter 3
  44. hmd.chromaAbCorrection[0] = 0.996f; // HMD chromatic aberration correction parameter 0
  45. hmd.chromaAbCorrection[1] = -0.004f; // HMD chromatic aberration correction parameter 1
  46. hmd.chromaAbCorrection[2] = 1.014f; // HMD chromatic aberration correction parameter 2
  47. hmd.chromaAbCorrection[3] = 0.0f; // HMD chromatic aberration correction parameter 3
  48. // Distortion shader (uses device lens distortion and chroma)
  49. Shader distortion = LoadShader(0, FormatText("resources/distortion%i.fs", GLSL_VERSION));
  50. SetVrConfiguration(hmd, distortion); // Set Vr device parameters for stereo rendering
  51. // Define the camera to look into our 3d world
  52. Camera camera = { 0 };
  53. camera.position = (Vector3){ 5.0f, 2.0f, 5.0f }; // Camera position
  54. camera.target = (Vector3){ 0.0f, 2.0f, 0.0f }; // Camera looking at point
  55. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  56. camera.fovy = 60.0f; // Camera field-of-view Y
  57. camera.type = CAMERA_PERSPECTIVE; // Camera type
  58. Vector3 cubePosition = { 0.0f, 0.0f, 0.0f };
  59. SetCameraMode(camera, CAMERA_FIRST_PERSON); // Set first person camera mode
  60. SetTargetFPS(90); // Set our game to run at 90 frames-per-second
  61. //--------------------------------------------------------------------------------------
  62. // Main game loop
  63. while (!WindowShouldClose()) // Detect window close button or ESC key
  64. {
  65. // Update
  66. //----------------------------------------------------------------------------------
  67. UpdateCamera(&camera); // Update camera (simulator mode)
  68. if (IsKeyPressed(KEY_SPACE)) ToggleVrMode(); // Toggle VR mode
  69. //----------------------------------------------------------------------------------
  70. // Draw
  71. //----------------------------------------------------------------------------------
  72. BeginDrawing();
  73. ClearBackground(RAYWHITE);
  74. BeginVrDrawing();
  75. BeginMode3D(camera);
  76. DrawCube(cubePosition, 2.0f, 2.0f, 2.0f, RED);
  77. DrawCubeWires(cubePosition, 2.0f, 2.0f, 2.0f, MAROON);
  78. DrawGrid(40, 1.0f);
  79. EndMode3D();
  80. EndVrDrawing();
  81. DrawFPS(10, 10);
  82. EndDrawing();
  83. //----------------------------------------------------------------------------------
  84. }
  85. // De-Initialization
  86. //--------------------------------------------------------------------------------------
  87. UnloadShader(distortion); // Unload distortion shader
  88. CloseVrSimulator(); // Close VR simulator
  89. CloseWindow(); // Close window and OpenGL context
  90. //--------------------------------------------------------------------------------------
  91. return 0;
  92. }