/
galileo.c
557 lines (478 loc) · 13 KB
/
galileo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#include "minisphere.h"
#include "galileo.h"
#include "color.h"
#include "shader.h"
#include "vector.h"
static void free_cached_uniform (group_t* group, const char* name);
static bool have_vertex_buffer (const shape_t* shape);
static void render_shape (shape_t* shape);
enum uniform_type
{
UNIFORM_INT,
UNIFORM_INT_VEC,
UNIFORM_FLOAT,
UNIFORM_FLOAT_VEC,
UNIFORM_MATRIX,
};
struct uniform
{
char name[256];
enum uniform_type type;
int vector_size;
union {
ALLEGRO_TRANSFORM mat_value;
int int_value;
int intvec[4];
float float_value;
float floatvec[4];
};
};
struct shape
{
unsigned int refcount;
unsigned int id;
image_t* texture;
shape_type_t type;
ALLEGRO_VERTEX* sw_vbuf;
int max_vertices;
int num_vertices;
vertex_t* vertices;
#ifdef MINISPHERE_USE_VERTEX_BUF
ALLEGRO_VERTEX_BUFFER* vbuf;
#endif
};
struct group
{
unsigned int refcount;
unsigned int id;
shader_t* shader;
vector_t* shapes;
matrix_t* transform;
vector_t* uniforms;
};
static shader_t* s_def_shader = NULL;
static unsigned int s_next_group_id = 0;
static unsigned int s_next_shape_id = 0;
void
initialize_galileo(void)
{
console_log(1, "initializing Galileo subsystem");
}
void
shutdown_galileo(void)
{
console_log(1, "shutting down Galileo subsystem");
shader_free(s_def_shader);
}
shader_t*
get_default_shader(void)
{
const char* fs_filename;
char* fs_pathname;
const char* vs_filename;
char* vs_pathname;
if (s_def_shader == NULL) {
console_log(3, "compiling Galileo default shaders");
vs_filename = kev_read_string(g_sys_conf, "GalileoVertShader", "shaders/galileo.vert");
fs_filename = kev_read_string(g_sys_conf, "GalileoFragShader", "shaders/galileo.frag");
vs_pathname = strdup(system_path(vs_filename));
fs_pathname = strdup(system_path(fs_filename));
s_def_shader = shader_new(vs_pathname, fs_pathname);
free(vs_pathname);
free(fs_pathname);
}
return s_def_shader;
}
vertex_t
vertex(float x, float y, float z, float u, float v, color_t color)
{
vertex_t vertex;
vertex.x = x;
vertex.y = y;
vertex.z = z;
vertex.u = u;
vertex.v = v;
vertex.color = color;
return vertex;
}
group_t*
group_new(shader_t* shader)
{
group_t* group;
console_log(4, "creating new group #%u", s_next_group_id);
group = calloc(1, sizeof(group_t));
group->shapes = vector_new(sizeof(shape_t*));
group->transform = matrix_new();
group->shader = shader_ref(shader);
group->uniforms = vector_new(sizeof(struct uniform));
group->id = s_next_group_id++;
return group_ref(group);
}
group_t*
group_ref(group_t* group)
{
if (group != NULL)
++group->refcount;
return group;
}
void
group_free(group_t* group)
{
shape_t** i_shape;
iter_t iter;
if (group == NULL || --group->refcount > 0)
return;
console_log(4, "disposing group #%u no longer in use", group->id);
iter = vector_enum(group->shapes);
while (i_shape = vector_next(&iter))
shape_free(*i_shape);
vector_free(group->shapes);
shader_free(group->shader);
matrix_free(group->transform);
vector_free(group->uniforms);
free(group);
}
shader_t*
group_get_shader(const group_t* group)
{
return group->shader;
}
matrix_t*
group_get_transform(const group_t* group)
{
return group->transform;
}
void
group_set_shader(group_t* group, shader_t* shader)
{
shader_t* old_shader;
old_shader = group->shader;
group->shader = shader_ref(shader);
shader_free(old_shader);
}
void
group_set_transform(group_t* group, matrix_t* transform)
{
matrix_t* old_transform;
old_transform = group->transform;
group->transform = matrix_ref(transform);
matrix_free(old_transform);
}
bool
group_add_shape(group_t* group, shape_t* shape)
{
console_log(4, "adding shape #%u to group #%u", shape->id, group->id);
shape = shape_ref(shape);
vector_push(group->shapes, &shape);
return true;
}
void
group_draw(const group_t* group, image_t* surface)
{
iter_t iter;
struct uniform* p;
if (surface != NULL)
al_set_target_bitmap(image_bitmap(surface));
#if defined(MINISPHERE_USE_SHADERS)
if (are_shaders_active()) {
shader_use(group->shader != NULL ? group->shader : get_default_shader());
iter = vector_enum(group->uniforms);
while (p = vector_next(&iter)) {
switch (p->type) {
case UNIFORM_FLOAT:
al_set_shader_float(p->name, p->float_value);
break;
case UNIFORM_FLOAT_VEC:
al_set_shader_float_vector(p->name, p->vector_size, p->floatvec, 1);
break;
case UNIFORM_INT:
al_set_shader_int(p->name, p->int_value);
break;
case UNIFORM_INT_VEC:
al_set_shader_int_vector(p->name, p->vector_size, p->intvec, 1);
break;
case UNIFORM_MATRIX:
al_set_shader_matrix(p->name, &p->mat_value);
break;
}
}
}
#endif
screen_transform(g_screen, group->transform);
iter = vector_enum(group->shapes);
while (vector_next(&iter))
render_shape(*(shape_t**)iter.ptr);
screen_transform(g_screen, NULL);
#if defined(MINISPHERE_USE_SHADERS)
shader_use(NULL);
#endif
if (surface != NULL)
al_set_target_bitmap(screen_backbuffer(g_screen));
}
void
group_put_float(group_t* group, const char* name, float value)
{
struct uniform unif;
free_cached_uniform(group, name);
strncpy(unif.name, name, 255);
unif.name[255] = '\0';
unif.type = UNIFORM_FLOAT;
unif.float_value = value;
vector_push(group->uniforms, &unif);
}
void
group_put_float_vector(group_t* group, const char* name, float values[], int size)
{
struct uniform unif;
free_cached_uniform(group, name);
strncpy(unif.name, name, 255);
unif.name[255] = '\0';
unif.type = UNIFORM_FLOAT_VEC;
unif.vector_size = size;
memcpy(unif.floatvec, values, sizeof(float) * size);
vector_push(group->uniforms, &unif);
}
void
group_put_int(group_t* group, const char* name, int value)
{
struct uniform unif;
free_cached_uniform(group, name);
strncpy(unif.name, name, 255);
unif.name[255] = '\0';
unif.type = UNIFORM_INT;
unif.int_value = value;
vector_push(group->uniforms, &unif);
}
void
group_put_int_vector(group_t* group, const char* name, int values[], int size)
{
struct uniform unif;
free_cached_uniform(group, name);
strncpy(unif.name, name, 255);
unif.name[255] = '\0';
unif.type = UNIFORM_INT_VEC;
unif.vector_size = size;
memcpy(unif.intvec, values, sizeof(int) * size);
vector_push(group->uniforms, &unif);
}
void
group_put_matrix(group_t* group, const char* name, const matrix_t* matrix)
{
struct uniform unif;
free_cached_uniform(group, name);
strncpy(unif.name, name, 255);
unif.name[255] = '\0';
unif.type = UNIFORM_MATRIX;
al_copy_transform(&unif.mat_value, matrix_transform(matrix));
vector_push(group->uniforms, &unif);
}
shape_t*
shape_new(shape_type_t type, image_t* texture)
{
shape_t* shape;
const char* type_name;
type_name = type == SHAPE_POINTS ? "point list"
: type == SHAPE_LINES ? "line list"
: type == SHAPE_LINE_LOOP ? "line loop"
: type == SHAPE_TRIANGLES ? "triangle list"
: type == SHAPE_TRI_FAN ? "triangle fan"
: type == SHAPE_TRI_STRIP ? "triangle strip"
: "automatic";
console_log(4, "creating shape #%u as %s", s_next_shape_id, type_name);
shape = calloc(1, sizeof(shape_t));
shape->texture = image_ref(texture);
shape->type = type;
shape->id = s_next_shape_id++;
return shape_ref(shape);
}
shape_t*
shape_ref(shape_t* shape)
{
if (shape != NULL)
++shape->refcount;
return shape;
}
void
shape_free(shape_t* shape)
{
if (shape == NULL || --shape->refcount > 0)
return;
console_log(4, "disposing shape #%u no longer in use", shape->id);
image_free(shape->texture);
#ifdef MINISPHERE_USE_VERTEX_BUF
if (shape->vbuf != NULL)
al_destroy_vertex_buffer(shape->vbuf);
#endif
free(shape->sw_vbuf);
free(shape->vertices);
free(shape);
}
float_rect_t
shape_bounds(const shape_t* shape)
{
float_rect_t bounds;
int i;
if (shape->num_vertices < 1)
return new_float_rect(0.0, 0.0, 0.0, 0.0);
bounds = new_float_rect(
shape->vertices[0].x, shape->vertices[0].y,
shape->vertices[0].x, shape->vertices[0].y);
for (i = 1; i < shape->num_vertices; ++i) {
bounds.x1 = fmin(shape->vertices[i].x, bounds.x1);
bounds.y1 = fmin(shape->vertices[i].y, bounds.y1);
bounds.x2 = fmax(shape->vertices[i].x, bounds.x2);
bounds.y2 = fmax(shape->vertices[i].y, bounds.y2);
}
return bounds;
}
image_t*
shape_texture(const shape_t* shape)
{
return shape->texture;
}
void
shape_set_texture(shape_t* shape, image_t* texture)
{
image_t* old_texture;
old_texture = shape->texture;
shape->texture = image_ref(texture);
image_free(old_texture);
shape_upload(shape);
}
bool
shape_add_vertex(shape_t* shape, vertex_t vertex)
{
int new_max;
vertex_t *new_buffer;
if (shape->num_vertices + 1 > shape->max_vertices) {
new_max = (shape->num_vertices + 1) * 2;
if (!(new_buffer = realloc(shape->vertices, new_max * sizeof(vertex_t))))
return false;
shape->vertices = new_buffer;
shape->max_vertices = new_max;
}
++shape->num_vertices;
shape->vertices[shape->num_vertices - 1] = vertex;
return true;
}
void
shape_calculate_uv(shape_t* shape)
{
// this assigns default UV coordinates to a shape's vertices. note that clockwise
// winding from top left is assumed; if the shape is wound any other way, the
// texture will be rotated accordingly. if this is not what you want, explicit U/V
// coordinates should be supplied.
double phi;
int i;
console_log(4, "auto-calculating U/V for shape #%u", shape->id);
for (i = 0; i < shape->num_vertices; ++i) {
// circumscribe the UV coordinate space.
// the circumcircle is rotated 135 degrees counterclockwise, which ensures
// that the top-left corner of a clockwise quad is mapped to (0,0).
phi = 2 * M_PI * i / shape->num_vertices - M_PI_4 * 3;
shape->vertices[i].u = cos(phi) * M_SQRT1_2 + 0.5;
shape->vertices[i].v = sin(phi) * -M_SQRT1_2 + 0.5;
}
}
void
shape_draw(shape_t* shape, matrix_t* matrix)
{
screen_transform(g_screen, matrix);
render_shape(shape);
screen_transform(g_screen, NULL);
}
void
shape_upload(shape_t* shape)
{
ALLEGRO_BITMAP* bitmap;
ALLEGRO_VERTEX* vertices = NULL;
int i;
console_log(3, "uploading shape #%u vertices to GPU", shape->id);
#ifdef MINISPHERE_USE_VERTEX_BUF
if (shape->vbuf != NULL)
al_destroy_vertex_buffer(shape->vbuf);
#endif
free(shape->sw_vbuf); shape->sw_vbuf = NULL;
bitmap = shape->texture != NULL ? image_bitmap(shape->texture) : NULL;
// create a vertex buffer
#ifdef MINISPHERE_USE_VERTEX_BUF
if (shape->vbuf = al_create_vertex_buffer(NULL, NULL, shape->num_vertices, ALLEGRO_PRIM_BUFFER_STATIC))
vertices = al_lock_vertex_buffer(shape->vbuf, 0, shape->num_vertices, ALLEGRO_LOCK_WRITEONLY);
#endif
if (vertices == NULL) {
// hardware buffer couldn't be created, fall back to software
console_log(3, "unable to create a VBO for shape #%u", shape->id);
if (!(shape->sw_vbuf = malloc(shape->num_vertices * sizeof(ALLEGRO_VERTEX))))
return;
vertices = shape->sw_vbuf;
}
// upload vertices
for (i = 0; i < shape->num_vertices; ++i) {
vertices[i].x = shape->vertices[i].x;
vertices[i].y = shape->vertices[i].y;
vertices[i].z = shape->vertices[i].z;
vertices[i].color = nativecolor(shape->vertices[i].color);
vertices[i].u = shape->vertices[i].u;
vertices[i].v = shape->vertices[i].v;
}
// unlock hardware buffer, if applicable
#ifdef MINISPHERE_USE_VERTEX_BUF
if (vertices != shape->sw_vbuf)
al_unlock_vertex_buffer(shape->vbuf);
else if (shape->vbuf != NULL) {
al_destroy_vertex_buffer(shape->vbuf);
shape->vbuf = NULL;
}
#endif
}
static bool
have_vertex_buffer(const shape_t* shape)
{
#ifdef MINISPHERE_USE_VERTEX_BUF
return shape->vbuf != NULL || shape->sw_vbuf != NULL;
#else
return shape->sw_vbuf != NULL;
#endif
}
static void
free_cached_uniform(group_t* group, const char* name)
{
iter_t iter;
struct uniform* p;
iter = vector_enum(group->uniforms);
while (p = vector_next(&iter)) {
if (strcmp(p->name, name) == 0)
iter_remove(&iter);
}
}
static void
render_shape(shape_t* shape)
{
ALLEGRO_BITMAP* bitmap;
int draw_mode;
if (shape->num_vertices == 0)
return;
if (!have_vertex_buffer(shape))
shape_upload(shape);
if (shape->type == SHAPE_AUTO)
draw_mode = shape->num_vertices == 1 ? ALLEGRO_PRIM_POINT_LIST
: shape->num_vertices == 2 ? ALLEGRO_PRIM_LINE_LIST
: ALLEGRO_PRIM_TRIANGLE_STRIP;
else
draw_mode = shape->type == SHAPE_LINES ? ALLEGRO_PRIM_LINE_LIST
: shape->type == SHAPE_LINE_LOOP ? ALLEGRO_PRIM_LINE_LOOP
: shape->type == SHAPE_LINE_STRIP ? ALLEGRO_PRIM_LINE_STRIP
: shape->type == SHAPE_TRIANGLES ? ALLEGRO_PRIM_TRIANGLE_LIST
: shape->type == SHAPE_TRI_STRIP ? ALLEGRO_PRIM_TRIANGLE_STRIP
: shape->type == SHAPE_TRI_FAN ? ALLEGRO_PRIM_TRIANGLE_FAN
: ALLEGRO_PRIM_POINT_LIST;
bitmap = shape->texture != NULL ? image_bitmap(shape->texture) : NULL;
#ifdef MINISPHERE_USE_VERTEX_BUF
if (shape->vbuf != NULL)
al_draw_vertex_buffer(shape->vbuf, bitmap, 0, shape->num_vertices, draw_mode);
else
al_draw_prim(shape->sw_vbuf, NULL, bitmap, 0, shape->num_vertices, draw_mode);
#else
al_draw_prim(shape->sw_vbuf, NULL, bitmap, 0, shape->num_vertices, draw_mode);
#endif
}