diff options
| author | raysan5 <raysan5@gmail.com> | 2016-05-29 11:49:13 +0200 |
|---|---|---|
| committer | raysan5 <raysan5@gmail.com> | 2016-05-29 11:49:13 +0200 |
| commit | ea5b00528b0cb1e5cc1e7169a195b75915c8607a (patch) | |
| tree | 293f5c6132973e211c0cdefc006097b67a02774d /src | |
| parent | 27df983ee0ab72917756b95b49fb31319a6d813f (diff) | |
| download | raylib-ea5b00528b0cb1e5cc1e7169a195b75915c8607a.tar.gz raylib-ea5b00528b0cb1e5cc1e7169a195b75915c8607a.zip | |
Improved render to texture
Support render texture size different than screen size
Diffstat (limited to 'src')
| -rw-r--r-- | src/core.c | 36 | ||||
| -rw-r--r-- | src/rlgl.c | 14 | ||||
| -rw-r--r-- | src/rlgl.h | 1 | ||||
| -rw-r--r-- | src/textures.c | 7 |
4 files changed, 49 insertions, 9 deletions
@@ -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); - + 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 @@ -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 } @@ -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 79047ab7..439311f6 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(); |
