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