This repository was archived by the owner on Oct 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcar.cpp
More file actions
80 lines (66 loc) · 2.04 KB
/
car.cpp
File metadata and controls
80 lines (66 loc) · 2.04 KB
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
//
// Created by lepet on 8/4/2018.
//
#include <iostream>
#include <system/engine.h>
#include <objects/car.h>
using namespace std;
class wheel : public object {
private:
glm::vec3 offset;
public:
explicit wheel(const glm::vec3& offset);
void update() override;
};
wheel::wheel(const glm::vec3& offset) :
object{engine::get_instance().get_model("car.wheel")},
offset{offset} {}
void wheel::update() {
object::update();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(offset[0], offset[1], offset[2]);
glGetFloatv(GL_MODELVIEW_MATRIX, reinterpret_cast<GLfloat*>(&transform));
}
car::car(const vector<glm::vec3>& points) :
object{engine::get_instance().get_model("car.body")},
points{points},
route{points, 0.2},
u{1.0f} {
// The first wheel.
add_child(std::make_unique<wheel>(glm::vec3{-2.0f, 0, 1.1f}));
// The second wheel.
add_child(std::make_unique<wheel>(glm::vec3{-2.0f, 0, -1.1f}));
// The third wheel.
add_child(std::make_unique<wheel>(glm::vec3{0.0f, 0, 1.1f}));
// The fourth wheel.
add_child(std::make_unique<wheel>(glm::vec3{0.0f, 0, -1.1f}));
}
void car::update() {
object::update();
auto n = static_cast<unsigned int>(u);
if (n < points.size()) {
auto point = route.get(n, u - n);
offset = point[0];
target = point[1];
}
u = u + 0.01f;
// Look-at transform.
// reference: https://stackoverflow.com/questions/6992541/opengl-rotation-in-given-direction
target.normalize();
glm::vec3 T = target;
glm::vec3 U = {0, 1.0f, 0};
glm::vec3 L = glm::cross(U, T);
glm::mat4 look(1.0);
look[0] = T;
look[1] = U;
look[2] = L;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(offset[0], offset[1], offset[2]);
if (n < points.size() - 2)
glRotatef(180, 0, 1.0f, 0);
glMultMatrixf(reinterpret_cast<const GLfloat*>(&look));
glScalef(0.15f, 0.15f, 0.15f);
glGetFloatv(GL_MODELVIEW_MATRIX, reinterpret_cast<GLfloat*>(&transform));
}