diff --git a/examples/shaders_custom_uniform.c b/examples/shaders_custom_uniform.c index 32dd7ff1c..740771439 100644 --- a/examples/shaders_custom_uniform.c +++ b/examples/shaders_custom_uniform.c @@ -89,6 +89,8 @@ int main() DrawGrid(10, 1.0f); // Draw a grid End3dMode(); + + DrawText("TEXT DRAWN IN RENDER TEXTURE", 200, 10, 30, RED); EndTextureMode(); // End drawing to texture (now we have a texture available for next passes) diff --git a/src/core.c b/src/core.c index a94ad48d0..08f9a7e23 100644 --- a/src/core.c +++ b/src/core.c @@ -562,9 +562,8 @@ void Begin2dMode(Camera2D camera) Matrix matOrigin = MatrixTranslate(-camera.target.x, -camera.target.y, 0.0f); Matrix matRotation = MatrixRotate((Vector3){ 0.0f, 0.0f, 1.0f }, camera.rotation*DEG2RAD); Matrix matScale = MatrixScale(camera.zoom, camera.zoom, 1.0f); - Matrix matTranslation = MatrixTranslate(camera.offset.x + camera.target.x, camera.offset.y + camera.target.y, 0.0f); - + Matrix matTransform = MatrixMultiply(MatrixMultiply(matOrigin, MatrixMultiply(matScale, matRotation)), matTranslation); rlMultMatrixf(MatrixToFloat(matTransform)); @@ -627,11 +626,24 @@ void BeginTextureMode(RenderTexture2D target) { rlglDraw(); // Draw Buffers (Only OpenGL 3+ and ES2) - rlEnableRenderTexture(target.id); - - rlClearScreenBuffers(); // Clear render texture buffers + rlEnableRenderTexture(target.id); // Enable render target + rlClearScreenBuffers(); // Clear render texture buffers + + // Set viewport to framebuffer size + rlViewport(0, 0, target.texture.width, target.texture.height); + + rlMatrixMode(RL_PROJECTION); // Switch to PROJECTION matrix + rlLoadIdentity(); // Reset current matrix (PROJECTION) + + // Set orthographic projection to current framebuffer size + // NOTE: Configured top-left corner as (0, 0) + rlOrtho(0, target.texture.width, target.texture.height, 0, 0.0f, 1.0f); + + rlMatrixMode(RL_MODELVIEW); // Switch back to MODELVIEW matrix rlLoadIdentity(); // Reset current matrix (MODELVIEW) + + //rlScalef(0.0f, -1.0f, 0.0f); // Flip Y-drawing (?) } // Ends drawing to render texture @@ -639,7 +651,21 @@ void EndTextureMode(void) { rlglDraw(); // Draw Buffers (Only OpenGL 3+ and ES2) - rlDisableRenderTexture(); + rlDisableRenderTexture(); // Disable render target + + // Set viewport to default framebuffer size (screen size) + // TODO: consider possible viewport offsets + rlViewport(0, 0, GetScreenWidth(), GetScreenHeight()); + + rlMatrixMode(RL_PROJECTION); // Switch to PROJECTION matrix + rlLoadIdentity(); // Reset current matrix (PROJECTION) + + // Set orthographic projection to current framebuffer size + // NOTE: Configured top-left corner as (0, 0) + rlOrtho(0, GetScreenWidth(), GetScreenHeight(), 0, 0.0f, 1.0f); + + rlMatrixMode(RL_MODELVIEW); // Switch back to MODELVIEW matrix + rlLoadIdentity(); // Reset current matrix (MODELVIEW) } // Set target FPS for the game diff --git a/src/rlgl.c b/src/rlgl.c index d319119f4..cc4c4c2f5 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -404,6 +404,12 @@ 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) +void rlViewport(int x, int y, int width, int height) +{ + glViewport(x, y, width, height); +} + //---------------------------------------------------------------------------------- // Module Functions Definition - Vertex level operations //---------------------------------------------------------------------------------- @@ -725,17 +731,25 @@ void rlDisableTexture(void) #endif } +// Enable rendering to texture (fbo) void rlEnableRenderTexture(unsigned int id) { #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) glBindFramebuffer(GL_FRAMEBUFFER, id); + + //glDisable(GL_CULL_FACE); // Allow double side drawing for texture flipping + //glCullFace(GL_FRONT); #endif } +// Disable rendering to texture void rlDisableRenderTexture(void) { #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) glBindFramebuffer(GL_FRAMEBUFFER, 0); + + //glEnable(GL_CULL_FACE); + //glCullFace(GL_BACK); #endif } diff --git a/src/rlgl.h b/src/rlgl.h index fa35dbc63..a3ba6cd5e 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -247,6 +247,7 @@ void rlScalef(float x, float y, float z); // Multiply the current matrix b void rlMultMatrixf(float *mat); // Multiply the current matrix by another matrix void rlFrustum(double left, double right, double bottom, double top, double near, double far); void rlOrtho(double left, double right, double bottom, double top, double near, double far); +void rlViewport(int x, int y, int width, int height); // Set the viewport area //------------------------------------------------------------------------------------ // Functions Declaration - Vertex level operations diff --git a/src/textures.c b/src/textures.c index 79047ab7e..439311f6b 100644 --- a/src/textures.c +++ b/src/textures.c @@ -1385,10 +1385,6 @@ void DrawTextureEx(Texture2D texture, Vector2 position, float rotation, float sc void DrawTextureRec(Texture2D texture, Rectangle sourceRec, Vector2 position, Color tint) { Rectangle destRec = { (int)position.x, (int)position.y, abs(sourceRec.width), abs(sourceRec.height) }; - - if (sourceRec.width < 0) sourceRec.x -= sourceRec.width; - if (sourceRec.height < 0) sourceRec.y -= sourceRec.height; - Vector2 origin = { 0, 0 }; DrawTexturePro(texture, sourceRec, destRec, origin, 0.0f, tint); @@ -1398,6 +1394,9 @@ void DrawTextureRec(Texture2D texture, Rectangle sourceRec, Vector2 position, Co // NOTE: origin is relative to destination rectangle size void DrawTexturePro(Texture2D texture, Rectangle sourceRec, Rectangle destRec, Vector2 origin, float rotation, Color tint) { + if (sourceRec.width < 0) sourceRec.x -= sourceRec.width; + if (sourceRec.height < 0) sourceRec.y -= sourceRec.height; + rlEnableTexture(texture.id); rlPushMatrix();