Add oculus simulator (in case device is not detected)

This commit is contained in:
raysan5 2016-06-21 13:49:13 +02:00
parent e913de58c7
commit 03d9583b94
3 changed files with 186 additions and 102 deletions

View File

@ -607,14 +607,15 @@ void Begin3dMode(Camera camera)
rlEnableDepthTest(); // Enable DEPTH_TEST for 3D
if (VrEnabled()) BeginOculusDrawing();
if (IsOculusReady()) BeginOculusDrawing();
}
// Ends 3D mode and returns to default 2D orthographic mode
void End3dMode(void)
{
if (VrEnabled()) EndOculusDrawing();
else rlglDraw(); // Draw Buffers (Only OpenGL 3+ and ES2)
if (IsOculusReady()) EndOculusDrawing();
rlglDraw(); // Process internal buffers (update + draw)
rlMatrixMode(RL_PROJECTION); // Switch to projection matrix
rlPopMatrix(); // Restore previous matrix (PROJECTION) from matrix stack

View File

@ -267,8 +267,11 @@ static OculusMirror mirror; // Oculus mirror texture and fbo
static unsigned int frameIndex = 0; // Oculus frames counter, used to discard frames from chain
#endif
static bool vrSimulator = false; // VR simulator (stereo rendering on window, without vr device)
static bool vrEnabled = false; // VR enabled flag (required by core module)
static bool oculusEnabled = false; // Oculus device enabled flag (required by core module)
static bool oculusSimulator = false; // Oculus device simulator
static RenderTexture2D stereoFbo;
static Shader distortion;
// Compressed textures support flags
static bool texCompDXTSupported = false; // DDS texture compression support
@ -865,6 +868,8 @@ void rlDeleteRenderTextures(RenderTexture2D target)
if (target.id != 0) glDeleteFramebuffers(1, &target.id);
if (target.texture.id != 0) glDeleteTextures(1, &target.texture.id);
if (target.depth.id != 0) glDeleteTextures(1, &target.depth.id);
TraceLog(INFO, "[FBO ID %i] Unloaded render texture data from VRAM (GPU)", target.id);
#endif
}
@ -2484,8 +2489,7 @@ void InitOculusDevice(void)
if (OVR_FAILURE(result))
{
TraceLog(WARNING, "OVR: Could not initialize Oculus device");
TraceLog(INFO, "VR: Initializing Oculus simulator");
vrSimulator = true;
oculusEnabled = false;
}
else
{
@ -2494,9 +2498,7 @@ void InitOculusDevice(void)
{
TraceLog(WARNING, "OVR: Could not create Oculus session");
ovr_Shutdown();
TraceLog(INFO, "VR: Initializing Oculus simulator");
vrSimulator = true;
oculusEnabled = false;
}
else
{
@ -2520,17 +2522,29 @@ void InitOculusDevice(void)
// Recenter OVR tracking origin
ovr_RecenterTrackingOrigin(session);
oculusEnabled = true;
}
}
#else
vrSimulator = true;
vrEnabled = true;
oculusEnabled = false;
#endif
if (vrSimulator)
if (!oculusEnabled)
{
// TODO: Initialize framebuffer and textures for stereo rendering
// TODO: Load oculus-distortion shader (oculus parameters setup internally)
TraceLog(INFO, "VR: Initializing Oculus simulator");
int screenWidth = 1080;
int screenHeight = 600;
// Initialize framebuffer and textures for stereo rendering
stereoFbo = rlglLoadRenderTexture(screenWidth, screenHeight);
// Load oculus-distortion shader (oculus parameters setup internally)
// TODO: Embed coulus distortion shader (in this function like default shader?)
distortion = LoadShader("resources/shaders/base.vs", "resources/shaders/distortion.fs");
oculusSimulator = true;
}
}
@ -2538,32 +2552,39 @@ void InitOculusDevice(void)
void CloseOculusDevice(void)
{
#if defined(RLGL_OCULUS_SUPPORT)
if (oculusEnabled)
{
UnloadOculusMirror(session, mirror); // Unload Oculus mirror buffer
UnloadOculusBuffer(session, buffer); // Unload Oculus texture buffers
ovr_Destroy(session); // Free Oculus session data
ovr_Shutdown(); // Close Oculus device connection
}
else
#endif
if (vrSimulator)
{
// TODO: Unload stereo framebuffer and texture
// TODO: Unload oculus-distortion shader
// Unload stereo framebuffer and texture
rlDeleteRenderTextures(stereoFbo);
// Unload oculus-distortion shader
UnloadShader(distortion);
}
vrEnabled = false;
oculusEnabled = false;
}
// Track stereoscopic rendering
bool VrEnabled(void)
// Detect if oculus device is available
bool IsOculusReady(void)
{
return vrEnabled;
return (oculusEnabled || oculusSimulator);
}
// Update Oculus Rift tracking (position and orientation)
void UpdateOculusTracking(void)
{
#if defined(RLGL_OCULUS_SUPPORT)
if (oculusEnabled)
{
frameIndex++;
ovrPosef eyePoses[2];
@ -2582,9 +2603,9 @@ void UpdateOculusTracking(void)
//if (sessionStatus.DisplayLost) // HMD was unplugged or the display driver was manually disabled or encountered a TDR.
//if (sessionStatus.HmdMounted) // HMD is on the user's head.
//if (sessionStatus.IsVisible) // the game or experience has VR focus and is visible in the HMD.
}
else
#endif
if (vrSimulator)
{
// TODO: Use alternative inputs (mouse, keyboard) to simulate tracking data (eyes position/orientation)
}
@ -2597,6 +2618,8 @@ void SetOculusView(int eye)
Matrix eyeModelView;
#if defined(RLGL_OCULUS_SUPPORT)
if (oculusEnabled)
{
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,
@ -2615,11 +2638,24 @@ void SetOculusView(int eye)
// TODO: Find a better way to get camera view matrix (instead of using internal modelview)
eyeProjection = layer.eyeProjections[eye];
}
else
#endif
if (vrSimulator)
{
// TODO: Setup viewport and projection/modelview matrices using tracking data
int screenWidth = 1080;
int screenHeight = 600;
float fovy = 45.0f;
// Setup viewport and projection/modelview matrices using tracking data
rlViewport(eye*screenWidth/2, 0, screenWidth/2, screenHeight);
eyeProjection = MatrixPerspective(fovy, (double)(screenWidth/2)/(double)screenHeight, 0.01, 1000.0);
MatrixTranspose(&eyeProjection);
// TODO: Compute eyes IPD and apply to current modelview matrix (camera)
Matrix eyeView = MatrixIdentity();
eyeModelView = MatrixMultiply(modelview, eyeView);
}
SetMatrixModelview(eyeModelView);
@ -2630,6 +2666,8 @@ void SetOculusView(int eye)
void BeginOculusDrawing(void)
{
#if defined(RLGL_OCULUS_SUPPORT)
if (oculusEnabled)
{
GLuint currentTexId;
int currentIndex;
@ -2639,12 +2677,12 @@ void BeginOculusDrawing(void)
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, buffer.fboId);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, currentTexId, 0);
//glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, buffer.depthId, 0); // Already binded
}
else
#endif
if (vrSimulator)
{
// TODO: Setup framebuffer for stereo rendering
//rlEnableRenderTexture(buffer.fboId);
// Setup framebuffer for stereo rendering
rlEnableRenderTexture(stereoFbo.id);
}
// NOTE: If your application is configured to treat the texture as a linear format (e.g. GL_RGBA)
@ -2661,6 +2699,8 @@ void BeginOculusDrawing(void)
void EndOculusDrawing(void)
{
#if defined(RLGL_OCULUS_SUPPORT)
if (oculusEnabled)
{
// Unbind current framebuffer (Oculus buffer)
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
@ -2672,17 +2712,60 @@ void EndOculusDrawing(void)
// Blit mirror texture to back buffer
BlitOculusMirror(session, mirror);
}
else
#endif
if (vrSimulator)
{
// Unbind current framebuffer
//rlDisableRenderTexture();
rlDisableRenderTexture();
// TODO: Draw RenderTexture (fbo) using distortion shader
//BeginShaderMode(distortion);
// TODO: DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, -target.texture.height }, (Vector2){ 0, 0 }, WHITE);
//EndShaderMode();
rlClearScreenBuffers(); // Clear current framebuffer
int screenWidth = 1080;
int screenHeight = 600;
// Set viewport to default framebuffer size (screen size)
rlViewport(0, 0, screenWidth, screenHeight);
// Let rlgl reconfigure internal matrices
rlMatrixMode(RL_PROJECTION); // Enable internal projection matrix
rlLoadIdentity(); // Reset internal projection matrix
rlOrtho(0.0, screenWidth, screenHeight, 0.0, 0.0, 1.0); // Recalculate internal projection matrix
rlMatrixMode(RL_MODELVIEW); // Enable internal modelview matrix
rlLoadIdentity(); // Reset internal modelview matrix
// Draw RenderTexture (stereoFbo) using distortion shader
BeginShaderMode(distortion);
rlEnableTexture(stereoFbo.texture.id);
rlPushMatrix();
rlBegin(RL_QUADS);
rlColor4ub(255, 255, 255, 255);
rlNormal3f(0.0f, 0.0f, 1.0f); // Normal vector pointing towards viewer
// Bottom-left corner for texture and quad
rlTexCoord2f(0.0f, 1.0f);
rlVertex2f(0.0f, 0.0f);
// Bottom-right corner for texture and quad
rlTexCoord2f(0.0f, 0.0f);
rlVertex2f(0.0f, stereoFbo.texture.height);
// Top-right corner for texture and quad
rlTexCoord2f(1.0f, 0.0f);
rlVertex2f(stereoFbo.texture.width, stereoFbo.texture.height);
// Top-left corner for texture and quad
rlTexCoord2f(1.0f, 1.0f);
rlVertex2f(stereoFbo.texture.width, 0.0f);
rlEnd();
rlPopMatrix();
rlDisableTexture();
//rlglDraw();
EndShaderMode();
}
rlDisableDepthTest();

View File

@ -362,7 +362,7 @@ void UpdateOculusTracking(void); // Update Oculus Rift tracking (posi
void SetOculusView(int eye); // Set internal projection and modelview matrix depending on eyes tracking data
void BeginOculusDrawing(void); // Begin Oculus drawing configuration
void EndOculusDrawing(void); // End Oculus drawing process (and desktop mirror)
bool VrEnabled(void); // Track stereoscopic rendering
bool IsOculusReady(void); // Detect if oculus device (or simulator) is ready
#ifdef __cplusplus
}