-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeometry.c
523 lines (440 loc) · 16.6 KB
/
geometry.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
/**
@file geometry.c
@author Alejandro Ambroa
@date 1 Oct 2017
@brief Game objects for pong3d and utils functions for geometry tranformations.
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "geometry.h"
#include "renderer.h"
#include "math_constants.h"
#define OVERLAY_ALPHA 0.8f
PONG_ELEMENT player_stick;
PONG_ELEMENT opponent_stick;
PONG_ELEMENT ball;
PONG_ELEMENT stage;
PONG_ELEMENT ball_shadow;
PONG_ELEMENT stick_shadow;
PONG_ELEMENT ball_mark;
PONG_ELEMENT overlay;
PONG_ELEMENT startText;
float opponent_z_coord;
void create_projection_matrix(float fovy, float aspect_ratio, float near_plane, float far_plane, float* out)
{
const float
y_scale
= 1.0f / (float)tan(M_PI / 180.0f * (fovy / 2.0f)),
x_scale = y_scale / aspect_ratio,
frustum_length = far_plane - near_plane;
out[1] = 0.0f;
out[2] = 0.0f;
out[3] = 0.0f;
out[4] = 0.0f;
out[6] = 0.0f;
out[7] = 0.0f;
out[8] = 0.0f;
out[9] = 0.0f;
out[12] = 0.0f;
out[13] = 0.0f;
out[15] = 0.0f;
out[0] = x_scale;
out[5] = y_scale;
out[10] = -((far_plane + near_plane) / frustum_length);
out[11] = -1;
out[14] = -((2 * near_plane * far_plane) / frustum_length);
}
void load_identity_matrix(float* out)
{
out[0] = 1.0f;
out[1] = 0.0f;
out[2] = 0.0f;
out[3] = 0.0f;
out[4] = 0.0f;
out[5] = 1.0f;
out[6] = 0.0f;
out[7] = 0.0f;
out[8] = 0.0f;
out[9] = 0.0f;
out[10] = 1.0f;
out[11] = 0.0f;
out[12] = 0.0f;
out[13] = 0.0f;
out[14] = 0.0f;
out[15] = 1.0f;
}
void assign_position_to_vertex(float* dest, int dest_index, float x, float y, float z)
{
int dest_offset = dest_index * VERTEX_SIZE;
dest[dest_offset] = x;
dest[dest_offset + 1] = y;
dest[dest_offset + 2] = z;
dest[dest_offset + 3] = 1.0f;
}
void cp_position_to_vertex(float* src, float* dest, int src_index, int dest_index)
{
int src_offset = src_index * 3;
int dest_offset = dest_index * VERTEX_SIZE;
dest[dest_offset] = src[src_offset];
dest[dest_offset + 1] = src[src_offset + 1];
dest[dest_offset + 2] = src[src_offset + 2];
dest[dest_offset + 3] = 1.0f;
}
void assign_color_to_vertex(float* vertex_buffer, int index, float r, float g, float b, float a)
{
int offset = index * VERTEX_SIZE + 4;
vertex_buffer[offset] = r;
vertex_buffer[offset + 1] = g;
vertex_buffer[offset + 2] = b;
vertex_buffer[offset + 3] = a;
}
void assign_uv_to_vertex(float* vertex_buffer, int index, float u, float v)
{
int offset = index * VERTEX_SIZE + 12;
vertex_buffer[offset] = u;
vertex_buffer[offset + 1] = v;
}
/*
Algorithm to build vertex indices of common mesh.
*/
void emit_mesh_triangle_pair(int index, int num_base_vertices, int* triangle1, int* triangle2)
{
int init_line = ((index + 1) % num_base_vertices) == 0;
triangle1[0] = index;
triangle1[1] = index + num_base_vertices;
triangle1[2] = init_line ? index + 1 : index + (num_base_vertices + 1);
triangle2[0] = index;
triangle2[1] = init_line ? index + 1 : index + (num_base_vertices + 1);
triangle2[2] = init_line ? index - (num_base_vertices - 1) : index + 1;
}
void setup_stick(PONG_ELEMENT* stick, float stick_width, float stick_height, const float* color)
{
stick->vertexType = GL_TRIANGLES;
stick->width = stick_width;
stick->height = stick_height;
stick->width2 = stick->width / 2.0f;
stick->height2 = stick->height / 2.0f;
stick->vertex = (float*)calloc(4 * VERTEX_SIZE, sizeof(float));
stick->vertex_count = 4;
stick->elements = (unsigned int*)malloc(sizeof(unsigned int) * 6);
stick->elements_count = 6;
assign_position_to_vertex(stick->vertex, 0, -stick->width2, -stick->height2, 0.0f);
assign_color_to_vertex(stick->vertex, 0, color[0], color[1], color[2], color[3]);
assign_uv_to_vertex(stick->vertex, 0, 0, 0);
assign_position_to_vertex(stick->vertex, 1, -stick->width2, stick->height2, 0.0f);
assign_color_to_vertex(stick->vertex, 1, color[0], color[1], color[2], color[3]);
assign_uv_to_vertex(stick->vertex, 1, 0, 1);
assign_position_to_vertex(stick->vertex, 2, stick->width2, stick->height2, 0.0f);
assign_color_to_vertex(stick->vertex, 2, color[0], color[1], color[2], color[3]);
assign_uv_to_vertex(stick->vertex, 2, 1, 1);
assign_position_to_vertex(stick->vertex, 3, stick->width2, -stick->height2, 0.0f);
assign_color_to_vertex(stick->vertex, 3, color[0], color[1], color[2], color[3]);
assign_uv_to_vertex(stick->vertex, 3, 1, 0);
stick->elements[0] = 0;
stick->elements[1] = 1;
stick->elements[2] = 2;
stick->elements[3] = 2;
stick->elements[4] = 3;
stick->elements[5] = 0;
load_identity_matrix(stick->model_matrix);
}
void setup_overlay(PONG_ELEMENT* pOverlay, float alpha, float stage_width, float stage_height)
{
pOverlay->width = stage_width;
pOverlay->height = stage_height;
pOverlay->vertexType = GL_TRIANGLES;
pOverlay->width2 = pOverlay->width / 2.0f;
pOverlay->height2 = pOverlay->height / 2.0f;
pOverlay->vertex = (float*)calloc(4 * VERTEX_SIZE, sizeof(float));
pOverlay->vertex_count = 4;
pOverlay->elements = (unsigned int*)malloc(sizeof(unsigned int) * 6);
pOverlay->elements_count = 6;
assign_position_to_vertex(pOverlay->vertex, 0, -pOverlay->width2, -pOverlay->height2, 0.0f);
assign_color_to_vertex(pOverlay->vertex, 0, 0.0f, 0.0f, 0.0f, alpha);
assign_position_to_vertex(pOverlay->vertex, 1, -pOverlay->width2, pOverlay->height2, 0.0f);
assign_color_to_vertex(pOverlay->vertex, 1, 0.0f, 0.0f, 0.0f, alpha);
assign_position_to_vertex(pOverlay->vertex, 2, pOverlay->width2, pOverlay->height2, 0.0f);
assign_color_to_vertex(pOverlay->vertex, 2, 0.0f, 0.0f, 0.0f, alpha);
assign_position_to_vertex(pOverlay->vertex, 3, pOverlay->width2, -pOverlay->height2, 0.0f);
assign_color_to_vertex(pOverlay->vertex, 3, 0.0f, 0.0f, 0.0f, alpha);
pOverlay->elements[0] = 0;
pOverlay->elements[1] = 1;
pOverlay->elements[2] = 2;
pOverlay->elements[3] = 2;
pOverlay->elements[4] = 3;
pOverlay->elements[5] = 0;
load_identity_matrix(pOverlay->model_matrix);
pOverlay->model_matrix[14] = 0.0f;
}
void setup_stage(PONG_ELEMENT* pStage,
int window_width,
int window_height,
int blocks,
float width,
float large,
const float* color)
{
int vertex = 0;
float aspect = (float)window_width / window_height;
float x_width, y_height;
pStage->width = width;
pStage->height = width / aspect;
pStage->large = large * blocks;
pStage->width2 = pStage->width / 2.0f;
pStage->height2 = pStage->height / 2.0f;
pStage->vertexType = GL_QUADS;
x_width = pStage->width2;
y_height = pStage->height2;
pStage->vertex_count = blocks * 16;
pStage->elements_count = 0;
pStage->vertex = (float*)calloc(VERTEX_SIZE * pStage->vertex_count, sizeof(float));
// first create temporal vertices for 3d box (4 vetices up and 4 vertices bottom)
int tmp_vertex_count = blocks * 8;
float* tmp_vertex = (float*)malloc(sizeof(float) * 3 * tmp_vertex_count);
for (int i = 0; i <= blocks; i++) {
float z_depth = (-large * (float)i);
tmp_vertex[vertex++] = -x_width;
tmp_vertex[vertex++] = y_height;
tmp_vertex[vertex++] = z_depth;
tmp_vertex[vertex++] = x_width;
tmp_vertex[vertex++] = y_height;
tmp_vertex[vertex++] = z_depth;
tmp_vertex[vertex++] = x_width;
tmp_vertex[vertex++] = -y_height;
tmp_vertex[vertex++] = z_depth;
tmp_vertex[vertex++] = -x_width;
tmp_vertex[vertex++] = -y_height;
tmp_vertex[vertex++] = z_depth;
}
// now join vertices for build quads using intermediate triangles
int triangle1[3];
int triangle2[3];
vertex = 0;
float alpha = color[3];
for (int i = 0, j = 0; i < blocks * 4; i++, j += 4) {
emit_mesh_triangle_pair(i, 4, triangle1, triangle2);
// each block has got a darker color
if (i > 0 && i % 4 == 0) {
alpha /= 2.0f;
}
cp_position_to_vertex(tmp_vertex, pStage->vertex, triangle1[0], j);
assign_color_to_vertex(pStage->vertex, j, color[0], color[1], color[2], alpha);
cp_position_to_vertex(tmp_vertex, pStage->vertex, triangle1[1], j + 1);
assign_color_to_vertex(pStage->vertex, j + 1, color[0], color[1], color[2], alpha);
cp_position_to_vertex(tmp_vertex, pStage->vertex, triangle1[2], j + 2);
assign_color_to_vertex(pStage->vertex, j + 2, color[0], color[1], color[2], alpha);
cp_position_to_vertex(tmp_vertex, pStage->vertex, triangle2[2], j + 3);
assign_color_to_vertex(pStage->vertex, j + 3, color[0], color[1], color[2], alpha);
}
free(tmp_vertex);
load_identity_matrix(pStage->model_matrix);
}
void setup_ball(PONG_ELEMENT* pBall, int segments, float radius, const float* color)
{
float theta;
float phi;
int vertex = 0;
int m, p;
int triangle1[3];
int triangle2[3];
float unit_angle = P_2PI / segments;
pBall->vertexType = GL_TRIANGLES;
pBall->width = radius;
pBall->vertex_count = segments * segments;
pBall->elements_count = (pBall->vertex_count * 6);
pBall->vertex = (float*)calloc(VERTEX_SIZE * pBall->vertex_count, sizeof(float));
pBall->elements = (unsigned int*)calloc(pBall->elements_count, sizeof(unsigned int));
for (p = 0, theta = (float)-M_PI_2; p < segments; p++, theta += unit_angle) {
for (m = 0, phi = 0.0f; m < segments; m++, phi += unit_angle) {
assign_position_to_vertex(
pBall->vertex, vertex,
cosf(theta) * sinf(phi) * pBall->width,
sinf(theta) * pBall->width,
cosf(theta) * cosf(phi) * pBall->width);
assign_color_to_vertex(pBall->vertex, vertex, color[0], color[1], color[2], color[3]);
vertex++;
}
}
for (int i = 0, j = 0; i < (pBall->vertex_count - segments); i++, j += 6) {
emit_mesh_triangle_pair(i, segments, triangle1, triangle2);
pBall->elements[j] = triangle1[0];
pBall->elements[j + 1] = triangle1[1];
pBall->elements[j + 2] = triangle1[2];
pBall->elements[j + 3] = triangle2[0];
pBall->elements[j + 4] = triangle2[1];
pBall->elements[j + 5] = triangle2[2];
}
load_identity_matrix(pBall->model_matrix);
}
void build_circle(PONG_ELEMENT* element, float radius, int segments, const float* color)
{
element->vertex_count = (segments + 2);
element->vertex = (float*)calloc(element->vertex_count * VERTEX_SIZE, sizeof(float));
element->elements_count = 0;
element->vertexType = GL_TRIANGLE_FAN;
int vertex = 1;
int p;
float unit_angle = P_2PI / (float)segments;
float theta;
element->width = radius;
assign_position_to_vertex(element->vertex, 0, 0.0f, 0.0f, 0.0f);
assign_color_to_vertex(element->vertex, 0, color[0], color[1], color[2], color[3]);
for (p = 0, theta = 0; p < element->vertex_count - 1; p++, theta -= unit_angle) {
assign_position_to_vertex(element->vertex, vertex, cosf(theta) * element->width, sinf(theta) * element->width, 0);
assign_color_to_vertex(element->vertex, vertex, color[0], color[1], color[2], color[3]);
vertex++;
}
load_identity_matrix(element->model_matrix);
element->model_matrix[12] = -0.2f;
}
void setup_ball_shadow(PONG_ELEMENT* element, int segments, float radius, const float* color)
{
load_identity_matrix(element->model_matrix);
build_circle(element, radius, segments, color);
}
void setup_ball_marks(PONG_ELEMENT* element, int segments, float radius, const float* color)
{
load_identity_matrix(element->model_matrix);
build_circle(element, radius, segments, color);
element->model_matrix[14] = 0.02f;
}
void setup_stick_shadows(PONG_ELEMENT* element, float width, float height, const float* color)
{
int elements[] = { 0, 1, 2, 2, 3, 0 };
element->vertex = (float*)calloc(4 * VERTEX_SIZE, sizeof(float));
element->elements = (unsigned int*)malloc(sizeof(unsigned int) * 6);
element->vertex_count = 4;
element->elements_count = 6;
element->width = width;
element->height = height;
element->z = 0.0f;
element->vertexType = GL_TRIANGLES;
float width2 = element->width / 2.0f;
float height2 = element->height / 2.0f;
element->width2 = width2;
element->height2 = height2;
assign_position_to_vertex(element->vertex, 0, -width2, -height2, element->z);
assign_color_to_vertex(element->vertex, 0, color[0], color[1], color[2], color[3]);
assign_position_to_vertex(element->vertex, 1, -width2, height2, element->z);
assign_color_to_vertex(element->vertex, 1, color[0], color[1], color[2], color[3]);
assign_position_to_vertex(element->vertex, 2, width2, height2, element->z);
assign_color_to_vertex(element->vertex, 2, color[0], color[1], color[2], color[3]);
assign_position_to_vertex(element->vertex, 3, width2, -height2, element->z);
assign_color_to_vertex(element->vertex, 3, color[0], color[1], color[2], color[3]);
memcpy(element->elements, elements, sizeof(elements));
load_identity_matrix(element->model_matrix);
}
void create_elements(int window_width, int window_height, int stage_blocks)
{
/**
Config for geometry of all objects in game
: */
float stage_width = 1.0f;
float stage_large = 1.5f;
float blocks_large = stage_large / stage_blocks;
float stage_color[] = { 0.0f, 1.0f, 0.0f, 0.2f };
float overlay_alpha = OVERLAY_ALPHA;
float aspect = (float)window_width / window_height;
float stick_width = stage_width / 6.0f;
float stick_color[] = { 0.5f, 0.5f, 0.5f, 0.5f };
int ball_segments = 20;
float ball_radius = stage_large / 80.0f;
float ball_color[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
float shadows_color[] = { 1.0f, 1.0f, 1.0f, 0.2f };
opponent_z_coord = -stage_blocks * blocks_large;
setup_stage(&stage, window_width, window_height, stage_blocks, stage_width, blocks_large, stage_color);
// setup_stage sets height of stage in relation to window aspect.
// height is read from stage object in last parameter.
setup_overlay(&overlay, overlay_alpha, stage_width, stage.height);
setup_stick(&opponent_stick, stick_width, stick_width / aspect, stick_color);
setup_ball(&ball, ball_segments, ball_radius, ball_color);
setup_ball_shadow(&ball_shadow, ball_segments, ball_radius, shadows_color);
setup_ball_marks(&ball_mark, ball_segments, ball_radius / 2.0f, ball_color);
setup_stick(&player_stick, stick_width, stick_width / aspect, stick_color);
setup_stick_shadows(&stick_shadow, stick_width, ball_radius, shadows_color);
reset_player_stick_position();
reset_opponent_stick_position();
upload_to_renderer(&stage);
upload_to_renderer(&overlay);
upload_to_renderer(&player_stick);
upload_to_renderer(&opponent_stick);
upload_to_renderer(&ball);
upload_to_renderer(&ball_shadow);
upload_to_renderer(&ball_mark);
upload_to_renderer(&stick_shadow);
}
void free_pong_element(PONG_ELEMENT* element)
{
if (element->vertex) {
free(element->vertex);
}
if (element->elements_count > 0 && element->elements) {
free(element->elements);
}
if (element->uploaded) {
remove_to_renderer(element);
}
}
void dispose_elements()
{
free_pong_element(&player_stick);
free_pong_element(&opponent_stick);
free_pong_element(&ball);
free_pong_element(&stage);
free_pong_element(&ball_shadow);
free_pong_element(&stick_shadow);
free_pong_element(&ball_mark);
}
void reset_player_stick_position()
{
player_stick.x = 0.0f;
player_stick.y = 0.0f;
player_stick.xprev = 0.0f;
player_stick.yprev = 0.0f;
player_stick.z = 0.0f;
}
void reset_opponent_stick_position()
{
opponent_stick.x = 0.0f;
opponent_stick.y = 0.0f;
opponent_stick.xprev = 0.0f;
opponent_stick.yprev = 0.0f;
opponent_stick.z = opponent_z_coord;
opponent_stick.model_matrix[12] = 0.0;
opponent_stick.model_matrix[13] = 0.0;
opponent_stick.model_matrix[14] = opponent_stick.z;
}
void move_player_stick(float x, float y)
{
player_stick.x = x;
player_stick.y = y;
player_stick.model_matrix[12] = x;
player_stick.model_matrix[13] = y;
}
void move_opponent_stick(float x, float y)
{
opponent_stick.x = x;
opponent_stick.y = y;
opponent_stick.model_matrix[12] = x;
opponent_stick.model_matrix[13] = y;
}
void reset_ball_position()
{
ball.x = 0.0f;
ball.y = 0.0f;
ball.z = player_stick.z - ball.width;
ball.model_matrix[12] = 0.0;
ball.model_matrix[13] = 0.0;
ball.model_matrix[14] = ball.z;
}
void move_ball(float x, float y, float z)
{
ball.x = x;
ball.y = y;
ball.z = z;
ball.model_matrix[12] = x;
ball.model_matrix[13] = y;
ball.model_matrix[14] = z;
}