Add oculus simulator (in case device is not detected)
This commit is contained in:
parent
e913de58c7
commit
03d9583b94
@ -607,14 +607,15 @@ void Begin3dMode(Camera camera)
|
|||||||
|
|
||||||
rlEnableDepthTest(); // Enable DEPTH_TEST for 3D
|
rlEnableDepthTest(); // Enable DEPTH_TEST for 3D
|
||||||
|
|
||||||
if (VrEnabled()) BeginOculusDrawing();
|
if (IsOculusReady()) BeginOculusDrawing();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ends 3D mode and returns to default 2D orthographic mode
|
// Ends 3D mode and returns to default 2D orthographic mode
|
||||||
void End3dMode(void)
|
void End3dMode(void)
|
||||||
{
|
{
|
||||||
if (VrEnabled()) EndOculusDrawing();
|
if (IsOculusReady()) EndOculusDrawing();
|
||||||
else rlglDraw(); // Draw Buffers (Only OpenGL 3+ and ES2)
|
|
||||||
|
rlglDraw(); // Process internal buffers (update + draw)
|
||||||
|
|
||||||
rlMatrixMode(RL_PROJECTION); // Switch to projection matrix
|
rlMatrixMode(RL_PROJECTION); // Switch to projection matrix
|
||||||
rlPopMatrix(); // Restore previous matrix (PROJECTION) from matrix stack
|
rlPopMatrix(); // Restore previous matrix (PROJECTION) from matrix stack
|
||||||
|
155
src/rlgl.c
155
src/rlgl.c
@ -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
|
static unsigned int frameIndex = 0; // Oculus frames counter, used to discard frames from chain
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static bool vrSimulator = false; // VR simulator (stereo rendering on window, without vr device)
|
static bool oculusEnabled = false; // Oculus device enabled flag (required by core module)
|
||||||
static bool vrEnabled = false; // VR enabled flag (required by core module)
|
static bool oculusSimulator = false; // Oculus device simulator
|
||||||
|
|
||||||
|
static RenderTexture2D stereoFbo;
|
||||||
|
static Shader distortion;
|
||||||
|
|
||||||
// Compressed textures support flags
|
// Compressed textures support flags
|
||||||
static bool texCompDXTSupported = false; // DDS texture compression support
|
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.id != 0) glDeleteFramebuffers(1, &target.id);
|
||||||
if (target.texture.id != 0) glDeleteTextures(1, &target.texture.id);
|
if (target.texture.id != 0) glDeleteTextures(1, &target.texture.id);
|
||||||
if (target.depth.id != 0) glDeleteTextures(1, &target.depth.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
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2484,8 +2489,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");
|
||||||
TraceLog(INFO, "VR: Initializing Oculus simulator");
|
oculusEnabled = false;
|
||||||
vrSimulator = true;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -2494,9 +2498,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;
|
||||||
TraceLog(INFO, "VR: Initializing Oculus simulator");
|
|
||||||
vrSimulator = true;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -2520,17 +2522,29 @@ void InitOculusDevice(void)
|
|||||||
|
|
||||||
// Recenter OVR tracking origin
|
// Recenter OVR tracking origin
|
||||||
ovr_RecenterTrackingOrigin(session);
|
ovr_RecenterTrackingOrigin(session);
|
||||||
|
|
||||||
|
oculusEnabled = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
vrSimulator = true;
|
oculusEnabled = false;
|
||||||
vrEnabled = true;
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (vrSimulator)
|
if (!oculusEnabled)
|
||||||
{
|
{
|
||||||
// TODO: Initialize framebuffer and textures for stereo rendering
|
TraceLog(INFO, "VR: Initializing Oculus simulator");
|
||||||
// TODO: Load oculus-distortion shader (oculus parameters setup internally)
|
|
||||||
|
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)
|
void CloseOculusDevice(void)
|
||||||
{
|
{
|
||||||
#if defined(RLGL_OCULUS_SUPPORT)
|
#if defined(RLGL_OCULUS_SUPPORT)
|
||||||
|
if (oculusEnabled)
|
||||||
|
{
|
||||||
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
|
||||||
|
|
||||||
ovr_Destroy(session); // Free Oculus session data
|
ovr_Destroy(session); // Free Oculus session data
|
||||||
ovr_Shutdown(); // Close Oculus device connection
|
ovr_Shutdown(); // Close Oculus device connection
|
||||||
|
}
|
||||||
|
else
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (vrSimulator)
|
|
||||||
{
|
{
|
||||||
// TODO: Unload stereo framebuffer and texture
|
// Unload stereo framebuffer and texture
|
||||||
// TODO: Unload oculus-distortion shader
|
rlDeleteRenderTextures(stereoFbo);
|
||||||
|
|
||||||
|
// Unload oculus-distortion shader
|
||||||
|
UnloadShader(distortion);
|
||||||
}
|
}
|
||||||
|
|
||||||
vrEnabled = false;
|
oculusEnabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Track stereoscopic rendering
|
// Detect if oculus device is available
|
||||||
bool VrEnabled(void)
|
bool IsOculusReady(void)
|
||||||
{
|
{
|
||||||
return vrEnabled;
|
return (oculusEnabled || oculusSimulator);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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)
|
||||||
|
{
|
||||||
frameIndex++;
|
frameIndex++;
|
||||||
|
|
||||||
ovrPosef eyePoses[2];
|
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.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.HmdMounted) // HMD is on the user's head.
|
||||||
//if (sessionStatus.IsVisible) // the game or experience has VR focus and is visible in the HMD.
|
//if (sessionStatus.IsVisible) // the game or experience has VR focus and is visible in the HMD.
|
||||||
|
}
|
||||||
|
else
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (vrSimulator)
|
|
||||||
{
|
{
|
||||||
// TODO: Use alternative inputs (mouse, keyboard) to simulate tracking data (eyes position/orientation)
|
// TODO: Use alternative inputs (mouse, keyboard) to simulate tracking data (eyes position/orientation)
|
||||||
}
|
}
|
||||||
@ -2597,6 +2618,8 @@ void SetOculusView(int eye)
|
|||||||
Matrix eyeModelView;
|
Matrix eyeModelView;
|
||||||
|
|
||||||
#if defined(RLGL_OCULUS_SUPPORT)
|
#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);
|
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,
|
||||||
@ -2615,11 +2638,24 @@ void SetOculusView(int eye)
|
|||||||
// 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
|
||||||
#endif
|
#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);
|
SetMatrixModelview(eyeModelView);
|
||||||
@ -2630,6 +2666,8 @@ void SetOculusView(int eye)
|
|||||||
void BeginOculusDrawing(void)
|
void BeginOculusDrawing(void)
|
||||||
{
|
{
|
||||||
#if defined(RLGL_OCULUS_SUPPORT)
|
#if defined(RLGL_OCULUS_SUPPORT)
|
||||||
|
if (oculusEnabled)
|
||||||
|
{
|
||||||
GLuint currentTexId;
|
GLuint currentTexId;
|
||||||
int currentIndex;
|
int currentIndex;
|
||||||
|
|
||||||
@ -2639,12 +2677,12 @@ void BeginOculusDrawing(void)
|
|||||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, buffer.fboId);
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, buffer.fboId);
|
||||||
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, currentTexId, 0);
|
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
|
//glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, buffer.depthId, 0); // Already binded
|
||||||
|
}
|
||||||
|
else
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (vrSimulator)
|
|
||||||
{
|
{
|
||||||
// TODO: Setup framebuffer for stereo rendering
|
// Setup framebuffer for stereo rendering
|
||||||
//rlEnableRenderTexture(buffer.fboId);
|
rlEnableRenderTexture(stereoFbo.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: If your application is configured to treat the texture as a linear format (e.g. GL_RGBA)
|
// 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)
|
void EndOculusDrawing(void)
|
||||||
{
|
{
|
||||||
#if defined(RLGL_OCULUS_SUPPORT)
|
#if defined(RLGL_OCULUS_SUPPORT)
|
||||||
|
if (oculusEnabled)
|
||||||
|
{
|
||||||
// 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);
|
||||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
||||||
@ -2672,17 +2712,60 @@ void EndOculusDrawing(void)
|
|||||||
|
|
||||||
// Blit mirror texture to back buffer
|
// Blit mirror texture to back buffer
|
||||||
BlitOculusMirror(session, mirror);
|
BlitOculusMirror(session, mirror);
|
||||||
|
}
|
||||||
|
else
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (vrSimulator)
|
|
||||||
{
|
{
|
||||||
// Unbind current framebuffer
|
// Unbind current framebuffer
|
||||||
//rlDisableRenderTexture();
|
rlDisableRenderTexture();
|
||||||
|
|
||||||
// TODO: Draw RenderTexture (fbo) using distortion shader
|
rlClearScreenBuffers(); // Clear current framebuffer
|
||||||
//BeginShaderMode(distortion);
|
|
||||||
// TODO: DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, -target.texture.height }, (Vector2){ 0, 0 }, WHITE);
|
int screenWidth = 1080;
|
||||||
//EndShaderMode();
|
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();
|
rlDisableDepthTest();
|
||||||
|
@ -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 SetOculusView(int eye); // Set internal projection and modelview matrix depending on eyes tracking data
|
||||||
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 VrEnabled(void); // Track stereoscopic rendering
|
bool IsOculusReady(void); // Detect if oculus device (or simulator) is ready
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user