-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparkviewer.cpp
357 lines (299 loc) · 13.9 KB
/
parkviewer.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
#include "parkviewer.h"
static Camera camera(SCREEN_WIDTH, SCREEN_HEIGHT, x_min, x_max, y_min, y_max, z_min, z_max);
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
camera.scroll_callback(window, xoffset, yoffset);
}
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
camera.mouse_callback(window, xpos, ypos);
}
void drawGenericObject(GLuint &VAO, GLuint programID,
glm::mat4 proj,
glm::mat4 view,
int size,
bool elemental,
glm::vec3 translationVector,
glm::vec3 scaleVector,
GLfloat rotationAngle,
glm::vec3 rotationAxis)
{
// Must match the name specified in the vertex shader.
GLuint matrixID = glGetUniformLocation(programID, "MVP");
GLuint modelID = glGetUniformLocation(programID, "model");
GLuint lightID = glGetUniformLocation(programID, "LightPos");
// Binding the vertex array is equivalent to setting a global variable for the rest of the gl calls here
glBindVertexArray(VAO);
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, translationVector);
model = glm::scale(model, scaleVector);
model = glm::rotate(model, glm::radians(rotationAngle), rotationAxis);
glm::mat4 MVP = proj*view*model;
// Pass our arrays over to the vertexshader for further transformations
glUniformMatrix4fv(matrixID, 1, GL_FALSE, &MVP[0][0]);
glUniformMatrix4fv(modelID, 1, GL_FALSE, &model[0][0]);
glUniform3fv(lightID, 1, &lightPos[0]);
if (elemental) {
glDrawElements(GL_TRIANGLES, size, GL_UNSIGNED_INT, 0);
} else {
glDrawArrays(GL_TRIANGLES, 0, size*3);
}
// Unbinds the vertex array
glBindVertexArray(0);
}
bool loadAssImp(const char * path, std::vector<unsigned short> &indices,
std::vector<glm::vec3> &vertices,
std::vector<glm::vec3> &normals) {
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(path, 0);
const aiMesh* mesh = scene->mMeshes[0];
vertices.reserve(mesh->mNumVertices);
for(ulong64_t i = 0; i < mesh->mNumVertices; i++){
aiVector3D pos = mesh->mVertices[i];
vertices.push_back(glm::vec3(pos.x, pos.y, pos.z));
}
normals.reserve(mesh->mNumVertices);
for(ulong64_t i = 0; i < mesh->mNumVertices; i++){
aiVector3D n = mesh->mNormals[i];
normals.push_back(glm::vec3(n.x, n.y, n.z));
}
indices.reserve(3*mesh->mNumFaces);
for (ulong64_t i=0; i<mesh->mNumFaces; i++){
indices.push_back(mesh->mFaces[i].mIndices[0]);
indices.push_back(mesh->mFaces[i].mIndices[1]);
indices.push_back(mesh->mFaces[i].mIndices[2]);
}
return true;
}
bool initOpenGL()
{
// Set OpenGL version to 3.3
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
return true;
}
void setupMeshVAO(Mesh mesh, GLfloat* color_vector, vector<ObjectData> &objectVector)
{
ObjectData object;
vector<GLfloat> v = mesh.getVertices();
GLfloat* vertices = &v[0];
object.indexSize = v.size()/3; //# of vertices = arraysize/3 (x,y,z)
int size = v.size()*sizeof(GLfloat);
vector<glm::vec3> normals = mesh.getNormals();
GLfloat ModelNormalArray[normals.size()*3];
ulong64_t i = 0;
// Working with a regular array is easier than working with vectors for passing on to the VS/FS
for (auto it = normals.begin(); it != normals.end(); it++) {
ModelNormalArray[i++] = it->x;
ModelNormalArray[i++] = it->y;
ModelNormalArray[i++] = it->z;
}
// We are only generating one VA at a time for each object
glGenVertexArrays(1, &(object.ModelArrayID));
glBindVertexArray(object.ModelArrayID);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
// Separate buffers are created for the vertex buffer, color buffer, and normal buffer of the 3D object
// These buffers store the data and forward it on to the GPU to be processed further by the VS/FS
glGenBuffers(1, &(object.ModelVBO));
glBindBuffer(GL_ARRAY_BUFFER, object.ModelVBO);
glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glGenBuffers(1, &(object.ModelColorVBO));
glBindBuffer(GL_ARRAY_BUFFER, object.ModelColorVBO);
glBufferData(GL_ARRAY_BUFFER, size, color_vector, GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glGenBuffers(1, &(object.ModelNormalVBO));
glBindBuffer(GL_ARRAY_BUFFER, object.ModelNormalVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(ModelNormalArray), ModelNormalArray, GL_STATIC_DRAW);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glBindVertexArray(0);
objectVector.push_back(object);
}
void setCallBacks(GLFWwindow* window)
{
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
}
void generateModelVAO(string path, ObjectData &object, const GLfloat* color_array)
{
std::vector<unsigned short> indices;
std::vector<glm::vec3> vertices;
std::vector<glm::vec3> normals;
if(loadAssImp(path.c_str(), indices, vertices, normals)) {
GLfloat* ModelVertexArray = new GLfloat[vertices.size()*3];
GLfloat* ModelColorArray = new GLfloat[vertices.size()*3];
GLfloat* ModelNormalArray = new GLfloat[normals.size()*3];
unsigned int indexList[indices.size()];
ulong64_t i = 0;
for (auto it = vertices.begin(); it != vertices.end(); it++) {
ModelVertexArray[i++] = it->x;
ModelVertexArray[i++] = it->y;
ModelVertexArray[i++] = it->z;
}
cout << path << i;
i = 0;
for (auto it = normals.begin(); it != normals.end(); it++) {
ModelNormalArray[i++] = it->x;
ModelNormalArray[i++] = it->y;
ModelNormalArray[i++] = it->z;
}
for (ulong64_t j = 0; j < indices.size(); j++) {
indexList[j] = indices[j];
}
for (ulong64_t j = 0; j < i;) {
for (int ctr = 0; ctr < 9; ctr++) {
ModelColorArray[j++] = color_array[ctr];
}
}
object.indexSize = indices.size();
glGenVertexArrays(1, &(object.ModelArrayID));
glBindVertexArray(object.ModelArrayID);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glGenBuffers(1, &(object.ModelVBO));
glBindBuffer(GL_ARRAY_BUFFER, object.ModelVBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size()*3*sizeof(GLfloat),
ModelVertexArray, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glGenBuffers(1, &(object.ModelColorVBO));
glBindBuffer(GL_ARRAY_BUFFER, object.ModelColorVBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size()*3*sizeof(GLfloat),
ModelColorArray, GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glGenBuffers(1, &(object.ModelNormalVBO));
glBindBuffer(GL_ARRAY_BUFFER, object.ModelNormalVBO);
glBufferData(GL_ARRAY_BUFFER, normals.size()*3*sizeof(GLfloat),
ModelNormalArray, GL_STATIC_DRAW);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glGenBuffers(1, &(object.EBO));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object.EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexList),
indexList, GL_STATIC_DRAW);
glBindVertexArray(0);
// We wouldn't want memory leaks, would we?
// Once we have pushed the data to the GPU, we can safely delete the data
delete[] ModelVertexArray;
delete[] ModelColorArray;
delete[] ModelNormalArray;
}
}
int main()
{
if(!glfwInit()) {
fprintf( stderr, "Failed to initialize GLFW\n" );
getchar();
return false;
}
if (!initOpenGL()) {
return -1;
}
window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Cubes!", glfwGetPrimaryMonitor(), NULL);
if(window == NULL){
glfwTerminate();
return -1;
}
setCallBacks(window);
glewExperimental = true;
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
getchar();
glfwTerminate();
return false;
}
GLuint ModelArrayID, ModelVBO, ModelColorVBO, EBO, indexSize;
ObjectData swing, carousel, swingChair, cg;
ObjectData coaster_track1, coaster_track2, coaster_track3, coaster_cart,
coaster_glasses, coaster_head, coaster_mouth;
generateModelVAO("swing_seat.obj", swingChair, swing_chair_color);
generateModelVAO("swing_frame.obj", swing, swing_frame_color);
generateModelVAO("1simple_round.obj", carousel, carousel_color);
generateModelVAO("coaster_cart.obj", coaster_cart, cart_color);
generateModelVAO("coaster_track1.obj", coaster_track1, track_color);
generateModelVAO("coaster_track2.obj", coaster_track2, track_color);
generateModelVAO("coaster_track3.obj", coaster_track3, track_color);
generateModelVAO("coaster_glasses.obj", coaster_glasses, glasses_color);
generateModelVAO("coaster_head.obj", coaster_head, monkey_color);
generateModelVAO("coaster_mouth.obj", coaster_mouth, mouth_color);
// Sky color
glClearColor(1.0f, 0.8f, 0.45f, 0.9f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
GLuint programID = LoadShaders("TransformVertexShader.vertexshader", "ColorFragmentShader.fragmentshader");
glm::mat4 proj;
glm::mat4 view;
Scene scene = Scene();
glm::mat4 T;
scene.addSeeSaw(glm::vec3(10, 0, 6));
T = glm::rotate(glm::mat4(1.0), (float)glm::radians(150.0), glm::vec3(0, 1, 0));
scene.addSlide(glm::vec3(-5, 0, 8), glm::vec3(0.6, 0.3, 0.1), 4, T);
T = glm::rotate(glm::mat4(1.0), (float)glm::radians(90.0), glm::vec3(0, 1, 0));
scene.addMonkeyBars(glm::vec3(-8, 0, 11), glm::vec3(.0, 1.0, 0.2), 7, 3, T);
scene.addPath(glm::vec2(-1, 0), glm::vec2(1, 20), glm::vec3(0.2, 0.2, 0.2));
scene.addPath(glm::vec2(-19, 0), glm::vec2(19, 2), glm::vec3(0.2, 0.2, 0.2));
scene.addFence(glm::vec4(x_min, x_max, z_min, z_max));
scene.addFloor(glm::vec4(x_min, x_max, z_min, z_max));
scene.addSculpture();
vector<Mesh> mesh_group = scene.getMesh();
vector<vector<GLfloat>> color_vector_group = scene.getColors();
vector<ObjectData> sceneMesh;
for (int i = 0; i < mesh_group.size(); i++) {
setupMeshVAO(mesh_group.at(i), &color_vector_group.at(i)[0], sceneMesh);
}
while(glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && !glfwWindowShouldClose(window)) {
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
if (SHOW_FPS) {
cout << "FPS: " << 1.0f/deltaTime << endl;
}
lastFrame = currentFrame;
// Used for capturing the WASD keys and mouse
camera.processInput(window, deltaTime);
// Defines what can be seen by the camera along with the clip boundaries of the scene
proj = glm::perspective(glm::radians(camera.getFOV()), (float)SCREEN_WIDTH/(float)SCREEN_HEIGHT, 0.2f, 100.0f);
view = camera.getCameraView();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(programID);
drawGenericObject(carousel.ModelArrayID, programID, proj, view,
carousel.indexSize, true, carouselPosition, carouselScale,
(float)glfwGetTime()*45.0f, glm::vec3(0,1,0));
drawGenericObject(swing.ModelArrayID, programID, proj, view,
swing.indexSize, true, swingPosition, swingScale);
drawGenericObject(swingChair.ModelArrayID, programID, proj, view,
swingChair.indexSize, true, swingChairPosition, swingChairScale);
drawGenericObject(coaster_cart.ModelArrayID, programID, proj, view,
coaster_cart.indexSize, true, coasterPosition, coasterScale);
drawGenericObject(coaster_track1.ModelArrayID, programID, proj, view,
coaster_track1.indexSize, true, coasterPosition, coasterScale);
drawGenericObject(coaster_track2.ModelArrayID, programID, proj, view,
coaster_track2.indexSize, true, coasterPosition, coasterScale);
drawGenericObject(coaster_track3.ModelArrayID, programID, proj, view,
coaster_track3.indexSize, true, coasterPosition, coasterScale);
drawGenericObject(coaster_mouth.ModelArrayID, programID, proj, view,
coaster_mouth.indexSize, true, coasterPosition, coasterScale);
drawGenericObject(coaster_head.ModelArrayID, programID, proj, view,
coaster_head.indexSize, true, coasterPosition, coasterScale);
drawGenericObject(coaster_glasses.ModelArrayID, programID, proj, view,
coaster_glasses.indexSize, true, coasterPosition, coasterScale);
for (auto it = sceneMesh.begin(); it != sceneMesh.end(); it++) {
drawGenericObject(it->ModelArrayID, programID, proj, view, it->indexSize, false);
}
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteProgram(programID);
glfwTerminate();
return 0;
}