Browse Source

VR Functions renaming (for generic HMD device)

Stereo rendering has been moved again to Begin3dMode() and End3dMode(),
it has some limitations but makes more sense...
pull/132/head
Ray 8 years ago
parent
commit
bc80174357
5 changed files with 64 additions and 53 deletions
  1. +8
    -11
      examples/core_oculus_rift.c
  2. +4
    -4
      src/core.c
  3. +21
    -8
      src/raylib.h
  4. +23
    -22
      src/rlgl.c
  5. +8
    -8
      src/rlgl.h

+ 8
- 11
examples/core_oculus_rift.c View File

@ -23,7 +23,8 @@ int main()
InitWindow(screenWidth, screenHeight, "raylib [core] example - oculus rift"); InitWindow(screenWidth, screenHeight, "raylib [core] example - oculus rift");
InitOculusDevice();
// NOTE: If device is not available, it fallbacks to default device (simulator)
InitVrDevice(HMD_OCULUS_RIFT_CV1); // Init VR device (Oculus Rift CV1)
// Define the camera to look into our 3d world // Define the camera to look into our 3d world
Camera camera; Camera camera;
@ -42,9 +43,9 @@ int main()
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
UpdateOculusTracking();
UpdateVrTracking();
if (IsKeyPressed(KEY_SPACE)) ToggleVR();
if (IsKeyPressed(KEY_SPACE)) ToggleVrMode();
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Draw // Draw
@ -52,20 +53,16 @@ int main()
BeginDrawing(); BeginDrawing();
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
if (IsOculusReady()) BeginOculusDrawing();
Begin3dMode(camera); Begin3dMode(camera);
DrawCube(cubePosition, 2.0f, 2.0f, 2.0f, RED); DrawCube(cubePosition, 2.0f, 2.0f, 2.0f, RED);
DrawCubeWires(cubePosition, 2.0f, 2.0f, 2.0f, MAROON); DrawCubeWires(cubePosition, 2.0f, 2.0f, 2.0f, MAROON);
DrawGrid(10, 1.0f); DrawGrid(10, 1.0f);
End3dMode(); End3dMode();
if (IsOculusReady()) EndOculusDrawing();
DrawFPS(10, 10); DrawFPS(10, 10);
EndDrawing(); EndDrawing();
@ -74,7 +71,7 @@ int main()
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
CloseOculusDevice(); // Close Oculus Rift device
CloseVrDevice(); // Close VR device
CloseWindow(); // Close window and OpenGL context CloseWindow(); // Close window and OpenGL context
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------

+ 4
- 4
src/core.c View File

@ -521,8 +521,6 @@ void BeginDrawing(void)
updateTime = currentTime - previousTime; updateTime = currentTime - previousTime;
previousTime = currentTime; previousTime = currentTime;
//if (IsOculusReady()) BeginOculusDrawing();
rlClearScreenBuffers(); // Clear current framebuffers rlClearScreenBuffers(); // Clear current framebuffers
rlLoadIdentity(); // Reset current matrix (MODELVIEW) rlLoadIdentity(); // Reset current matrix (MODELVIEW)
rlMultMatrixf(MatrixToFloat(downscaleView)); // If downscale required, apply it here rlMultMatrixf(MatrixToFloat(downscaleView)); // If downscale required, apply it here
@ -535,8 +533,6 @@ void BeginDrawing(void)
void EndDrawing(void) void EndDrawing(void)
{ {
rlglDraw(); // Draw Buffers (Only OpenGL 3+ and ES2) rlglDraw(); // Draw Buffers (Only OpenGL 3+ and ES2)
//if (IsOculusReady()) EndOculusDrawing();
SwapBuffers(); // Copy back buffer to front buffer SwapBuffers(); // Copy back buffer to front buffer
PollInputEvents(); // Poll user events PollInputEvents(); // Poll user events
@ -590,6 +586,8 @@ void End2dMode(void)
void Begin3dMode(Camera camera) void Begin3dMode(Camera camera)
{ {
rlglDraw(); // Draw Buffers (Only OpenGL 3+ and ES2) rlglDraw(); // Draw Buffers (Only OpenGL 3+ and ES2)
if (IsVrDeviceReady()) BeginVrDrawing();
rlMatrixMode(RL_PROJECTION); // Switch to projection matrix rlMatrixMode(RL_PROJECTION); // Switch to projection matrix
@ -618,6 +616,8 @@ void Begin3dMode(Camera camera)
void End3dMode(void) void End3dMode(void)
{ {
rlglDraw(); // Process internal buffers (update + draw) rlglDraw(); // Process internal buffers (update + draw)
if (IsVrDeviceReady()) EndVrDrawing();
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

+ 21
- 8
src/raylib.h View File

@ -527,6 +527,19 @@ typedef struct GestureEvent {
// Camera system modes // Camera system modes
typedef enum { CAMERA_CUSTOM = 0, CAMERA_FREE, CAMERA_ORBITAL, CAMERA_FIRST_PERSON, CAMERA_THIRD_PERSON } CameraMode; typedef enum { CAMERA_CUSTOM = 0, CAMERA_FREE, CAMERA_ORBITAL, CAMERA_FIRST_PERSON, CAMERA_THIRD_PERSON } CameraMode;
// Head Mounted Display devices
typedef enum {
HMD_DEFAULT_DEVICE = 0,
HMD_OCULUS_RIFT_DK2,
HMD_OCULUS_RIFT_CV1,
HMD_VALVE_HTC_VIVE,
HMD_SAMSUNG_GEAR_VR,
HMD_GOOGLE_CARDBOARD,
HMD_SONY_PLAYSTATION_VR,
HMD_RAZER_OSVR,
HMD_FOVE_VR,
} HmdDevice;
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { // Prevents name mangling of functions extern "C" { // Prevents name mangling of functions
#endif #endif
@ -846,16 +859,16 @@ Light CreateLight(int type, Vector3 position, Color diffuse); // Create a
void DestroyLight(Light light); // Destroy a light and take it out of the list void DestroyLight(Light light); // Destroy a light and take it out of the list
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
// Oculus Rift CV1 Functions (Module: rlgl)
// VR experience Functions (Module: rlgl)
// NOTE: This functions are useless when using OpenGL 1.1 // NOTE: This functions are useless when using OpenGL 1.1
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
void InitOculusDevice(void); // Init Oculus Rift device
void CloseOculusDevice(void); // Close Oculus Rift device
void UpdateOculusTracking(void); // Update Oculus Rift tracking (position and orientation)
void BeginOculusDrawing(void); // Begin Oculus drawing configuration
void EndOculusDrawing(void); // End Oculus drawing process (and desktop mirror)
bool IsOculusReady(void); // Detect if oculus device (or simulator) is ready
void ToggleVR(void); // Enable/Disable VR experience (Oculus device or simulator)
void InitVrDevice(int hmdDevice); // Init VR device
void CloseVrDevice(void); // Close VR device
void UpdateVrTracking(void); // Update VR tracking (position and orientation)
void BeginVrDrawing(void); // Begin VR drawing configuration
void EndVrDrawing(void); // End VR drawing process (and desktop mirror)
bool IsVrDeviceReady(void); // Detect if VR device (or simulator) is ready
void ToggleVrMode(void); // Enable/Disable VR experience (device or simulator)
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
// Audio Loading and Playing Functions (Module: audio) // Audio Loading and Playing Functions (Module: audio)

+ 23
- 22
src/rlgl.c View File

@ -322,7 +322,7 @@ static void DrawDefaultBuffers(int eyesCount); // Draw default internal buffers
static void UnloadDefaultBuffers(void); // Unload default internal buffers vertex data from CPU and GPU static void UnloadDefaultBuffers(void); // Unload default internal buffers vertex data from CPU and GPU
// Set internal projection and modelview matrix depending on eyes tracking data // Set internal projection and modelview matrix depending on eyes tracking data
static void SetOculusView(int eye, Matrix matProjection, Matrix matModelView);
static void SetStereoView(int eye, Matrix matProjection, Matrix matModelView);
static void SetShaderLights(Shader shader); // Sets shader uniform values for lights array static void SetShaderLights(Shader shader); // Sets shader uniform values for lights array
@ -2001,7 +2001,7 @@ void rlglDrawMesh(Mesh mesh, Material material, Matrix transform)
for (int eye = 0; eye < eyesCount; eye++) for (int eye = 0; eye < eyesCount; eye++)
{ {
if (eyesCount == 2) SetOculusView(eye, matProjection, matModelView);
if (eyesCount == 2) SetStereoView(eye, matProjection, matModelView);
else modelview = matModelView; else modelview = matModelView;
// Calculate model-view-projection matrix (MVP) // Calculate model-view-projection matrix (MVP)
@ -2504,8 +2504,9 @@ void DestroyLight(Light light)
#endif #endif
} }
// Init Oculus Rift device (or Oculus device simulator)
void InitOculusDevice(void)
// Init VR device (or simulator)
// NOTE: If device is not available, it fallbacks to default device (simulator)
void InitVrDevice(int hmdDevice)
{ {
#if defined(RLGL_OCULUS_SUPPORT) #if defined(RLGL_OCULUS_SUPPORT)
// Initialize Oculus device // Initialize Oculus device
@ -2557,7 +2558,7 @@ void InitOculusDevice(void)
if (!oculusReady) if (!oculusReady)
{ {
TraceLog(WARNING, "VR: Initializing Oculus simulator");
TraceLog(WARNING, "HMD Device not found: Initializing VR simulator");
// Initialize framebuffer and textures for stereo rendering // Initialize framebuffer and textures for stereo rendering
stereoFbo = rlglLoadRenderTexture(screenWidth, screenHeight); stereoFbo = rlglLoadRenderTexture(screenWidth, screenHeight);
@ -2571,8 +2572,8 @@ void InitOculusDevice(void)
} }
} }
// Close Oculus Rift device (or Oculus device simulator)
void CloseOculusDevice(void)
// Close VR device (or simulator)
void CloseVrDevice(void)
{ {
#if defined(RLGL_OCULUS_SUPPORT) #if defined(RLGL_OCULUS_SUPPORT)
if (oculusReady) if (oculusReady)
@ -2596,20 +2597,20 @@ void CloseOculusDevice(void)
oculusReady = false; oculusReady = false;
} }
// Detect if oculus device is available
bool IsOculusReady(void)
// Detect if VR device is available
bool IsVrDeviceReady(void)
{ {
return (oculusReady || oculusSimulator) && vrEnabled; return (oculusReady || oculusSimulator) && vrEnabled;
} }
// Enable/Disable VR experience (Oculus device or simulator)
void ToggleVR(void)
// Enable/Disable VR experience (device or simulator)
void ToggleVrMode(void)
{ {
vrEnabled = !vrEnabled; vrEnabled = !vrEnabled;
} }
// Update Oculus Rift tracking (position and orientation)
void UpdateOculusTracking(void)
// Update VR tracking (position and orientation)
void UpdateVrTracking(void)
{ {
#if defined(RLGL_OCULUS_SUPPORT) #if defined(RLGL_OCULUS_SUPPORT)
if (oculusReady) if (oculusReady)
@ -2641,7 +2642,7 @@ void UpdateOculusTracking(void)
} }
// Set internal projection and modelview matrix depending on eyes tracking data // Set internal projection and modelview matrix depending on eyes tracking data
static void SetOculusView(int eye, Matrix matProjection, Matrix matModelView)
static void SetStereoView(int eye, Matrix matProjection, Matrix matModelView)
{ {
if (vrEnabled) if (vrEnabled)
{ {
@ -2675,12 +2676,12 @@ static void SetOculusView(int eye, Matrix matProjection, Matrix matModelView)
// 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);
static float IPD = 0.064f; // InterpupillaryDistance
static float IPD = 0.064f; // InterpupillaryDistance
float HScreenSize = 0.14976f; float HScreenSize = 0.14976f;
float VScreenSize = 0.0936f; // HScreenSize/(1280.0f/800.0f)
float VScreenCenter = 0.04675f;
float VScreenSize = 0.0936f; // HScreenSize/(1280.0f/800.0f) (DK2)
float VScreenCenter = 0.04675f; // VScreenSize/2
float EyeToScreenDistance = 0.041f; float EyeToScreenDistance = 0.041f;
float LensSeparationDistance = 0.064f; //0.0635f (DK1)
float LensSeparationDistance = 0.064f; //0.0635f (DK1)
// NOTE: fovy value obtained from device parameters (Oculus Rift CV1) // NOTE: fovy value obtained from device parameters (Oculus Rift CV1)
float halfScreenDistance = VScreenSize/2.0f; float halfScreenDistance = VScreenSize/2.0f;
@ -2730,13 +2731,13 @@ static void SetOculusView(int eye, Matrix matProjection, Matrix matModelView)
MatrixTranspose(&eyeProjection); MatrixTranspose(&eyeProjection);
} }
SetMatrixModelview(eyeModelView); // ERROR! We are modifying modelview for next eye!!!
SetMatrixModelview(eyeModelView);
SetMatrixProjection(eyeProjection); SetMatrixProjection(eyeProjection);
} }
} }
// Begin Oculus drawing configuration // Begin Oculus drawing configuration
void BeginOculusDrawing(void)
void BeginVrDrawing(void)
{ {
#if defined(RLGL_OCULUS_SUPPORT) #if defined(RLGL_OCULUS_SUPPORT)
if (oculusReady) if (oculusReady)
@ -2771,7 +2772,7 @@ void BeginOculusDrawing(void)
} }
// End Oculus drawing process (and desktop mirror) // End Oculus drawing process (and desktop mirror)
void EndOculusDrawing(void)
void EndVrDrawing(void)
{ {
#if defined(RLGL_OCULUS_SUPPORT) #if defined(RLGL_OCULUS_SUPPORT)
if (oculusReady) if (oculusReady)
@ -3414,7 +3415,7 @@ static void DrawDefaultBuffers(int eyesCount)
for (int eye = 0; eye < eyesCount; eye++) for (int eye = 0; eye < eyesCount; eye++)
{ {
if (eyesCount == 2) SetOculusView(eye, matProjection, matModelView);
if (eyesCount == 2) SetStereoView(eye, matProjection, matModelView);
// Set current shader and upload current MVP matrix // Set current shader and upload current MVP matrix
if ((lines.vCounter > 0) || (triangles.vCounter > 0) || (quads.vCounter > 0)) if ((lines.vCounter > 0) || (triangles.vCounter > 0) || (quads.vCounter > 0))

+ 8
- 8
src/rlgl.h View File

@ -350,15 +350,15 @@ Light CreateLight(int type, Vector3 position, Color diffuse); // Create a
void DestroyLight(Light light); // Destroy a light and take it out of the list void DestroyLight(Light light); // Destroy a light and take it out of the list
void TraceLog(int msgType, const char *text, ...); void TraceLog(int msgType, const char *text, ...);
#endif
void InitOculusDevice(void); // Init Oculus Rift device
void CloseOculusDevice(void); // Close Oculus Rift device
void UpdateOculusTracking(void); // Update Oculus Rift tracking (position and orientation)
void BeginOculusDrawing(void); // Begin Oculus drawing configuration
void EndOculusDrawing(void); // End Oculus drawing process (and desktop mirror)
bool IsOculusReady(void); // Detect if oculus device (or simulator) is ready
void ToggleVR(void); // Enable/Disable VR experience (Oculus device or simulator)
void InitVrDevice(int hmdDevice); // Init VR device
void CloseVrDevice(void); // Close VR device
void UpdateVrTracking(void); // Update VR tracking (position and orientation)
void BeginVrDrawing(void); // Begin VR drawing configuration
void EndVrDrawing(void); // End VR drawing process (and desktop mirror)
bool IsVrDeviceReady(void); // Detect if VR device (or simulator) is ready
void ToggleVrMode(void); // Enable/Disable VR experience (device or simulator)
#endif
#ifdef __cplusplus #ifdef __cplusplus
} }

Loading…
Cancel
Save