-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject3C.cxx
725 lines (645 loc) · 22.4 KB
/
project3C.cxx
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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <cmath>
#include <random>
#include <algorithm>
using std::endl;
using std::cerr;
#include <GL/glew.h> // include GLEW and new version of GL on Windows
#include <GLFW/glfw3.h> // GLFW helper libray
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/vec3.hpp> // glm::vec3
#include <glm/vec4.hpp> // glm::vec4
#include <glm/mat4x4.hpp> // glm::mat4
#include <glm/gtx/string_cast.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/matrix_transform.hpp> // glm::translate, glm::rotate, glm::scale
// Macros to define the level of recusion for each sphere
#define L1 5
#define L2 6
#define L3 7
// How many number of balls to render
#define numBalls 100
class RenderManager;
struct Ball_physics {
double h0; // initial height
double v = 0; // m/s, current velocity
double g = 10; // m/s/s
double t = 0; // starting time
double dt = 0.02; // time step
double rho = 0.75; // coefficient of restitution
double tau = 0.10; // contact time for bounce
double hmax; // keep track of the maximum height
double h;
double hstop = 0.01; // stop when bounce is less than 1 cm
bool freefall = true;
double t_last;
double vmax;
};
// Ball has a x,z coordinate, distance to camera and a bunch of paramters to track its movement
struct Ball{
double x; // x coord of ball movement
double z; // z coord of ball movement
double distToCamera;
Ball_physics BP;
};
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> distrXZ(-10, 10);
std::uniform_int_distribution<int> distrHeight(10, 40);
void BounceBall(std::vector<Ball> &balls, RenderManager &rm, glm::vec3 camPos);
Ball createNewBall();
const char *GetVertexShader();
const char *GetFragmentShader();
glm::mat4 RotateMatrix(float degrees, float x, float y, float z)
{
glm::mat4 identity(1.0f);
glm::mat4 rotation = glm::rotate(identity,
glm::radians(degrees),
glm::vec3(x, y, z));
return rotation;
}
glm::mat4 ScaleMatrix(double x, double y, double z)
{
glm::mat4 identity(1.0f);
glm::vec3 scale(x, y, z);
return glm::scale(identity, scale);
}
glm::mat4 TranslateMatrix(double x, double y, double z)
{
glm::mat4 identity(1.0f);
glm::vec3 translate(x, y, z);
return glm::translate(identity, translate);
}
//
//
// PART 1: code to set up spheres and plane
//
//
class Triangle
{
public:
glm::vec3 v0;
glm::vec3 v1;
glm::vec3 v2;
};
std::vector<Triangle> SplitTriangle(std::vector<Triangle> &list)
{
std::vector<Triangle> output(4*list.size());
output.resize(4*list.size());
for (unsigned int i = 0 ; i < list.size() ; i++)
{
Triangle t = list[i];
glm::vec3 vmid1, vmid2, vmid3;
vmid1 = (t.v0 + t.v1) / 2.0f;
vmid2 = (t.v1 + t.v2) / 2.0f;
vmid3 = (t.v0 + t.v2) / 2.0f;
output[4*i+0].v0 = t.v0;
output[4*i+0].v1 = vmid1;
output[4*i+0].v2 = vmid3;
output[4*i+1].v0 = t.v1;
output[4*i+1].v1 = vmid2;
output[4*i+1].v2 = vmid1;
output[4*i+2].v0 = t.v2;
output[4*i+2].v1 = vmid3;
output[4*i+2].v2 = vmid2;
output[4*i+3].v0 = vmid1;
output[4*i+3].v1 = vmid2;
output[4*i+3].v2 = vmid3;
}
return output;
}
void PushVertex(std::vector<float>& coords,
const glm::vec3& v)
{
coords.push_back(v.x);
coords.push_back(v.y);
coords.push_back(v.z);
}
void GetPlaneData(std::vector<float>& coords, std::vector<float>& normals){
glm::vec3 v1 = glm::vec3(-15, -1, -15);
glm::vec3 v2 = glm::vec3(0, -1, -15);
glm::vec3 v3 = glm::vec3(0.0, -1, 0.0);
glm::vec3 v4 = glm::vec3(-15, -1, 0);
glm::vec3 norm = glm::vec3(0.0f, 1.0f, 0.0f);
PushVertex(coords, v1);
PushVertex(coords, v2);
PushVertex(coords, v4);
PushVertex(coords, v2);
PushVertex(coords, v3);
PushVertex(coords, v4);
PushVertex(normals, norm);
PushVertex(normals, norm);
PushVertex(normals, norm);
PushVertex(normals, norm);
PushVertex(normals, norm);
PushVertex(normals, norm);
}
//
// Sets up a sphere with equation x^2+y^2+z^2=1
//
void
GetSphereData(std::vector<float>& coords, std::vector<float>& normals, int recursionLevel)
{
/* int recursionLevel = 3; */
std::vector<Triangle> list;
{
Triangle t;
t.v0 = glm::vec3(1.0f,0.0f,0.0f);
t.v1 = glm::vec3(0.0f,1.0f,0.0f);
t.v2 = glm::vec3(0.0f,0.0f,1.0f);
list.push_back(t);
}
for (int r = 0 ; r < recursionLevel ; r++)
{
list = SplitTriangle(list);
}
for (int octant = 0 ; octant < 8 ; octant++)
{
glm::mat4 view(1.0f);
float angle = 90.0f*(octant%4);
if(angle != 0.0f)
view = glm::rotate(view, glm::radians(angle), glm::vec3(1, 0, 0));
if (octant >= 4)
view = glm::rotate(view, glm::radians(180.0f), glm::vec3(0, 0, 1));
for(int i = 0; i < list.size(); i++)
{
Triangle t = list[i];
float mag_reci;
glm::vec3 v0 = view*glm::vec4(t.v0, 1.0f);
glm::vec3 v1 = view*glm::vec4(t.v1, 1.0f);
glm::vec3 v2 = view*glm::vec4(t.v2, 1.0f);
mag_reci = 1.0f / glm::length(v0);
v0 = glm::vec3(v0.x * mag_reci, v0.y * mag_reci, v0.z * mag_reci);
mag_reci = 1.0f / glm::length(v1);
v1 = glm::vec3(v1.x * mag_reci, v1.y * mag_reci, v1.z * mag_reci);
mag_reci = 1.0f / glm::length(v2);
v2 = glm::vec3(v2.x * mag_reci, v2.y * mag_reci, v2.z * mag_reci);
PushVertex(coords, v0);
PushVertex(coords, v1);
PushVertex(coords, v2);
PushVertex(normals, v0);
PushVertex(normals, v1);
PushVertex(normals, v2);
}
}
}
//
//
// PART 2: RenderManager module
//
//
void _print_shader_info_log(GLuint shader_index) {
int max_length = 2048;
int actual_length = 0;
char shader_log[2048];
glGetShaderInfoLog(shader_index, max_length, &actual_length, shader_log);
printf("shader info log for GL index %u:\n%s\n", shader_index, shader_log);
}
class RenderManager
{
public:
enum ShapeType
{
SPHERE1,
SPHERE2,
SPHERE3,
PLANE
};
RenderManager();
void SetView(glm::vec3 &c, glm::vec3 &, glm::vec3 &);
void SetUpGeometry();
void SetColor(double r, double g, double b);
void Render(ShapeType, glm::mat4 model);
GLFWwindow *GetWindow() { return window; };
private:
glm::vec3 color;
GLuint sphere1VAO;
GLuint sphere1NumPrimitives;
GLuint sphere2VAO;
GLuint sphere2NumPrimitives;
GLuint sphere3VAO;
GLuint sphere3NumPrimitives;
GLuint planeVAO;
GLuint planeNumPrimitives;
GLuint mvploc;
GLuint colorloc;
GLuint camloc;
GLuint ldirloc;
glm::mat4 projection;
glm::mat4 view;
GLuint shaderProgram;
GLFWwindow *window;
void SetUpWindowAndShaders();
void MakeModelView(glm::mat4 &);
};
RenderManager::RenderManager()
{
SetUpWindowAndShaders();
SetUpGeometry();
projection = glm::perspective(
glm::radians(45.0f), (float)1000 / (float)1000, 5.0f, 100.0f);
// Get a handle for our MVP and color uniforms
mvploc = glGetUniformLocation(shaderProgram, "MVP");
colorloc = glGetUniformLocation(shaderProgram, "color");
camloc = glGetUniformLocation(shaderProgram, "cameraloc");
ldirloc = glGetUniformLocation(shaderProgram, "lightdir");
glm::vec4 lightcoeff(0.3, 0.7, 2.8, 50.5); // Lighting coeff, Ka, Kd, Ks, alpha
GLuint lcoeloc = glGetUniformLocation(shaderProgram, "lightcoeff");
glUniform4fv(lcoeloc, 1, &lightcoeff[0]);
};
void
RenderManager::SetView(glm::vec3 &camera, glm::vec3 &origin, glm::vec3 &up)
{
glm::mat4 v = glm::lookAt(
camera, // Camera in world space
origin, // looks at the origin
up // and the head is up
);
view = v;
glUniform3fv(camloc, 1, &camera[0]);
// Direction of light
glm::vec3 lightdir = glm::normalize(camera - origin);
glUniform3fv(ldirloc, 1, &lightdir[0]);
};
void
RenderManager::SetUpWindowAndShaders()
{
// start GL context and O/S window using the GLFW helper library
if (!glfwInit()) {
fprintf(stderr, "ERROR: could not start GLFW3\n");
exit(EXIT_FAILURE);
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(700, 700, "CIS 441", NULL, NULL);
if (!window) {
fprintf(stderr, "ERROR: could not open window with GLFW3\n");
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
// start GLEW extension handler
glewExperimental = GL_TRUE;
glewInit();
// get version info
const GLubyte *renderer = glGetString(GL_RENDERER); // get renderer string
const GLubyte *version = glGetString(GL_VERSION); // version as a string
printf("Renderer: %s\n", renderer);
printf("OpenGL version supported %s\n", version);
// tell GL to only draw onto a pixel if the shape is closer to the viewer
glEnable(GL_DEPTH_TEST); // enable depth-testing
glDepthFunc(GL_LESS); // depth-testing interprets a smaller value as "closer"
const char* vertex_shader = GetVertexShader();
const char* fragment_shader = GetFragmentShader();
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &vertex_shader, NULL);
glCompileShader(vs);
int params = -1;
glGetShaderiv(vs, GL_COMPILE_STATUS, ¶ms);
if (GL_TRUE != params) {
fprintf(stderr, "ERROR: GL shader index %i did not compile\n", vs);
_print_shader_info_log(vs);
exit(EXIT_FAILURE);
}
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, &fragment_shader, NULL);
glCompileShader(fs);
glGetShaderiv(fs, GL_COMPILE_STATUS, ¶ms);
if (GL_TRUE != params) {
fprintf(stderr, "ERROR: GL shader index %i did not compile\n", fs);
_print_shader_info_log(fs);
exit(EXIT_FAILURE);
}
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, fs);
glAttachShader(shaderProgram, vs);
glLinkProgram(shaderProgram);
glUseProgram(shaderProgram);
}
void RenderManager::SetColor(double r, double g, double b)
{
color[0] = r;
color[1] = g;
color[2] = b;
}
void RenderManager::MakeModelView(glm::mat4 &model)
{
glm::mat4 modelview = projection * view * model;
glUniformMatrix4fv(mvploc, 1, GL_FALSE, &modelview[0][0]);
}
void RenderManager::Render(ShapeType st, glm::mat4 model)
{
int numPrimitives = 0;
if (st == SPHERE1)
{
glBindVertexArray(sphere1VAO);
numPrimitives = sphere1NumPrimitives;
}
else if (st == SPHERE2)
{
glBindVertexArray(sphere2VAO);
numPrimitives = sphere2NumPrimitives;
}
else if (st == SPHERE3)
{
glBindVertexArray(sphere3VAO);
numPrimitives = sphere3NumPrimitives;
}
else if (st == PLANE)
{
glBindVertexArray(planeVAO);
numPrimitives = planeNumPrimitives;
}
MakeModelView(model);
glUniform3fv(colorloc, 1, &color[0]);
glDrawElements(GL_TRIANGLES, numPrimitives, GL_UNSIGNED_INT, NULL);
}
void SetUpVBOs(std::vector<float> &coords, std::vector<float> &normals,
GLuint &points_vbo, GLuint &normals_vbo, GLuint &index_vbo)
{
int numIndices = coords.size()/3;
std::vector<GLuint> indices(numIndices);
for(int i = 0; i < numIndices; i++)
indices[i] = i;
points_vbo = 0;
glGenBuffers(1, &points_vbo);
glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
glBufferData(GL_ARRAY_BUFFER, coords.size() * sizeof(float), coords.data(), GL_STATIC_DRAW);
normals_vbo = 0;
glGenBuffers(1, &normals_vbo);
glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(float), normals.data(), GL_STATIC_DRAW);
index_vbo = 0; // Index buffer object
glGenBuffers(1, &index_vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_vbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), indices.data(), GL_STATIC_DRAW);
}
void RenderManager::SetUpGeometry()
{
std::vector<float> sphere1Coords;
std::vector<float> sphere1Normals;
GetSphereData(sphere1Coords, sphere1Normals, L1);
sphere1NumPrimitives = sphere1Coords.size() / 3;
GLuint sphere1_points_vbo, sphere1_normals_vbo, sphere1_indices_vbo;
SetUpVBOs(sphere1Coords, sphere1Normals,
sphere1_points_vbo, sphere1_normals_vbo, sphere1_indices_vbo);
std::vector<float> sphere2Coords;
std::vector<float> sphere2Normals;
GetSphereData(sphere2Coords, sphere2Normals, L2);
sphere2NumPrimitives = sphere2Coords.size() / 3;
GLuint sphere2_points_vbo, sphere2_normals_vbo, sphere2_indices_vbo;
SetUpVBOs(sphere2Coords, sphere2Normals,
sphere2_points_vbo, sphere2_normals_vbo, sphere2_indices_vbo);
std::vector<float> sphere3Coords;
std::vector<float> sphere3Normals;
GetSphereData(sphere3Coords, sphere3Normals, L3);
sphere3NumPrimitives = sphere3Coords.size() / 3;
GLuint sphere3_points_vbo, sphere3_normals_vbo, sphere3_indices_vbo;
SetUpVBOs(sphere3Coords, sphere3Normals,
sphere3_points_vbo, sphere3_normals_vbo, sphere3_indices_vbo);
std::vector<float> planeCoords;
std::vector<float> planeNormals;
GetPlaneData(planeCoords, planeNormals);
planeNumPrimitives = planeCoords.size() / 3;
GLuint plane_points_vbo, plane_normals_vbo, plane_indices_vbo;
SetUpVBOs(planeCoords, planeNormals,
plane_points_vbo, plane_normals_vbo, plane_indices_vbo);
GLuint vao[4];
glGenVertexArrays(4, vao);
glBindVertexArray(vao[SPHERE1]);
sphere1VAO = vao[SPHERE1];
glBindBuffer(GL_ARRAY_BUFFER, sphere1_points_vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, sphere1_normals_vbo);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sphere1_indices_vbo);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glBindVertexArray(vao[SPHERE2]);
sphere2VAO = vao[SPHERE2];
glBindBuffer(GL_ARRAY_BUFFER, sphere2_points_vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, sphere2_normals_vbo);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sphere2_indices_vbo);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glBindVertexArray(vao[SPHERE3]);
sphere3VAO = vao[SPHERE3];
glBindBuffer(GL_ARRAY_BUFFER, sphere3_points_vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, sphere3_normals_vbo);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sphere3_indices_vbo);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glBindVertexArray(vao[PLANE]);
planeVAO = vao[PLANE];
glBindBuffer(GL_ARRAY_BUFFER, plane_points_vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, plane_normals_vbo);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, plane_indices_vbo);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
}
void DrawPlane(RenderManager &rm){
glm::mat4 identity(1.0f);
rm.SetColor(1,1,1);
rm.Render(RenderManager::PLANE, identity);
glm::mat4 transform = TranslateMatrix(15, 0, 0);
rm.SetColor(192/255.0, 192/255.0, 192/255.0);
rm.Render(RenderManager::PLANE, transform);
transform = TranslateMatrix(0,0, 15);
rm.Render(RenderManager::PLANE, transform);
transform = TranslateMatrix(15, 0, 15);
rm.SetColor(1,1,1);
rm.Render(RenderManager::PLANE, transform);
}
//
// PART3: main function
//
int main()
{
std::random_device r;
RenderManager rm;
GLFWwindow *window = rm.GetWindow();
glm::vec3 origin(0, 0, 0);
glm::vec3 up(0, 1, 0);
// Vector that holds all the balls
std::vector<Ball> balls;
for (int i = 0; i < numBalls; i++)
{
balls.push_back(createNewBall());
}
glm::mat4 identity(1.0f);
int counter=0;
double t, t0;
t0 = glfwGetTime();
while (!glfwWindowShouldClose(window))
{
t = glfwGetTime();
double angle=counter/1000.0*2*M_PI;
counter++;
// print FPS to console
if (counter % 100 == 99)
{
double time = t-t0;
cerr << "Frame rate = " << 100.0 / time << " FPS" << endl;
t0 = t;
}
// adjust the camera
glm::vec3 cameraPos = glm::vec3(15.0f*sin(angle), 10.0f, 15.0f*cos(angle));
glm::vec3 cameraDirection = glm::normalize(cameraPos - origin);
glm::vec3 cameraRight = glm::normalize(glm::cross(up, cameraDirection));
glm::vec3 cameraUp = glm::cross(cameraDirection, cameraRight);
rm.SetView(cameraPos, origin, cameraUp);
// wipe the drawing surface clear
glClearColor(0.3, 0.3, 0.8, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// draw the plane at the bottom
DrawPlane(rm);
// render the bouncing balls!!
BounceBall(balls, rm, cameraPos);
// update other events like input handling
glfwPollEvents();
// put the stuff we've been drawing onto the display
glfwSwapBuffers(window);
}
// close GL context and any other GLFW resources
glfwTerminate();
return 0;
}
void
UpdateBallPhysics(Ball& ball){
auto& ball_physics = ball.BP;
// If the ball is still bouncing, calculate the new height
if (ball_physics.hmax > ball_physics.hstop){
if (ball_physics.freefall){
double hnew = ball_physics.h + ball_physics.v*ball_physics.dt - 0.5*ball_physics.g*ball_physics.dt*ball_physics.dt;
if (hnew<0){
ball_physics.t = ball_physics.t_last + 2*sqrt(2*ball_physics.hmax/ball_physics.g);
ball_physics.freefall = false;
ball_physics.t_last = ball_physics.t + ball_physics.tau;
ball_physics.h = 0;
}
else{
ball_physics.t += ball_physics.dt;
ball_physics.v -= ball_physics.g*ball_physics.dt;
ball_physics.h = hnew;
}
}
else{
ball_physics.t += ball_physics.tau;
ball_physics.vmax *= ball_physics.rho;
ball_physics.v = ball_physics.vmax;
ball_physics.freefall = true;
ball_physics.h = 0;
}
ball_physics.hmax = 0.5*ball_physics.vmax*ball_physics.vmax/ball_physics.g;
} else { // When its done, make a new ball
ball = createNewBall();
}
}
static bool
comp(const Ball &x, const Ball &y)
{
return x.distToCamera < y.distToCamera;
}
void
BounceBall(std::vector<Ball> &balls, RenderManager &rm, glm::vec3 camPos)
{
Ball *currentBall;
int currentDist;
for (int i = 0; i < numBalls; i++)
{
// TODO see if fix is for memory
currentBall = &balls[i];
UpdateBallPhysics(*currentBall);
currentBall->distToCamera = sqrt(pow((camPos.x - currentBall->x), 2) +
pow((camPos.y - currentBall->BP.h), 2) +
pow((camPos.z - currentBall->z), 2));
}
std::sort(balls.begin(), balls.end(), comp);
for (int i = 0; i < numBalls; i++)
{
currentBall = &balls[i];
glm::mat4 transform = TranslateMatrix(currentBall->x, currentBall->BP.h, currentBall->z);
if (i < numBalls/3)
{
rm.Render(RenderManager::SPHERE3, transform);
}
else if (i < (numBalls/3)*2)
{
rm.Render(RenderManager::SPHERE2, transform);
}
else
{
rm.Render(RenderManager::SPHERE1, transform);
}
}
}
Ball
createNewBall()
{
int X = distrXZ(gen);
int Z = distrXZ(gen);
int height = distrHeight(gen);
Ball newBall;
newBall.x = X;
newBall.z = Z;
Ball_physics BP;
BP.h = height;
BP.h0 = height;
BP.hmax = height;
BP.t_last = -1*sqrt(2*BP.h0/BP.g);
BP.vmax = sqrt(2*BP.hmax*BP.g);
newBall.BP = BP;
return newBall;
}
const char *GetVertexShader()
{
static char vertexShader[1024];
strcpy(vertexShader,
"#version 400\n"
"layout (location = 0) in vec3 vertex_position;\n"
"layout (location = 1) in vec3 vertex_normal;\n"
"uniform mat4 MVP;\n"
"uniform vec3 cameraloc; // Camera position \n"
"uniform vec3 lightdir; // Lighting direction \n"
"uniform vec4 lightcoeff; // Lighting coeff, Ka, Kd, Ks, alpha\n"
"vec3 reflection, viewDir;\n"
"float LdotN, diffuse, RdotV, specular;\n"
"out float shading_amount;\n"
"void main() {\n"
" gl_Position = MVP*vec4(vertex_position, 1.0);\n"
" LdotN = dot(lightdir, vertex_normal);\n"
" diffuse = lightcoeff[1] * max(0.0, LdotN);\n"
" reflection = 2 * LdotN * vertex_normal - lightdir;\n"
" viewDir = cameraloc-vertex_position;\n"
" RdotV = dot(normalize(reflection), normalize(viewDir));\n"
" specular = abs(lightcoeff[2] * pow(max(0.0, RdotV), lightcoeff[3]));\n"
" shading_amount = lightcoeff[0] + diffuse + specular;\n"
"}\n"
);
return vertexShader;
}
const char *GetFragmentShader()
{
static char fragmentShader[1024];
strcpy(fragmentShader,
"#version 400\n"
"in float shading_amount;\n"
"uniform vec3 color;\n"
"out vec4 frag_color;\n"
"void main() {\n"
" frag_color = vec4(min(1.0, shading_amount*color[0]),min(1.0, shading_amount*color[1]), min(1.0, shading_amount*color[2]), 1.0);\n"
"}\n"
);
return fragmentShader;
}