-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAss1Good.cpp
530 lines (421 loc) · 14.7 KB
/
Ass1Good.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
519
520
521
522
523
524
525
526
527
528
529
530
//Lab 2
//modified from http://learnopengl.com/
#include "stdafx.h"
#include "..\glew\glew.h" // include GL Extension Wrangler
#include "..\glfw\glfw3.h" // include GLFW helper library
#include <stdio.h>
#include <iostream>
#include <string>
#include <fstream>
#include <gtc/matrix_transform.hpp>
#include <gtc/type_ptr.hpp>
#include <vector>
#include <glm.hpp>
using namespace std;
// Window dimensions
const GLuint WIDTH = 800, HEIGHT = 800;
glm::vec3 triangle_scale;
glm::vec3 camera_translation = glm::vec3(0.0f, 0.0f, -1.0f);
float rotation = 9.0;
glm::mat4 model_matrix;
glm::mat4 view_matrix;
const float TRIANGLE_MOVEMENT_STEP = 0.1f;
const float CAMERA_PAN_STEP = 0.2f;
GLfloat radius = 10.0f;
GLfloat camX = sin(glfwGetTime()) * radius;
GLfloat camZ = cos(glfwGetTime()) * radius;
glm::mat4 view;
//view_matrix = glm::lookAt(glm::vec3(camX, 0.0, camZ), glm::vec3(0.0, 0.0, 0.0), glm::vec3(0.0, 1.0, 0.0));
float valx= 10.0;
float valy = 10.0;
// Is called whenever a key is pressed/released via GLFW
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
std::cout << key << std::endl;
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
if (key == GLFW_KEY_LEFT && action == GLFW_PRESS)
valx-=10;
if (key == GLFW_KEY_RIGHT && action == GLFW_PRESS)
valx+=10;
if (key == GLFW_KEY_UP && action == GLFW_PRESS)
valy+= 10;;
if (key == GLFW_KEY_DOWN && action == GLFW_PRESS)
valy-= 10;
if (key == GLFW_KEY_D && action == GLFW_PRESS)
camera_translation.x += CAMERA_PAN_STEP;
if (key == GLFW_KEY_A && action == GLFW_PRESS)
camera_translation.x -= CAMERA_PAN_STEP;
if (key == GLFW_KEY_S && action == GLFW_PRESS)
camera_translation.y -= CAMERA_PAN_STEP;
if (key == GLFW_KEY_W && action == GLFW_PRESS)
camera_translation.y += CAMERA_PAN_STEP;
if (key == GLFW_KEY_P && action == GLFW_PRESS)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
if (key == GLFW_KEY_R && action == GLFW_PRESS)
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
//model_matrix = glm::scale(model_matrix, triangle_scale);
if (key == GLFW_KEY_O && action == GLFW_PRESS)
{
glPointSize(10);
glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
}
}
// The MAIN function, from here we start the application and run the game loop
int main()
{
std::cout << "Starting GLFW context, OpenGL 3.3" << std::endl;
// Init GLFW
glfwInit();
// Set all the required options for GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
// Create a GLFWwindow object that we can use for GLFW's functions
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Triangle", nullptr, nullptr);
if (window == nullptr)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// Set the required callback functions
glfwSetKeyCallback(window, key_callback);
// Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions
glewExperimental = GL_TRUE;
// Initialize GLEW to setup the OpenGL Function pointers
if (glewInit() != GLEW_OK)
{
std::cout << "Failed to initialize GLEW" << std::endl;
return -1;
}
// Define the viewport dimensions
int width, height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
// Build and compile our shader program
// Vertex shader
// Read the Vertex Shader code from the file
string vertex_shader_path = "vertex.shader";
string VertexShaderCode;
std::ifstream VertexShaderStream(vertex_shader_path, ios::in);
if (VertexShaderStream.is_open()) {
string Line = "";
while (getline(VertexShaderStream, Line))
VertexShaderCode += "\n" + Line;
VertexShaderStream.close();
}
else {
printf("Impossible to open %s. Are you in the right directory ?\n", vertex_shader_path.c_str());
getchar();
exit(-1);
}
// Read the Fragment Shader code from the file
string fragment_shader_path = "fragment.shader";
std::string FragmentShaderCode;
std::ifstream FragmentShaderStream(fragment_shader_path, std::ios::in);
if (FragmentShaderStream.is_open()) {
std::string Line = "";
while (getline(FragmentShaderStream, Line))
FragmentShaderCode += "\n" + Line;
FragmentShaderStream.close();
}
else {
printf("Impossible to open %s. Are you in the right directory?\n", fragment_shader_path.c_str());
getchar();
exit(-1);
}
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
char const * VertexSourcePointer = VertexShaderCode.c_str();
glShaderSource(vertexShader, 1, &VertexSourcePointer, NULL);
glCompileShader(vertexShader);
// Check for compile time errors
GLint success;
GLchar infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// Fragment shader
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
char const * FragmentSourcePointer = FragmentShaderCode.c_str();
glShaderSource(fragmentShader, 1, &FragmentSourcePointer, NULL);
glCompileShader(fragmentShader);
// Check for compile time errors
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// Link shaders
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
// Check for linking errors
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
glDeleteShader(vertexShader); //free up memory
glDeleteShader(fragmentShader);
glUseProgram(shaderProgram);
//Reading vertices from from a file
ifstream input;
input.open("input_a1.txt");
int curveType;
int numPoints;
string line;
input >> curveType;
cout << curveType <<endl;
float x = 0;
float y = 0;
float z = 0;
//the two vector curves
vector < glm::vec3*>* profile = new vector<glm::vec3*>;
vector < glm::vec3*>* trajectory = new vector<glm::vec3*>;
GLfloat* vertices2;
int indexSize;
int *indices;
int size;
//If curveType==0 then it is a translational curve
if (curveType == 0)
{
//Gets te numbwe or rows and skips lines
getline(input, line);
input >> numPoints;
cout << numPoints << endl;
getline(input, line);
//Getting profile the actual data
for (int i = 0; i < numPoints; i++)
{
input >> x;
input >> y;
input >> z;
profile->push_back(new glm::vec3(x, y, z));
cout << x << ", " << y << ", " << z << std::endl;
getline(input, line);
}
//Getting the trajectory data
int numPoints2;
input >> numPoints2;
for (int i = 0; i < numPoints2; i++)
{
input >> x;
input >> y;
input >> z;
trajectory->push_back(new glm::vec3(x, y, z));
cout << x << ", " << y << ", " << z << std::endl;
getline(input, line);
}
//Size, which is the profile vector size times the trajectory vect size times 6
size = profile->size()*trajectory->size() * 6;
vertices2 = new GLfloat[size];
//A position which we will need to keep for the indices
int pos = 0;
int indexEBO = 0;
/////////////////////////////////////////////////////////////////////
indexSize = (profile->size() - 1)*(trajectory->size() - 1) * 3 * 2;
indices = new int[indexSize];
//Translating the profile and trajectory vertices
for (int i = 0; i < profile->size(); i++)
{
for (int k = 0; k < trajectory->size(); k++)
{
float height = (float(i) / float(profile->size()));
vertices2[pos] = profile->at(i)->x + trajectory->at(k)->x;
vertices2[pos + 1] = profile->at(i)->y + trajectory->at(k)->y;
vertices2[pos + 2] = profile->at(i)->z + trajectory->at(k)->z;
vertices2[pos + 3] = height;
vertices2[pos + 4] = height;
vertices2[pos + 5] = 0;
pos += 6;
//If we are after the first iteration of i and k, then it means we are ontop.
//We must now take the indices for the EBO in order to draw the triangles
if (i > 0 && k > 0)
{
//Getting the indices for the EBO
indices[indexEBO] = pos / 6 - 1;
indices[indexEBO + 1] = pos / 6 - 1 - 1;
indices[indexEBO + 2] = pos / 6 - trajectory->size() - 2;
indices[indexEBO + 3] = pos / 6 - 1;
indices[indexEBO + 4] = pos / 6 - trajectory->size() - 1;
indices[indexEBO + 5] = pos / 6 - trajectory->size() - 2;
indexEBO += 6;
}
}
}
cout << "\nEBO size: " << indexSize << " VBO size: " << size << endl;
//TESTING
int c = 0;
for (int i = 0; i < size; i++)
{
printf("%f ", vertices2[i]);
c++;
if (c > 2)
{
c = 0;
std::cout << std::endl;
}
}
c = 0;
std::cout << "Indices " << indexSize << std::endl;
for (int i = 0; i < indexSize; i++)
{
printf("%d ", indices[i]);
c++;
if (c > 2)
{
c = 0;
std::cout << std::endl;
}
}
}
//Else, if this is a rotational curve
else if (curveType != 0)
{
int spans = 0;
input >> spans;
getline(input, line);
input >> numPoints;
//Getting profile the actual data
for (int i = 0; i < numPoints; i++)
{
input >> x;
input >> z;//cuz they messed up
input >> y;//cuz they messed up
profile->push_back(new glm::vec3(x, y, z));
cout << x << ", " << y << ", " << z << std::endl;
getline(input, line);
}
glm::mat4x4 rotation = glm::mat4(1.0f);
size = profile->size() *spans * 6;
indexSize = (profile->size() - 1)*(spans) * 6;
int pos = 0;
int indexEBO = 0;
vertices2 = new GLfloat[size];
indices = new int[ indexSize];
for (int i = 0; i < spans; i++)
{
for (int k = 0; k < numPoints; k++)
{
glm::vec4 profile2 = glm::vec4(*profile->at(k), 1.0);
glm::vec4 finalVec = profile2 * glm::rotate(rotation, glm::radians(360.0f / (float)spans)*i, glm::vec3(0, 1, 0));
float height = (float(i) / float(profile->size()));
vertices2[pos] = finalVec.x;
vertices2[pos + 1] = finalVec.y;
vertices2[pos + 2] = finalVec.z;
vertices2[pos + 3] = height;
vertices2[pos + 4] = height;
vertices2[pos + 5] = 0;
pos += 6;
//If we are after the first iteration of i and k, then it means we are ontop.
//We must now take the indices for the EBO in order to draw the triangles
if (i > 0 && k > 0)
{
//Getting the indices for the EBO
indices[indexEBO] = pos / 6 - 1;
indices[indexEBO + 1] = pos / 6 - 1 - 1;
indices[indexEBO + 2] = pos / 6 - profile->size() - 2;
indices[indexEBO + 3] = pos / 6 - 1;
indices[indexEBO + 4] = pos / 6 - profile->size() - 1;
indices[indexEBO + 5] = pos / 6 - profile->size() - 2;
indexEBO += 6;
}
else if (k > 0)
{
indices[indexEBO] = pos / 6 - 1;
indices[indexEBO + 1] = pos / 6 - 1 - 1;
indices[indexEBO + 2] = size / 6 - profile->size()+pos/6 - 2;
indices[indexEBO + 3] = pos / 6 - 1;
indices[indexEBO + 4] = size / 6 - profile->size()+pos/6 - 1;
indices[indexEBO + 5] = size / 6 - profile->size()+pos/6 - 2;
indexEBO += 6;
}
}
}
}
//CameraStuff
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
glm::vec3 cameraTarget = glm::vec3(0.0f, 0.0f, 0.0f);
//We need this one
glm::vec3 cameraDirection = glm::normalize(cameraPos - cameraTarget);
//Do cross product in order to get the right vector
//Cross between up and camearadirection
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
//Good one we need
glm::vec3 cameraRight = glm::normalize(glm::cross(up, cameraDirection));
//Cross between cameraDir and camright
//Another one we need
glm::vec3 cameraUp = glm::cross(cameraDirection, cameraRight);
glm::mat4 view;
view = glm::lookAt(glm::vec3(0.0f, 0.0f, 3.0f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 1.0f, 0.0f));
//VAO, VBO, and EBO stuff
GLuint VAO, VBO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
//Binding the VAO
glBindVertexArray(VAO);
// Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s).
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices2)*size, vertices2, GL_STATIC_DRAW);
//EBO STUFF
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices)*indexSize, indices, GL_STATIC_DRAW);
//For the vertices
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
//For the colors
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
triangle_scale = glm::vec3(1.0f); //shorthand, initializes all 4 components to 1.0f
GLuint transformLoc = glGetUniformLocation(shaderProgram, "model_matrix");
GLuint viewMatrixLoc = glGetUniformLocation(shaderProgram, "view_matrix");
GLuint projectionLoc = glGetUniformLocation(shaderProgram, "projection_matrix");
float rotation = 9;
// Game loop
while (!glfwWindowShouldClose(window))
{
// Check if any events have been activated (key pressed, mouse moved etc.) and call corresponding response functions
glfwPollEvents();
// Render
// Clear the colorbuffer
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT );
glm::mat4 model_matrix;
model_matrix = glm::scale(model_matrix, triangle_scale);
model_matrix = glm::rotate(model_matrix, glm::radians(valx), glm::vec3(1, 0, 0));
model_matrix = glm::rotate(model_matrix, glm::radians(valy), glm::vec3(0, 1, 0));
model_matrix = glm::rotate(model_matrix, glm::radians(z), glm::vec3(0, 0, 1));
//rotation++;
;
view_matrix = glm::lookAt(glm::vec3(0.0f, 0.0f, 5.0f), //camera positioned here
glm::vec3(0.0f, 0.0f, 0.0f), //looks at origin
glm::vec3(0.0f, rotation, 0.0f)); //up vector
rotation++;
glm::mat4 projection_matrix;
projection_matrix = glm::perspective(45.0f, (GLfloat)width / (GLfloat)height, 0.0f, 100.0f);
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(model_matrix)); //broadcast the uniform value to the shaders
glUniformMatrix4fv(viewMatrixLoc, 1, GL_FALSE, glm::value_ptr(view_matrix));
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection_matrix));
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, indexSize, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
// Swap the screen buffers
glfwSwapBuffers(window);
}
// Terminate GLFW, clearing any resources allocated by GLFW.
glfwTerminate();
return 0;
}