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...
This commit is contained in:
parent
2ff2096b36
commit
bc80174357
5 changed files with 64 additions and 53 deletions
|
@ -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
|
||||||
|
@ -53,8 +54,6 @@ int main()
|
||||||
|
|
||||||
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);
|
||||||
|
@ -64,8 +63,6 @@ int main()
|
||||||
|
|
||||||
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
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
|
@ -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
|
||||||
|
@ -536,8 +534,6 @@ 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
|
||||||
|
|
||||||
|
@ -591,6 +587,8 @@ 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
|
||||||
|
|
||||||
rlPushMatrix(); // Save previous matrix, which contains the settings for the 2d ortho projection
|
rlPushMatrix(); // Save previous matrix, which contains the settings for the 2d ortho projection
|
||||||
|
@ -619,6 +617,8 @@ 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
|
||||||
|
|
||||||
|
|
29
src/raylib.h
29
src/raylib.h
|
@ -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 InitVrDevice(int hmdDevice); // Init VR device
|
||||||
void CloseOculusDevice(void); // Close Oculus Rift device
|
void CloseVrDevice(void); // Close VR device
|
||||||
void UpdateOculusTracking(void); // Update Oculus Rift tracking (position and orientation)
|
void UpdateVrTracking(void); // Update VR tracking (position and orientation)
|
||||||
void BeginOculusDrawing(void); // Begin Oculus drawing configuration
|
void BeginVrDrawing(void); // Begin VR drawing configuration
|
||||||
void EndOculusDrawing(void); // End Oculus drawing process (and desktop mirror)
|
void EndVrDrawing(void); // End VR drawing process (and desktop mirror)
|
||||||
bool IsOculusReady(void); // Detect if oculus device (or simulator) is ready
|
bool IsVrDeviceReady(void); // Detect if VR device (or simulator) is ready
|
||||||
void ToggleVR(void); // Enable/Disable VR experience (Oculus device or simulator)
|
void ToggleVrMode(void); // Enable/Disable VR experience (device or simulator)
|
||||||
|
|
||||||
//------------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------------
|
||||||
// Audio Loading and Playing Functions (Module: audio)
|
// Audio Loading and Playing Functions (Module: audio)
|
||||||
|
|
45
src/rlgl.c
45
src/rlgl.c
|
@ -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)
|
// Init VR device (or simulator)
|
||||||
void InitOculusDevice(void)
|
// 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)
|
// Close VR device (or simulator)
|
||||||
void CloseOculusDevice(void)
|
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
|
// Detect if VR device is available
|
||||||
bool IsOculusReady(void)
|
bool IsVrDeviceReady(void)
|
||||||
{
|
{
|
||||||
return (oculusReady || oculusSimulator) && vrEnabled;
|
return (oculusReady || oculusSimulator) && vrEnabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enable/Disable VR experience (Oculus device or simulator)
|
// Enable/Disable VR experience (device or simulator)
|
||||||
void ToggleVR(void)
|
void ToggleVrMode(void)
|
||||||
{
|
{
|
||||||
vrEnabled = !vrEnabled;
|
vrEnabled = !vrEnabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update Oculus Rift tracking (position and orientation)
|
// Update VR tracking (position and orientation)
|
||||||
void UpdateOculusTracking(void)
|
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 VScreenSize = 0.0936f; // HScreenSize/(1280.0f/800.0f) (DK2)
|
||||||
float VScreenCenter = 0.04675f;
|
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))
|
||||||
|
|
16
src/rlgl.h
16
src/rlgl.h
|
@ -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 InitVrDevice(int hmdDevice); // Init VR device
|
||||||
void CloseOculusDevice(void); // Close Oculus Rift device
|
void CloseVrDevice(void); // Close VR device
|
||||||
void UpdateOculusTracking(void); // Update Oculus Rift tracking (position and orientation)
|
void UpdateVrTracking(void); // Update VR tracking (position and orientation)
|
||||||
void BeginOculusDrawing(void); // Begin Oculus drawing configuration
|
void BeginVrDrawing(void); // Begin VR drawing configuration
|
||||||
void EndOculusDrawing(void); // End Oculus drawing process (and desktop mirror)
|
void EndVrDrawing(void); // End VR drawing process (and desktop mirror)
|
||||||
bool IsOculusReady(void); // Detect if oculus device (or simulator) is ready
|
bool IsVrDeviceReady(void); // Detect if VR device (or simulator) is ready
|
||||||
void ToggleVR(void); // Enable/Disable VR experience (Oculus device or simulator)
|
void ToggleVrMode(void); // Enable/Disable VR experience (device or simulator)
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue