aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorraysan5 <raysan5@gmail.com>2016-05-29 11:49:13 +0200
committerraysan5 <raysan5@gmail.com>2016-05-29 11:49:13 +0200
commitea5b00528b0cb1e5cc1e7169a195b75915c8607a (patch)
tree293f5c6132973e211c0cdefc006097b67a02774d /src
parent27df983ee0ab72917756b95b49fb31319a6d813f (diff)
downloadraylib-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.c36
-rw-r--r--src/rlgl.c14
-rw-r--r--src/rlgl.h1
-rw-r--r--src/textures.c7
4 files changed, 49 insertions, 9 deletions
diff --git a/src/core.c b/src/core.c
index a94ad48d..08f9a7e2 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);
-
+ 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 d319119f..cc4c4c2f 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 fa35dbc6..a3ba6cd5 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 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();