-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimple2DTransformation.cpp
More file actions
454 lines (371 loc) · 15.3 KB
/
Simple2DTransformation.cpp
File metadata and controls
454 lines (371 loc) · 15.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
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
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include "Shaders/LoadShaders.h"
GLuint h_ShaderProgram; // handle to shader program
GLint loc_ModelViewProjectionMatrix, loc_primitive_color; // indices of uniform variables
// include glm/*.hpp only if necessary
//#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp> //translate, rotate, scale, ortho, etc.
glm::mat4 ModelViewProjectionMatrix;
glm::mat4 ViewMatrix, ProjectionMatrix, ViewProjectionMatrix;
#define TO_RADIAN 0.01745329252f
#define TO_DEGREE 57.295779513f
#define BUFFER_OFFSET(offset) ((GLvoid *) (offset))
#define LOC_VERTEX 0
int win_width = 0, win_height = 0;
float centerx = 0.0f, centery = 0.0f, rotate_angle = 0.0f;
GLfloat axes[4][2];
GLfloat axes_color[3] = { 0.0f, 0.0f, 0.0f };
GLuint VBO_axes, VAO_axes;
void prepare_axes(void) { // Draw axes in their MC.
axes[0][0] = -win_width / 2.5f; axes[0][1] = 0.0f;
axes[1][0] = win_width / 2.5f; axes[1][1] = 0.0f;
axes[2][0] = 0.0f; axes[2][1] = -win_height / 2.5f;
axes[3][0] = 0.0f; axes[3][1] = win_height / 2.5f;
// Initialize vertex buffer object.
glGenBuffers(1, &VBO_axes);
glBindBuffer(GL_ARRAY_BUFFER, VBO_axes);
glBufferData(GL_ARRAY_BUFFER, sizeof(axes), axes, GL_STATIC_DRAW);
// Initialize vertex array object.
glGenVertexArrays(1, &VAO_axes);
glBindVertexArray(VAO_axes);
glBindBuffer(GL_ARRAY_BUFFER, VBO_axes);
glVertexAttribPointer(LOC_VERTEX, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void update_axes(void) {
axes[0][0] = -win_width / 2.5f; axes[1][0] = win_width / 2.5f;
axes[2][1] = -win_height / 2.5f;
axes[3][1] = win_height / 2.5f;
glBindBuffer(GL_ARRAY_BUFFER, VBO_axes);
glBufferData(GL_ARRAY_BUFFER, sizeof(axes), axes, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void draw_axes(void) {
glUniform3fv(loc_primitive_color, 1, axes_color);
glBindVertexArray(VAO_axes);
glDrawArrays(GL_LINES, 0, 4);
glBindVertexArray(0);
}
GLfloat line[2][2];
GLfloat line_color[3] = { 1.0f, 0.0f, 0.0f };
GLuint VBO_line, VAO_line;
void prepare_line(void) { // y = x - win_height/4
line[0][0] = (1.0f / 4.0f - 1.0f / 2.5f)*win_height;
line[0][1] = (1.0f / 4.0f - 1.0f / 2.5f)*win_height - win_height / 4.0f;
line[1][0] = win_width / 2.5f;
line[1][1] = win_width / 2.5f - win_height / 4.0f;
// Initialize vertex buffer object.
glGenBuffers(1, &VBO_line);
glBindBuffer(GL_ARRAY_BUFFER, VBO_line);
glBufferData(GL_ARRAY_BUFFER, sizeof(line), line, GL_STATIC_DRAW);
// Initialize vertex array object.
glGenVertexArrays(1, &VAO_line);
glBindVertexArray(VAO_line);
glBindBuffer(GL_ARRAY_BUFFER, VBO_line);
glVertexAttribPointer(LOC_VERTEX, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void update_line(void) { // y = x - win_height/4
line[0][0] = (1.0f / 4.0f - 1.0f / 2.5f)*win_height;
line[0][1] = (1.0f / 4.0f - 1.0f / 2.5f)*win_height - win_height / 4.0f;
line[1][0] = win_width / 2.5f;
line[1][1] = win_width / 2.5f - win_height / 4.0f;
glBindBuffer(GL_ARRAY_BUFFER, VBO_line);
glBufferData(GL_ARRAY_BUFFER, sizeof(line), line, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void draw_line(void) { // Draw line in its MC.
// y = x - win_height/4
glUniform3fv(loc_primitive_color, 1, line_color);
glBindVertexArray(VAO_line);
glDrawArrays(GL_LINES, 0, 2);
glBindVertexArray(0);
}
#define AIRPLANE_BIG_WING 0
#define AIRPLANE_SMALL_WING 1
#define AIRPLANE_BODY 2
#define AIRPLANE_BACK 3
#define AIRPLANE_SIDEWINDER1 4
#define AIRPLANE_SIDEWINDER2 5
#define AIRPLANE_CENTER 6
GLfloat big_wing[6][2] = { { 0.0, 0.0 }, { -20.0, 15.0 }, { -20.0, 20.0 }, { 0.0, 23.0 }, { 20.0, 20.0 }, { 20.0, 15.0 } };
GLfloat small_wing[6][2] = { { 0.0, -18.0 }, { -11.0, -12.0 }, { -12.0, -7.0 }, { 0.0, -10.0 }, { 12.0, -7.0 }, { 11.0, -12.0 } };
GLfloat body[5][2] = { { 0.0, -25.0 }, { -6.0, 0.0 }, { -6.0, 22.0 }, { 6.0, 22.0 }, { 6.0, 0.0 } };
GLfloat back[5][2] = { { 0.0, 25.0 }, { -7.0, 24.0 }, { -7.0, 21.0 }, { 7.0, 21.0 }, { 7.0, 24.0 } };
GLfloat sidewinder1[5][2] = { { -20.0, 10.0 }, { -18.0, 3.0 }, { -16.0, 10.0 }, { -18.0, 20.0 }, { -20.0, 20.0 } };
GLfloat sidewinder2[5][2] = { { 20.0, 10.0 }, { 18.0, 3.0 }, { 16.0, 10.0 }, { 18.0, 20.0 }, { 20.0, 20.0 } };
GLfloat center[1][2] = { { 0.0, 0.0 } };
GLfloat airplane_color[7][3] = {
{ 150 / 255.0f, 129 / 255.0f, 183 / 255.0f }, // big_wing
{ 245 / 255.0f, 211 / 255.0f, 0 / 255.0f }, // small_wing
{ 111 / 255.0f, 85 / 255.0f, 157 / 255.0f }, // body
{ 150 / 255.0f, 129 / 255.0f, 183 / 255.0f }, // back
{ 245 / 255.0f, 211 / 255.0f, 0 / 255.0f }, // sidewinder1
{ 245 / 255.0f, 211 / 255.0f, 0 / 255.0f }, // sidewinder2
{ 255 / 255.0f, 0 / 255.0f, 0 / 255.0f } // center
};
GLuint VBO_airplane, VAO_airplane;
void prepare_airplane() {
GLsizeiptr buffer_size = sizeof(big_wing)+sizeof(small_wing)+sizeof(body)+sizeof(back)
+sizeof(sidewinder1)+sizeof(sidewinder2)+sizeof(center);
// Initialize vertex buffer object.
glGenBuffers(1, &VBO_airplane);
glBindBuffer(GL_ARRAY_BUFFER, VBO_airplane);
glBufferData(GL_ARRAY_BUFFER, buffer_size, NULL, GL_STATIC_DRAW); // allocate buffer object memory
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(big_wing), big_wing);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(big_wing), sizeof(small_wing), small_wing);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(big_wing)+sizeof(small_wing), sizeof(body), body);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(big_wing)+sizeof(small_wing)+sizeof(body), sizeof(back), back);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(big_wing)+sizeof(small_wing)+sizeof(body)+sizeof(back),
sizeof(sidewinder1), sidewinder1);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(big_wing)+sizeof(small_wing)+sizeof(body)+sizeof(back)
+sizeof(sidewinder1), sizeof(sidewinder2), sidewinder2);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(big_wing)+sizeof(small_wing)+sizeof(body)+sizeof(back)
+sizeof(sidewinder1)+sizeof(sidewinder2), sizeof(center), center);
// Initialize vertex array object.
glGenVertexArrays(1, &VAO_airplane);
glBindVertexArray(VAO_airplane);
glBindBuffer(GL_ARRAY_BUFFER, VBO_airplane);
glVertexAttribPointer(LOC_VERTEX, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void draw_airplane() { // Draw airplane in its MC.
glBindVertexArray(VAO_airplane);
glUniform3fv(loc_primitive_color, 1, airplane_color[AIRPLANE_BIG_WING]);
glDrawArrays(GL_TRIANGLE_FAN, 0, 6);
glUniform3fv(loc_primitive_color, 1, airplane_color[AIRPLANE_SMALL_WING]);
glDrawArrays(GL_TRIANGLE_FAN, 6, 6);
glUniform3fv(loc_primitive_color, 1, airplane_color[AIRPLANE_BODY]);
glDrawArrays(GL_TRIANGLE_FAN, 12, 5);
glUniform3fv(loc_primitive_color, 1, airplane_color[AIRPLANE_BACK]);
glDrawArrays(GL_TRIANGLE_FAN, 17, 5);
glUniform3fv(loc_primitive_color, 1, airplane_color[AIRPLANE_SIDEWINDER1]);
glDrawArrays(GL_TRIANGLE_FAN, 22, 5);
glUniform3fv(loc_primitive_color, 1, airplane_color[AIRPLANE_SIDEWINDER2]);
glDrawArrays(GL_TRIANGLE_FAN, 27, 5);
glUniform3fv(loc_primitive_color, 1, airplane_color[AIRPLANE_CENTER]);
glPointSize(5.0);
glDrawArrays(GL_POINTS, 32, 1);
glPointSize(1.0);
glBindVertexArray(0);
}
void display(void) {
int i;
float x, r, s, delx, delr, dels;
glm::mat4 ModelMatrix;
glClear(GL_COLOR_BUFFER_BIT);
ModelMatrix = glm::mat4(1.0f);
ModelViewProjectionMatrix = ViewProjectionMatrix * ModelMatrix;
glUniformMatrix4fv(loc_ModelViewProjectionMatrix, 1, GL_FALSE, &ModelViewProjectionMatrix[0][0]);
draw_axes();
draw_line();
draw_airplane();
ModelMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(centerx, centery, 0.0f));
ModelMatrix = glm::rotate(ModelMatrix, rotate_angle, glm::vec3(0.0f, 0.0f, 1.0f));
ModelViewProjectionMatrix = ViewProjectionMatrix * ModelMatrix;
glUniformMatrix4fv(loc_ModelViewProjectionMatrix, 1, GL_FALSE, &ModelViewProjectionMatrix[0][0]);
draw_airplane(); // 0
ModelMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(-win_width / 4.0f, -win_height / 4.0f, 0.0f));
ModelMatrix = glm::rotate(ModelMatrix, 90.0f*TO_RADIAN, glm::vec3(0.0f, 0.0f, 1.0f));
ModelMatrix = glm::scale(ModelMatrix, glm::vec3(3.0f, 3.0f, 1.0f));
ModelViewProjectionMatrix = ViewProjectionMatrix * ModelMatrix;
glUniformMatrix4fv(loc_ModelViewProjectionMatrix, 1, GL_FALSE, &ModelViewProjectionMatrix[0][0]);
draw_airplane(); // 1
ModelMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(win_width / 2.5f, -win_height / 8.0f, 0.0f));
ModelMatrix = glm::rotate(ModelMatrix, 270.0f*TO_RADIAN, glm::vec3(0.0f, 0.0f, 1.0f));
ModelViewProjectionMatrix = ViewProjectionMatrix * ModelMatrix;
glUniformMatrix4fv(loc_ModelViewProjectionMatrix, 1, GL_FALSE, &ModelViewProjectionMatrix[0][0]);
draw_airplane(); // 2
ModelMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(win_height / 4.0f, 0.0f, 0.0f));
ModelMatrix = glm::rotate(ModelMatrix, 45.0f*TO_RADIAN, glm::vec3(0.0f, 0.0f, 1.0f));
ModelMatrix = glm::scale(ModelMatrix, glm::vec3(1.0f, -1.0f, 1.0f));
ModelMatrix = glm::rotate(ModelMatrix, -45.0f*TO_RADIAN, glm::vec3(0.0f, 0.0f, 1.0f));
ModelMatrix = glm::translate(ModelMatrix, glm::vec3(-win_height / 4.0f, 0.0f, 0.0f));
ModelMatrix = glm::translate(ModelMatrix, glm::vec3(win_width / 2.5f, -win_height / 8.0f, 0.0f));
ModelMatrix = glm::scale(ModelMatrix, glm::vec3(2.0f, 2.0f, 1.0f));
ModelMatrix = glm::translate(ModelMatrix, glm::vec3(-win_width / 2.5f, win_height / 8.0f, 0.0f));
ModelMatrix = glm::translate(ModelMatrix, glm::vec3(win_width / 2.5f, -win_height / 8.0f, 0.0f));
ModelMatrix = glm::rotate(ModelMatrix, 270.0f*TO_RADIAN, glm::vec3(0.0f, 0.0f, 1.0f));
ModelViewProjectionMatrix = ViewProjectionMatrix * ModelMatrix;
glUniformMatrix4fv(loc_ModelViewProjectionMatrix, 1, GL_FALSE, &ModelViewProjectionMatrix[0][0]);
draw_airplane(); // 3
delx = win_width/14.0f; delr = 15.0f; dels = 1.1f;
x = -delx; r = delr; s = dels;
for (i = 0; i < 5; i++, x -= delx, r += delr, s *= dels) {
ModelMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(x, 15.0f*sqrtf(-x), 0.0f));
ModelMatrix = glm::rotate(ModelMatrix, r*TO_RADIAN, glm::vec3(0.0f, 0.0f, 1.0f));
glTranslatef(x, 15.0f*sqrtf(-x), 0.0f);
ModelMatrix = glm::scale(ModelMatrix, glm::vec3(s, s, 1.0f));
ModelViewProjectionMatrix = ViewProjectionMatrix * ModelMatrix;
glUniformMatrix4fv(loc_ModelViewProjectionMatrix, 1, GL_FALSE, &ModelViewProjectionMatrix[0][0]);
draw_airplane(); // 4
}
glFlush();
}
void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 27: // ESC key
glutLeaveMainLoop(); // Incur destuction callback for cleanups.
break;
}
}
void special(int key, int x, int y) {
#define SENSITIVITY 2.0
switch (key) {
case GLUT_KEY_LEFT:
centerx -= SENSITIVITY;
glutPostRedisplay();
break;
case GLUT_KEY_RIGHT:
centerx += SENSITIVITY;
glutPostRedisplay();
break;
case GLUT_KEY_DOWN:
centery -= SENSITIVITY;
glutPostRedisplay();
break;
case GLUT_KEY_UP:
centery += SENSITIVITY;
glutPostRedisplay();
break;
}
}
int leftbuttonpressed = 0;
void mouse(int button, int state, int x, int y) {
if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN))
leftbuttonpressed = 1;
else if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_UP))
leftbuttonpressed = 0;
}
void motion(int x, int y) {
static int delay = 0;
static float tmpx = 0.0, tmpy = 0.0;
float dx, dy;
if (leftbuttonpressed) {
centerx = x - win_width/2.0f, centery = (win_height - y) - win_height/2.0f;
if (delay == 8) {
dx = centerx - tmpx;
dy = centery - tmpy;
if (dx > 0.0) {
rotate_angle = atan(dy / dx) + 90.0f*TO_RADIAN;
}
else if (dx < 0.0) {
rotate_angle = atan(dy / dx) - 90.0f*TO_RADIAN;
}
else if (dx == 0.0) {
if (dy > 0.0) rotate_angle = 180.0f*TO_RADIAN;
else rotate_angle = 0.0f;
}
tmpx = centerx, tmpy = centery;
delay = 0;
}
glutPostRedisplay();
delay++;
}
}
void reshape(int width, int height) {
win_width = width, win_height = height;
glViewport(0, 0, win_width, win_height);
ProjectionMatrix = glm::ortho(-win_width / 2.0, win_width / 2.0,
-win_height / 2.0, win_height / 2.0, -1000.0, 1000.0);
ViewProjectionMatrix = ProjectionMatrix * ViewMatrix;
update_axes();
update_line();
glutPostRedisplay();
}
void cleanup(void) {
glDeleteVertexArrays(1, &VAO_axes);
glDeleteBuffers(1, &VBO_axes);
glDeleteVertexArrays(1, &VAO_line);
glDeleteBuffers(1, &VBO_line);
glDeleteVertexArrays(1, &VAO_airplane);
glDeleteBuffers(1, &VBO_airplane);
}
void register_callbacks(void) {
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutSpecialFunc(special);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutReshapeFunc(reshape);
glutCloseFunc(cleanup);
}
void prepare_shader_program(void) {
ShaderInfo shader_info[3] = {
{ GL_VERTEX_SHADER, "Shaders/simple.vert" },
{ GL_FRAGMENT_SHADER, "Shaders/simple.frag" },
{ GL_NONE, NULL }
};
h_ShaderProgram = LoadShaders(shader_info);
glUseProgram(h_ShaderProgram);
loc_ModelViewProjectionMatrix = glGetUniformLocation(h_ShaderProgram, "u_ModelViewProjectionMatrix");
loc_primitive_color = glGetUniformLocation(h_ShaderProgram, "u_primitive_color");
}
void initialize_OpenGL(void) {
glEnable(GL_MULTISAMPLE);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glClearColor(44 / 255.0f, 180 / 255.0f, 49 / 255.0f, 1.0f);
ViewMatrix = glm::mat4(1.0f);
}
void prepare_scene(void) {
prepare_axes();
prepare_line();
prepare_airplane();
}
void initialize_renderer(void) {
register_callbacks();
prepare_shader_program();
initialize_OpenGL();
prepare_scene();
}
void initialize_glew(void) {
GLenum error;
glewExperimental = GL_TRUE;
error = glewInit();
if (error != GLEW_OK) {
fprintf(stderr, "Error: %s\n", glewGetErrorString(error));
exit(-1);
}
fprintf(stdout, "*********************************************************\n");
fprintf(stdout, " - GLEW version supported: %s\n", glewGetString(GLEW_VERSION));
fprintf(stdout, " - OpenGL renderer: %s\n", glGetString(GL_RENDERER));
fprintf(stdout, " - OpenGL version supported: %s\n", glGetString(GL_VERSION));
fprintf(stdout, "*********************************************************\n\n");
}
void greetings(char *program_name, char messages[][256], int n_message_lines) {
fprintf(stdout, "**************************************************************\n\n");
fprintf(stdout, " PROGRAM NAME: %s\n\n", program_name);
fprintf(stdout, " This program was coded for CSE4170 students\n");
fprintf(stdout, " of Dept. of Comp. Sci. & Eng., Sogang University.\n\n");
for (int i = 0; i < n_message_lines; i++)
fprintf(stdout, "%s\n", messages[i]);
fprintf(stdout, "\n**************************************************************\n\n");
initialize_glew();
}
#define N_MESSAGE_LINES 2
void main(int argc, char *argv[]) {
char program_name[64] = "Sogang CSE4170 Simple2DTransformation_GLSL_3.0";
char messages[N_MESSAGE_LINES][256] = {
" - Keys used: 'ESC', four arrows",
" - Mouse used: L-click and move"
};
glutInit (&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_MULTISAMPLE);
glutInitWindowSize (1200*0.95, 800*0.95);
glutInitContextVersion(4, 0);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutCreateWindow(program_name);
greetings(program_name, messages, N_MESSAGE_LINES);
initialize_renderer();
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
glutMainLoop ();
}