From 71ab20229564a4c519ab46ee34ec785846bdbe83 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sat, 25 Jun 2016 23:28:50 +0200 Subject: [PATCH] Removed rlglInitGraphics(), integrated into rlglInit() Redesigned rlgl usage: - rlViewport() must be called by user - Internal projection/modelview matrices must be setup by user --- CHANGELOG | 1 + src/core.c | 28 ++++++++++++---- src/rlgl.c | 97 +++++++++++++++++++++--------------------------------- src/rlgl.h | 1 - 4 files changed, 60 insertions(+), 67 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index debc7ff5d..d8370b61a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -37,6 +37,7 @@ other changes: [rlgl] Improved 2D vs 3D drawing system (lines, triangles, quads) [rlgl] Improved DXT-ETC1 support on HTML5 [rlgl] Review function: rlglUnproject() +[rlgl] Removed function: rlglInitGraphics(), integrated into rlglInit() [rlgl] Updated Mesh and Shader structs [rlgl] Simplified internal (default) dynamic buffers [rlgl] Added support for indexed and dynamic mesh data diff --git a/src/core.c b/src/core.c index 62b916fbb..de632c09f 100644 --- a/src/core.c +++ b/src/core.c @@ -1761,8 +1761,19 @@ static void InitGraphicsDevice(int width, int height) #endif // defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI) // Initialize OpenGL context (states and resources) - rlglInit(); // Init rlgl - rlglInitGraphics(renderOffsetX, renderOffsetY, renderWidth, renderHeight); // Init graphics (OpenGL stuff) + rlglInit(); + + // 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); + + // 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 @@ -2127,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; @@ -2137,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 diff --git a/src/rlgl.c b/src/rlgl.c index 185ea4fc8..99afd681c 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -290,8 +290,7 @@ static int blendMode = 0; // Track current blending mode // White texture useful for plain color polys (required by shader) static unsigned int whiteTexture; -// Default framebuffer size -// NOTE: Updated when calling rlglInitGraphics() +// Default framebuffer size (required by Oculus device) static int screenWidth; // Default framebuffer width static int screenHeight; // Default framebuffer height @@ -481,10 +480,15 @@ void rlOrtho(double left, double right, double bottom, double top, double near, #endif -// Set the viewport area (trasnformation from normalized device coordinates to window coordinates) +// Set the viewport area (transformation from normalized device coordinates to window coordinates) +// NOTE: Updates global variables: screenWidth, screenHeight void rlViewport(int x, int y, int width, int height) { glViewport(x, y, width, height); + + // Store default framebuffer size + screenWidth = width; + screenHeight = height; } //---------------------------------------------------------------------------------- @@ -947,7 +951,7 @@ int rlGetVersion(void) // Module Functions Definition - rlgl Functions //---------------------------------------------------------------------------------- -// Init OpenGL 3.3+ required data +// Initialize rlgl: OpenGL extensions, default buffers/shaders/textures, OpenGL states void rlglInit(void) { // Check OpenGL information and capabilities @@ -1134,7 +1138,37 @@ void rlglInit(void) projection = MatrixIdentity(); modelview = MatrixIdentity(); currentMatrix = &modelview; +#endif // defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) + + // Initialize OpenGL default states + //---------------------------------------------------------- + + // Init state: Depth test + glDepthFunc(GL_LEQUAL); // Type of depth testing to apply + glDisable(GL_DEPTH_TEST); // Disable depth testing for 2D (only used for 3D) + + // Init state: Blending mode + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Color blending function (how colors are mixed) + glEnable(GL_BLEND); // Enable color blending (required to work with transparencies) + + // Init state: Culling + // NOTE: All shapes/models triangles are drawn CCW + glCullFace(GL_BACK); // Cull the back face (default) + glFrontFace(GL_CCW); // Front face are defined counter clockwise (default) + glEnable(GL_CULL_FACE); // Enable backface culling + +#if defined(GRAPHICS_API_OPENGL_11) + // Init state: Color hints (deprecated in OpenGL 3.0+) + glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Improve quality of color and texture coordinate interpolation + glShadeModel(GL_SMOOTH); // Smooth shading between vertex (vertex colors interpolation) #endif + + // Init state: Color/Depth buffers clear + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set clear color (black) + glClearDepth(1.0f); // Set clear depth value (default) + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers (depth buffer required for 3D) + + TraceLog(INFO, "OpenGL default states initialized successfully"); } // Vertex Buffer Object deinitialization (memory free) @@ -1176,61 +1210,6 @@ void rlglDraw(void) #endif } -// Initialize OpenGL states -// NOTE: Stores global variables screenWidth and screenHeight -void rlglInitGraphics(int offsetX, int offsetY, int width, int height) -{ - // Store default framebuffer size - screenWidth = width; - screenHeight = height; - - // Init state: Viewport - // NOTE: Viewport must be recalculated if screen is resized, don't confuse glViewport with the - // transformation matrix, glViewport just defines the area of the context that you will actually draw to. - glViewport(offsetX/2, offsetY/2, width - offsetX, height - offsetY); // Set viewport width and height - - // Init state: Depth Test - glDepthFunc(GL_LEQUAL); // Type of depth testing to apply - glDisable(GL_DEPTH_TEST); // Disable depth testing for 2D (only used for 3D) - - // Init state: Blending mode - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Color blending function (how colors are mixed) - glEnable(GL_BLEND); // Enable color blending (required to work with transparencies) - - // Init state: Culling - // NOTE: All shapes/models triangles are drawn CCW - glCullFace(GL_BACK); // Cull the back face (default) - glFrontFace(GL_CCW); // Front face are defined counter clockwise (default) - glEnable(GL_CULL_FACE); // Enable backface culling - -#if defined(GRAPHICS_API_OPENGL_11) - // Init state: Color hints (deprecated in OpenGL 3.0+) - glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Improve quality of color and texture coordinate interpolation - glShadeModel(GL_SMOOTH); // Smooth shading between vertex (vertex colors interpolation) -#endif - - // Init state: Color/Depth buffers clear - glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set clear color (black) - glClearDepth(1.0f); // Set clear depth value (default) - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers (depth buffer required for 3D) - - // Init state: projection and modelview matrices - // NOTE: Setup global variables: projection and modelview - rlMatrixMode(RL_PROJECTION); // Switch to PROJECTION matrix - rlLoadIdentity(); // Reset current matrix (PROJECTION) - rlOrtho(0, width - offsetX, height - offsetY, 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) - - // NOTE: projection and modelview could be set alternatively using: - //Matrix matOrtho = MatrixOrtho(0, width - offsetX, height - offsetY, 0, 0.0f, 1.0f); - //MatrixTranspose(&matOrtho); - //projection = matOrtho; // Global variable - //modelview = MatrixIdentity(); // Global variable - - TraceLog(INFO, "OpenGL graphic device initialized successfully"); -} - // Load OpenGL extensions // NOTE: External loader function could be passed as a pointer void rlglLoadExtensions(void *loader) diff --git a/src/rlgl.h b/src/rlgl.h index 2bd7a6b41..61d11d664 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -301,7 +301,6 @@ int rlGetVersion(void); // Returns current OpenGL versio void rlglInit(void); // Initialize rlgl (shaders, VAO, VBO...) void rlglClose(void); // De-init rlgl void rlglDraw(void); // Draw VAO/VBO -void rlglInitGraphics(int offsetX, int offsetY, int width, int height); // Initialize Graphics (OpenGL stuff) void rlglLoadExtensions(void *loader); // Load OpenGL extensions void rlglUpdateDefaultBuffers(void); // Update default internal buffers (VAOs/VBOs) with vertex data