This repository has been archived by the owner on Aug 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
PongMode.cpp
439 lines (349 loc) · 16 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
#include "PongMode.hpp"
//for the GL_ERRORS() macro:
#include "gl_errors.hpp"
//for glm::value_ptr() :
#include <glm/gtc/type_ptr.hpp>
#include <random>
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;
}
return false;
}
void PongMode::update(float elapsed) {
static std::mt19937 mt; //mersenne twister pseudo-random number generator
//----- paddle update -----
{ //right player ai:
ai_offset_update -= elapsed;
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;
}
if (right_paddle.y < ball.y + ai_offset) {
right_paddle.y = std::min(ball.y + ai_offset, right_paddle.y + 2.0f * elapsed);
} else {
right_paddle.y = std::max(ball.y + ai_offset, right_paddle.y - 2.0f * 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);
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);
ball += elapsed * speed_multiplier * 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;
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);
}
};
paddle_vs_ball(left_paddle);
paddle_vs_ball(right_paddle);
//court walls:
if (ball.y > court_radius.y - ball_radius.y) {
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) {
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) {
ball.x = court_radius.x - ball_radius.x;
if (ball_velocity.x > 0.0f) {
ball_velocity.x = -ball_velocity.x;
left_score += 1;
}
}
if (ball.x < -court_radius.x + ball_radius.x) {
ball.x = -court_radius.x + ball_radius.x;
if (ball_velocity.x < 0.0f) {
ball_velocity.x = -ball_velocity.x;
right_score += 1;
}
}
//----- 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(0xf3ffc6ff);
const glm::u8vec4 fg_color = HEX_TO_U8VEC4(0x000000ff);
const glm::u8vec4 shadow_color = HEX_TO_U8VEC4(0xa5df40ff);
const std::vector< glm::u8vec4 > rainbow_colors = {
HEX_TO_U8VEC4(0xe2ff70ff), HEX_TO_U8VEC4(0xcbff70ff), HEX_TO_U8VEC4(0xaeff5dff),
HEX_TO_U8VEC4(0x88ff52ff), HEX_TO_U8VEC4(0x6cff47ff), HEX_TO_U8VEC4(0x3aff37ff),
HEX_TO_U8VEC4(0x2eff94ff), HEX_TO_U8VEC4(0x2effa5ff), HEX_TO_U8VEC4(0x17ffc1ff),
HEX_TO_U8VEC4(0x00f4e7ff), HEX_TO_U8VEC4(0x00cbe4ff), HEX_TO_U8VEC4(0x00b0d8ff),
HEX_TO_U8VEC4(0x00a5d1ff), HEX_TO_U8VEC4(0x0098cfd8), HEX_TO_U8VEC4(0x0098cf54),
HEX_TO_U8VEC4(0x0098cf54), HEX_TO_U8VEC4(0x0098cf54), HEX_TO_U8VEC4(0x0098cf54),
HEX_TO_U8VEC4(0x0098cf54), HEX_TO_U8VEC4(0x0098cf54), HEX_TO_U8VEC4(0x0098cf54),
HEX_TO_U8VEC4(0x0098cf54)
};
#undef HEX_TO_U8VEC4
//other useful drawing constants:
const float wall_radius = 0.05f;
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) {
//split rectangle into 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));
};
//shadows for everything (except the trail):
glm::vec2 s = glm::vec2(0.0f,-shadow_offset);
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);
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);
draw_rectangle(glm::vec2( 0.0f,-court_radius.y-wall_radius)+s, glm::vec2(court_radius.x, wall_radius), shadow_color);
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);
draw_rectangle(ball+s, ball_radius, shadow_color);
//ball's trail:
if (ball_trail.size() >= 2) {
//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:
draw_rectangle(glm::vec2(-court_radius.x-wall_radius, 0.0f), glm::vec2(wall_radius, court_radius.y + 2.0f * wall_radius), fg_color);
draw_rectangle(glm::vec2( court_radius.x+wall_radius, 0.0f), glm::vec2(wall_radius, court_radius.y + 2.0f * wall_radius), fg_color);
draw_rectangle(glm::vec2( 0.0f,-court_radius.y-wall_radius), glm::vec2(court_radius.x, wall_radius), fg_color);
draw_rectangle(glm::vec2( 0.0f, court_radius.y+wall_radius), glm::vec2(court_radius.x, wall_radius), fg_color);
//paddles:
draw_rectangle(left_paddle, paddle_radius, fg_color);
draw_rectangle(right_paddle, paddle_radius, fg_color);
//ball:
draw_rectangle(ball, ball_radius, fg_color);
//scores:
glm::vec2 score_radius = glm::vec2(0.1f, 0.1f);
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 < right_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 ------
//compute area that should be visible:
glm::vec2 scene_min = glm::vec2(
-court_radius.x - 2.0f * wall_radius - padding,
-court_radius.y - 2.0f * wall_radius - padding
);
glm::vec2 scene_max = glm::vec2(
court_radius.x + 2.0f * wall_radius + padding,
court_radius.y + 2.0f * wall_radius + 3.0f * score_radius.y + padding
);
//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 this matrix is actually transposed from how it appears.
//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:
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.
}