forked from 15-466/15-466-f20-base0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PongMode.cpp
654 lines (541 loc) · 25 KB
/
PongMode.cpp
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
#include "PongMode.hpp"
//for the GL_ERRORS() macro:
#include "gl_errors.hpp"
//for glm::value_ptr() :
#include <glm/gtc/type_ptr.hpp>
#include <random>
#include <math.h>
PongMode::PongMode() {
//set up trail as if ball has been here for 'forever':
ball_trail.clear();
ball_trail.emplace_back(ball, trail_length);
ball_trail.emplace_back(ball, 0.0f);
//----- allocate OpenGL resources -----
{ //vertex buffer:
glGenBuffers(1, &vertex_buffer);
//for now, buffer will be un-filled.
GL_ERRORS(); //PARANOIA: print out any OpenGL errors that may have happened
}
{ //vertex array mapping buffer for color_texture_program:
//ask OpenGL to fill vertex_buffer_for_color_texture_program with the name of an unused vertex array object:
glGenVertexArrays(1, &vertex_buffer_for_color_texture_program);
//set vertex_buffer_for_color_texture_program as the current vertex array object:
glBindVertexArray(vertex_buffer_for_color_texture_program);
//set vertex_buffer as the source of glVertexAttribPointer() commands:
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
//set up the vertex array object to describe arrays of PongMode::Vertex:
glVertexAttribPointer(
color_texture_program.Position_vec4, //attribute
3, //size
GL_FLOAT, //type
GL_FALSE, //normalized
sizeof(Vertex), //stride
(GLbyte *)0 + 0 //offset
);
glEnableVertexAttribArray(color_texture_program.Position_vec4);
//[Note that it is okay to bind a vec3 input to a vec4 attribute -- the w component will be filled with 1.0 automatically]
glVertexAttribPointer(
color_texture_program.Color_vec4, //attribute
4, //size
GL_UNSIGNED_BYTE, //type
GL_TRUE, //normalized
sizeof(Vertex), //stride
(GLbyte *)0 + 4*3 //offset
);
glEnableVertexAttribArray(color_texture_program.Color_vec4);
glVertexAttribPointer(
color_texture_program.TexCoord_vec2, //attribute
2, //size
GL_FLOAT, //type
GL_FALSE, //normalized
sizeof(Vertex), //stride
(GLbyte *)0 + 4*3 + 4*1 //offset
);
glEnableVertexAttribArray(color_texture_program.TexCoord_vec2);
//done referring to vertex_buffer, so unbind it:
glBindBuffer(GL_ARRAY_BUFFER, 0);
//done setting up vertex array object, so unbind it:
glBindVertexArray(0);
GL_ERRORS(); //PARANOIA: print out any OpenGL errors that may have happened
}
{ //solid white texture:
//ask OpenGL to fill white_tex with the name of an unused texture object:
glGenTextures(1, &white_tex);
//bind that texture object as a GL_TEXTURE_2D-type texture:
glBindTexture(GL_TEXTURE_2D, white_tex);
//upload a 1x1 image of solid white to the texture:
glm::uvec2 size = glm::uvec2(1,1);
std::vector< glm::u8vec4 > data(size.x*size.y, glm::u8vec4(0xff, 0xff, 0xff, 0xff));
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.x, size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data.data());
//set filtering and wrapping parameters:
//(it's a bit silly to mipmap a 1x1 texture, but I'm doing it because you may want to use this code to load different sizes of texture)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//since texture uses a mipmap and we haven't uploaded one, instruct opengl to make one for us:
glGenerateMipmap(GL_TEXTURE_2D);
//Okay, texture uploaded, can unbind it:
glBindTexture(GL_TEXTURE_2D, 0);
GL_ERRORS(); //PARANOIA: print out any OpenGL errors that may have happened
}
}
PongMode::~PongMode() {
//----- free OpenGL resources -----
glDeleteBuffers(1, &vertex_buffer);
vertex_buffer = 0;
glDeleteVertexArrays(1, &vertex_buffer_for_color_texture_program);
vertex_buffer_for_color_texture_program = 0;
glDeleteTextures(1, &white_tex);
white_tex = 0;
}
bool PongMode::handle_event(SDL_Event const &evt, glm::uvec2 const &window_size) {
// if (evt.type == SDL_MOUSEMOTION) {
// //convert mouse from window pixels (top-left origin, +y is down) to clip space ([-1,1]x[-1,1], +y is up):
// glm::vec2 clip_mouse = glm::vec2(
// (evt.motion.x + 0.5f) / window_size.x * 2.0f - 1.0f,
// (evt.motion.y + 0.5f) / window_size.y *-2.0f + 1.0f
// );
// left_paddle.y = (clip_to_court * glm::vec3(clip_mouse, 1.0f)).y;
// }
if (evt.type == SDL_KEYDOWN) {
if (evt.key.keysym.sym == SDLK_DOWN) {
ball_dir -= 0.1f;
} else if (evt.key.keysym.sym == SDLK_UP) {
ball_dir += 0.1f;
} else if (evt.key.keysym.sym == SDLK_SPACE && draw_arrow) {
draw_arrow = false;
ball_velocity = glm::vec2(4.0f * cos(ball_dir), 4.0f * sin(ball_dir));
}
}
return false;
}
void PongMode::update(float elapsed) {
static std::mt19937 mt; //mersenne twister pseudo-random number generator
// timer accumulation
timer += elapsed;
if (timer > reveal_checkpoint &&
fall_through[0] == 0 && fall_through[1] == 0 &&
fall_through[2] == 0 && fall_through[3] == 0) {
static std::mt19937 mt;
fall_through[(mt() >> 3) % 4] = 1;
}
if ((draw_ok || draw_not_ok) && timer > reveal_checkpoint) {
draw_ok = false;
draw_not_ok = false;
ball.x = 0.0f;
ball.y = 0.0f;
ball_velocity.x = 0.0f;
ball_velocity.y = 0.0f;
draw_arrow = true;
timer = 0.0f;
for (int i = 0; i < 4; i++) {
fall_through[i] = 0;
}
fallen_through = false;
}
//----- paddle update -----
{ // right player ai:
ai_offset_update -= elapsed;
float div = std::min((float) (num_win + 1), 3.0f);
if (ai_offset_update < elapsed) {
//update again in [0.5,1.0) seconds:
ai_offset_update = (mt() / float(mt.max())) * 0.5f + 0.5f;
ai_offset = (mt() / float(mt.max())) * 2.5f - 1.25f;
}
float divt = fall_through[0] == 0 ? div : div * 1.5f;
float divb = fall_through[2] == 0 ? div : div * 1.5f;
float divl = fall_through[3] == 0 ? div : div * 1.5f;
float divr = fall_through[1] == 0 ? div : div * 1.5f;
if (right_paddle.y < ball.y + ai_offset) {
right_paddle.y = std::min(ball.y + ai_offset, right_paddle.y + divr * elapsed);
} else {
right_paddle.y = std::max(ball.y + ai_offset, right_paddle.y - divr * elapsed);
}
// bottom player ai
if (bottom_paddle.x < ball.x + ai_offset) {
bottom_paddle.x = std::min(ball.x + ai_offset, bottom_paddle.x + divb * elapsed);
} else {
bottom_paddle.x = std::max(ball.x + ai_offset, bottom_paddle.x - divb * elapsed);
}
// top player ai
if (top_paddle.x < ball.x + ai_offset) {
top_paddle.x = std::min(ball.x + ai_offset, top_paddle.x + divt * elapsed);
} else {
top_paddle.x = std::max(ball.x + ai_offset, top_paddle.x - divt * elapsed);
}
// left player ai
if (left_paddle.y < ball.y + ai_offset) {
left_paddle.y = std::min(ball.y + ai_offset, left_paddle.y + divl * elapsed);
} else {
left_paddle.y = std::max(ball.y + ai_offset, left_paddle.y - divl * elapsed);
}
}
// //clamp paddles to court:
right_paddle.y = std::max(right_paddle.y, -court_radius.y + paddle_radius.y);
right_paddle.y = std::min(right_paddle.y, court_radius.y - paddle_radius.y);
top_paddle.x = std::max(top_paddle.x, -court_radius.x + paddle_radius_h.x);
top_paddle.x = std::min(top_paddle.x, court_radius.x - paddle_radius_h.x);
bottom_paddle.x = std::max(bottom_paddle.x, -court_radius.x + paddle_radius_h.x);
bottom_paddle.x = std::min(bottom_paddle.x, court_radius.x - paddle_radius_h.x);
left_paddle.y = std::max(left_paddle.y, -court_radius.y + paddle_radius.y);
left_paddle.y = std::min(left_paddle.y, court_radius.y - paddle_radius.y);
//----- ball update -----
//speed of ball doubles every four points:
// float speed_multiplier = 4.0f * std::pow(2.0f, (left_score + right_score) / 4.0f);
//velocity cap, though (otherwise ball can pass through paddles):
// speed_multiplier = std::min(speed_multiplier, 10.0f);
float mult = std::pow(1.5f, num_win);
ball += elapsed * (std::min(mult, 3.0f)) * ball_velocity;
//---- collision handling ----
//paddles:
auto paddle_vs_ball = [this](glm::vec2 const &paddle) {
//compute area of overlap:
glm::vec2 min = glm::max(paddle - paddle_radius, ball - ball_radius);
glm::vec2 max = glm::min(paddle + paddle_radius, ball + ball_radius);
//if no overlap, no collision:
if (min.x > max.x || min.y > max.y) return false;
if (max.x - min.x > max.y - min.y) {
//wider overlap in x => bounce in y direction:
if (ball.y > paddle.y) {
ball.y = paddle.y + paddle_radius.y + ball_radius.y;
ball_velocity.y = std::abs(ball_velocity.y);
} else {
ball.y = paddle.y - paddle_radius.y - ball_radius.y;
ball_velocity.y = -std::abs(ball_velocity.y);
}
} else {
//wider overlap in y => bounce in x direction:
if (ball.x > paddle.x) {
ball.x = paddle.x + paddle_radius.x + ball_radius.x;
ball_velocity.x = std::abs(ball_velocity.x);
} else {
ball.x = paddle.x - paddle_radius.x - ball_radius.x;
ball_velocity.x = -std::abs(ball_velocity.x);
}
//warp y velocity based on offset from paddle center:
float vel = (ball.y - paddle.y) / (paddle_radius.y + ball_radius.y);
ball_velocity.y = glm::mix(ball_velocity.y, vel, 0.75f);
}
return true;
};
auto paddle_vs_ball_h = [this](glm::vec2 const &paddle) {
//compute area of overlap:
glm::vec2 min = glm::max(paddle - paddle_radius_h, ball - ball_radius);
glm::vec2 max = glm::min(paddle + paddle_radius_h, ball + ball_radius);
//if no overlap, no collision:
if (min.x > max.x || min.y > max.y) return false;
if (max.x - min.x > max.y - min.y) {
//wider overlap in x => bounce in y direction:
if (ball.y > paddle.y) {
ball.y = paddle.y + paddle_radius_h.y + ball_radius.y;
ball_velocity.y = std::abs(ball_velocity.y);
} else {
ball.y = paddle.y - paddle_radius_h.y - ball_radius.y;
ball_velocity.y = -std::abs(ball_velocity.y);
}
} else {
//wider overlap in y => bounce in x direction:
if (ball.x > paddle.x) {
ball.x = paddle.x + paddle_radius_h.x + ball_radius.x;
ball_velocity.x = std::abs(ball_velocity.x);
} else {
ball.x = paddle.x - paddle_radius_h.x - ball_radius.x;
ball_velocity.x = -std::abs(ball_velocity.x);
}
//warp x velocity based on offset from paddle center:
float vel = (ball.x - paddle.x) / (paddle_radius_h.x + ball_radius.x);
ball_velocity.x = glm::mix(ball_velocity.x, vel, 0.75f);
}
return true;
};
if (!draw_ok && !draw_not_ok) {
if (paddle_vs_ball(left_paddle) || paddle_vs_ball(right_paddle) ||
paddle_vs_ball_h(top_paddle) || paddle_vs_ball_h(bottom_paddle)) {
draw_not_ok = true;
timer = 0.0f;
}
}
//court walls:
if (!draw_ok && !draw_not_ok) {
if (ball.y > court_radius.y - ball_radius.y && !fall_through[2] && !fallen_through) {
ball.y = court_radius.y - ball_radius.y;
if (ball_velocity.y > 0.0f) {
ball_velocity.y = -ball_velocity.y;
}
}
if (ball.y < -court_radius.y + ball_radius.y && !fall_through[0] && !fallen_through) {
ball.y = -court_radius.y + ball_radius.y;
if (ball_velocity.y < 0.0f) {
ball_velocity.y = -ball_velocity.y;
}
}
if (ball.x > court_radius.x - ball_radius.x && !fall_through[1] && !fallen_through) {
ball.x = court_radius.x - ball_radius.x;
if (ball_velocity.x > 0.0f) {
ball_velocity.x = -ball_velocity.x;
}
}
if (ball.x < -court_radius.x + ball_radius.x && !fall_through[3] && !fallen_through) {
ball.x = -court_radius.x + ball_radius.x;
if (ball_velocity.x < 0.0f) {
ball_velocity.x = -ball_velocity.x;
}
}
if (ball.x < -court_radius.x + ball_radius.x ||
ball.x > court_radius.x - ball_radius.x ||
ball.y < -court_radius.y + ball_radius.y ||
ball.y > court_radius.y - ball_radius.y) {
fallen_through = true;
}
if (ball.x < -court_radius.x - ball_trail.size() * ball_radius.x ||
ball.x > court_radius.x + ball_trail.size() * ball_radius.x ||
ball.y < -court_radius.y - ball_trail.size() * ball_radius.y ||
ball.y > court_radius.y + ball_trail.size() * ball_radius.y) {
draw_ok = true;
num_win += 1;
timer = 0.0f;
}
}
//----- rainbow trails -----
//age up all locations in ball trail:
for (auto &t : ball_trail) {
t.z += elapsed;
}
//store fresh location at back of ball trail:
ball_trail.emplace_back(ball, 0.0f);
//trim any too-old locations from back of trail:
//NOTE: since trail drawing interpolates between points, only removes back element if second-to-back element is too old:
while (ball_trail.size() >= 2 && ball_trail[1].z > trail_length) {
ball_trail.pop_front();
}
}
void PongMode::draw(glm::uvec2 const &drawable_size) {
//some nice colors from the course web page:
#define HEX_TO_U8VEC4( HX ) (glm::u8vec4( (HX >> 24) & 0xff, (HX >> 16) & 0xff, (HX >> 8) & 0xff, (HX) & 0xff ))
const glm::u8vec4 bg_color = HEX_TO_U8VEC4(0x171714ff);
const glm::u8vec4 fg_color = HEX_TO_U8VEC4(0xd1bb54ff);
const glm::u8vec4 shadow_color = HEX_TO_U8VEC4(0x604d29ff);
const glm::u8vec4 arrow_color = HEX_TO_U8VEC4(0xffff00ff);
const glm::u8vec4 green = HEX_TO_U8VEC4(0x00ff00ff);
const glm::u8vec4 red = HEX_TO_U8VEC4(0xff0000ff);
const std::vector< glm::u8vec4 > rainbow_colors = {
HEX_TO_U8VEC4(0x604d29ff), HEX_TO_U8VEC4(0x624f29fc), HEX_TO_U8VEC4(0x69542df2),
HEX_TO_U8VEC4(0x6a552df1), HEX_TO_U8VEC4(0x6b562ef0), HEX_TO_U8VEC4(0x6b562ef0),
HEX_TO_U8VEC4(0x6d572eed), HEX_TO_U8VEC4(0x6f592feb), HEX_TO_U8VEC4(0x725b31e7),
HEX_TO_U8VEC4(0x745d31e3), HEX_TO_U8VEC4(0x755e32e0), HEX_TO_U8VEC4(0x765f33de),
HEX_TO_U8VEC4(0x7a6234d8), HEX_TO_U8VEC4(0x826838ca), HEX_TO_U8VEC4(0x977840a4),
HEX_TO_U8VEC4(0x96773fa5), HEX_TO_U8VEC4(0xa07f4493), HEX_TO_U8VEC4(0xa1814590),
HEX_TO_U8VEC4(0x9e7e4496), HEX_TO_U8VEC4(0xa6844887), HEX_TO_U8VEC4(0xa9864884),
HEX_TO_U8VEC4(0xad8a4a7c),
};
#undef HEX_TO_U8VEC4
//other useful drawing constants:
const float wall_radius = 0.5f;
const float shadow_offset = 0.07f;
const float padding = 0.14f; //padding between outside of walls and edge of window
//---- compute vertices to draw ----
//vertices will be accumulated into this list and then uploaded+drawn at the end of this function:
std::vector< Vertex > vertices;
//inline helper function for rectangle drawing:
auto draw_rectangle = [&vertices](glm::vec2 const ¢er, glm::vec2 const &radius, glm::u8vec4 const &color) {
//draw rectangle as two CCW-oriented triangles:
vertices.emplace_back(glm::vec3(center.x-radius.x, center.y-radius.y, 0.0f), color, glm::vec2(0.5f, 0.5f));
vertices.emplace_back(glm::vec3(center.x+radius.x, center.y-radius.y, 0.0f), color, glm::vec2(0.5f, 0.5f));
vertices.emplace_back(glm::vec3(center.x+radius.x, center.y+radius.y, 0.0f), color, glm::vec2(0.5f, 0.5f));
vertices.emplace_back(glm::vec3(center.x-radius.x, center.y-radius.y, 0.0f), color, glm::vec2(0.5f, 0.5f));
vertices.emplace_back(glm::vec3(center.x+radius.x, center.y+radius.y, 0.0f), color, glm::vec2(0.5f, 0.5f));
vertices.emplace_back(glm::vec3(center.x-radius.x, center.y+radius.y, 0.0f), color, glm::vec2(0.5f, 0.5f));
};
auto draw_check = [&vertices](glm::vec2 const ¢er, glm::u8vec4 const &color) {
//draw rectangle as two CCW-oriented triangles:
vertices.emplace_back(glm::vec3(center.x, center.y, 0.0f), color, glm::vec2(0.5f, 0.5f));
vertices.emplace_back(glm::vec3(center.x+0.5f, center.y+0.5f, 0.0f), color, glm::vec2(0.5f, 0.5f));
vertices.emplace_back(glm::vec3(center.x-1.0f, center.y+1.0f, 0.0f), color, glm::vec2(0.5f, 0.5f));
vertices.emplace_back(glm::vec3(center.x-0.5f, center.y+1.5f, 0.0f), color, glm::vec2(0.5f, 0.5f));
vertices.emplace_back(glm::vec3(center.x-1.0f, center.y+1.0f, 0.0f), color, glm::vec2(0.5f, 0.5f));
vertices.emplace_back(glm::vec3(center.x+0.5f, center.y+0.5f, 0.0f), color, glm::vec2(0.5f, 0.5f));
vertices.emplace_back(glm::vec3(center.x, center.y, 0.0f), color, glm::vec2(0.5f, 0.5f));
vertices.emplace_back(glm::vec3(center.x+2.0f, center.y+2.0f, 0.0f), color, glm::vec2(0.5f, 0.5f));
vertices.emplace_back(glm::vec3(center.x-0.5f, center.y+0.5f, 0.0f), color, glm::vec2(0.5f, 0.5f));
vertices.emplace_back(glm::vec3(center.x+1.5f, center.y+2.5f, 0.0f), color, glm::vec2(0.5f, 0.5f));
vertices.emplace_back(glm::vec3(center.x-0.5f, center.y+0.5f, 0.0f), color, glm::vec2(0.5f, 0.5f));
vertices.emplace_back(glm::vec3(center.x+2.0f, center.y+2.0f, 0.0f), color, glm::vec2(0.5f, 0.5f));
};
auto draw_cross = [&vertices](glm::vec2 const ¢er, glm::u8vec4 const &color) {
//draw rectangle as two CCW-oriented triangles:
vertices.emplace_back(glm::vec3(center.x+1.5f, center.y-1.0f, 0.0f), color, glm::vec2(0.5f, 0.5f));
vertices.emplace_back(glm::vec3(center.x-1.5f, center.y+1.0f, 0.0f), color, glm::vec2(0.5f, 0.5f));
vertices.emplace_back(glm::vec3(center.x-1.0f, center.y+1.5f, 0.0f), color, glm::vec2(0.5f, 0.5f));
vertices.emplace_back(glm::vec3(center.x+1.0f, center.y-1.5f, 0.0f), color, glm::vec2(0.5f, 0.5f));
vertices.emplace_back(glm::vec3(center.x-1.5f, center.y+1.0f, 0.0f), color, glm::vec2(0.5f, 0.5f));
vertices.emplace_back(glm::vec3(center.x+1.5f, center.y-1.0f, 0.0f), color, glm::vec2(0.5f, 0.5f));
vertices.emplace_back(glm::vec3(center.x-1.5f, center.y-1.0f, 0.0f), color, glm::vec2(0.5f, 0.5f));
vertices.emplace_back(glm::vec3(center.x+1.5f, center.y+1.0f, 0.0f), color, glm::vec2(0.5f, 0.5f));
vertices.emplace_back(glm::vec3(center.x-1.0f, center.y-1.5f, 0.0f), color, glm::vec2(0.5f, 0.5f));
vertices.emplace_back(glm::vec3(center.x+1.0f, center.y+1.5f, 0.0f), color, glm::vec2(0.5f, 0.5f));
vertices.emplace_back(glm::vec3(center.x+1.5f, center.y+1.0f, 0.0f), color, glm::vec2(0.5f, 0.5f));
vertices.emplace_back(glm::vec3(center.x-1.5f, center.y-1.0f, 0.0f), color, glm::vec2(0.5f, 0.5f));
};
//shadows for everything (except the trail):
glm::vec2 s = glm::vec2(0.0f,-shadow_offset);
if (!fall_through[3]) {
draw_rectangle(glm::vec2(-court_radius.x-wall_radius, 0.0f)+s, glm::vec2(wall_radius, court_radius.y + 2.0f * wall_radius), shadow_color);
}
if (!fall_through[1]) {
draw_rectangle(glm::vec2( court_radius.x+wall_radius, 0.0f)+s, glm::vec2(wall_radius, court_radius.y + 2.0f * wall_radius), shadow_color);
}
if (!fall_through[0]) {
draw_rectangle(glm::vec2( 0.0f,-court_radius.y-wall_radius)+s, glm::vec2(court_radius.x, wall_radius), shadow_color);
}
if (!fall_through[2]) {
draw_rectangle(glm::vec2( 0.0f, court_radius.y+wall_radius)+s, glm::vec2(court_radius.x, wall_radius), shadow_color);
}
draw_rectangle(left_paddle+s, paddle_radius, shadow_color);
draw_rectangle(right_paddle+s, paddle_radius, shadow_color);
if (!draw_ok && !draw_not_ok) {
draw_rectangle(ball+s, ball_radius, shadow_color);
//ball's trail:
if (ball_trail.size() >= 2 && ball.x != 0.0f && ball.y != 0.0f) {
//start ti at second element so there is always something before it to interpolate from:
std::deque< glm::vec3 >::iterator ti = ball_trail.begin() + 1;
//draw trail from oldest-to-newest:
for (uint32_t i = uint32_t(rainbow_colors.size())-1; i < rainbow_colors.size(); --i) {
//time at which to draw the trail element:
float t = (i + 1) / float(rainbow_colors.size()) * trail_length;
//advance ti until 'just before' t:
while (ti != ball_trail.end() && ti->z > t) ++ti;
//if we ran out of tail, stop drawing:
if (ti == ball_trail.end()) break;
//interpolate between previous and current trail point to the correct time:
glm::vec3 a = *(ti-1);
glm::vec3 b = *(ti);
glm::vec2 at = (t - a.z) / (b.z - a.z) * (glm::vec2(b) - glm::vec2(a)) + glm::vec2(a);
//draw:
draw_rectangle(at, ball_radius, rainbow_colors[i]);
}
}
}
//solid objects:
//walls:
if (!fall_through[0]) {
draw_rectangle(glm::vec2( 0.0f,-court_radius.y-wall_radius), glm::vec2(court_radius.x, wall_radius), fg_color);
}
if (!fall_through[2]) {
draw_rectangle(glm::vec2( 0.0f, court_radius.y+wall_radius), glm::vec2(court_radius.x, wall_radius), fg_color);
}
if (!fall_through[3]) {
draw_rectangle(glm::vec2(-court_radius.x-wall_radius, 0.0f), glm::vec2(wall_radius, court_radius.y + 2.0f * wall_radius), fg_color);
}
if (!fall_through[1]) {
draw_rectangle(glm::vec2( court_radius.x+wall_radius, 0.0f), glm::vec2(wall_radius, court_radius.y + 2.0f * wall_radius), fg_color);
}
//paddles:
draw_rectangle(left_paddle, paddle_radius, fg_color);
draw_rectangle(right_paddle, paddle_radius, fg_color);
draw_rectangle(top_paddle, paddle_radius_h, fg_color);
draw_rectangle(bottom_paddle, paddle_radius_h, fg_color);
//ball:
if (!draw_ok && !draw_not_ok) {
draw_rectangle(ball, ball_radius, fg_color);
}
if (draw_ok) {
draw_check(glm::vec2(0.0f, 0.0f), green);
}
if (draw_not_ok) {
draw_cross(glm::vec2(0.0f, 0.0f), red);
}
//scores:
glm::vec2 score_radius = glm::vec2(0.1f, 0.1f);
// for (uint32_t i = 0; i < player_score; ++i) {
// draw_rectangle(glm::vec2( -court_radius.x + (2.0f + 3.0f * i) * score_radius.x, court_radius.y + 2.0f * wall_radius + 2.0f * score_radius.y), score_radius, fg_color);
// }
// for (uint32_t i = 0; i < left_score; ++i) {
// draw_rectangle(glm::vec2( -court_radius.x + (2.0f + 3.0f * i) * score_radius.x, court_radius.y + 2.0f * wall_radius + 2.0f * score_radius.y), score_radius, fg_color);
// }
// for (uint32_t i = 0; i < ai_score; ++i) {
// draw_rectangle(glm::vec2( court_radius.x - (2.0f + 3.0f * i) * score_radius.x, court_radius.y + 2.0f * wall_radius + 2.0f * score_radius.y), score_radius, fg_color);
// }
//------ compute court-to-window transform ------
// float lb = -court_radius.y - 2.0f * wall_radius - padding;
// float ub = court_radius.y + 2.0f * wall_radius + padding + 3.0f * score_radius.y;
// if (left_paddle.y - court_radius.y / 2.0f < -court_radius.y - 2.0f * wall_radius - padding) {
// ub = lb + court_radius.y;
// } else if (left_paddle.y + court_radius.y / 2.0f > court_radius.y + 2.0f * wall_radius + padding + 3.0f * score_radius.y) {
// lb = ub - court_radius.y;
// } else {
// lb = left_paddle.y - court_radius.y / 2.0f;
// ub = left_paddle.y + court_radius.y / 2.0f;
// }
if (draw_arrow) {
vertices.emplace_back(glm::vec3(ball.x + 0.2f * sin(ball_dir), ball.y + 0.2f * cos(ball_dir), 0.0f), arrow_color, glm::vec2(0.5f, 0.5f));
vertices.emplace_back(glm::vec3(ball.x - 0.2f * sin(ball_dir), ball.y - 0.2f * cos(ball_dir), 0.0f), arrow_color, glm::vec2(0.5f, 0.5f));
vertices.emplace_back(glm::vec3(ball.x + 3.0f * cos(ball_dir), ball.y + 3.0f * sin(ball_dir), 0.0f), arrow_color, glm::vec2(0.5f, 0.5f));
}
//compute area that should be visible:
glm::vec2 scene_min = glm::vec2(
-court_radius.x - 2.0f * wall_radius - padding,
// lb
-court_radius.y - 2.0f * wall_radius - padding
);
glm::vec2 scene_max = glm::vec2(
court_radius.x + 2.0f * wall_radius + padding,
// ub
court_radius.y + 2.0f * wall_radius + padding + 3.0f * score_radius.y
);
//compute window aspect ratio:
float aspect = drawable_size.x / float(drawable_size.y);
//we'll scale the x coordinate by 1.0 / aspect to make sure things stay square.
//compute scale factor for court given that...
float scale = std::min(
(2.0f * aspect) / (scene_max.x - scene_min.x), //... x must fit in [-aspect,aspect] ...
(2.0f) / (scene_max.y - scene_min.y) //... y must fit in [-1,1].
);
glm::vec2 center = 0.5f * (scene_max + scene_min);
//build matrix that scales and translates appropriately:
glm::mat4 court_to_clip = glm::mat4(
glm::vec4(scale / aspect, 0.0f, 0.0f, 0.0f),
glm::vec4(0.0f, scale, 0.0f, 0.0f),
glm::vec4(0.0f, 0.0f, 1.0f, 0.0f),
glm::vec4(-center.x * (scale / aspect), -center.y * scale, 0.0f, 1.0f)
);
//NOTE: glm matrices are specified in *Column-Major* order,
// so each line above is specifying a *column* of the matrix(!)
//also build the matrix that takes clip coordinates to court coordinates (used for mouse handling):
clip_to_court = glm::mat3x2(
glm::vec2(aspect / scale, 0.0f),
glm::vec2(0.0f, 1.0f / scale),
glm::vec2(center.x, center.y)
);
//---- actual drawing ----
//clear the color buffer:
glClearColor(bg_color.r / 255.0f, bg_color.g / 255.0f, bg_color.b / 255.0f, bg_color.a / 255.0f);
glClear(GL_COLOR_BUFFER_BIT);
//use alpha blending:
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//don't use the depth test:
glDisable(GL_DEPTH_TEST);
//upload vertices to vertex_buffer:
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); //set vertex_buffer as current
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(vertices[0]), vertices.data(), GL_STREAM_DRAW); //upload vertices array
glBindBuffer(GL_ARRAY_BUFFER, 0);
//set color_texture_program as current program:
glUseProgram(color_texture_program.program);
//upload OBJECT_TO_CLIP to the proper uniform location:
glUniformMatrix4fv(color_texture_program.OBJECT_TO_CLIP_mat4, 1, GL_FALSE, glm::value_ptr(court_to_clip));
//use the mapping vertex_buffer_for_color_texture_program to fetch vertex data:
glBindVertexArray(vertex_buffer_for_color_texture_program);
//bind the solid white texture to location zero so things will be drawn just with their colors:
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, white_tex);
//run the OpenGL pipeline:
glDrawArrays(GL_TRIANGLES, 0, GLsizei(vertices.size()));
//unbind the solid white texture:
glBindTexture(GL_TEXTURE_2D, 0);
//reset vertex array to none:
glBindVertexArray(0);
//reset current program to none:
glUseProgram(0);
GL_ERRORS(); //PARANOIA: print errors just in case we did something wrong.
}