aboutsummaryrefslogtreecommitdiff
path: root/src/core.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/core.c')
-rw-r--r--src/core.c629
1 files changed, 112 insertions, 517 deletions
diff --git a/src/core.c b/src/core.c
index 4ba7505b..a3253d79 100644
--- a/src/core.c
+++ b/src/core.c
@@ -9,7 +9,7 @@
* PLATFORM_ANDROID - Only OpenGL ES 2.0 devices
* PLATFORM_RPI - Rapsberry Pi (tested on Raspbian)
* PLATFORM_WEB - Emscripten, HTML5
-* PLATFORM_OCULUS - Oculus Rift CV1 (with desktop mirror)
+* Oculus Rift CV1 (with desktop mirror) - View [rlgl] module to enable it
*
* On PLATFORM_DESKTOP, the external lib GLFW3 (www.glfw.com) is used to manage graphic
* device, OpenGL context and input on multiple operating systems (Windows, Linux, OSX).
@@ -54,18 +54,6 @@
#include <string.h> // String function definitions, memset()
#include <errno.h> // Macros for reporting and retrieving error conditions through error codes
-#if defined(PLATFORM_OCULUS)
- #define PLATFORM_DESKTOP // Enable PLATFORM_DESKTOP code-base
-#endif
-
-#if defined(PLATFORM_DESKTOP)
- #include "external/glad.h" // GLAD library: Manage OpenGL headers and extensions
-#endif
-
-#if defined(PLATFORM_OCULUS)
- #include "../examples/oculus_glfw_sample/OculusSDK/LibOVR/Include/OVR_CAPI_GL.h" // Oculus SDK for OpenGL
-#endif
-
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
//#define GLFW_INCLUDE_NONE // Disable the standard OpenGL header inclusion on GLFW3
#include <GLFW/glfw3.h> // GLFW3 library: Windows, OpenGL context and Input management
@@ -138,31 +126,7 @@
//----------------------------------------------------------------------------------
// Types and Structures Definition
//----------------------------------------------------------------------------------
-#if defined(PLATFORM_OCULUS)
-typedef struct OculusBuffer {
- ovrTextureSwapChain textureChain;
- GLuint depthId;
- GLuint fboId;
- int width;
- int height;
-} OculusBuffer;
-
-typedef struct OculusMirror {
- ovrMirrorTexture texture;
- GLuint fboId;
- int width;
- int height;
-} OculusMirror;
-
-typedef struct OculusLayer {
- ovrViewScaleDesc viewScaleDesc;
- ovrLayerEyeFov eyeLayer; // layer 0
- //ovrLayerQuad quadLayer; // TODO: layer 1: '2D' quad for GUI
- Matrix eyeProjections[2];
- int width;
- int height;
-} OculusLayer;
-#endif
+// ...
//----------------------------------------------------------------------------------
// Global Variables Definition
@@ -209,35 +173,22 @@ float gamepadAxisValues[MAX_GAMEPADS][MAX_GAMEPAD_AXIS]; // Gamepad axis st
#endif
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI)
-static EGLDisplay display; // Native display device (physical screen connection)
-static EGLSurface surface; // Surface to draw on, framebuffers (connected to context)
-static EGLContext context; // Graphic context, mode in which drawing can be done
-static EGLConfig config; // Graphic config
-static uint64_t baseTime; // Base time measure for hi-res timer
-static bool windowShouldClose = false; // Flag to set window for closing
-#endif
-
-#if defined(PLATFORM_OCULUS)
-// OVR device variables
-static ovrSession session;
-static ovrHmdDesc hmdDesc;
-static ovrGraphicsLuid luid;
-static OculusLayer layer;
-static OculusBuffer buffer;
-static OculusMirror mirror;
-static unsigned int frameIndex = 0;
-#endif
-
-static unsigned int displayWidth, displayHeight; // Display width and height (monitor, device-screen, LCD, ...)
-static int screenWidth, screenHeight; // Screen width and height (used render area)
-static int renderWidth, renderHeight; // Framebuffer width and height (render area)
- // NOTE: Framebuffer could include black bars
+static EGLDisplay display; // Native display device (physical screen connection)
+static EGLSurface surface; // Surface to draw on, framebuffers (connected to context)
+static EGLContext context; // Graphic context, mode in which drawing can be done
+static EGLConfig config; // Graphic config
+static uint64_t baseTime; // Base time measure for hi-res timer
+static bool windowShouldClose = false; // Flag to set window for closing
+#endif
+// Display size-related data
+static unsigned int displayWidth, displayHeight; // Display width and height (monitor, device-screen, LCD, ...)
+static int screenWidth, screenHeight; // Screen width and height (used render area)
+static int renderWidth, renderHeight; // Framebuffer width and height (render area, including black bars if required)
static int renderOffsetX = 0; // Offset X from render area (must be divided by 2)
static int renderOffsetY = 0; // Offset Y from render area (must be divided by 2)
static bool fullscreen = false; // Fullscreen mode (useful only for PLATFORM_DESKTOP)
static Matrix downscaleView; // Matrix to downscale view (in case screen size bigger than display size)
-static Matrix cameraView; // Store camera view matrix (required for Oculus Rift)
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) || defined(PLATFORM_WEB)
static const char *windowTitle; // Window text title...
@@ -262,7 +213,7 @@ static bool cursorHidden; // Track if cursor is hidden
#endif
static Vector2 mousePosition; // Mouse position on screen
-static Vector2 touchPosition[MAX_TOUCH_POINTS]; // Touch position on screen
+static Vector2 touchPosition[MAX_TOUCH_POINTS]; // Touch position on screen
#if defined(PLATFORM_DESKTOP)
static char **dropFilesPath; // Store dropped files paths as strings
@@ -274,20 +225,19 @@ static double updateTime, drawTime; // Time measures for update and draw
static double frameTime; // Time measure for one frame
static double targetTime = 0.0; // Desired time for one frame, if 0 not applied
-static char configFlags = 0; // Configuration flags (bit based)
+static char configFlags = 0; // Configuration flags (bit based)
static bool showLogo = false; // Track if showing logo at init is enabled
//----------------------------------------------------------------------------------
// Other Modules Functions Declaration (required by core)
//----------------------------------------------------------------------------------
-extern void LoadDefaultFont(void); // [Module: text] Loads default font on InitWindow()
-extern void UnloadDefaultFont(void); // [Module: text] Unloads default font from GPU memory
+extern void LoadDefaultFont(void); // [Module: text] Loads default font on InitWindow()
+extern void UnloadDefaultFont(void); // [Module: text] Unloads default font from GPU memory
//----------------------------------------------------------------------------------
// Module specific Functions Declaration
//----------------------------------------------------------------------------------
-static void InitDisplay(int width, int height); // Initialize display device and framebuffer
-static void InitGraphics(void); // Initialize OpenGL graphics
+static void InitGraphicsDevice(int width, int height); // Initialize graphics device
static void SetupFramebufferSize(int displayWidth, int displayHeight);
static void InitTimer(void); // Initialize timer
static double GetTime(void); // Returns time since InitTimer() was run
@@ -336,19 +286,6 @@ static void InitGamepad(void); // Init raw gamepad inpu
static void *GamepadThread(void *arg); // Mouse reading thread
#endif
-#if defined(PLATFORM_OCULUS)
-// Oculus Rift functions
-static Matrix FromOvrMatrix(ovrMatrix4f ovrM);
-static OculusBuffer LoadOculusBuffer(ovrSession session, int width, int height);
-static void UnloadOculusBuffer(ovrSession session, OculusBuffer buffer);
-static void SetOculusBuffer(ovrSession session, OculusBuffer buffer);
-static void UnsetOculusBuffer(OculusBuffer buffer);
-static OculusMirror LoadOculusMirror(ovrSession session, int width, int height); // Load Oculus mirror buffers
-static void UnloadOculusMirror(ovrSession session, OculusMirror mirror); // Unload Oculus mirror buffers
-static void BlitOculusMirror(ovrSession session, OculusMirror mirror);
-static OculusLayer InitOculusLayer(ovrSession session);
-#endif
-
//----------------------------------------------------------------------------------
// Module Functions Definition - Window and OpenGL Context Functions
//----------------------------------------------------------------------------------
@@ -361,11 +298,8 @@ void InitWindow(int width, int height, const char *title)
// Store window title (could be useful...)
windowTitle = title;
- // Init device display (monitor, LCD, ...)
- InitDisplay(width, height);
-
- // Init OpenGL graphics
- InitGraphics();
+ // Init graphics device (display device and OpenGL context)
+ InitGraphicsDevice(width, height);
// Load default font for convenience
// NOTE: External function (defined in module: text)
@@ -397,11 +331,6 @@ void InitWindow(int width, int height, const char *title)
//emscripten_set_gamepaddisconnected_callback(NULL, 1, EmscriptenInputCallback);
#endif
-#if defined(PLATFORM_OCULUS)
- // Recenter OVR tracking origin
- ovr_RecenterTrackingOrigin(session);
-#endif
-
mousePosition.x = (float)screenWidth/2.0f;
mousePosition.y = (float)screenHeight/2.0f;
@@ -510,74 +439,20 @@ void CloseWindow(void)
eglTerminate(display);
display = EGL_NO_DISPLAY;
- }
+ }
#endif
-#if defined(PLATFORM_OCULUS)
- ovr_Destroy(session); // Must be called after glfwTerminate()
- ovr_Shutdown();
+#if defined(PLATFORM_RPI)
+ // Wait for mouse and gamepad threads to finish before closing
+ // NOTE: Those threads should already have finished at this point
+ // because they are controlled by windowShouldClose variable
+ pthread_join(mouseThreadId, NULL);
+ pthread_join(gamepadThreadId, NULL);
#endif
TraceLog(INFO, "Window closed successfully");
}
-#if defined(PLATFORM_OCULUS)
-// Init Oculus Rift device
-// NOTE: Device initialization should be done before window creation?
-void InitOculusDevice(void)
-{
- ovrResult result = ovr_Initialize(NULL);
- if (OVR_FAILURE(result)) TraceLog(ERROR, "OVR: Could not initialize Oculus device");
-
- result = ovr_Create(&session, &luid);
- if (OVR_FAILURE(result))
- {
- TraceLog(WARNING, "OVR: Could not create Oculus session");
- ovr_Shutdown();
- }
-
- hmdDesc = ovr_GetHmdDesc(session);
-
- TraceLog(INFO, "OVR: Product Name: %s", hmdDesc.ProductName);
- TraceLog(INFO, "OVR: Manufacturer: %s", hmdDesc.Manufacturer);
- TraceLog(INFO, "OVR: Product ID: %i", hmdDesc.ProductId);
- TraceLog(INFO, "OVR: Product Type: %i", hmdDesc.Type);
- TraceLog(INFO, "OVR: Serian Number: %s", hmdDesc.SerialNumber);
- TraceLog(INFO, "OVR: Resolution: %ix%i", hmdDesc.Resolution.w, hmdDesc.Resolution.h);
-
- screenWidth = hmdDesc.Resolution.w/2;
- screenHeight = hmdDesc.Resolution.h/2;
-
- // Initialize Oculus Buffers
- layer = InitOculusLayer(session);
- buffer = LoadOculusBuffer(session, layer.width, layer.height);
- mirror = LoadOculusMirror(session, hmdDesc.Resolution.w/2, hmdDesc.Resolution.h/2);
- layer.eyeLayer.ColorTexture[0] = buffer.textureChain; //SetOculusLayerTexture(eyeLayer, buffer.textureChain);
-}
-
-// Close Oculus Rift device
-void CloseOculusDevice(void)
-{
- UnloadOculusMirror(session, mirror); // Unload Oculus mirror buffer
- UnloadOculusBuffer(session, buffer); // Unload Oculus texture buffers
-
- ovr_Destroy(session); // Must be called after glfwTerminate() --> REALLY???
- ovr_Shutdown();
-}
-
-// Update Oculus Rift tracking (position and orientation)
-void UpdateOculusTracking(void)
-{
- frameIndex++;
-
- ovrPosef eyePoses[2];
- ovr_GetEyePoses(session, frameIndex, ovrTrue, layer.viewScaleDesc.HmdToEyeOffset, eyePoses, &layer.eyeLayer.SensorSampleTime);
-
- layer.eyeLayer.RenderPose[0] = eyePoses[0];
- layer.eyeLayer.RenderPose[1] = eyePoses[1];
-}
-#endif
-
// Detect if KEY_ESCAPE pressed or Close icon pressed
bool WindowShouldClose(void)
{
@@ -604,16 +479,14 @@ bool IsWindowMinimized(void)
}
// Fullscreen toggle
-// TODO: When destroying window context is lost and resources too, take care!
void ToggleFullscreen(void)
{
#if defined(PLATFORM_DESKTOP)
fullscreen = !fullscreen; // Toggle fullscreen flag
- rlglClose(); // De-init rlgl
- glfwDestroyWindow(window); // Destroy the current window (we will recreate it!)
-
- InitWindow(screenWidth, screenHeight, windowTitle);
+ // NOTE: glfwSetWindowMonitor() doesn't work properly (bugs)
+ if (fullscreen) glfwSetWindowMonitor(window, glfwGetPrimaryMonitor(), 0, 0, screenWidth, screenHeight, GLFW_DONT_CARE);
+ else glfwSetWindowMonitor(window, NULL, 0, 0, screenWidth, screenHeight, GLFW_DONT_CARE);
#endif
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI)
@@ -647,18 +520,6 @@ void BeginDrawing(void)
updateTime = currentTime - previousTime;
previousTime = currentTime;
-#if defined(PLATFORM_OCULUS)
- frameIndex++;
-
- ovrPosef eyePoses[2];
- ovr_GetEyePoses(session, frameIndex, ovrTrue, layer.viewScaleDesc.HmdToEyeOffset, eyePoses, &layer.eyeLayer.SensorSampleTime);
-
- layer.eyeLayer.RenderPose[0] = eyePoses[0];
- layer.eyeLayer.RenderPose[1] = eyePoses[1];
-
- SetOculusBuffer(session, buffer);
-#endif
-
rlClearScreenBuffers(); // Clear current framebuffers
rlLoadIdentity(); // Reset current matrix (MODELVIEW)
rlMultMatrixf(MatrixToFloat(downscaleView)); // If downscale required, apply it here
@@ -670,49 +531,7 @@ void BeginDrawing(void)
// End canvas drawing and Swap Buffers (Double Buffering)
void EndDrawing(void)
{
-#if defined(PLATFORM_OCULUS)
- for (int eye = 0; eye < 2; eye++)
- {
- 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 eyeRPose = (Quaternion){ layer.eyeLayer.RenderPose[eye].Orientation.x,
- layer.eyeLayer.RenderPose[eye].Orientation.y,
- layer.eyeLayer.RenderPose[eye].Orientation.z,
- layer.eyeLayer.RenderPose[eye].Orientation.w };
- QuaternionInvert(&eyeRPose);
- Matrix eyeOrientation = QuaternionToMatrix(eyeRPose);
- Matrix eyeTranslation = MatrixTranslate(-layer.eyeLayer.RenderPose[eye].Position.x,
- -layer.eyeLayer.RenderPose[eye].Position.y,
- -layer.eyeLayer.RenderPose[eye].Position.z);
-
- Matrix eyeView = MatrixMultiply(eyeTranslation, eyeOrientation);
- Matrix modelEyeView = MatrixMultiply(cameraView, eyeView); // Using internal camera modelview matrix
-
- SetMatrixModelview(modelEyeView);
- SetMatrixProjection(layer.eyeProjections[eye]);
-#endif
-
- rlglDraw(); // Draw Buffers (Only OpenGL 3+ and ES2)
-
-#if defined(PLATFORM_OCULUS)
- }
-
- UnsetOculusBuffer(buffer);
-
- ovr_CommitTextureSwapChain(session, buffer.textureChain);
-
- ovrLayerHeader *layers = &layer.eyeLayer.Header;
- ovr_SubmitFrame(session, frameIndex, &layer.viewScaleDesc, &layers, 1);
-
- // Blit mirror texture to back buffer
- BlitOculusMirror(session, mirror);
-
- // Get session status information
- ovrSessionStatus sessionStatus;
- ovr_GetSessionStatus(session, &sessionStatus);
- if (sessionStatus.ShouldQuit) TraceLog(WARNING, "OVR: Session should quit...");
- if (sessionStatus.ShouldRecenter) ovr_RecenterTrackingOrigin(session);
-#endif
+ rlglDraw(); // Draw Buffers (Only OpenGL 3+ and ES2)
SwapBuffers(); // Copy back buffer to front buffer
PollInputEvents(); // Poll user events
@@ -766,6 +585,8 @@ void End2dMode(void)
void Begin3dMode(Camera camera)
{
rlglDraw(); // Draw Buffers (Only OpenGL 3+ and ES2)
+
+ if (IsVrDeviceReady()) BeginVrDrawing();
rlMatrixMode(RL_PROJECTION); // Switch to projection matrix
@@ -784,7 +605,7 @@ void Begin3dMode(Camera camera)
rlLoadIdentity(); // Reset current matrix (MODELVIEW)
// Setup Camera view
- cameraView = MatrixLookAt(camera.position, camera.target, camera.up);
+ Matrix cameraView = MatrixLookAt(camera.position, camera.target, camera.up);
rlMultMatrixf(MatrixToFloat(cameraView)); // Multiply MODELVIEW matrix by view matrix (camera)
rlEnableDepthTest(); // Enable DEPTH_TEST for 3D
@@ -792,8 +613,10 @@ void Begin3dMode(Camera camera)
// Ends 3D mode and returns to default 2D orthographic mode
void End3dMode(void)
-{
- rlglDraw(); // Draw Buffers (Only OpenGL 3+ and ES2)
+{
+ rlglDraw(); // Process internal buffers (update + draw)
+
+ if (IsVrDeviceReady()) EndVrDrawing();
rlMatrixMode(RL_PROJECTION); // Switch to projection matrix
rlPopMatrix(); // Restore previous matrix (PROJECTION) from matrix stack
@@ -1615,7 +1438,7 @@ bool IsButtonReleased(int button)
// Initialize display device and framebuffer
// NOTE: width and height represent the screen (framebuffer) desired size, not actual display size
// If width or height are 0, default display size will be used for framebuffer size
-static void InitDisplay(int width, int height)
+static void InitGraphicsDevice(int width, int height)
{
screenWidth = width; // User desired width
screenHeight = height; // User desired height
@@ -1625,31 +1448,7 @@ static void InitDisplay(int width, int height)
// Downscale matrix is required in case desired screen area is bigger than display area
downscaleView = MatrixIdentity();
-
-#if defined(PLATFORM_OCULUS)
- ovrResult result = ovr_Initialize(NULL);
- if (OVR_FAILURE(result)) TraceLog(ERROR, "OVR: Could not initialize Oculus device");
- result = ovr_Create(&session, &luid);
- if (OVR_FAILURE(result))
- {
- TraceLog(WARNING, "OVR: Could not create Oculus session");
- ovr_Shutdown();
- }
-
- hmdDesc = ovr_GetHmdDesc(session);
-
- TraceLog(INFO, "OVR: Product Name: %s", hmdDesc.ProductName);
- TraceLog(INFO, "OVR: Manufacturer: %s", hmdDesc.Manufacturer);
- TraceLog(INFO, "OVR: Product ID: %i", hmdDesc.ProductId);
- TraceLog(INFO, "OVR: Product Type: %i", hmdDesc.Type);
- TraceLog(INFO, "OVR: Serian Number: %s", hmdDesc.SerialNumber);
- TraceLog(INFO, "OVR: Resolution: %ix%i", hmdDesc.Resolution.w, hmdDesc.Resolution.h);
-
- screenWidth = hmdDesc.Resolution.w/2;
- screenHeight = hmdDesc.Resolution.h/2;
-#endif
-
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
glfwSetErrorCallback(ErrorCallback);
@@ -1686,16 +1485,21 @@ static void InitDisplay(int width, int height)
// NOTE: When asking for an OpenGL context version, most drivers provide highest supported version
// with forward compatibility to older OpenGL versions.
// For example, if using OpenGL 1.1, driver can provide a 3.3 context fordward compatible.
-
- // Check selection OpenGL version (not initialized yet!)
- if (rlGetVersion() == OPENGL_33)
+
+ if (configFlags & FLAG_MSAA_4X_HINT)
{
- if (configFlags & FLAG_MSAA_4X_HINT)
- {
- glfwWindowHint(GLFW_SAMPLES, 4); // Enables multisampling x4 (MSAA), default is 0
- TraceLog(INFO, "Trying to enable MSAA x4");
- }
+ glfwWindowHint(GLFW_SAMPLES, 4); // Enables multisampling x4 (MSAA), default is 0
+ TraceLog(INFO, "Trying to enable MSAA x4");
+ }
+ // Check selection OpenGL version
+ if (rlGetVersion() == OPENGL_21)
+ {
+ glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); // Choose OpenGL major version (just hint)
+ glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); // Choose OpenGL minor version (just hint)
+ }
+ else if (rlGetVersion() == OPENGL_33)
+ {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // Choose OpenGL major version (just hint)
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); // Choose OpenGL minor version (just hint)
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // Profiles Hint: Only 3.3 and above!
@@ -1710,25 +1514,40 @@ static void InitDisplay(int width, int height)
if (fullscreen)
{
- // At this point we need to manage render size vs screen size
- // NOTE: This function uses and modifies global module variables:
- // screenWidth/screenHeight - renderWidth/renderHeight - downscaleView
- SetupFramebufferSize(displayWidth, displayHeight);
-
- // TODO: SetupFramebufferSize() does not consider properly display video modes.
- // It setups a renderWidth/renderHeight with black bars that could not match a valid video mode,
- // and so, framebuffer is not scaled properly to some monitors.
-
+ // Obtain recommended displayWidth/displayHeight from a valid videomode for the monitor
int count;
const GLFWvidmode *modes = glfwGetVideoModes(glfwGetPrimaryMonitor(), &count);
+ // Get closest videomode to desired screenWidth/screenHeight
for (int i = 0; i < count; i++)
{
- // TODO: Check modes[i]->width;
- // TODO: Check modes[i]->height;
+ if (modes[i].width >= screenWidth)
+ {
+ if (modes[i].height >= screenHeight)
+ {
+ displayWidth = modes[i].width;
+ displayHeight = modes[i].height;
+ break;
+ }
+ }
}
- window = glfwCreateWindow(screenWidth, screenHeight, windowTitle, glfwGetPrimaryMonitor(), NULL);
+ TraceLog(WARNING, "Closest fullscreen videomode: %i x %i", displayWidth, displayHeight);
+
+ // NOTE: ISSUE: Closest videomode could not match monitor aspect-ratio, for example,
+ // for a desired screen size of 800x450 (16:9), closest supported videomode is 800x600 (4:3),
+ // framebuffer is rendered correctly but once displayed on a 16:9 monitor, it gets stretched
+ // by the sides to fit all monitor space...
+
+ // At this point we need to manage render size vs screen size
+ // NOTE: This function uses and modifies global module variables:
+ // screenWidth/screenHeight - renderWidth/renderHeight - downscaleView
+ SetupFramebufferSize(displayWidth, displayHeight);
+
+ window = glfwCreateWindow(displayWidth, displayHeight, windowTitle, glfwGetPrimaryMonitor(), NULL);
+
+ // NOTE: Full-screen change, not working properly...
+ //glfwSetWindowMonitor(window, glfwGetPrimaryMonitor(), 0, 0, screenWidth, screenHeight, GLFW_DONT_CARE);
}
else
{
@@ -1778,24 +1597,12 @@ static void InitDisplay(int width, int height)
#endif
glfwMakeContextCurrent(window);
-#if defined(PLATFORM_OCULUS)
- glfwSwapInterval(0);
-#endif
+ glfwSwapInterval(0); // Disable VSync by default
#if defined(PLATFORM_DESKTOP)
- // Load OpenGL 3.3 extensions using GLAD
- if (rlGetVersion() == OPENGL_33)
- {
- // NOTE: glad is generated and contains only required OpenGL 3.3 Core extensions
- if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) TraceLog(WARNING, "GLAD: Cannot load OpenGL extensions");
- else TraceLog(INFO, "GLAD: OpenGL extensions loaded successfully");
-
- if (GLAD_GL_VERSION_3_3) TraceLog(INFO, "OpenGL 3.3 Core profile supported");
- else TraceLog(ERROR, "OpenGL 3.3 Core profile not supported");
-
- // With GLAD, we can check if an extension is supported using the GLAD_GL_xxx booleans
- //if (GLAD_GL_ARB_vertex_array_object) // Use GL_ARB_vertex_array_object
- }
+ // Load OpenGL 3.3 extensions
+ // NOTE: GLFW loader function is passed as parameter
+ rlglLoadExtensions(glfwGetProcAddress);
#endif
// Enables GPU v-sync, so frames are not limited to screen refresh rate (60Hz -> 60 FPS)
@@ -1956,37 +1763,33 @@ static void InitDisplay(int width, int height)
TraceLog(INFO, "Viewport offsets: %i, %i", renderOffsetX, renderOffsetY);
}
#endif // defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI)
-}
-// Initialize OpenGL graphics
-static void InitGraphics(void)
-{
- rlglInit(); // Init rlgl
- rlglInitGraphics(renderOffsetX, renderOffsetY, renderWidth, renderHeight); // Init graphics (OpenGL stuff)
+ // Initialize OpenGL context (states and resources)
+ rlglInit(screenWidth, screenHeight);
+
+ // Initialize screen viewport (area of the screen that you will actually draw to)
+ // NOTE: Viewport must be recalculated if screen is resized
+ rlViewport(renderOffsetX/2, renderOffsetY/2, renderWidth - renderOffsetX, renderHeight - renderOffsetY);
-#if defined(PLATFORM_OCULUS)
- // Initialize Oculus Buffers
- layer = InitOculusLayer(session);
- buffer = LoadOculusBuffer(session, layer.width, layer.height);
- mirror = LoadOculusMirror(session, hmdDesc.Resolution.w/2, hmdDesc.Resolution.h/2);
- layer.eyeLayer.ColorTexture[0] = buffer.textureChain; //SetOculusLayerTexture(eyeLayer, buffer.textureChain);
-#endif
+ // Initialize internal projection and modelview matrices
+ // NOTE: Default to orthographic projection mode with top-left corner at (0,0)
+ rlMatrixMode(RL_PROJECTION); // Switch to PROJECTION matrix
+ rlLoadIdentity(); // Reset current matrix (PROJECTION)
+ rlOrtho(0, renderWidth - renderOffsetX, renderHeight - renderOffsetY, 0, 0.0f, 1.0f);
+ rlMatrixMode(RL_MODELVIEW); // Switch back to MODELVIEW matrix
+ rlLoadIdentity(); // Reset current matrix (MODELVIEW)
ClearBackground(RAYWHITE); // Default background color for raylib games :P
#if defined(PLATFORM_ANDROID)
- windowReady = true; // IMPORTANT!
+ windowReady = true; // IMPORTANT!
#endif
}
// Compute framebuffer size relative to screen size and display size
-// NOTE: Global variables renderWidth/renderHeight can be modified
+// NOTE: Global variables renderWidth/renderHeight and renderOffsetX/renderOffsetY can be modified
static void SetupFramebufferSize(int displayWidth, int displayHeight)
-{
- // TODO: SetupFramebufferSize() does not consider properly display video modes.
- // It setups a renderWidth/renderHeight with black bars that could not match a valid video mode,
- // and so, framebuffer is not scaled properly to some monitors.
-
+{
// Calculate renderWidth and renderHeight, we have the display size (input params) and the desired screen size (global var)
if ((screenWidth > displayWidth) || (screenHeight > displayHeight))
{
@@ -2335,8 +2138,14 @@ static void CursorEnterCallback(GLFWwindow *window, int enter)
// NOTE: Window resizing not allowed by default
static void WindowSizeCallback(GLFWwindow *window, int width, int height)
{
- // If window is resized, graphics device is re-initialized (but only ortho mode)
- rlglInitGraphics(0, 0, width, height);
+ // If window is resized, viewport and projection matrix needs to be re-calculated
+ rlViewport(0, 0, width, height); // Set viewport width and height
+ rlMatrixMode(RL_PROJECTION); // Switch to PROJECTION matrix
+ rlLoadIdentity(); // Reset current matrix (PROJECTION)
+ rlOrtho(0, width, height, 0, 0.0f, 1.0f); // Orthographic projection mode with top-left corner at (0,0)
+ rlMatrixMode(RL_MODELVIEW); // Switch back to MODELVIEW matrix
+ rlLoadIdentity(); // Reset current matrix (MODELVIEW)
+ rlClearScreenBuffers(); // Clear screen buffers (color and depth)
// Window size must be updated to be used on 3D mode to get new aspect ratio (Begin3dMode())
screenWidth = width;
@@ -2345,9 +2154,6 @@ static void WindowSizeCallback(GLFWwindow *window, int width, int height)
renderHeight = height;
// NOTE: Postprocessing texture is not scaled to new size
-
- // Background must be also re-cleared
- ClearBackground(RAYWHITE);
}
// GLFW3 WindowIconify Callback, runs when window is minimized/restored
@@ -2414,11 +2220,8 @@ static void AndroidCommandCallback(struct android_app *app, int32_t cmd)
}
else
{
- // Init device display (monitor, LCD, ...)
- InitDisplay(screenWidth, screenHeight);
-
- // Init OpenGL graphics
- InitGraphics();
+ // Init graphics device (display device and OpenGL context)
+ InitGraphicsDevice(screenWidth, screenHeight);
// Load default font for convenience
// NOTE: External function (defined in module: text)
@@ -2871,7 +2674,7 @@ static void *MouseThread(void *arg)
int mouseRelX = 0;
int mouseRelY = 0;
- while(1)
+ while (!windowShouldClose)
{
if (read(mouseStream, &mouse, sizeof(MouseEvent)) == (int)sizeof(MouseEvent))
{
@@ -2961,7 +2764,7 @@ static void *GamepadThread(void *arg)
// Read gamepad event
struct js_event gamepadEvent;
- while (1)
+ while (!windowShouldClose)
{
for (int i = 0; i < MAX_GAMEPADS; i++)
{
@@ -2996,215 +2799,7 @@ static void *GamepadThread(void *arg)
return NULL;
}
-#endif
-
-
-#if defined(PLATFORM_OCULUS)
-// Convert from Oculus ovrMatrix4f struct to raymath Matrix struct
-static Matrix FromOvrMatrix(ovrMatrix4f ovrmat)
-{
- Matrix rmat;
-
- rmat.m0 = ovrmat.M[0][0];
- rmat.m1 = ovrmat.M[1][0];
- rmat.m2 = ovrmat.M[2][0];
- rmat.m3 = ovrmat.M[3][0];
- rmat.m4 = ovrmat.M[0][1];
- rmat.m5 = ovrmat.M[1][1];
- rmat.m6 = ovrmat.M[2][1];
- rmat.m7 = ovrmat.M[3][1];
- rmat.m8 = ovrmat.M[0][2];
- rmat.m9 = ovrmat.M[1][2];
- rmat.m10 = ovrmat.M[2][2];
- rmat.m11 = ovrmat.M[3][2];
- rmat.m12 = ovrmat.M[0][3];
- rmat.m13 = ovrmat.M[1][3];
- rmat.m14 = ovrmat.M[2][3];
- rmat.m15 = ovrmat.M[3][3];
-
- MatrixTranspose(&rmat);
-
- return rmat;
-}
-
-// Load Oculus required buffers: texture-swap-chain, fbo, texture-depth
-static OculusBuffer LoadOculusBuffer(ovrSession session, int width, int height)
-{
- OculusBuffer buffer;
- buffer.width = width;
- buffer.height = height;
-
- // Create OVR texture chain
- ovrTextureSwapChainDesc desc = {};
- desc.Type = ovrTexture_2D;
- desc.ArraySize = 1;
- desc.Width = width;
- desc.Height = height;
- desc.MipLevels = 1;
- desc.Format = OVR_FORMAT_R8G8B8A8_UNORM_SRGB; // Requires glEnable(GL_FRAMEBUFFER_SRGB);
- desc.SampleCount = 1;
- desc.StaticImage = ovrFalse;
-
- ovrResult result = ovr_CreateTextureSwapChainGL(session, &desc, &buffer.textureChain);
-
- if (!OVR_SUCCESS(result)) TraceLog(WARNING, "OVR: Failed to create swap textures buffer");
-
- int textureCount = 0;
- ovr_GetTextureSwapChainLength(session, buffer.textureChain, &textureCount);
-
- if (!OVR_SUCCESS(result) || !textureCount) TraceLog(WARNING, "OVR: Unable to count swap chain textures");
-
- for (int i = 0; i < textureCount; ++i)
- {
- GLuint chainTexId;
- ovr_GetTextureSwapChainBufferGL(session, buffer.textureChain, i, &chainTexId);
- glBindTexture(GL_TEXTURE_2D, chainTexId);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- }
-
- glBindTexture(GL_TEXTURE_2D, 0);
-
- /*
- // Setup framebuffer object (using depth texture)
- glGenFramebuffers(1, &buffer.fboId);
- glGenTextures(1, &buffer.depthId);
- glBindTexture(GL_TEXTURE_2D, buffer.depthId);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, buffer.width, buffer.height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL);
- */
-
- // Setup framebuffer object (using depth renderbuffer)
- glGenFramebuffers(1, &buffer.fboId);
- glGenRenderbuffers(1, &buffer.depthId);
- glBindFramebuffer(GL_DRAW_FRAMEBUFFER, buffer.fboId);
- glBindRenderbuffer(GL_RENDERBUFFER, buffer.depthId);
- glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, buffer.width, buffer.height);
- glBindRenderbuffer(GL_RENDERBUFFER, 0);
- glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, buffer.depthId);
- glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
-
- return buffer;
-}
-
-// Unload texture required buffers
-static void UnloadOculusBuffer(ovrSession session, OculusBuffer buffer)
-{
- if (buffer.textureChain)
- {
- ovr_DestroyTextureSwapChain(session, buffer.textureChain);
- buffer.textureChain = NULL;
- }
-
- if (buffer.depthId != 0) glDeleteTextures(1, &buffer.depthId);
- if (buffer.fboId != 0) glDeleteFramebuffers(1, &buffer.fboId);
-}
-
-// Set current Oculus buffer
-static void SetOculusBuffer(ovrSession session, OculusBuffer buffer)
-{
- GLuint currentTexId;
- int currentIndex;
-
- ovr_GetTextureSwapChainCurrentIndex(session, buffer.textureChain, &currentIndex);
- ovr_GetTextureSwapChainBufferGL(session, buffer.textureChain, currentIndex, &currentTexId);
-
- 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
-
- //glViewport(0, 0, buffer.width, buffer.height); // Useful if rendering to separate framebuffers (every eye)
- //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
- // Required if OculusBuffer format is OVR_FORMAT_R8G8B8A8_UNORM_SRGB
- glEnable(GL_FRAMEBUFFER_SRGB);
-}
-
-// Unset Oculus buffer
-static void UnsetOculusBuffer(OculusBuffer buffer)
-{
- glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
- glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
-}
-
-// Load Oculus mirror buffers
-static OculusMirror LoadOculusMirror(ovrSession session, int width, int height)
-{
- OculusMirror mirror;
- mirror.width = width;
- mirror.height = height;
-
- ovrMirrorTextureDesc mirrorDesc;
- memset(&mirrorDesc, 0, sizeof(mirrorDesc));
- mirrorDesc.Format = OVR_FORMAT_R8G8B8A8_UNORM_SRGB;
- mirrorDesc.Width = mirror.width;
- mirrorDesc.Height = mirror.height;
-
- if (!OVR_SUCCESS(ovr_CreateMirrorTextureGL(session, &mirrorDesc, &mirror.texture))) TraceLog(WARNING, "Could not create mirror texture");
-
- glGenFramebuffers(1, &mirror.fboId);
-
- return mirror;
-}
-
-// Unload Oculus mirror buffers
-static void UnloadOculusMirror(ovrSession session, OculusMirror mirror)
-{
- if (mirror.fboId != 0) glDeleteFramebuffers(1, &mirror.fboId);
- if (mirror.texture) ovr_DestroyMirrorTexture(session, mirror.texture);
-}
-
-static void BlitOculusMirror(ovrSession session, OculusMirror mirror)
-{
- GLuint mirrorTextureId;
-
- ovr_GetMirrorTextureBufferGL(session, mirror.texture, &mirrorTextureId);
-
- glBindFramebuffer(GL_READ_FRAMEBUFFER, mirror.fboId);
- glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mirrorTextureId, 0);
- glBlitFramebuffer(0, 0, mirror.width, mirror.height, 0, mirror.height, mirror.width, 0, GL_COLOR_BUFFER_BIT, GL_NEAREST);
- glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
-}
-
-// Requires: session, hmdDesc
-static OculusLayer InitOculusLayer(ovrSession session)
-{
- OculusLayer layer = { 0 };
-
- layer.viewScaleDesc.HmdSpaceToWorldScaleInMeters = 1.0f;
-
- memset(&layer.eyeLayer, 0, sizeof(ovrLayerEyeFov));
- layer.eyeLayer.Header.Type = ovrLayerType_EyeFov;
- layer.eyeLayer.Header.Flags = ovrLayerFlag_TextureOriginAtBottomLeft;
-
- ovrEyeRenderDesc eyeRenderDescs[2];
-
- for (int eye = 0; eye < 2; eye++)
- {
- eyeRenderDescs[eye] = ovr_GetRenderDesc(session, eye, hmdDesc.DefaultEyeFov[eye]);
- ovrMatrix4f ovrPerspectiveProjection = ovrMatrix4f_Projection(eyeRenderDescs[eye].Fov, 0.01f, 10000.0f, ovrProjection_None); //ovrProjection_ClipRangeOpenGL);
- layer.eyeProjections[eye] = FromOvrMatrix(ovrPerspectiveProjection); // NOTE: struct ovrMatrix4f { float M[4][4] } --> struct Matrix
-
- layer.viewScaleDesc.HmdToEyeOffset[eye] = eyeRenderDescs[eye].HmdToEyeOffset;
- layer.eyeLayer.Fov[eye] = eyeRenderDescs[eye].Fov;
-
- ovrSizei eyeSize = ovr_GetFovTextureSize(session, eye, layer.eyeLayer.Fov[eye], 1.0f);
- layer.eyeLayer.Viewport[eye].Size = eyeSize;
- layer.eyeLayer.Viewport[eye].Pos.x = layer.width;
- layer.eyeLayer.Viewport[eye].Pos.y = 0;
-
- layer.height = eyeSize.h; //std::max(renderTargetSize.y, (uint32_t)eyeSize.h);
- layer.width += eyeSize.w;
- }
-
- return layer;
-}
-#endif
+#endif // PLATFORM_RPI
// Plays raylib logo appearing animation
static void LogoAnimation(void)