core_vr_simulator.c 5.4 KB

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