-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
518 lines (411 loc) · 14.4 KB
/
main.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
#include <iostream>
#include <memory>
#include <chrono>
#include <ratio>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtx/rotate_vector.hpp>
#include "sound.h"
#include "mesh.h"
#include "camera.h"
#include "platform.h"
#include "shader.h"
#include "texture.h"
#include "modelcurve.h"
#include "text.h"
#include "animation.h"
#include "soundcloud.h"
#include "track.h"
#include "sky.h"
#include "boat.h"
#include "water.h"
#include "sparks.h"
#if 0
//FIXME!!!
#include "glad/glad.h"
#endif
int main(int argc, char* argv[]) {
std::cout << "Hey!" << std::endl;
Platform::initialize();
#if 0
// Check if soundtrack downloading works
auto trackId = SoundCloud::resolveTrackUrl("https://soundcloud.com/howl-naj-basit/mudmen-animal");
SoundCloud::Track foo(trackId);
foo.download();
#endif
std::shared_ptr<Animation> animation = std::make_shared<Animation>("data/anim/fire");
animation->play();
#if 0
if (!Music::initialize()) {
// 1. schedule uncached songs for online-check
// 2. ask the user if accessing soundcloud is okay
// 3. perform check
// 4. report errors via HUD
// 5. schedule background download and report errors via ticker
}
Music::play();
#endif
std::vector<std::shared_ptr<Sparks>> sparks;
std::shared_ptr<Sound> sound = std::make_shared<Sound>("data/music/music");
// sound->play();
std::shared_ptr<Texture> texture = std::make_shared<Texture>("data/font/main");
std::shared_ptr<Text> text = std::make_shared<Text>("data/font/liberation_sans_bold_65");
auto drawText = [&](glm::vec2 position, std::string message) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR);
glPushMatrix();
glLoadIdentity();
glTranslatef(position.x, position.y, 0.0f);
//FIXME: Do AR correction elsewhere
float aspectRatio = 800.0f / 450.0f;
glScalef(0.0005f, 0.0005f * aspectRatio, 1.0f);
// Test text
text->draw(message);
glPopMatrix();
};
std::string meshPath = "data/mesh/cube";
std::shared_ptr<Mesh> mesh = std::make_shared<Mesh>();
std::shared_ptr<Mesh> meshSliced = std::make_shared<Mesh>();
glm::vec3 slicePlaneOrigin = glm::vec3(0.0f, 0.0f, 0.0f);
Plane slicePlane = Plane(glm::normalize(glm::vec3(0.0f, 0.0f, 1.0f)), slicePlaneOrigin);
#if 0
// Unit test Plane::collideRay
std::cout << "Length 1: " << slicePlane.collideRay(glm::vec3(0.0f, 0.0f, -1.0f), glm::normalize(glm::vec3(0.0f, 0.0f, 1.0f))) << std::endl;
std::cout << "Length 2: " << slicePlane.collideRay(glm::vec3(0.0f, 0.0f, 3.0f), glm::normalize(glm::vec3(0.0f, 0.0f, -1.0f))) << std::endl;
std::cout << "Length 3: " << slicePlane.collideRay(glm::vec3(0.0f, 2.0f, 3.0f), glm::normalize(glm::vec3(0.0f, 0.0f, -1.0f))) << std::endl;
std::cout << "Length 4: " << slicePlane.collideRay(glm::vec3(0.0f, 0.0f, 1.0f), glm::normalize(glm::vec3(0.0f, -1.0f, -1.0f))) << std::endl;
std::cout << "Length 5: " << slicePlane.collideRay(glm::vec3(0.0f, 0.0f, 1.0f), glm::normalize(glm::vec3(0.0f, -2.0f, -1.0f))) << std::endl;
#endif
#if 0
// Unit test slicer
for(unsigned int i = 0; i < 10; i++) {
meshSliced->slice(Plane(glm::vec3(1.0,0.0,0.0),-4.0f + i * 1.0f));
}
#endif
std::shared_ptr<Shader> genericShader = std::make_shared<Shader>("data/shader/generic");
std::shared_ptr<Shader> genericTexturedShader = std::make_shared<Shader>("data/shader/generic","data/shader/font");
genericShader->activate();
std::shared_ptr<Camera> camera = std::make_shared<Camera>();
auto updateMatrix = [&]() {
#if 1
//Hack to get simple output with legacy GL
glMatrixMode(GL_PROJECTION);
#if 0
glLoadIdentity();
gluPerspective(45.0f, 1.0f, 1.0f, 1000.0f);
#else
glLoadMatrixf(glm::value_ptr(camera->projectionMatrix()));
#endif
glMatrixMode(GL_MODELVIEW);
#if 0
glLoadIdentity();
gluLookAt(10.0f, 10.0f, 10.0f,
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f);
#else
glLoadMatrixf(glm::value_ptr(camera->viewMatrix()));
#endif
#endif
};
Curve2 curve;
Curve2 curveRigid;
float l = 10.0f;
float sl = 3.5f;
for(int i = 0; i <= l / sl; i++) {
float a = (float)i / (l / sl) * 2.0f * 3.141;
float x = -l/2.0f + i * sl;
float y = 1*sinf(a * 3.0f);
float r = 4.0f + y;
float nx = cosf(a) * r;
float ny = sinf(a) * r;
curve.nodes.emplace_back(glm::vec2(nx, ny-0.3));
curveRigid.nodes.emplace_back(glm::vec2(nx, ny+0.3));
}
Track track("data/track/test");
std::shared_ptr<Model> model = std::make_shared<Model>(mesh);
std::shared_ptr<ModelCurve> modelCurve = std::make_shared<ModelCurve>(curve);
std::shared_ptr<ModelCurve> modelCurveRigid = std::make_shared<ModelCurve>(curveRigid);
std::vector<std::shared_ptr<Model>> models;
models.push_back(model);
#if 1
track.fillCurve(modelCurve, false, true, models);
track.fillCurve(modelCurveRigid, true, true, models);
#endif
// Some boats in the name of science :P
std::vector<Boat> boats;
for(unsigned int i = 0; i < 6; i++) {
boats.emplace_back("data/boat/test");
auto& boat = boats.back();
boat.position.x = i * 12.0f;
}
std::shared_ptr<Model> testModel = std::make_shared<Model>("data/model/test");
while(!Platform::hasQuit()) {
static float t = -1000.0f;
t += 0.01f;
//std::cout << "Top: " << slicePlane.getDistance(camera.getPosition()) << std::endl;
// renderer->setDepth(false);
// renderer->sprite(anim->texture(), glm::mat3::identity());
// Get input
const Uint8 *state = SDL_GetKeyboardState(NULL);
bool quit = state[SDL_SCANCODE_ESCAPE];
bool showSliced = state[SDL_SCANCODE_RETURN];
// FIXME: Use simplefilewatcher later
bool reloadTrack = state[SDL_SCANCODE_F5];
bool removeParticles = state[SDL_SCANCODE_F6];
if (quit) {
break; //FIXME: Request SDL / platform to quit instead?!
}
if (reloadTrack) {
track.reload();
}
if (removeParticles) {
std::cout << "Clearing particles" << std::endl;
sparks.clear();
}
// FIXME: Do logic
#if 0
// Test sounds by making music sound absolutely ridiculous
sound->setPitch(sinf(t*20.0f)*0.3+1.0f);
#endif
// Start drawing
glClearColor(sinf(t * 3.0f)*0.5f+0.5f, 0.5f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
genericShader->activate();
genericShader->set("viewProjectionMatrix", camera->viewProjectionMatrix());
auto& boat = boats[0];
#if 1
// Update player boat input
auto& input = boat.input;
input.accelerate = state[SDL_SCANCODE_UP] ? 1.0f : 0.0f;
input.brake = state[SDL_SCANCODE_DOWN] ? 1.0f : 0.0f;
if (state[SDL_SCANCODE_LEFT]) {
input.steer -= 1.0f;
}
if (state[SDL_SCANCODE_RIGHT]) {
input.steer += 1.0f;
}
#endif
#if 1
// Update boats
for(unsigned int i = 0; i < boats.size(); i++) {
auto& boat = boats[i];
boat.step();
}
#endif
//FIXME: Resolve collisions
for(unsigned int i = 0; i < boats.size(); i++) {
auto& boatI = boats[i];
for(unsigned int j = (i + 1); j < boats.size(); j++) {
auto& boatJ = boats[j];
glm::vec3 d = boatJ.position - boatI.position;
//FIXME: Handle boats on exact same location!
float r = 5.0f;
if (glm::length(d) < r) {
glm::vec3 p = boatI.position + d * 0.5f;
float eps = 0.001; // 1mm (if you go lower than this you'll have infinite collisions)
//FIXME: Add epsilon to make sure we don't have a collision after setting these back!
boatI.position = p - glm::normalize(d) * (r + eps) / 2.0f;
boatJ.position = p + glm::normalize(d) * (r + eps) / 2.0f;
sparks.push_back(std::make_shared<Sparks>(p, glm::vec3(0.0f, 0.0f, 10.5f)));
}
boatI.position.z = 0.0f;
boatJ.position.z = 0.0f;
}
}
#if 1
// Set camera to follow player boat
glm::vec2 o(cosf(boat.heading), sinf(boat.heading));
glm::vec3 cp(boat.getPosition() + glm::vec3(o * 5.0f, 5.0f));
//cp = glm::mix(camera->getPosition(), cp, 0.5f);
glm::vec3 d = camera->getPosition() - cp;
float maxDist = 15.0f;
if (glm::length(d) > maxDist) {
cp = glm::mix(camera->getPosition(), cp + glm::normalize(d) * maxDist, 0.3f);
camera->setPosition(cp);
}
camera->lookAt(boat.getPosition() + glm::vec3(0.0f, 0.0f, 2.0f));
updateMatrix();
glm::vec3 p = camera->getPosition();
alListener3f(AL_POSITION, p.x, p.y, p.z);
//FIXME: velocity, forward and up
alListener3f( AL_VELOCITY, 0.0f, 0.0f, 0.0f);
float orient[6] = { 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f };
alListenerfv( AL_ORIENTATION, orient );
#endif
// Draw sky
static Sky sky = Sky();
genericTexturedShader->activate();
genericTexturedShader->set("texture", 0u);
glColor3f(1.0f, 1.0f, 1.0f);
sky.draw();
static Water water = Water();
//water.draw();
#if 1
// Draw all boats
for(unsigned int i = 0; i < boats.size(); i++) {
auto& boat = boats[i];
glColor3f(1.0f, 0.0f, 0.0f);
genericTexturedShader->activate();
genericTexturedShader->set("texture", 0u);
boat.draw();
}
#endif
struct Checkpoint {
glm::vec3 origin;
float angle;
};
std::vector<Checkpoint> checkpoints{
{ glm::vec3(-100.0f, 400.0f, 0.0f), glm::radians<float>(0.0f) },
{ glm::vec3(0.0f, 500.0f, 0.0f), glm::radians<float>(90.0f) },
{ glm::vec3(100.0f, 400.0f, 0.0f), glm::radians<float>(180.0f) },
{ glm::vec3(100.0f, -400.0f, 0.0f), glm::radians<float>(180.0f) },
{ glm::vec3(0.0f, -500.0f, 0.0f), glm::radians<float>(270.0f) },
{ glm::vec3(-100.0f, -400.0f, 0.0f), glm::radians<float>(180.0f) },
};
for(unsigned int i = 0; i < checkpoints.size(); i++) {
auto& cp = checkpoints[i];
// Draw imaginary checkpoint wall
genericShader->activate();
Plane cpPlane = Plane(glm::rotateZ(glm::vec3(0.0f, 1.0f, 0.0f), cp.angle), cp.origin);
glColor3f(1.0f, 0.0f, 1.0f);
cpPlane.draw(1.0f, 100, 100, glm::vec3(0.0f, 0.0f, 1.0f));
// Draw tag text
genericTexturedShader->activate();
genericTexturedShader->set("texture", 0u);
glm::vec3 screenPosition = camera->project(cp.origin);
glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
if (screenPosition.z >= 0.0f && screenPosition.z <= 1.0f) {
drawText(glm::vec2(screenPosition) / glm::vec2(800.0f, 480.0f) * glm::vec2(2.0f, -2.0f) - glm::vec2(1.0f, -1.0f), std::string("Checkpoint ") + std::to_string(i));
}
}
genericShader->activate();
float gap = 10.0f;
glColor3f(1.0f, 1.0f, 1.0f);
slicePlane.draw(gap, 1100.0f / gap, 1100.0f / gap);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glLineWidth(1.0f);
#if 0
if (showSliced) {
glColor3f(1.0f, 0.0f, 0.0f);
mesh->draw();
} else {
glColor3f(0.0f, 0.0f, 1.0f);
meshSliced->draw();
}
#endif
#if 0
// Test curves
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
glPointSize(20.0f);
float offset = t * 2.0f + 2000.0f; // - 10.0f;
//offset = 1.0f + 8.0f;
glPointSize(1.0f);
glm::vec2 p = curveRigid.getNode(offset).position;
int ni = curveRigid.getNodeIndex(offset);
std::cout << "Node " << ni << " (" << curveRigid.getOffset(1) << ")" << std::endl;
glm::vec2 lp = curveRigid.getNode(ni).position;
glBegin(GL_POINTS);
glColor3f(0.5f, 0.0f, 0.5f);
glVertex3f(lp.x, lp.y, 0.0f);
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(p.x, p.y, 0.0f);
glEnd();
#endif
#if 0
// Test curve drawing
glColor3f(0.0f, 1.0f, 0.0f);
modelCurve->draw();
//modelCurveRigid->draw();
genericShader->activate();
modelCurve->curve.draw();
//modelCurveRigid->curve.draw();
#endif
glLineWidth(1.0f);
// FIXME: Draw the world
// FIXME: Draw the boats
#if 1
// Draw a small axis system in legacy GL
glBegin(GL_LINES);
glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(0.0f, 0.0f, 0.0f); glVertex3f(1.0f, 0.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(0.0f, 0.0f, 0.0f); glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(0.0f, 0.0f, 0.0f); glVertex3f(0.0f, 0.0f, 1.0f);
glEnd();
#endif
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
#if 0
// Test models
genericTexturedShader->activate();
genericTexturedShader->set("texture", 0u);
glColor3f(1.0f, 1.0f, 1.0f);
testModel->draw();
#endif
#if 1
// Test picking and mouse
{
int x, y;
Uint32 buttons = SDL_GetMouseState(&x, &y);
glm::vec3 p = Platform::pick(glm::vec2(x, y), camera);
glPointSize(20.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glUseProgram(0);
Platform::drawPoint(p);
glPointSize(1.0f);
static bool lock = false;
if (buttons & SDL_BUTTON(SDL_BUTTON_LEFT)) {
if (!lock) {
camera->lookAt(p);
lock = true;
}
} else {
lock = false;
}
}
#endif
#if 1
glDepthMask(GL_FALSE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
genericTexturedShader->activate();
genericTexturedShader->set("texture", 0u);
for(auto& s : sparks) {
s->step(); //FIXME: Do this in an earlier step so these can react with the world
s->draw();
}
glDepthMask(GL_TRUE);
#endif
// FIXME: Draw HUD
#if 0
// Animation and sprite test
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR);
//animation->texture()->activate(0);
animation->step();
//FIXME: Draw stuff!
glDisable(GL_BLEND);
#endif
#if 1
//FIXME!!!
auto current = std::chrono::steady_clock::now();
static auto last = current;
// std::chrono::duration<uint64_t, std::milli> mspf = std::chrono::duration_cast<uint64_t, std::milli>(current - last);
using Duration = std::chrono::duration<uint64_t, std::micro>;
Duration uspf = std::chrono::duration_cast<Duration>(current - last);
last = current;
#endif
#if 1
glColor4f(0.1f, 0.2f, 0.5f, 1.0f);
char symbolMicro = '\xB5';
drawText(glm::vec2(-0.95f, 0.9f - 0.2f), std::string("boats: ") + std::to_string(boats.size()) + std::string("\nsparks: ") + std::to_string(sparks.size()) + "\n" + symbolMicro + "/F: " + std::to_string(uspf.count()));
#endif
glFinish();
Platform::step();
}
return 0;
}