[test-fbo] greatly simplify the test

This test tried to do too much, and I can't remember the last time I saw this
test work.

It no longer tries to create a texture from an offscreen actor and it no
longer tries to use shaders.

It does though show that chaining of clutter_texture_new_from_actor now
works, and that animating the source actor is reflected in textures created
from it.

When run you should see three actors:
- on the left is the pristine source actor rotating around the y-axis
- in the middle is the first texture created from the source actor
- and on the right a texture created from the middle actor

Note: the somewhat strange bobbing of the middle and right textures is
actually correct given how it was decided long ago to map the transformed
(to screen space) allocation of the source actor to the texture.  When the
hand spins around the perspective projection of the top of the hand results
in the origin of the texture bobbing up to a higher stage position, but the
position of the textures is fixed.  This design also means we end up
reallocating our offscreen draw buffer every frame that the actors
transformed size changes, which isn't ideal.
This commit is contained in:
Robert Bragg 2009-10-07 10:40:15 +01:00
parent ffb592de59
commit 4258214e50

View File

@ -1,25 +1,4 @@
/*#define TEST_GROUP */
/* These variables are used instead of the standard GLSL variables on
GLES 2 */
#ifdef HAVE_COGL_GLES2
#define GLES2_VARS \
"precision mediump float;\n" \
"varying vec2 tex_coord;\n" \
"varying vec4 frag_color;\n"
#define TEX_COORD "tex_coord"
#define COLOR_VAR "frag_color"
#else /* HAVE_COGL_GLES2 */
#define GLES2_VARS ""
#define TEX_COORD "gl_TexCoord[0]"
#define COLOR_VAR "gl_Color"
#endif /* HAVE_COGL_GLES2 */
#include <clutter/clutter.h>
#include <errno.h>
@ -27,13 +6,16 @@
#include <glib.h>
#include <gmodule.h>
ClutterActor*
make_source(void)
{
ClutterActor *source, *actor;
GError *error = NULL;
#define STAGE_WIDTH 800
#define STAGE_HEIGHT 600
ClutterColor yellow = {0xff, 0xff, 0x00, 0xff};
ClutterActor *
make_source (void)
{
ClutterActor *source, *actor;
GError *error = NULL;
ClutterColor yellow = {0xff, 0xff, 0x00, 0xff};
source = clutter_group_new();
actor = clutter_texture_new_from_file ("redhand.png", &error);
@ -51,65 +33,17 @@ make_source(void)
return source;
}
ClutterShader*
make_shader(void)
{
ClutterShader *shader;
GError *error = NULL;
shader = clutter_shader_new ();
clutter_shader_set_fragment_source (shader,
GLES2_VARS
"uniform float radius ;"
"uniform sampler2D rectTexture;"
"uniform float x_step, y_step;"
""
"void main()"
"{"
" vec4 color = texture2D(rectTexture, " TEX_COORD ".st);"
" float u;"
" float v;"
" int count = 1;"
" for (u=-radius;u<radius;u++)"
" for (v=-radius;v<radius;v++)"
" {"
" color += texture2D(rectTexture, "
" vec2(" TEX_COORD ".s + u"
" * 2.0 * x_step,"
" " TEX_COORD ".t + v"
" * 2.0 * y_step));"
" count ++;"
" }"
""
" gl_FragColor = color / float(count);"
" gl_FragColor = gl_FragColor * " COLOR_VAR ";"
"}",
-1
);
if (!clutter_shader_compile (shader, &error))
{
fprintf (stderr, "shader compilation failed:\n%s", error->message);
g_error_free (error);
}
return shader;
}
G_MODULE_EXPORT gint
test_fbo_main (gint argc, gchar *argv[])
G_MODULE_EXPORT int
test_fbo_main (int argc, char *argv[])
{
ClutterColor blue = {0x33, 0x44, 0x55, 0xff};
ClutterActor *fbo;
ClutterActor *onscreen_source, *offscreen_source, *trans_source;
ClutterActor *foo_source;
ClutterActor *onscreen_source;
ClutterActor *stage;
ClutterActor *clone;
ClutterShader *shader;
gint padx, pady;
gint fbo_width, fbo_height;
ClutterAnimation *animation;
int x_pos = 200;
int y_pos = 100;
clutter_init (&argc, &argv);
@ -117,6 +51,7 @@ test_fbo_main (gint argc, gchar *argv[])
g_error("This test requires CLUTTER_FEATURE_OFFSCREEN");
stage = clutter_stage_get_default ();
clutter_actor_set_size (stage, STAGE_WIDTH, STAGE_HEIGHT);
clutter_stage_set_color (CLUTTER_STAGE (stage), &blue);
/* Create the first source */
@ -124,93 +59,34 @@ test_fbo_main (gint argc, gchar *argv[])
clutter_actor_show_all (onscreen_source);
clutter_group_add (stage, onscreen_source);
/* Basic sizing for alignment */
fbo_width = clutter_actor_get_width (onscreen_source);
fbo_height = clutter_actor_get_height (onscreen_source);
padx = fbo_width + 10;
pady = fbo_height + 10;
clutter_actor_set_size (stage, padx*4, pady*2);
y_pos = (STAGE_HEIGHT/2.0) -
(clutter_actor_get_height (onscreen_source)/2.0);
clutter_actor_set_position (onscreen_source, x_pos, y_pos);
x_pos += clutter_actor_get_width (onscreen_source);
/* Second hand from fbo onscreen */
animation = clutter_actor_animate (onscreen_source,
CLUTTER_LINEAR,
5000, /* 1 second duration */
"rotation-angle-y", 360.0f,
NULL);
clutter_animation_set_loop (animation, TRUE);
/* Second hand = actor from onscreen_source */
if ((fbo = clutter_texture_new_from_actor (onscreen_source)) == NULL)
g_error("onscreen fbo creation failed");
clutter_actor_set_position (fbo, padx, 0);
clutter_actor_set_position (fbo, x_pos, y_pos);
x_pos += clutter_actor_get_width (fbo);
clutter_group_add (stage, fbo);
/* apply a shader to it */
shader = make_shader();
clutter_actor_set_shader (fbo, shader);
clutter_actor_set_shader_param_float (fbo, "radius", 2.0);
clutter_actor_set_shader_param_float (fbo, "x_step",
1.0f / clutter_util_next_p2 (fbo_width));
clutter_actor_set_shader_param_float (fbo, "y_step",
1.0f / clutter_util_next_p2 (fbo_height));
/* Third hand = actor from Second hand */
if ((fbo = clutter_texture_new_from_actor (fbo)) == NULL)
g_error("fbo from fbo creation failed");
/* Third from cloning the fbo texture */
clone = clutter_clone_new (fbo);
clutter_container_add_actor (CLUTTER_CONTAINER (stage), clone);
clutter_actor_set_position (clone, padx*2, 0);
/* Forth - an offscreen source */
offscreen_source = make_source();
clutter_actor_show_all (offscreen_source); /* need to show() offscreen */
if ((fbo = clutter_texture_new_from_actor (offscreen_source)) == NULL)
g_error("offscreen fbo creation failed");
clutter_actor_set_position (fbo, padx*3, 0);
clutter_actor_set_position (fbo, x_pos, y_pos);
x_pos += clutter_actor_get_width (fbo);
clutter_group_add (stage, fbo);
/* 5th transformed */
trans_source = make_source();
clutter_actor_show_all (trans_source); /* need to show() offscreen */
clutter_actor_set_scale (trans_source, 2.5, 2.5);
if ((fbo = clutter_texture_new_from_actor (trans_source)) == NULL)
g_error("transformed fbo creation failed");
clutter_actor_set_position (fbo, 0, pady);
clutter_group_add (stage, fbo);
/* 6th resized bigger, but after fbo creation */
trans_source = make_source();
clutter_actor_show_all (trans_source); /* need to show() offscreen */
if ((fbo = clutter_texture_new_from_actor (trans_source)) == NULL)
g_error("transformed fbo creation failed");
/* rotate after */
clutter_actor_move_anchor_point_from_gravity (trans_source,
CLUTTER_GRAVITY_CENTER);
clutter_actor_set_rotation (trans_source, CLUTTER_Z_AXIS, 90.0, 0, 0, 0);
clutter_actor_set_position (fbo, padx, pady);
clutter_group_add (stage, fbo);
/* non visual breaks */
foo_source = make_source();
g_object_ref_sink (foo_source);
clutter_actor_show_all (foo_source);
if ((fbo = clutter_texture_new_from_actor (foo_source)) == NULL)
g_error("foo fbo creation failed");
g_object_unref (foo_source); /* fbo should keep it around */
clutter_actor_set_position (fbo, padx*3, pady);
clutter_group_add (stage, fbo);
/* TODO:
* Check realize/unrealize
* get_pixbuf()
* set_rgba on fbo texture.
*/
clutter_actor_show_all (stage);
clutter_main ();