| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <string.h> |
| 4 |
|
| 5 |
#include <glib.h> |
| 6 |
#include <glib-object.h> |
| 7 |
#include <gmodule.h> |
| 8 |
|
| 9 |
#include <clutter/clutter.h> |
| 10 |
#include <cogl/cogl.h> |
| 11 |
|
| 12 |
typedef struct _TestMultiLayerMaterialState |
| 13 |
{ |
| 14 |
ClutterActor *group; |
| 15 |
CoglHandle alpha_tex; |
| 16 |
CoglHandle redhand_tex; |
| 17 |
gfloat *tex_coords; |
| 18 |
|
| 19 |
ClutterTimeline *timeline; |
| 20 |
|
| 21 |
CoglHandle material0; |
| 22 |
CoglMatrix tex_matrix0; |
| 23 |
CoglMatrix rot_matrix0; |
| 24 |
CoglHandle light_tex0; |
| 25 |
|
| 26 |
CoglHandle material1; |
| 27 |
CoglMatrix tex_matrix1; |
| 28 |
CoglMatrix rot_matrix1; |
| 29 |
CoglHandle light_tex1; |
| 30 |
|
| 31 |
} TestMultiLayerMaterialState; |
| 32 |
|
| 33 |
|
| 34 |
static void |
| 35 |
frame_cb (ClutterTimeline *timeline, |
| 36 |
gint frame_no, |
| 37 |
gpointer data) |
| 38 |
{ |
| 39 |
TestMultiLayerMaterialState *state = data; |
| 40 |
|
| 41 |
cogl_matrix_multiply (&state->tex_matrix0, |
| 42 |
&state->tex_matrix0, |
| 43 |
&state->rot_matrix0); |
| 44 |
cogl_material_set_layer_matrix (state->material0, 2, &state->tex_matrix0); |
| 45 |
|
| 46 |
cogl_matrix_multiply (&state->tex_matrix1, |
| 47 |
&state->tex_matrix1, |
| 48 |
&state->rot_matrix1); |
| 49 |
cogl_material_set_layer_matrix (state->material1, 2, &state->tex_matrix1); |
| 50 |
} |
| 51 |
|
| 52 |
static void |
| 53 |
material_rectangle_paint (ClutterActor *actor, gpointer data) |
| 54 |
{ |
| 55 |
TestMultiLayerMaterialState *state = data; |
| 56 |
|
| 57 |
cogl_push_matrix (); |
| 58 |
|
| 59 |
cogl_translate (150, 15, 0); |
| 60 |
|
| 61 |
cogl_set_source (state->material0); |
| 62 |
cogl_rectangle_with_multitexture_coords (0, 0, 200, 213, |
| 63 |
state->tex_coords, |
| 64 |
12); |
| 65 |
cogl_translate (-300, -30, 0); |
| 66 |
cogl_set_source (state->material1); |
| 67 |
cogl_rectangle_with_multitexture_coords (0, 0, 200, 213, |
| 68 |
state->tex_coords, |
| 69 |
12); |
| 70 |
|
| 71 |
cogl_pop_matrix (); |
| 72 |
} |
| 73 |
|
| 74 |
static void |
| 75 |
animation_completed_cb (ClutterAnimation *animation, |
| 76 |
TestMultiLayerMaterialState *state) |
| 77 |
{ |
| 78 |
static gboolean go_back = FALSE; |
| 79 |
gdouble new_rotation_y; |
| 80 |
|
| 81 |
if (go_back) |
| 82 |
new_rotation_y = 30; |
| 83 |
else |
| 84 |
new_rotation_y = -30; |
| 85 |
go_back = !go_back; |
| 86 |
|
| 87 |
clutter_actor_animate_with_timeline (state->group, |
| 88 |
CLUTTER_LINEAR, |
| 89 |
state->timeline, |
| 90 |
"rotation-angle-y", new_rotation_y, |
| 91 |
"signal-after::completed", |
| 92 |
animation_completed_cb, state, |
| 93 |
NULL); |
| 94 |
|
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
G_MODULE_EXPORT int |
| 99 |
test_cogl_multitexture_main (int argc, char *argv[]) |
| 100 |
{ |
| 101 |
GError *error = NULL; |
| 102 |
ClutterActor *stage; |
| 103 |
ClutterColor stage_color = { 0x61, 0x56, 0x56, 0xff }; |
| 104 |
TestMultiLayerMaterialState *state = g_new0 (TestMultiLayerMaterialState, 1); |
| 105 |
gfloat stage_w, stage_h; |
| 106 |
gchar **files; |
| 107 |
gfloat tex_coords[] = |
| 108 |
{ |
| 109 |
/* tx1 ty1 tx2 ty2 */ |
| 110 |
0, 0, 1, 1, |
| 111 |
0, 0, 1, 1, |
| 112 |
0, 0, 1, 1 |
| 113 |
}; |
| 114 |
|
| 115 |
if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS) |
| 116 |
return 1; |
| 117 |
|
| 118 |
stage = clutter_stage_new (); |
| 119 |
clutter_actor_get_size (stage, &stage_w, &stage_h); |
| 120 |
|
| 121 |
clutter_stage_set_title (CLUTTER_STAGE (stage), "Cogl: Multi-texturing"); |
| 122 |
clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color); |
| 123 |
|
| 124 |
g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL); |
| 125 |
|
| 126 |
/* We create a non-descript actor that we know doesn't have a |
| 127 |
* default paint handler, so that we can easily control |
| 128 |
* painting in a paint signal handler, without having to |
| 129 |
* sub-class anything etc. */ |
| 130 |
state->group = clutter_group_new (); |
| 131 |
clutter_actor_set_position (state->group, stage_w / 2, stage_h / 2); |
| 132 |
g_signal_connect (state->group, "paint", |
| 133 |
G_CALLBACK(material_rectangle_paint), state); |
| 134 |
|
| 135 |
files = g_new (gchar*, 4); |
| 136 |
files[0] = g_build_filename (TESTS_DATADIR, "redhand_alpha.png", NULL); |
| 137 |
files[1] = g_build_filename (TESTS_DATADIR, "redhand.png", NULL); |
| 138 |
files[2] = g_build_filename (TESTS_DATADIR, "light0.png", NULL); |
| 139 |
files[3] = NULL; |
| 140 |
|
| 141 |
state->alpha_tex = |
| 142 |
cogl_texture_new_from_file (files[0], |
| 143 |
COGL_TEXTURE_NO_SLICING, |
| 144 |
COGL_PIXEL_FORMAT_ANY, |
| 145 |
&error); |
| 146 |
if (!state->alpha_tex) |
| 147 |
g_critical ("Failed to load redhand_alpha.png: %s", error->message); |
| 148 |
|
| 149 |
state->redhand_tex = |
| 150 |
cogl_texture_new_from_file (files[1], |
| 151 |
COGL_TEXTURE_NO_SLICING, |
| 152 |
COGL_PIXEL_FORMAT_ANY, |
| 153 |
&error); |
| 154 |
if (!state->redhand_tex) |
| 155 |
g_critical ("Failed to load redhand.png: %s", error->message); |
| 156 |
|
| 157 |
state->light_tex0 = |
| 158 |
cogl_texture_new_from_file (files[2], |
| 159 |
COGL_TEXTURE_NO_SLICING, |
| 160 |
COGL_PIXEL_FORMAT_ANY, |
| 161 |
&error); |
| 162 |
if (!state->light_tex0) |
| 163 |
g_critical ("Failed to load light0.png: %s", error->message); |
| 164 |
|
| 165 |
state->light_tex1 = |
| 166 |
cogl_texture_new_from_file (files[2], |
| 167 |
COGL_TEXTURE_NO_SLICING, |
| 168 |
COGL_PIXEL_FORMAT_ANY, |
| 169 |
&error); |
| 170 |
if (!state->light_tex1) |
| 171 |
g_critical ("Failed to load light0.png: %s", error->message); |
| 172 |
|
| 173 |
g_strfreev (files); |
| 174 |
|
| 175 |
state->material0 = cogl_material_new (); |
| 176 |
cogl_material_set_layer (state->material0, 0, state->alpha_tex); |
| 177 |
cogl_material_set_layer (state->material0, 1, state->redhand_tex); |
| 178 |
cogl_material_set_layer (state->material0, 2, state->light_tex0); |
| 179 |
|
| 180 |
state->material1 = cogl_material_new (); |
| 181 |
cogl_material_set_layer (state->material1, 0, state->alpha_tex); |
| 182 |
cogl_material_set_layer (state->material1, 1, state->redhand_tex); |
| 183 |
cogl_material_set_layer (state->material1, 2, state->light_tex1); |
| 184 |
|
| 185 |
state->tex_coords = tex_coords; |
| 186 |
|
| 187 |
cogl_matrix_init_identity (&state->tex_matrix0); |
| 188 |
cogl_matrix_init_identity (&state->tex_matrix1); |
| 189 |
cogl_matrix_init_identity (&state->rot_matrix0); |
| 190 |
cogl_matrix_init_identity (&state->rot_matrix1); |
| 191 |
|
| 192 |
cogl_matrix_translate (&state->rot_matrix0, 0.5, 0.5, 0); |
| 193 |
cogl_matrix_rotate (&state->rot_matrix0, 10.0, 0, 0, 1.0); |
| 194 |
cogl_matrix_translate (&state->rot_matrix0, -0.5, -0.5, 0); |
| 195 |
|
| 196 |
cogl_matrix_translate (&state->rot_matrix1, 0.5, 0.5, 0); |
| 197 |
cogl_matrix_rotate (&state->rot_matrix1, -10.0, 0, 0, 1.0); |
| 198 |
cogl_matrix_translate (&state->rot_matrix1, -0.5, -0.5, 0); |
| 199 |
|
| 200 |
clutter_actor_set_anchor_point (state->group, 86, 125); |
| 201 |
clutter_container_add_actor (CLUTTER_CONTAINER(stage), |
| 202 |
state->group); |
| 203 |
|
| 204 |
state->timeline = clutter_timeline_new (2812); |
| 205 |
|
| 206 |
g_signal_connect (state->timeline, "new-frame", G_CALLBACK (frame_cb), state); |
| 207 |
|
| 208 |
clutter_actor_animate_with_timeline (state->group, |
| 209 |
CLUTTER_LINEAR, |
| 210 |
state->timeline, |
| 211 |
"rotation-angle-y", 30.0, |
| 212 |
"signal-after::completed", |
| 213 |
animation_completed_cb, state, |
| 214 |
NULL); |
| 215 |
|
| 216 |
/* start the timeline and thus the animations */ |
| 217 |
clutter_timeline_start (state->timeline); |
| 218 |
|
| 219 |
clutter_actor_show_all (stage); |
| 220 |
|
| 221 |
clutter_main(); |
| 222 |
|
| 223 |
cogl_handle_unref (state->material1); |
| 224 |
cogl_handle_unref (state->material0); |
| 225 |
cogl_handle_unref (state->alpha_tex); |
| 226 |
cogl_handle_unref (state->redhand_tex); |
| 227 |
cogl_handle_unref (state->light_tex0); |
| 228 |
cogl_handle_unref (state->light_tex1); |
| 229 |
g_free (state); |
| 230 |
|
| 231 |
return 0; |
| 232 |
} |
| 233 |
|
| 234 |
G_MODULE_EXPORT const char * |
| 235 |
test_cogl_multitexture_describe (void) |
| 236 |
{ |
| 237 |
return "Multi-texturing support in Cogl."; |
| 238 |
} |