| 1 |
#include <glib.h> |
| 2 |
#include <gmodule.h> |
| 3 |
#include <stdlib.h> |
| 4 |
#include <clutter/clutter.h> |
| 5 |
#include <cogl/cogl.h> |
| 6 |
#include <math.h> |
| 7 |
|
| 8 |
/* Defines the size and resolution of the quad mesh we morph: |
| 9 |
*/ |
| 10 |
#define MESH_WIDTH 100.0 /* number of quads along x axis */ |
| 11 |
#define MESH_HEIGHT 100.0 /* number of quads along y axis */ |
| 12 |
#define QUAD_WIDTH 5.0 /* width in pixels of a single quad */ |
| 13 |
#define QUAD_HEIGHT 5.0 /* height in pixels of a single quad */ |
| 14 |
|
| 15 |
/* Defines a sine wave that sweeps across the mesh: |
| 16 |
*/ |
| 17 |
#define WAVE_DEPTH ((MESH_WIDTH * QUAD_WIDTH) / 16.0) /* peak amplitude */ |
| 18 |
#define WAVE_PERIODS 4.0 |
| 19 |
#define WAVE_SPEED 10.0 |
| 20 |
|
| 21 |
/* Defines a rippling sine wave emitted from a point: |
| 22 |
*/ |
| 23 |
#define RIPPLE_CENTER_X ((MESH_WIDTH / 2.0) * QUAD_WIDTH) |
| 24 |
#define RIPPLE_CENTER_Y ((MESH_HEIGHT / 2.0) * QUAD_HEIGHT) |
| 25 |
#define RIPPLE_RADIUS (MESH_WIDTH * QUAD_WIDTH) |
| 26 |
#define RIPPLE_DEPTH ((MESH_WIDTH * QUAD_WIDTH) / 16.0) /* peak amplitude */ |
| 27 |
#define RIPPLE_PERIODS 4.0 |
| 28 |
#define RIPPLE_SPEED -10.0 |
| 29 |
|
| 30 |
/* Defines the width of the gaussian bell used to fade out the alpha |
| 31 |
* towards the edges of the mesh (starting from the ripple center): |
| 32 |
*/ |
| 33 |
#define GAUSSIAN_RADIUS ((MESH_WIDTH * QUAD_WIDTH) / 6.0) |
| 34 |
|
| 35 |
/* Our hues lie in the range [0, 1], and this defines how we map amplitude |
| 36 |
* to hues (before scaling by {WAVE,RIPPLE}_DEPTH) |
| 37 |
* As we are interferring two sine waves together; amplitudes lie in the |
| 38 |
* range [-2, 2] |
| 39 |
*/ |
| 40 |
#define HSL_OFFSET 0.5 /* the hue that we map an amplitude of 0 too */ |
| 41 |
#define HSL_SCALE 0.25 |
| 42 |
|
| 43 |
typedef struct _TestState |
| 44 |
{ |
| 45 |
ClutterActor *dummy; |
| 46 |
CoglHandle buffer; |
| 47 |
float *quad_mesh_verts; |
| 48 |
guint8 *quad_mesh_colors; |
| 49 |
guint16 *static_indices; |
| 50 |
guint n_static_indices; |
| 51 |
CoglHandle indices; |
| 52 |
ClutterTimeline *timeline; |
| 53 |
guint frame_id; |
| 54 |
} TestState; |
| 55 |
|
| 56 |
static void |
| 57 |
frame_cb (ClutterTimeline *timeline, |
| 58 |
gint elapsed_msecs, |
| 59 |
TestState *state) |
| 60 |
{ |
| 61 |
guint x, y; |
| 62 |
float period_progress = clutter_timeline_get_progress (timeline); |
| 63 |
float period_progress_sin = sinf (period_progress); |
| 64 |
float wave_shift = period_progress * WAVE_SPEED; |
| 65 |
float ripple_shift = period_progress * RIPPLE_SPEED; |
| 66 |
|
| 67 |
for (y = 0; y <= MESH_HEIGHT; y++) |
| 68 |
for (x = 0; x <= MESH_WIDTH; x++) |
| 69 |
{ |
| 70 |
guint vert_index = (MESH_WIDTH + 1) * y + x; |
| 71 |
float *vert = &state->quad_mesh_verts[3 * vert_index]; |
| 72 |
|
| 73 |
float real_x = x * QUAD_WIDTH; |
| 74 |
float real_y = y * QUAD_HEIGHT; |
| 75 |
|
| 76 |
float wave_offset = (float)x / (MESH_WIDTH + 1); |
| 77 |
float wave_angle = |
| 78 |
(WAVE_PERIODS * 2 * G_PI * wave_offset) + wave_shift; |
| 79 |
float wave_sin = sinf (wave_angle); |
| 80 |
|
| 81 |
float a_sqr = (RIPPLE_CENTER_X - real_x) * (RIPPLE_CENTER_X - real_x); |
| 82 |
float b_sqr = (RIPPLE_CENTER_Y - real_y) * (RIPPLE_CENTER_Y - real_y); |
| 83 |
float ripple_offset = sqrtf (a_sqr + b_sqr) / RIPPLE_RADIUS; |
| 84 |
float ripple_angle = |
| 85 |
(RIPPLE_PERIODS * 2 * G_PI * ripple_offset) + ripple_shift; |
| 86 |
float ripple_sin = sinf (ripple_angle); |
| 87 |
|
| 88 |
float h, s, l; |
| 89 |
guint8 *color; |
| 90 |
|
| 91 |
vert[2] = (wave_sin * WAVE_DEPTH) + (ripple_sin * RIPPLE_DEPTH); |
| 92 |
|
| 93 |
/* Burn some CPU time picking a pretty color... */ |
| 94 |
h = (HSL_OFFSET |
| 95 |
+ wave_sin |
| 96 |
+ ripple_sin |
| 97 |
+ period_progress_sin) * HSL_SCALE; |
| 98 |
s = 0.5; |
| 99 |
l = 0.25 + (period_progress_sin + 1.0) / 4.0; |
| 100 |
color = &state->quad_mesh_colors[4 * vert_index]; |
| 101 |
/* A bit of a sneaky cast, but it seems safe to assume the ClutterColor |
| 102 |
* typedef is set in stone... */ |
| 103 |
clutter_color_from_hls ((ClutterColor *)color, h * 360.0, l, s); |
| 104 |
|
| 105 |
color[0] = (color[0] * color[3] + 128) / 255; |
| 106 |
color[1] = (color[1] * color[3] + 128) / 255; |
| 107 |
color[2] = (color[2] * color[3] + 128) / 255; |
| 108 |
} |
| 109 |
|
| 110 |
cogl_vertex_buffer_add (state->buffer, |
| 111 |
"gl_Vertex", |
| 112 |
3, /* n components */ |
| 113 |
COGL_ATTRIBUTE_TYPE_FLOAT, |
| 114 |
FALSE, /* normalized */ |
| 115 |
0, /* stride */ |
| 116 |
state->quad_mesh_verts); |
| 117 |
cogl_vertex_buffer_add (state->buffer, |
| 118 |
"gl_Color", |
| 119 |
4, /* n components */ |
| 120 |
COGL_ATTRIBUTE_TYPE_UNSIGNED_BYTE, |
| 121 |
FALSE, /* normalized */ |
| 122 |
0, /* stride */ |
| 123 |
state->quad_mesh_colors); |
| 124 |
|
| 125 |
cogl_vertex_buffer_submit (state->buffer); |
| 126 |
|
| 127 |
clutter_actor_set_rotation (state->dummy, |
| 128 |
CLUTTER_Z_AXIS, |
| 129 |
360 * period_progress, |
| 130 |
(MESH_WIDTH * QUAD_WIDTH) / 2, |
| 131 |
(MESH_HEIGHT * QUAD_HEIGHT) / 2, |
| 132 |
0); |
| 133 |
clutter_actor_set_rotation (state->dummy, |
| 134 |
CLUTTER_X_AXIS, |
| 135 |
360 * period_progress, |
| 136 |
(MESH_WIDTH * QUAD_WIDTH) / 2, |
| 137 |
(MESH_HEIGHT * QUAD_HEIGHT) / 2, |
| 138 |
0); |
| 139 |
} |
| 140 |
|
| 141 |
static void |
| 142 |
on_paint (ClutterActor *actor, TestState *state) |
| 143 |
{ |
| 144 |
cogl_set_source_color4ub (0xff, 0x00, 0x00, 0xff); |
| 145 |
cogl_vertex_buffer_draw_elements (state->buffer, |
| 146 |
COGL_VERTICES_MODE_TRIANGLE_STRIP, |
| 147 |
state->indices, |
| 148 |
0, /* min index */ |
| 149 |
(MESH_WIDTH + 1) * |
| 150 |
(MESH_HEIGHT + 1) - 1, /* max index */ |
| 151 |
0, /* indices offset */ |
| 152 |
state->n_static_indices); |
| 153 |
} |
| 154 |
|
| 155 |
static void |
| 156 |
init_static_index_arrays (TestState *state) |
| 157 |
{ |
| 158 |
guint n_indices; |
| 159 |
int x, y; |
| 160 |
guint16 *i; |
| 161 |
guint dir; |
| 162 |
|
| 163 |
/* - Each row takes (2 + 2 * MESH_WIDTH indices) |
| 164 |
* - Thats 2 to start the triangle strip then 2 indices to add 2 triangles |
| 165 |
* per mesh quad. |
| 166 |
* - We have MESH_HEIGHT rows |
| 167 |
* - It takes one extra index for linking between rows (MESH_HEIGHT - 1) |
| 168 |
* - A 2 x 3 mesh == 20 indices... */ |
| 169 |
n_indices = (2 + 2 * MESH_WIDTH) * MESH_HEIGHT + (MESH_HEIGHT - 1); |
| 170 |
state->static_indices = g_malloc (sizeof (guint16) * n_indices); |
| 171 |
state->n_static_indices = n_indices; |
| 172 |
|
| 173 |
#define MESH_INDEX(X, Y) (Y) * (MESH_WIDTH + 1) + (X) |
| 174 |
|
| 175 |
i = state->static_indices; |
| 176 |
|
| 177 |
/* NB: front facing == anti-clockwise winding */ |
| 178 |
|
| 179 |
i[0] = MESH_INDEX (0, 0); |
| 180 |
i[1] = MESH_INDEX (0, 1); |
| 181 |
i += 2; |
| 182 |
|
| 183 |
#define LEFT 0 |
| 184 |
#define RIGHT 1 |
| 185 |
|
| 186 |
dir = RIGHT; |
| 187 |
|
| 188 |
for (y = 0; y < MESH_HEIGHT; y++) |
| 189 |
{ |
| 190 |
for (x = 0; x < MESH_WIDTH; x++) |
| 191 |
{ |
| 192 |
/* Add 2 triangles per mesh quad... */ |
| 193 |
if (dir == RIGHT) |
| 194 |
{ |
| 195 |
i[0] = MESH_INDEX (x + 1, y); |
| 196 |
i[1] = MESH_INDEX (x + 1, y + 1); |
| 197 |
} |
| 198 |
else |
| 199 |
{ |
| 200 |
i[0] = MESH_INDEX (MESH_WIDTH - x - 1, y); |
| 201 |
i[1] = MESH_INDEX (MESH_WIDTH - x - 1, y + 1); |
| 202 |
} |
| 203 |
i += 2; |
| 204 |
} |
| 205 |
|
| 206 |
/* Link rows... */ |
| 207 |
|
| 208 |
if (y == (MESH_HEIGHT - 1)) |
| 209 |
break; |
| 210 |
|
| 211 |
if (dir == RIGHT) |
| 212 |
{ |
| 213 |
i[0] = MESH_INDEX (MESH_WIDTH, y + 1); |
| 214 |
i[1] = MESH_INDEX (MESH_WIDTH, y + 1); |
| 215 |
i[2] = MESH_INDEX (MESH_WIDTH, y + 2); |
| 216 |
} |
| 217 |
else |
| 218 |
{ |
| 219 |
i[0] = MESH_INDEX (0, y + 1); |
| 220 |
i[1] = MESH_INDEX (0, y + 1); |
| 221 |
i[2] = MESH_INDEX (0, y + 2); |
| 222 |
} |
| 223 |
i += 3; |
| 224 |
dir = !dir; |
| 225 |
} |
| 226 |
|
| 227 |
#undef MESH_INDEX |
| 228 |
|
| 229 |
state->indices = |
| 230 |
cogl_vertex_buffer_indices_new (COGL_INDICES_TYPE_UNSIGNED_SHORT, |
| 231 |
state->static_indices, |
| 232 |
state->n_static_indices); |
| 233 |
} |
| 234 |
|
| 235 |
static float |
| 236 |
gaussian (float x, float y) |
| 237 |
{ |
| 238 |
/* Bell width */ |
| 239 |
float c = GAUSSIAN_RADIUS; |
| 240 |
|
| 241 |
/* Peak amplitude */ |
| 242 |
float a = 1.0; |
| 243 |
/* float a = 1.0 / (c * sqrtf (2.0 * G_PI)); */ |
| 244 |
|
| 245 |
/* Center offset */ |
| 246 |
float b = 0.0; |
| 247 |
|
| 248 |
float dist; |
| 249 |
x = x - RIPPLE_CENTER_X; |
| 250 |
y = y - RIPPLE_CENTER_Y; |
| 251 |
dist = sqrtf (x*x + y*y); |
| 252 |
|
| 253 |
return a * exp ((- ((dist - b) * (dist - b))) / (2.0 * c * c)); |
| 254 |
} |
| 255 |
|
| 256 |
static void |
| 257 |
init_quad_mesh (TestState *state) |
| 258 |
{ |
| 259 |
int x, y; |
| 260 |
float *vert; |
| 261 |
guint8 *color; |
| 262 |
|
| 263 |
/* Note: we maintain the minimum number of vertices possible. This minimizes |
| 264 |
* the work required when we come to morph the geometry. |
| 265 |
* |
| 266 |
* We use static indices into our mesh so that we can treat the data like a |
| 267 |
* single triangle list and drawing can be done in one operation (Note: We |
| 268 |
* are using degenerate triangles at the edges to link to the next row) |
| 269 |
*/ |
| 270 |
state->quad_mesh_verts = |
| 271 |
g_malloc0 (sizeof (float) * 3 * (MESH_WIDTH + 1) * (MESH_HEIGHT + 1)); |
| 272 |
|
| 273 |
state->quad_mesh_colors = |
| 274 |
g_malloc0 (sizeof (guint8) * 4 * (MESH_WIDTH + 1) * (MESH_HEIGHT + 1)); |
| 275 |
|
| 276 |
vert = state->quad_mesh_verts; |
| 277 |
color = state->quad_mesh_colors; |
| 278 |
for (y = 0; y <= MESH_HEIGHT; y++) |
| 279 |
for (x = 0; x <= MESH_WIDTH; x++) |
| 280 |
{ |
| 281 |
vert[0] = x * QUAD_WIDTH; |
| 282 |
vert[1] = y * QUAD_HEIGHT; |
| 283 |
vert += 3; |
| 284 |
|
| 285 |
color[3] = gaussian (x * QUAD_WIDTH, |
| 286 |
y * QUAD_HEIGHT) * 255.0; |
| 287 |
color += 4; |
| 288 |
} |
| 289 |
|
| 290 |
state->buffer = cogl_vertex_buffer_new ((MESH_WIDTH + 1)*(MESH_HEIGHT + 1)); |
| 291 |
cogl_vertex_buffer_add (state->buffer, |
| 292 |
"gl_Vertex", |
| 293 |
3, /* n components */ |
| 294 |
COGL_ATTRIBUTE_TYPE_FLOAT, |
| 295 |
FALSE, /* normalized */ |
| 296 |
0, /* stride */ |
| 297 |
state->quad_mesh_verts); |
| 298 |
|
| 299 |
cogl_vertex_buffer_add (state->buffer, |
| 300 |
"gl_Color", |
| 301 |
4, /* n components */ |
| 302 |
COGL_ATTRIBUTE_TYPE_UNSIGNED_BYTE, |
| 303 |
FALSE, /* normalized */ |
| 304 |
0, /* stride */ |
| 305 |
state->quad_mesh_colors); |
| 306 |
|
| 307 |
cogl_vertex_buffer_submit (state->buffer); |
| 308 |
|
| 309 |
init_static_index_arrays (state); |
| 310 |
} |
| 311 |
|
| 312 |
/* This creates an actor that has a specific size but that does not result |
| 313 |
* in any drawing so we can do our own drawing using Cogl... */ |
| 314 |
static ClutterActor * |
| 315 |
create_dummy_actor (guint width, guint height) |
| 316 |
{ |
| 317 |
ClutterActor *group, *rect; |
| 318 |
ClutterColor clr = { 0xff, 0xff, 0xff, 0xff}; |
| 319 |
|
| 320 |
group = clutter_group_new (); |
| 321 |
rect = clutter_rectangle_new_with_color (&clr); |
| 322 |
clutter_actor_set_size (rect, width, height); |
| 323 |
clutter_actor_hide (rect); |
| 324 |
clutter_container_add_actor (CLUTTER_CONTAINER (group), rect); |
| 325 |
return group; |
| 326 |
} |
| 327 |
|
| 328 |
static void |
| 329 |
stop_and_quit (ClutterActor *actor, |
| 330 |
TestState *state) |
| 331 |
{ |
| 332 |
clutter_timeline_stop (state->timeline); |
| 333 |
clutter_main_quit (); |
| 334 |
} |
| 335 |
|
| 336 |
G_MODULE_EXPORT int |
| 337 |
test_cogl_vertex_buffer_main (int argc, char *argv[]) |
| 338 |
{ |
| 339 |
TestState state; |
| 340 |
ClutterActor *stage; |
| 341 |
gfloat stage_w, stage_h; |
| 342 |
gint dummy_width, dummy_height; |
| 343 |
|
| 344 |
if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS) |
| 345 |
return 1; |
| 346 |
|
| 347 |
stage = clutter_stage_new (); |
| 348 |
|
| 349 |
clutter_stage_set_title (CLUTTER_STAGE (stage), "Cogl Vertex Buffers"); |
| 350 |
clutter_stage_set_color (CLUTTER_STAGE (stage), CLUTTER_COLOR_Black); |
| 351 |
g_signal_connect (stage, "destroy", G_CALLBACK (stop_and_quit), &state); |
| 352 |
clutter_actor_get_size (stage, &stage_w, &stage_h); |
| 353 |
|
| 354 |
dummy_width = MESH_WIDTH * QUAD_WIDTH; |
| 355 |
dummy_height = MESH_HEIGHT * QUAD_HEIGHT; |
| 356 |
state.dummy = create_dummy_actor (dummy_width, dummy_height); |
| 357 |
clutter_container_add_actor (CLUTTER_CONTAINER (stage), state.dummy); |
| 358 |
clutter_actor_set_position (state.dummy, |
| 359 |
(stage_w / 2.0) - (dummy_width / 2.0), |
| 360 |
(stage_h / 2.0) - (dummy_height / 2.0)); |
| 361 |
|
| 362 |
state.timeline = clutter_timeline_new (1000); |
| 363 |
clutter_timeline_set_loop (state.timeline, TRUE); |
| 364 |
|
| 365 |
state.frame_id = g_signal_connect (state.timeline, |
| 366 |
"new-frame", |
| 367 |
G_CALLBACK (frame_cb), |
| 368 |
&state); |
| 369 |
|
| 370 |
g_signal_connect (state.dummy, "paint", G_CALLBACK (on_paint), &state); |
| 371 |
|
| 372 |
init_quad_mesh (&state); |
| 373 |
|
| 374 |
clutter_actor_show_all (stage); |
| 375 |
|
| 376 |
clutter_timeline_start (state.timeline); |
| 377 |
|
| 378 |
clutter_main (); |
| 379 |
|
| 380 |
cogl_handle_unref (state.buffer); |
| 381 |
cogl_handle_unref (state.indices); |
| 382 |
|
| 383 |
return 0; |
| 384 |
} |
| 385 |
|
| 386 |
G_MODULE_EXPORT const char * |
| 387 |
test_cogl_vertex_buffer_describe (void) |
| 388 |
{ |
| 389 |
return "Vertex buffers support in Cogl."; |
| 390 |
} |