-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObstacle.hpp
162 lines (142 loc) · 4.78 KB
/
Obstacle.hpp
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
#pragma once
#include <AGL3Drawable.hpp>
#include <epoxy/gl.h>
#include <epoxy/glx.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <array>
#include <GLFW/glfw3.h>
#include <glm/gtc/type_ptr.hpp>
#include <algorithm>
using Triangle = std::array<glm::vec3, 3>;
enum class ObstacleType: uint8_t { finish, trap, item, normal };
class Obstacle : public AGLDrawable {
public:
Obstacle(
glm::vec3 _center,
glm::vec3 angles,
float _scale,
float& _aspect,
const glm::vec3& plPos,
ObstacleType obs)
: AGLDrawable(0)
, aspect(_aspect)
, playerPos(plPos)
, obstacleType(obs)
{
setShaders();
setBuffers();
setCenter(_center);
setAngles(angles.x, angles.y, angles.z);
setScale(_scale);
calcModelMatrix();
calcVertices();
}
void setAngles(const float x, const float y, const float z)
{
x_angle = x;
y_angle = y;
z_angle = z;
}
void setCenter(const glm::vec3 _center)
{
center = _center;
color = {0.4*center.x+0.6, 0.01*center.y, 0.5*center.z+0.7};
}
void calcModelMatrix()
{
model = glm::mat4(1.0f);
model = glm::translate(model, center);
model = glm::rotate(model, glm::radians(x_angle), {1.0, 0.0, 0.0});
model = glm::rotate(model, glm::radians(y_angle), {0.0, 1.0, 0.0});
model = glm::rotate(model, glm::radians(z_angle), {0.0, 0.0, 1.0});
model = glm::scale(model, {scale, scale, scale});
}
void calcVertices()
{
for (auto& v : vertices)
{
auto u = model * glm::vec4(v, 1.0);
v.x = u.x;
v.y = u.y;
v.z = u.z;
}
for (auto i{0u}; i < 4; i++)
{
triangles[i][0] = vertices[i];
triangles[i][1] = vertices[(i + 1) % 4];
triangles[i][2] = vertices[(i + 2) % 4];
}
}
void setScale(const float s) { scale = s; }
void setShaders() { compileShadersFromFile("Obstacle.vesh", "Obstacle.frsh"); }
void setBuffers()
{
bindBuffers();
GLfloat vert[6][3]{
{-1, -1, -1},
{-1, 1, 1},
{ 1, 1, -1},
{1, -1, 1},
{-1, -1, -1},
{-1, 1, 1}};
glBufferData(GL_ARRAY_BUFFER, 6*3*sizeof(float), vert, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(
0, // attribute 0, must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0,//24, // stride
(void*)0); // array buffer offset
}
void draw(glm::mat4 view, bool border=false)
{
bindProgram();
bindBuffers();
auto projection = glm::mat4(1.0f);
projection = glm::perspective(glm::radians(45.0f), aspect, 0.01f, 100.0f);
glm::vec3 currColor{1.0, 1.0, 1.0};
glUniformMatrix4fv(1, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(2, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(3, 1, GL_FALSE, glm::value_ptr(projection));
if (not border)
{
if (isFinishObstacle())
currColor = finishColor;
else if (isTrap())
currColor = trapColor;
else if (isItem())
currColor = itemColor;
else currColor = color;
}
glUniform3f(4, currColor.x, currColor.y, currColor.z);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 6);
AGLErrors("Obstacle-draw");
}
float getDistanceFromPlayer() const { return glm::length(center - playerPos); }
bool isFinishObstacle() const { return obstacleType == ObstacleType::finish; }
bool isTrap() const { return obstacleType == ObstacleType::trap; }
bool isItem() const { return obstacleType == ObstacleType::item; }
void setObstacleType(ObstacleType ot) { obstacleType = ot; }
std::array<Triangle, 4>& getTriangles() { return triangles; }
private:
const glm::vec3 trapColor{0.77f, 0.01f, 0.02f};
const glm::vec3 finishColor{0.1f, 0.7f, 0.1f};
const glm::vec3 itemColor{1.0f, 0.83f, 0.0f};
float scale{1.0/18};
float x_angle{0.0}, y_angle{0.0}, z_angle{0.0};
glm::mat4 model;
glm::vec3 color{1.0, 1.0, 1.0};
glm::vec3 center{0.0, 0.0, 0.0};
std::array<glm::vec3, 6> vertices{
glm::vec3{-1.0f, -1.0f, -1.0f},
glm::vec3{-1.0f, 1.0f, 1.0f},
glm::vec3{ 1.0f, 1.0f, -1.0f},
glm::vec3{ 1.0f, -1.0f, 1.0f}};
std::array<Triangle, 4> triangles;
bool isFirstDraw{true};
float& aspect;
const glm::vec3& playerPos;
ObstacleType obstacleType;
};