-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
397 lines (305 loc) · 13.3 KB
/
main.cpp
File metadata and controls
397 lines (305 loc) · 13.3 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
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
// Code adapted from www.learnopengl.com, www.glfw.org
#include <iostream>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
// GLM Mathematics
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtx/matrix_interpolation.hpp>
#include <glm/gtc/constants.hpp>
#include "shader.h"
int main(void)
{
//create a glfw window
GLFWwindow* window;
if (!glfwInit()) //Initialize the library
return -1;
int wHeight = 960;
int wWidth = 960;
window = glfwCreateWindow(wWidth, wHeight, "OpenGL Window", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);//Make the window's context current
//Initialize GLEW to setup the OpenGL Function pointers
glewExperimental = GL_TRUE;
glewInit();
//Define the viewport dimensions
glViewport(0, 0, 960, 960);
//Set up vertex data (and buffer(s)) and attribute pointers
//Large Right Angled Triangle 1 (in red)
GLfloat triangle1[] = {
-0.5, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, //bottom left
-0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, //top left
0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f //middle
};
//Large Right Angled Triangle 2 (in green)
GLfloat triangle2[] = {
-0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, //top left
0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, //middle
0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f //top right
};
//Small Right Angled Triangle 1 (in blue)
GLfloat triangle3[] = {
0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, //top right
0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, //bottom right
0.25f, 0.25f, 0.0f, 0.0f, 0.0f, 1.0f //middle
};
//Small Right Angled Triangle 2 (in turqoise)
GLfloat triangle4[] = {
-0.25f, -0.25f, 0.0f, 0.0f, 1.0f, 1.0f, //bottom left
0.25f, -0.25f, 0.0f, 0.0f, 1.0f, 1.0f, //bottom right
0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f //middle
};
//Medium Right Angled Triangle (in yellow)
GLfloat triangle5[] = {
0.0f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f, //bottom left
0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f, //bottom right
0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f //top right
};
//Square (in purple)
GLfloat square1[] = {
0.25f, 0.25f, 0.0f, 1.0f, 0.0f, 1.0f, //top
0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, //left
0.25f, -0.25f, 0.0f, 1.0f, 0.0f, 1.0f, //bottom
0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f //right
};
//Parallelogram (in orange)
GLfloat parallelogram[] = {
-0.5f, -0.5f, 0.0f, 1.0f, 0.65f, 0.0f, //bottom left
-0.25f, -0.25, 0.0f, 1.0f, 0.65f, 0.0f, //top left
0.0f, -0.5f, 0.0f, 1.0f, 0.65f, 0.0f, //bottom left
0.25f, -0.25f, 0.0f, 1.0f, 0.65f, 0.0f //bottom right
};
//I've done the vertices for all of the shapes.
//Triangles 1-5 correspond with indices 1-5, indices 6 is the square, indices 7 is the parallelogram
GLuint indices1[] = {
0,1,2
};
GLuint indices2[] = {
0,1,2
};
GLuint indices3[] = {
0,1,2
};
GLuint indices4[] = {
0,1,2
};
GLuint indices5[] = {
0,1,2
};
GLuint indices6[] = {
0,1,3,
1,2,3,
};
GLuint indices7[] = {
0,1,2,
1,2,3,
};
//Create the Buffers & Vertes Arrays
GLuint VBOs[7], VAOs[7], EBOs[7];
glGenVertexArrays(7, VAOs);
glGenBuffers(7, VBOs);
glGenBuffers(7, EBOs);
// ================================
// buffer setup
// ===============================
//Triangle One
glBindVertexArray(VAOs[0]);
glBindBuffer(GL_ARRAY_BUFFER, VBOs[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(triangle1), triangle1, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBOs[0]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices1), indices1, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0); // Vertex attributes stay the same
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
//Triangle Two
glBindVertexArray(VAOs[1]);
glBindBuffer(GL_ARRAY_BUFFER, VBOs[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(triangle2), triangle2, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBOs[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices2), indices2, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0); // Vertex attributes stay the same
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
//Triangle 3
glBindVertexArray(VAOs[2]);
glBindBuffer(GL_ARRAY_BUFFER, VBOs[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(triangle3), triangle3, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBOs[2]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices3), indices3, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0); // Vertex attributes stay the same
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
//Triangle 4
glBindVertexArray(VAOs[3]);
glBindBuffer(GL_ARRAY_BUFFER, VBOs[3]);
glBufferData(GL_ARRAY_BUFFER, sizeof(triangle4), triangle4, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBOs[3]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices4), indices4, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0); // Vertex attributes stay the same
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
//Triangle 5
glBindVertexArray(VAOs[4]);
glBindBuffer(GL_ARRAY_BUFFER, VBOs[4]);
glBufferData(GL_ARRAY_BUFFER, sizeof(triangle5), triangle5, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBOs[4]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices5), indices5, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0); // Vertex attributes stay the same
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
//Square
glBindVertexArray(VAOs[5]);
glBindBuffer(GL_ARRAY_BUFFER, VBOs[5]);
glBufferData(GL_ARRAY_BUFFER, sizeof(square1), square1, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBOs[5]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices6), indices6, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0); // Vertex attributes stay the same
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
//Parallelogram
glBindVertexArray(VAOs[6]);
glBindBuffer(GL_ARRAY_BUFFER, VBOs[6]);
glBufferData(GL_ARRAY_BUFFER, sizeof(parallelogram), parallelogram, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBOs[6]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices7), indices7, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0); // Vertex attributes stay the same
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
//Bind all the vertex arrays
glBindVertexArray(0);
glBindVertexArray(1);
glBindVertexArray(2);
glBindVertexArray(3);
glBindVertexArray(4);
glBindVertexArray(5);
glBindVertexArray(6);
//Build and compile shader program
GLuint shaderProgram = initShader("vert.glsl", "frag.glsl");
//animating is set to true on mouse click, and then false when the animation is done
bool animating = false;
//sets the running time to 0
glfwSetTime(0);
//mvp matrix for zooming the camera out.
glm::mat4 Projection = glm::perspective(45.0f, GLfloat(wWidth) / GLfloat(wHeight), 0.1f, 100.0f);
glm::mat4 View = glm::lookAt(
glm::vec3(0.0f, 0.0f, 2.0f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 1.0f, 0.0f)
);
glm::mat4 VP = Projection * View;
// Loop until the user closes the window
while (!glfwWindowShouldClose(window))
{
// Render here
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Draw the first Triangle
// use shader
glUseProgram(shaderProgram);
// Define transform matrix
glm::mat4 transform;
//if the mouse is clicked, the animation begins and time is reset.
if (glfwGetMouseButton(window,0) == GLFW_PRESS) {
animating = true;
glfwSetTime(0);
}
//anim_time is the time the animation should run for in seconds.
float anim_time = 3.5f;
//anim_fraction is the fraction of the animation that is done.
float anim_fraction = 0.0f;
//if it's animating
if (animating){
//if its been animating for less time than the animation time
if (glfwGetTime() < anim_time) {
//set a new anim_fraction
anim_fraction = glfwGetTime() / anim_time;
} else {
//else, it is done animating
anim_fraction = 1.0f;
}
}
// Get matrix's uniform location and set matrix
GLint transformLoc = glGetUniformLocation(shaderProgram, "transform");
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(transform));
// draw objects
//red triangle
//Translations
transform = glm::translate(transform, glm::vec3((0.75f * anim_fraction), (0.0f * anim_fraction), (0.0f*anim_fraction))); //translate
transform = glm::rotate(transform, glm::radians(-45.0f * anim_fraction), glm::vec3(0.0f, 0.0f, -1.0f)); //rotate
transform = VP * transform; //scale
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(transform));
glBindVertexArray(VAOs[0]);
glDrawArrays(GL_TRIANGLES, 0, GLint((sizeof(triangle1) / sizeof(GLfloat)) / 3));
//green triangle
transform = glm::mat4(1.0);
transform = glm::translate(transform, glm::vec3((-0.31f*anim_fraction), (-0.355f * anim_fraction), (0.0f *anim_fraction)));
transform = glm::rotate(transform, glm::radians(45.0f * anim_fraction), glm::vec3(0.0f, 0.0f, -1.0f));
transform = VP * transform;
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(transform));
glBindVertexArray(VAOs[1]);
glDrawArrays(GL_TRIANGLES, 0, GLint((sizeof(triangle2) / sizeof(GLfloat)) / 3));
//small blue triangle
transform = glm::mat4(1.0);
transform = glm::translate(transform, glm::vec3((0.4f * anim_fraction), (-1.065f * anim_fraction), (0.0f * anim_fraction)));
transform = glm::rotate(transform, glm::radians(-45.0f * anim_fraction), glm::vec3(0.0f, 0.0f, -1.0f));
transform = VP * transform;
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(transform));
glBindVertexArray(VAOs[2]);
glDrawArrays(GL_TRIANGLES, 0, GLint((sizeof(triangle3) / sizeof(GLfloat)) / 3));
//turqoise triangle
transform = glm::mat4(1.0);
transform = glm::translate(transform, glm::vec3((0.75f * anim_fraction), (0.35f * anim_fraction), (0.0f * anim_fraction)));
transform = glm::rotate(transform, glm::radians(-45.0f * anim_fraction), glm::vec3(0.0f, 0.0f, -1.0f));
transform = VP * transform;
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(transform));
glBindVertexArray(VAOs[3]);
glDrawArrays(GL_TRIANGLES, 0, GLint((sizeof(triangle4) / sizeof(GLfloat)) / 3));
//yellow triangle
transform = glm::mat4(1.0);
transform = glm::translate(transform, glm::vec3((-0.6625f * anim_fraction), (-0.2f * anim_fraction), (0.0f * anim_fraction)));
transform = glm::rotate(transform, glm::radians(225.0f * anim_fraction), glm::vec3(0.0f, 0.0f, -1.0f));
transform = VP * transform;
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(transform));
glBindVertexArray(VAOs[4]);
glDrawArrays(GL_TRIANGLES, 0, GLint((sizeof(triangle5) / sizeof(GLfloat)) / 3));
//square
transform = glm::mat4(1.0);
transform = glm::translate(transform, glm::vec3((-0.31f * anim_fraction), (-0.355f * anim_fraction), (0.0f * anim_fraction)));
transform = glm::rotate(transform, glm::radians(45.0f * anim_fraction), glm::vec3(0.0f, 0.0f, -1.0f));
transform = VP * transform;
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(transform));
glBindVertexArray(VAOs[5]);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
//parallelogram
transform = glm::mat4(1.0);
transform = glm::translate(transform, glm::vec3((-0.662f * anim_fraction), (0.155f * anim_fraction), (0.0f * anim_fraction)));
transform = glm::rotate(transform, glm::radians(180.0f * anim_fraction), glm::vec3(-1.0f, 0.0f, 0.0f));
transform = glm::rotate(transform, glm::radians(-45.0f * anim_fraction), glm::vec3(0.0f, 0.0f, -1.0f));
transform = VP * transform;
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(transform));
glBindVertexArray(VAOs[6]);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
// Properly de-allocate all resources once they've outlived their purpose
glDeleteVertexArrays(7, VAOs);
glDeleteBuffers(7, VBOs);
glfwTerminate();
return 0;
}