Enable/Disable VR experience

This commit is contained in:
raysan5 2016-06-26 15:36:12 +02:00
parent 8652e644dd
commit 9127b5a57d
3 changed files with 64 additions and 48 deletions

View file

@ -30,7 +30,7 @@ int main()
camera.position = (Vector3){ 5.0f, 5.0f, 5.0f }; // Camera position camera.position = (Vector3){ 5.0f, 5.0f, 5.0f }; // Camera position
camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target) camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
camera.fovy = 45.0f; // Camera field-of-view Y camera.fovy = 60.0f; // Camera field-of-view Y
Vector3 cubePosition = { 0.0f, 0.0f, 0.0f }; Vector3 cubePosition = { 0.0f, 0.0f, 0.0f };
@ -43,6 +43,8 @@ int main()
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
UpdateOculusTracking(); UpdateOculusTracking();
if (IsKeyPressed(KEY_SPACE)) ToggleVR();
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Draw // Draw

View file

@ -267,8 +267,9 @@ static OculusMirror mirror; // Oculus mirror texture and fbo
static unsigned int frameIndex = 0; // Oculus frames counter, used to discard frames from chain static unsigned int frameIndex = 0; // Oculus frames counter, used to discard frames from chain
#endif #endif
static bool oculusEnabled = false; // Oculus device enabled flag (required by core module) static bool oculusReady = false; // Oculus device ready flag
static bool oculusSimulator = false; // Oculus device simulator static bool oculusSimulator = false; // Oculus device simulator
static bool vrEnabled = false; // VR experience enabled (Oculus device or simulator)
static RenderTexture2D stereoFbo; static RenderTexture2D stereoFbo;
static Shader distortion; static Shader distortion;
@ -2480,7 +2481,7 @@ void InitOculusDevice(void)
if (OVR_FAILURE(result)) if (OVR_FAILURE(result))
{ {
TraceLog(WARNING, "OVR: Could not initialize Oculus device"); TraceLog(WARNING, "OVR: Could not initialize Oculus device");
oculusEnabled = false; oculusReady = false;
} }
else else
{ {
@ -2489,7 +2490,7 @@ void InitOculusDevice(void)
{ {
TraceLog(WARNING, "OVR: Could not create Oculus session"); TraceLog(WARNING, "OVR: Could not create Oculus session");
ovr_Shutdown(); ovr_Shutdown();
oculusEnabled = false; oculusReady = false;
} }
else else
{ {
@ -2514,14 +2515,15 @@ void InitOculusDevice(void)
// Recenter OVR tracking origin // Recenter OVR tracking origin
ovr_RecenterTrackingOrigin(session); ovr_RecenterTrackingOrigin(session);
oculusEnabled = true; oculusReady = true;
vrEnabled = true;
} }
} }
#else #else
oculusEnabled = false; oculusReady = false;
#endif #endif
if (!oculusEnabled) if (!oculusReady)
{ {
TraceLog(WARNING, "VR: Initializing Oculus simulator"); TraceLog(WARNING, "VR: Initializing Oculus simulator");
@ -2533,6 +2535,7 @@ void InitOculusDevice(void)
distortion = LoadShader("resources/shaders/base.vs", "resources/shaders/distortion.fs"); distortion = LoadShader("resources/shaders/base.vs", "resources/shaders/distortion.fs");
oculusSimulator = true; oculusSimulator = true;
vrEnabled = true;
} }
} }
@ -2540,7 +2543,7 @@ void InitOculusDevice(void)
void CloseOculusDevice(void) void CloseOculusDevice(void)
{ {
#if defined(RLGL_OCULUS_SUPPORT) #if defined(RLGL_OCULUS_SUPPORT)
if (oculusEnabled) if (oculusReady)
{ {
UnloadOculusMirror(session, mirror); // Unload Oculus mirror buffer UnloadOculusMirror(session, mirror); // Unload Oculus mirror buffer
UnloadOculusBuffer(session, buffer); // Unload Oculus texture buffers UnloadOculusBuffer(session, buffer); // Unload Oculus texture buffers
@ -2558,20 +2561,26 @@ void CloseOculusDevice(void)
UnloadShader(distortion); UnloadShader(distortion);
} }
oculusEnabled = false; oculusReady = false;
} }
// Detect if oculus device is available // Detect if oculus device is available
bool IsOculusReady(void) bool IsOculusReady(void)
{ {
return (oculusEnabled || oculusSimulator); return (oculusReady || oculusSimulator) && vrEnabled;
}
// Enable/Disable VR experience (Oculus device or simulator)
void ToggleVR(void)
{
vrEnabled = !vrEnabled;
} }
// Update Oculus Rift tracking (position and orientation) // Update Oculus Rift tracking (position and orientation)
void UpdateOculusTracking(void) void UpdateOculusTracking(void)
{ {
#if defined(RLGL_OCULUS_SUPPORT) #if defined(RLGL_OCULUS_SUPPORT)
if (oculusEnabled) if (oculusReady)
{ {
frameIndex++; frameIndex++;
@ -2604,54 +2613,58 @@ void SetOculusView(int eye)
{ {
Matrix eyeProjection; Matrix eyeProjection;
Matrix eyeModelView; Matrix eyeModelView;
if (vrEnabled)
{
#if defined(RLGL_OCULUS_SUPPORT) #if defined(RLGL_OCULUS_SUPPORT)
if (oculusEnabled) if (oculusReady)
{ {
rlViewport(layer.eyeLayer.Viewport[eye].Pos.x, layer.eyeLayer.Viewport[eye].Pos.y, layer.eyeLayer.Viewport[eye].Size.w, layer.eyeLayer.Viewport[eye].Size.h); rlViewport(layer.eyeLayer.Viewport[eye].Pos.x, layer.eyeLayer.Viewport[eye].Pos.y,
layer.eyeLayer.Viewport[eye].Size.w, layer.eyeLayer.Viewport[eye].Size.h);
Quaternion eyeRenderPose = (Quaternion){ layer.eyeLayer.RenderPose[eye].Orientation.x, Quaternion eyeRenderPose = (Quaternion){ layer.eyeLayer.RenderPose[eye].Orientation.x,
layer.eyeLayer.RenderPose[eye].Orientation.y, layer.eyeLayer.RenderPose[eye].Orientation.y,
layer.eyeLayer.RenderPose[eye].Orientation.z, layer.eyeLayer.RenderPose[eye].Orientation.z,
layer.eyeLayer.RenderPose[eye].Orientation.w }; layer.eyeLayer.RenderPose[eye].Orientation.w };
QuaternionInvert(&eyeRenderPose); QuaternionInvert(&eyeRenderPose);
Matrix eyeOrientation = QuaternionToMatrix(eyeRenderPose); Matrix eyeOrientation = QuaternionToMatrix(eyeRenderPose);
Matrix eyeTranslation = MatrixTranslate(-layer.eyeLayer.RenderPose[eye].Position.x, Matrix eyeTranslation = MatrixTranslate(-layer.eyeLayer.RenderPose[eye].Position.x,
-layer.eyeLayer.RenderPose[eye].Position.y, -layer.eyeLayer.RenderPose[eye].Position.y,
-layer.eyeLayer.RenderPose[eye].Position.z); -layer.eyeLayer.RenderPose[eye].Position.z);
Matrix eyeView = MatrixMultiply(eyeTranslation, eyeOrientation); // Matrix containing eye-head movement Matrix eyeView = MatrixMultiply(eyeTranslation, eyeOrientation); // Matrix containing eye-head movement
eyeModelView = MatrixMultiply(modelview, eyeView); // Combine internal camera matrix (modelview) wih eye-head movement eyeModelView = MatrixMultiply(modelview, eyeView); // Combine internal camera matrix (modelview) wih eye-head movement
// TODO: Find a better way to get camera view matrix (instead of using internal modelview) // TODO: Find a better way to get camera view matrix (instead of using internal modelview)
eyeProjection = layer.eyeProjections[eye]; eyeProjection = layer.eyeProjections[eye];
} }
else else
#endif #endif
{ {
// Setup viewport and projection/modelview matrices using tracking data // Setup viewport and projection/modelview matrices using tracking data
rlViewport(eye*screenWidth/2, 0, screenWidth/2, screenHeight); rlViewport(eye*screenWidth/2, 0, screenWidth/2, screenHeight);
// NOTE: fovy vaule hardcoded to 45.0 degrees // NOTE: fovy value hardcoded to 60 degrees (Oculus Rift CV1 vertical FOV is 100 degrees)
eyeProjection = MatrixPerspective(45.0, (double)(screenWidth/2)/(double)screenHeight, 0.01, 1000.0); eyeProjection = MatrixPerspective(60.0, (double)(screenWidth/2)/(double)screenHeight, 0.01, 1000.0);
MatrixTranspose(&eyeProjection); MatrixTranspose(&eyeProjection);
// TODO: Compute eyes IPD and apply to current modelview matrix (camera) // TODO: Compute eyes IPD and apply to current modelview matrix (camera)
Matrix eyeView = MatrixIdentity(); Matrix eyeView = MatrixIdentity();
eyeModelView = MatrixMultiply(modelview, eyeView); eyeModelView = MatrixMultiply(modelview, eyeView);
} }
SetMatrixModelview(eyeModelView); SetMatrixModelview(eyeModelView);
SetMatrixProjection(eyeProjection); SetMatrixProjection(eyeProjection);
}
} }
// Begin Oculus drawing configuration // Begin Oculus drawing configuration
void BeginOculusDrawing(void) void BeginOculusDrawing(void)
{ {
#if defined(RLGL_OCULUS_SUPPORT) #if defined(RLGL_OCULUS_SUPPORT)
if (oculusEnabled) if (oculusReady)
{ {
GLuint currentTexId; GLuint currentTexId;
int currentIndex; int currentIndex;
@ -2684,7 +2697,7 @@ void BeginOculusDrawing(void)
void EndOculusDrawing(void) void EndOculusDrawing(void)
{ {
#if defined(RLGL_OCULUS_SUPPORT) #if defined(RLGL_OCULUS_SUPPORT)
if (oculusEnabled) if (oculusReady)
{ {
// Unbind current framebuffer (Oculus buffer) // Unbind current framebuffer (Oculus buffer)
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0); glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);

View file

@ -362,6 +362,7 @@ void SetOculusView(int eye); // Set internal projection and model
void BeginOculusDrawing(void); // Begin Oculus drawing configuration void BeginOculusDrawing(void); // Begin Oculus drawing configuration
void EndOculusDrawing(void); // End Oculus drawing process (and desktop mirror) void EndOculusDrawing(void); // End Oculus drawing process (and desktop mirror)
bool IsOculusReady(void); // Detect if oculus device (or simulator) is ready bool IsOculusReady(void); // Detect if oculus device (or simulator) is ready
void ToggleVR(void); // Enable/Disable VR experience (Oculus device or simulator)
#ifdef __cplusplus #ifdef __cplusplus
} }