-
Notifications
You must be signed in to change notification settings - Fork 1
/
Wall.cpp
246 lines (206 loc) · 9.57 KB
/
Wall.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
#include "Wall.h"
#include "ModelContainer.h"
#include "MyHelpers.h"
void Wall::BufferData() const
{
vector<GLfloat> wallVertices;
switch (location)
{
case LEFT_WALL:
wallVertices =
{
-distance, +0.0f , -width / 2.0f, 1.0f, 0.0f,
-distance, +width / 2.0f, -width / 2.0f, 1.0f, 1.0f,
-distance, +width / 2.0f, +width / 2.0f, 0.0f, 1.0f,
-distance, +0.0f , +width / 2.0f, 0.0f, 0.0f
};
break;
case RIGHT_WALL:
wallVertices =
{
+distance, +0.0f , +width / 2.0f, 1.0f, 0.0f,
+distance, +width / 2.0f, +width / 2.0f, 1.0f, 1.0f,
+distance, +width / 2.0f, -width / 2.0f, 0.0f, 1.0f,
+distance, +0.0f , -width / 2.0f, 0.0f, 0.0f
};
break;
case BACK_WALL:
wallVertices =
{
+width / 2.0f, +0.0f , -distance, 1.0f, 0.0f,
+width / 2.0f, +width / 2.0f, -distance, 1.0f, 1.0f,
-width / 2.0f, +width / 2.0f, -distance, 0.0f, 1.0f,
-width / 2.0f, +0.0f , -distance, 0.0f, 0.0f
};
break;
}
vector<GLushort> wallIndices = {0, 1, 2, 3, 0, 2};
vector<GLushort> secondWallIndices = {2, 1, 0, 2, 0, 3};
targetWidget->makeCurrent();
// Main wall
glBindVertexArray(wallVao);
glBindBuffer(GL_ARRAY_BUFFER, wallVbo);
glBufferData(GL_ARRAY_BUFFER, wallVertices.size() * sizeof wallVertices[0], &wallVertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, wallEbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, wallIndices.size() * sizeof wallIndices[0], &wallIndices[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof GLfloat, reinterpret_cast<GLvoid*>(0));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof GLfloat, reinterpret_cast<GLvoid*>(3 * sizeof GLfloat));
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDeleteBuffers(1, &wallVbo);
glDeleteBuffers(1, &wallEbo);
// Second wall
glBindVertexArray(secondWallVao);
glBindBuffer(GL_ARRAY_BUFFER, secondWallVbo);
glBufferData(GL_ARRAY_BUFFER, wallVertices.size() * sizeof wallVertices[0], &wallVertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, secondWallEbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, secondWallIndices.size() * sizeof secondWallIndices[0], &secondWallIndices[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof GLfloat, reinterpret_cast<GLvoid*>(0));
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDeleteBuffers(1, &secondWallVbo);
glDeleteBuffers(1, &secondWallEbo);
targetWidget->doneCurrent();
}
Wall::Wall(QOpenGLWidget* targetWidget, GLfloat distance, GLfloat width, Location location, WallRenderMode renderMode) :
distance{ distance }, width{ width }, location{ location }, renderMode{ renderMode }
{
this->targetWidget = targetWidget;
targetWidget->makeCurrent();
// Generate vertex array and buffers
glGenVertexArrays(1, &wallVao);
glGenBuffers(1, &wallVbo);
glGenBuffers(1, &wallEbo);
glGenVertexArrays(1, &secondWallVao);
glGenBuffers(1, &secondWallVbo);
glGenBuffers(1, &secondWallEbo);
// Create the shader
shader = make_unique<Shader>("shaders/wall.vs", "shaders/wall.fs");
shader->Create();
targetWidget->doneCurrent();
BufferData();
}
Wall::~Wall()
{
}
Location Wall::GetLocationByName(string locationName)
{
auto name = QString::fromStdString(locationName);
if (name.toLower() == "right")
return RIGHT_WALL;
if (name.toLower() == "back")
return BACK_WALL;
// LEFT_WALL for unknown string!
return LEFT_WALL;
}
void Wall::SetColor(glm::vec3 color)
{
this->color = color;
renderMode = WALL_COLOR;
}
void Wall::SetTexture(string textureFilePath)
{
glDeleteTextures(1, &textureId);
textureId = loadTexture(const_cast<char*>(textureFilePath.c_str()));
renderMode = WALL_TEXTURE;
}
void Wall::Draw(glm::mat4 const& view, glm::mat4 const& projection) const
{
shader->Use();
glUniformMatrix4fv(glGetUniformLocation(shader->GetProgram(), "model"), 1, GL_FALSE, value_ptr(glm::mat4()));
glUniformMatrix4fv(glGetUniformLocation(shader->GetProgram(), "view"), 1, GL_FALSE, value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(shader->GetProgram(), "projection"), 1, GL_FALSE, value_ptr(projection));
// Set the color/texture
glUniform1i(glGetUniformLocation(shader->GetProgram(), "textureMode"), renderMode == WALL_TEXTURE);
glUniform3fv(glGetUniformLocation(shader->GetProgram(), "wallColor"), 1, value_ptr(color));
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureId);
// Main wall
glBindVertexArray(wallVao);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, nullptr);
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
// Second (outer) wall
shader->Use();
glBindVertexArray(secondWallVao);
// This one is always rendered using color
glUniform1i(glGetUniformLocation(shader->GetProgram(), "textureMode"), false);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, nullptr);
glBindVertexArray(0);
}
void Wall::Bind(ModelContainer* container) const
{
float containerWidth, containerDepth, containerHeight;
switch (location)
{
case LEFT_WALL:
// Also rotate the model towards the wall (we need to rotate first in order to get
// the width and height correctly)
container->SetInitialRotationAngles(glm::vec3(0.0f, 90.0f, 0.0f));
// Move the container to wall location (with a small offset); we also need to consider the width of the model
// We also move the model to the middle of the wall
containerWidth = (container->GetMaximumVertices() - container->GetMinimumVertices()).x;
containerHeight = (container->GetMaximumVertices() - container->GetMinimumVertices()).y;
container->SetInitialTranslateVector(glm::vec3(-distance + containerWidth + 0.001f,
distance / 2.0f - containerHeight / 2.0f, 0.0f));
// Rotation is bound to 'x' and translation to 'yz'
container->SetRotationBound(glm::vec3(1.0f, 0.0f, 0.0f));
container->SetTranslationBound(glm::vec3(0.0f, 1.0f, 1.0f));
container->SetBoundedWall(location);
container->Reset();
break;
case RIGHT_WALL:
// Also rotate the model towards the wall (we need to rotate first in order to get
// the width and height correctly)
container->SetInitialRotationAngles(glm::vec3(0.0f, -90.0f, 0.0f));
// Move the container to wall location (with a small offset); we also need to consider the width of the model
// We also move the model to the middle of the wall
containerWidth = (container->GetMaximumVertices() - container->GetMinimumVertices()).x;
containerHeight = (container->GetMaximumVertices() - container->GetMinimumVertices()).y;
container->SetInitialTranslateVector(glm::vec3(+distance - containerWidth - 0.001f,
distance / 2.0f - containerHeight / 2.0f, 0.0f));
// Rotation is bound to 'x' and translation to 'yz'
container->SetRotationBound(glm::vec3(1.0f, 0.0f, 0.0f));
container->SetTranslationBound(glm::vec3(0.0f, 1.0f, 1.0f));
container->SetBoundedWall(location);
container->Reset();
break;
case BACK_WALL:
// Also rotate the model towards the wall
container->SetInitialRotationAngles(glm::vec3(0.0f, 0.0f, 0.0f));
// Move the container to wall location (with a small offset); we also need to consider the depth of the model
// We also move the model to the middle of the wall
containerDepth = (container->GetMaximumVertices() - container->GetMinimumVertices()).z;
containerHeight = (container->GetMaximumVertices() - container->GetMinimumVertices()).y;
container->SetInitialTranslateVector(glm::vec3(0.0f, distance / 2.0f - containerHeight / 2.0f,
-distance + containerDepth + 0.001f));
// Rotation is bound to 'z' and translation to 'xy'
container->SetRotationBound(glm::vec3(0.0f, 0.0f, 1.0f));
container->SetTranslationBound(glm::vec3(1.0f, 1.0f, 0.0f));
container->SetBoundedWall(location);
container->Reset();
break;
}
}
bool Wall::IsAttached(ModelContainer* container) const
{
auto containerMinimumCoords = container->GetMinimumVertices();
auto containerMaximumCoords = container->GetMaximumVertices();
auto offset = 0.1f;
// Check to see whether the model is inside the offset with respect to the wall
switch (location)
{
case LEFT_WALL:
return containerMinimumCoords.x <= -distance + offset;
case RIGHT_WALL:
return containerMaximumCoords.x >= distance - offset;
case BACK_WALL:
return containerMinimumCoords.z <= -distance + offset;
}
return false;
}