[cogl/wip/rib/master-next: 44/44] examples: Adds repeat-mode test
- From: Robert Bragg <rbragg src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [cogl/wip/rib/master-next: 44/44] examples: Adds repeat-mode test
- Date: Fri, 28 Oct 2011 15:46:12 +0000 (UTC)
commit 564cc2f2128b035c8724d5977a377dfdf1843829
Author: Robert Bragg <robert linux intel com>
Date: Wed Oct 12 18:14:40 2011 +0100
examples: Adds repeat-mode test
examples/Makefile.am | 4 +-
examples/cogl-repeat-modes.c | 246 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 249 insertions(+), 1 deletions(-)
---
diff --git a/examples/Makefile.am b/examples/Makefile.am
index 12e0449..3c7b4ef 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -18,7 +18,7 @@ common_ldadd = \
$(COGL_DEP_LIBS) \
$(top_builddir)/cogl/libcogl.la
-programs = cogl-hello cogl-info cogl-msaa
+programs = cogl-hello cogl-info cogl-msaa cogl-repeat-modes
examples_datadir = $(pkgdatadir)/examples-data
examples_data_DATA =
@@ -28,6 +28,8 @@ cogl_info_SOURCES = cogl-info.c
cogl_info_LDADD = $(common_ldadd)
cogl_msaa_SOURCES = cogl-msaa.c
cogl_msaa_LDADD = $(common_ldadd)
+cogl_repeat_modes_SOURCES = cogl-repeat-modes.c
+cogl_repeat_modes_LDADD = $(common_ldadd)
if BUILD_COGL_PANGO
programs += cogl-crate
diff --git a/examples/cogl-repeat-modes.c b/examples/cogl-repeat-modes.c
new file mode 100644
index 0000000..a82605d
--- /dev/null
+++ b/examples/cogl-repeat-modes.c
@@ -0,0 +1,246 @@
+#include <cogl/cogl.h>
+#include <glib.h>
+#include <stdio.h>
+
+CoglColor black;
+
+typedef struct _TestState
+{
+ CoglTexture2D *tex_2d;
+ CoglSubTexture *sub_tex;
+ CoglTextureRectangle *rectangle_tex;
+ CoglSubTexture *sub_rectangle_tex;
+} TestState;
+
+
+guint8 *
+gen_grid_texture (int size)
+{
+ guint8 *data = g_malloc (size * size * 4);
+ int half_size = size / 2;
+ int quater_size = half_size / 2;
+ int x, y;
+
+ g_assert (data);
+
+ for (y = 0; y < size; y++)
+ for (x = 0; x < size; x++)
+ {
+ guint8 *p = &data[size * 4 * y + x * 4];
+ guint8 red = 0xff / (1 + (x/quater_size));
+
+ p[0] = red;
+ p[1] = 0xff - red;
+
+ if (y < half_size)
+ p[2] = 0xff;
+ else
+ p[2] = 0;
+ p[3] = 0xff;
+
+ if (x == 0 || x == (size -1) || y == 0 || y == (size - 1))
+ {
+ p[0] = p[3] = 0xff;
+ p[1] = p[2] = 0;
+ }
+ }
+
+ return data;
+}
+
+typedef struct _ForeachData
+{
+ float zoom;
+ float s0, t0;
+} ForeachData;
+
+static void
+texture_2d_cb (CoglTexture *sub_texture,
+ const float *sub_texture_coords,
+ const float *meta_coords,
+ void *user_data)
+{
+ ForeachData *data = user_data;
+ cogl_set_source_texture (sub_texture);
+ cogl_rectangle_with_texture_coords ((meta_coords[0] - data->s0) * data->zoom,
+ (meta_coords[1] - data->t0) * data->zoom,
+ (meta_coords[2] - data->s0) * data->zoom,
+ (meta_coords[3] - data->t0) * data->zoom,
+ sub_texture_coords[0],
+ sub_texture_coords[1],
+ sub_texture_coords[2],
+ sub_texture_coords[3]);
+}
+
+static void
+paint (TestState *state)
+{
+ CoglPipeline *pipeline;
+#if 1
+#if 0
+ ForeachData data;
+
+ data.zoom = 100;
+ data.s0 = -2;
+ data.t0 = -2;
+ cogl_meta_texture_foreach_in_region (COGL_META_TEXTURE (state->tex_2d),
+ -2, -2,
+ 1, 1,
+ COGL_PIPELINE_WRAP_MODE_MIRRORED_REPEAT,
+ //COGL_PIPELINE_WRAP_MODE_REPEAT,
+ //COGL_PIPELINE_WRAP_MODE_MIRRORED_REPEAT,
+ COGL_PIPELINE_WRAP_MODE_REPEAT,
+ texture_2d_cb,
+ &data);
+#else
+ pipeline = cogl_pipeline_new ();
+ cogl_pipeline_set_layer_texture (pipeline, 0, COGL_TEXTURE (state->tex_2d));
+ cogl_pipeline_set_layer_wrap_mode_s (pipeline, 0,
+ //COGL_PIPELINE_WRAP_MODE_MIRRORED_REPEAT);
+ COGL_PIPELINE_WRAP_MODE_REPEAT);
+ //COGL_PIPELINE_WRAP_MODE_CLAMP_TO_EDGE);
+ cogl_pipeline_set_layer_wrap_mode_t (pipeline, 0,
+ //COGL_PIPELINE_WRAP_MODE_MIRRORED_REPEAT);
+ COGL_PIPELINE_WRAP_MODE_REPEAT);
+ //COGL_PIPELINE_WRAP_MODE_CLAMP_TO_EDGE);
+
+ cogl_set_source (pipeline);
+ //cogl_rectangle_with_texture_coords (0, 0, 200, 200, -0.25, -0.25, 0, 0);
+ cogl_rectangle_with_texture_coords (0, 0, 200, 200, 0, 0, 1, 1);
+ cogl_object_unref (pipeline);
+#endif
+#endif
+
+#if 1
+ pipeline = cogl_pipeline_new ();
+ cogl_pipeline_set_layer_texture (pipeline, 0, COGL_TEXTURE (state->sub_tex));
+ cogl_pipeline_set_layer_wrap_mode_s (pipeline, 0,
+ //COGL_PIPELINE_WRAP_MODE_MIRRORED_REPEAT);
+ COGL_PIPELINE_WRAP_MODE_REPEAT);
+ //COGL_PIPELINE_WRAP_MODE_CLAMP_TO_EDGE);
+ cogl_pipeline_set_layer_wrap_mode_t (pipeline, 0,
+ //COGL_PIPELINE_WRAP_MODE_MIRRORED_REPEAT);
+ COGL_PIPELINE_WRAP_MODE_REPEAT);
+ //COGL_PIPELINE_WRAP_MODE_CLAMP_TO_EDGE);
+
+ cogl_set_source (pipeline);
+ //cogl_rectangle_with_texture_coords (0, 0, 200, 200, -0.25, -0.25, 0, 0);
+ cogl_rectangle_with_texture_coords (210, 0, 410, 200, 0, 0, 2, 2);
+ cogl_object_unref (pipeline);
+#endif
+
+#if 1
+ pipeline = cogl_pipeline_new ();
+ cogl_pipeline_set_layer_texture (pipeline, 0, COGL_TEXTURE (state->rectangle_tex));
+ cogl_pipeline_set_layer_wrap_mode_s (pipeline, 0,
+ COGL_PIPELINE_WRAP_MODE_MIRRORED_REPEAT);
+ //COGL_PIPELINE_WRAP_MODE_REPEAT);
+ //COGL_PIPELINE_WRAP_MODE_CLAMP_TO_EDGE);
+ cogl_pipeline_set_layer_wrap_mode_t (pipeline, 0,
+ //COGL_PIPELINE_WRAP_MODE_MIRRORED_REPEAT);
+ COGL_PIPELINE_WRAP_MODE_REPEAT);
+ //COGL_PIPELINE_WRAP_MODE_CLAMP_TO_EDGE);
+
+ cogl_set_source (pipeline);
+ //cogl_rectangle_with_texture_coords (0, 0, 200, 200, -0.25, -0.25, 0, 0);
+ cogl_rectangle_with_texture_coords (420, 0, 620, 200, 100, 0, 400, 200);
+ //cogl_rectangle_with_texture_coords (420, 0, 620, 200, 0, 0, 200, 200);
+ cogl_object_unref (pipeline);
+#endif
+
+#if 1
+ pipeline = cogl_pipeline_new ();
+ cogl_pipeline_set_layer_texture (pipeline, 0, COGL_TEXTURE (state->sub_rectangle_tex));
+ cogl_pipeline_set_layer_wrap_mode_s (pipeline, 0,
+ //COGL_PIPELINE_WRAP_MODE_MIRRORED_REPEAT);
+ //COGL_PIPELINE_WRAP_MODE_REPEAT);
+ COGL_PIPELINE_WRAP_MODE_CLAMP_TO_EDGE);
+ cogl_pipeline_set_layer_wrap_mode_t (pipeline, 0,
+ //COGL_PIPELINE_WRAP_MODE_MIRRORED_REPEAT);
+ //COGL_PIPELINE_WRAP_MODE_REPEAT);
+ COGL_PIPELINE_WRAP_MODE_CLAMP_TO_EDGE);
+
+ cogl_set_source (pipeline);
+ //cogl_rectangle_with_texture_coords (0, 0, 200, 200, -0.25, -0.25, 0, 0);
+ cogl_rectangle_with_texture_coords (0, 210, 200, 410, -1, -1, 2, 2);
+ cogl_object_unref (pipeline);
+#endif
+}
+
+int
+main (int argc, char **argv)
+{
+ CoglContext *ctx;
+ CoglOnscreen *onscreen;
+ CoglFramebuffer *fb;
+ guint8 *data;
+ GError *error = NULL;
+ TestState state;
+
+ ctx = cogl_context_new (NULL, &error);
+ if (!ctx) {
+ fprintf (stderr, "Failed to create context: %s\n", error->message);
+ return 1;
+ }
+
+ onscreen = cogl_onscreen_new (ctx, 640, 480);
+ /* Eventually there will be an implicit allocate on first use so this
+ * will become optional... */
+ fb = COGL_FRAMEBUFFER (onscreen);
+ if (!cogl_framebuffer_allocate (fb, &error)) {
+ fprintf (stderr, "Failed to allocate framebuffer: %s\n", error->message);
+ return 1;
+ }
+
+ cogl_onscreen_show (onscreen);
+
+ data = gen_grid_texture (200);
+ state.tex_2d = cogl_texture_2d_new_from_data (ctx,
+ 200, 200,
+ COGL_PIXEL_FORMAT_BGRA_8888,
+ COGL_PIXEL_FORMAT_BGRA_8888,
+ 0,
+ data,
+ &error);
+ g_assert_no_error (error);
+
+ state.sub_tex = cogl_sub_texture_new (ctx,
+ COGL_TEXTURE (state.tex_2d),
+ 0, 0, 100, 100);
+
+ state.rectangle_tex =
+ cogl_texture_rectangle_new_with_size (ctx,
+ 200, 200,
+ COGL_PIXEL_FORMAT_BGRA_8888,
+ &error);
+ g_assert_no_error (error);
+ cogl_texture_set_region (COGL_TEXTURE (state.rectangle_tex),
+ 0, 0,
+ 0, 0,
+ 200, 200,
+ 200, 200,
+ COGL_PIXEL_FORMAT_BGRA_8888,
+ 0,
+ data);
+
+ state.sub_rectangle_tex =
+ cogl_sub_texture_new (ctx,
+ COGL_TEXTURE (state.rectangle_tex),
+ 0, 0, 100, 100);
+
+ g_free (data);
+
+ cogl_push_framebuffer (fb);
+
+ cogl_ortho (0, 640, /* left, right */
+ 480, 0, /* bottom, top */
+ -1, 100 /* z near, far */);
+
+ for (;;) {
+ cogl_clear (&black, COGL_BUFFER_BIT_COLOR);
+ paint (&state);
+ cogl_framebuffer_swap_buffers (fb);
+ }
+
+ return 0;
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]