From ae57c30e937d90eb1e541edd36ae9f81aedc2854 Mon Sep 17 00:00:00 2001 From: Robert Bragg Date: Sun, 1 Nov 2009 23:56:03 +0000 Subject: [PATCH] [texture] fix rounding when calculating update_fbo viewport offset When rendering to an fbo for supporting clutter_texture_new_from_actor we render to an fbo with the same size as the source actor, but with a viewport the same size as the stage. We offset the viewport so when we render the source actor in its normal transformed stage position it lands on the fbo. Previously we were rounding the transformed position given as a float by truncating the fraction (just using a C cast) but that resulted in an incorrect pixel offset when rendering offscreen depending on the source position. We now simply + 0.5 before casting (or -0.5 for negative numbers) --- clutter/clutter-texture.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/clutter/clutter-texture.c b/clutter/clutter-texture.c index 6d240a05f..abe0f8485 100644 --- a/clutter/clutter-texture.c +++ b/clutter/clutter-texture.c @@ -455,6 +455,7 @@ set_viewport_with_buffer_under_fbo_source (ClutterActor *fbo_source, { ClutterVertex verts[4]; float x_min = G_MAXFLOAT, y_min = G_MAXFLOAT; + int x_offset, y_offset; int i; /* Get the actors allocation transformed into screen coordinates. @@ -472,9 +473,19 @@ set_viewport_with_buffer_under_fbo_source (ClutterActor *fbo_source, y_min = verts[i].y; } + /* XXX: It's not good enough to round by simply truncating the fraction here + * via a cast, as it results in offscreen rendering being offset by 1 pixel + * in many cases... */ +#define ROUND(x) ((x) >= 0 ? (long)((x) + 0.5) : (long)((x) - 0.5)) + + x_offset = ROUND (-x_min); + y_offset = ROUND (-y_min); + +#undef ROUND + /* translate the viewport so that the source actor lands on the * sub-region backed by the offscreen draw buffer... */ - cogl_set_viewport (-x_min, -y_min, viewport_width, viewport_height); + cogl_set_viewport (x_offset, y_offset, viewport_width, viewport_height); } static void @@ -543,6 +554,7 @@ update_fbo (ClutterActor *self) NULL); } + /* cogl_clear is called to clear the buffers */ cogl_color_set_from_4ub (&transparent_col, 0, 0, 0, 0); cogl_clear (&transparent_col,