| 1 |
#include <stdlib.h> |
| 2 |
#include <clutter/clutter.h> |
| 3 |
#include <math.h> |
| 4 |
#include <gmodule.h> |
| 5 |
#include <string.h> |
| 6 |
|
| 7 |
#define N_FIREWORKS 32 |
| 8 |
/* Units per second per second */ |
| 9 |
#define GRAVITY -1.5f |
| 10 |
|
| 11 |
#define N_SPARKS (N_FIREWORKS * 32) /* Must be a power of two */ |
| 12 |
#define TIME_PER_SPARK 0.01f /* in seconds */ |
| 13 |
|
| 14 |
#define TEXTURE_SIZE 32 |
| 15 |
|
| 16 |
typedef struct _Firework Firework; |
| 17 |
|
| 18 |
struct _Firework |
| 19 |
{ |
| 20 |
float size; |
| 21 |
float x, y; |
| 22 |
float start_x, start_y; |
| 23 |
ClutterColor color; |
| 24 |
|
| 25 |
/* Velocities are in units per second */ |
| 26 |
float initial_x_velocity; |
| 27 |
float initial_y_velocity; |
| 28 |
|
| 29 |
GTimer *timer; |
| 30 |
}; |
| 31 |
|
| 32 |
typedef struct _Spark Spark; |
| 33 |
|
| 34 |
struct _Spark |
| 35 |
{ |
| 36 |
float x, y; |
| 37 |
ClutterColor color; |
| 38 |
ClutterColor base_color; |
| 39 |
}; |
| 40 |
|
| 41 |
typedef struct _Data Data; |
| 42 |
|
| 43 |
struct _Data |
| 44 |
{ |
| 45 |
Firework fireworks[N_FIREWORKS]; |
| 46 |
|
| 47 |
int next_spark_num; |
| 48 |
Spark sparks[N_SPARKS]; |
| 49 |
GTimer *last_spark_time; |
| 50 |
|
| 51 |
CoglMaterial *material; |
| 52 |
}; |
| 53 |
|
| 54 |
static CoglHandle |
| 55 |
generate_round_texture (void) |
| 56 |
{ |
| 57 |
guint8 *p, *data; |
| 58 |
int x, y; |
| 59 |
CoglHandle tex; |
| 60 |
|
| 61 |
p = data = g_malloc (TEXTURE_SIZE * TEXTURE_SIZE * 4); |
| 62 |
|
| 63 |
/* Generate a yellow circle which gets transparent towards the edges */ |
| 64 |
for (y = 0; y < TEXTURE_SIZE; y++) |
| 65 |
for (x = 0; x < TEXTURE_SIZE; x++) |
| 66 |
{ |
| 67 |
int dx = x - TEXTURE_SIZE / 2; |
| 68 |
int dy = y - TEXTURE_SIZE / 2; |
| 69 |
float value = sqrtf (dx * dx + dy * dy) * 255.0 / (TEXTURE_SIZE / 2); |
| 70 |
if (value > 255.0f) |
| 71 |
value = 255.0f; |
| 72 |
value = 255.0f - value; |
| 73 |
*(p++) = value; |
| 74 |
*(p++) = value; |
| 75 |
*(p++) = value; |
| 76 |
*(p++) = value; |
| 77 |
} |
| 78 |
|
| 79 |
tex = cogl_texture_new_from_data (TEXTURE_SIZE, TEXTURE_SIZE, |
| 80 |
COGL_TEXTURE_NO_SLICING, |
| 81 |
COGL_PIXEL_FORMAT_RGBA_8888_PRE, |
| 82 |
COGL_PIXEL_FORMAT_ANY, |
| 83 |
TEXTURE_SIZE * 4, |
| 84 |
data); |
| 85 |
|
| 86 |
g_free (data); |
| 87 |
|
| 88 |
return tex; |
| 89 |
} |
| 90 |
|
| 91 |
static void |
| 92 |
paint_cb (ClutterActor *stage, Data *data) |
| 93 |
{ |
| 94 |
CoglMatrix old_matrix, new_matrix; |
| 95 |
int i; |
| 96 |
float diff_time; |
| 97 |
CoglHandle vbo; |
| 98 |
|
| 99 |
cogl_get_projection_matrix (&old_matrix); |
| 100 |
/* Use an orthogonal projection from -1 -> 1 in both axes */ |
| 101 |
cogl_matrix_init_identity (&new_matrix); |
| 102 |
cogl_set_projection_matrix (&new_matrix); |
| 103 |
|
| 104 |
cogl_push_matrix (); |
| 105 |
cogl_set_modelview_matrix (&new_matrix); |
| 106 |
|
| 107 |
/* Update all of the firework's positions */ |
| 108 |
for (i = 0; i < N_FIREWORKS; i++) |
| 109 |
{ |
| 110 |
Firework *firework = data->fireworks + i; |
| 111 |
|
| 112 |
if ((fabsf (firework->x - firework->start_x) > 2.0f) || |
| 113 |
firework->y < -1.0f) |
| 114 |
{ |
| 115 |
firework->size = g_random_double_range (0.001f, 0.1f); |
| 116 |
firework->start_x = 1.0f + firework->size; |
| 117 |
firework->start_y = -1.0f; |
| 118 |
firework->initial_x_velocity = g_random_double_range (-0.1f, -2.0f); |
| 119 |
firework->initial_y_velocity = g_random_double_range (0.1f, 4.0f); |
| 120 |
g_timer_reset (firework->timer); |
| 121 |
|
| 122 |
/* Pick a random color out of six */ |
| 123 |
if (g_random_boolean ()) |
| 124 |
{ |
| 125 |
memset (&firework->color, 0, sizeof (ClutterColor)); |
| 126 |
((guint8 *) &firework->color)[g_random_int_range (0, 3)] = 255; |
| 127 |
} |
| 128 |
else |
| 129 |
{ |
| 130 |
memset (&firework->color, 255, sizeof (ClutterColor)); |
| 131 |
((guint8 *) &firework->color)[g_random_int_range (0, 3)] = 0; |
| 132 |
} |
| 133 |
firework->color.alpha = 255; |
| 134 |
|
| 135 |
/* Fire some of the fireworks from the other side */ |
| 136 |
if (g_random_boolean ()) |
| 137 |
{ |
| 138 |
firework->start_x = -firework->start_x; |
| 139 |
firework->initial_x_velocity = -firework->initial_x_velocity; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
diff_time = g_timer_elapsed (firework->timer, NULL); |
| 144 |
|
| 145 |
firework->x = (firework->start_x + |
| 146 |
firework->initial_x_velocity * diff_time); |
| 147 |
|
| 148 |
firework->y = ((firework->initial_y_velocity * diff_time + |
| 149 |
0.5f * GRAVITY * diff_time * diff_time) + |
| 150 |
firework->start_y); |
| 151 |
} |
| 152 |
|
| 153 |
diff_time = g_timer_elapsed (data->last_spark_time, NULL); |
| 154 |
if (diff_time < 0.0f || diff_time >= TIME_PER_SPARK) |
| 155 |
{ |
| 156 |
/* Add a new spark for each firework, overwriting the oldest ones */ |
| 157 |
for (i = 0; i < N_FIREWORKS; i++) |
| 158 |
{ |
| 159 |
Spark *spark = data->sparks + data->next_spark_num; |
| 160 |
Firework *firework = data->fireworks + i; |
| 161 |
|
| 162 |
spark->x = (firework->x + |
| 163 |
g_random_double_range (-firework->size / 2.0f, |
| 164 |
firework->size / 2.0f)); |
| 165 |
spark->y = (firework->y + |
| 166 |
g_random_double_range (-firework->size / 2.0f, |
| 167 |
firework->size / 2.0f)); |
| 168 |
spark->base_color = firework->color; |
| 169 |
|
| 170 |
data->next_spark_num = (data->next_spark_num + 1) & (N_SPARKS - 1); |
| 171 |
} |
| 172 |
|
| 173 |
/* Update the colour of each spark */ |
| 174 |
for (i = 0; i < N_SPARKS; i++) |
| 175 |
{ |
| 176 |
float color_value; |
| 177 |
|
| 178 |
/* First spark is the oldest */ |
| 179 |
Spark *spark = data->sparks + ((data->next_spark_num + i) |
| 180 |
& (N_SPARKS - 1)); |
| 181 |
|
| 182 |
color_value = i / (N_SPARKS - 1.0f); |
| 183 |
spark->color.red = spark->base_color.red * color_value; |
| 184 |
spark->color.green = spark->base_color.green * color_value; |
| 185 |
spark->color.blue = spark->base_color.blue * color_value; |
| 186 |
spark->color.alpha = 255.0f * color_value; |
| 187 |
} |
| 188 |
|
| 189 |
g_timer_reset (data->last_spark_time); |
| 190 |
} |
| 191 |
|
| 192 |
vbo = cogl_vertex_buffer_new (N_SPARKS); |
| 193 |
cogl_vertex_buffer_add (vbo, "gl_Vertex", 2, |
| 194 |
COGL_ATTRIBUTE_TYPE_FLOAT, FALSE, |
| 195 |
sizeof (Spark), |
| 196 |
&data->sparks[0].x); |
| 197 |
cogl_vertex_buffer_add (vbo, "gl_Color", 4, |
| 198 |
COGL_ATTRIBUTE_TYPE_UNSIGNED_BYTE, TRUE, |
| 199 |
sizeof (Spark), |
| 200 |
&data->sparks[0].color.red); |
| 201 |
cogl_vertex_buffer_submit (vbo); |
| 202 |
|
| 203 |
cogl_set_source (data->material); |
| 204 |
cogl_vertex_buffer_draw (vbo, COGL_VERTICES_MODE_POINTS, 0, N_SPARKS); |
| 205 |
|
| 206 |
cogl_handle_unref (vbo); |
| 207 |
|
| 208 |
cogl_set_projection_matrix (&old_matrix); |
| 209 |
cogl_pop_matrix (); |
| 210 |
} |
| 211 |
|
| 212 |
static gboolean |
| 213 |
idle_cb (gpointer data) |
| 214 |
{ |
| 215 |
clutter_actor_queue_redraw (data); |
| 216 |
|
| 217 |
return G_SOURCE_CONTINUE; |
| 218 |
} |
| 219 |
|
| 220 |
G_MODULE_EXPORT int |
| 221 |
test_cogl_point_sprites_main (int argc, char *argv[]) |
| 222 |
{ |
| 223 |
ClutterActor *stage; |
| 224 |
CoglHandle tex; |
| 225 |
Data data; |
| 226 |
GError *error = NULL; |
| 227 |
int i; |
| 228 |
|
| 229 |
if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS) |
| 230 |
return EXIT_FAILURE; |
| 231 |
|
| 232 |
data.material = cogl_material_new (); |
| 233 |
data.last_spark_time = g_timer_new (); |
| 234 |
data.next_spark_num = 0; |
| 235 |
cogl_material_set_point_size (data.material, TEXTURE_SIZE); |
| 236 |
|
| 237 |
tex = generate_round_texture (); |
| 238 |
cogl_material_set_layer (data.material, 0, tex); |
| 239 |
cogl_handle_unref (tex); |
| 240 |
|
| 241 |
if (!cogl_material_set_layer_point_sprite_coords_enabled (data.material, |
| 242 |
0, TRUE, |
| 243 |
&error)) |
| 244 |
{ |
| 245 |
g_warning ("Failed to enable point sprite coords: %s", error->message); |
| 246 |
g_clear_error (&error); |
| 247 |
} |
| 248 |
|
| 249 |
for (i = 0; i < N_FIREWORKS; i++) |
| 250 |
{ |
| 251 |
data.fireworks[i].x = -FLT_MAX; |
| 252 |
data.fireworks[i].y = FLT_MAX; |
| 253 |
data.fireworks[i].size = 0.0f; |
| 254 |
data.fireworks[i].timer = g_timer_new (); |
| 255 |
} |
| 256 |
|
| 257 |
for (i = 0; i < N_SPARKS; i++) |
| 258 |
{ |
| 259 |
data.sparks[i].x = 2.0f; |
| 260 |
data.sparks[i].y = 2.0f; |
| 261 |
} |
| 262 |
|
| 263 |
stage = clutter_stage_new (); |
| 264 |
clutter_stage_set_color (CLUTTER_STAGE (stage), CLUTTER_COLOR_Black); |
| 265 |
clutter_stage_set_title (CLUTTER_STAGE (stage), "Cogl Point Sprites"); |
| 266 |
g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL); |
| 267 |
g_signal_connect_after (stage, "paint", G_CALLBACK (paint_cb), &data); |
| 268 |
|
| 269 |
clutter_actor_show (stage); |
| 270 |
|
| 271 |
clutter_threads_add_idle (idle_cb, stage); |
| 272 |
|
| 273 |
clutter_main (); |
| 274 |
|
| 275 |
cogl_object_unref (data.material); |
| 276 |
g_timer_destroy (data.last_spark_time); |
| 277 |
|
| 278 |
for (i = 0; i < N_FIREWORKS; i++) |
| 279 |
g_timer_destroy (data.fireworks[i].timer); |
| 280 |
|
| 281 |
return 0; |
| 282 |
} |
| 283 |
|
| 284 |
G_MODULE_EXPORT const char * |
| 285 |
test_cogl_point_sprites_describe (void) |
| 286 |
{ |
| 287 |
return "Point sprites support in Cogl."; |
| 288 |
} |