-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraphics.cpp
512 lines (461 loc) · 16 KB
/
graphics.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
#include "graphics.h"
#include "Scene.h"
#include <iostream>
#include <vector>
using namespace std;
GLdouble width, height;
int wd;
Scene scene;
// Used to map 2d coordinates to the screen
//double ORTHO_ZOOM = -1;
/* Movement Keybinds */
const char UP_KEY = ' ';
const char DOWN_KEY = GLUT_ACTIVE_SHIFT;
const char RIGHT_KEY = 'd';
const char LEFT_KEY = 'a';
const char FORWARD_KEY = 'w';
const char BACKWARD_KEY = 's';
const char OUT_KEY = 'q';
const char IN_KEY = 'e';
/* Rotation Keybinds */
const char ROT_RIGHT_KEY = 'l';
const char ROT_LEFT_KEY = 'j';
const char ROT_UP_KEY = 'i';
const char ROT_DOWN_KEY = 'k';
const char ROT_OUT_KEY = 'o';
const char ROT_IN_KEY = 'u';
/* Other Keybinds */
const char TOGGLE_ACTIVE_CAMERA_KEY = 't';
/**
* Create and Initialize Scene
*/
void init() {
width = 800;
height = 500;
/** Create Axes */
/// X Axis
vector<point3d> xAxisPoints({
point3d(0, 0, 0),
point3d(10, 0, 0),
point3d(1, 0, 0),
point3d(1, 0.1, 0),
point3d(2, 0, 0),
point3d(2, 0.1, 0),
point3d(3, 0, 0),
point3d(3, 0.1, 0),
point3d(4, 0, 0),
point3d(4, 0.1, 0),
point3d(5, 0, 0),
point3d(5, 0.1, 0),
point3d(6, 0, 0),
point3d(6, 0.1, 0),
point3d(7, 0, 0),
point3d(7, 0.1, 0),
point3d(8, 0, 0),
point3d(8, 0.1, 0),
point3d(9, 0, 0),
point3d(9, 0.1, 0),
point3d(10, 0, 0),
point3d(10, 0.1, 0),
});
vector<edge3d> xAxisEdges({
edge3d(xAxisPoints[0], xAxisPoints[1]),
edge3d(xAxisPoints[2], xAxisPoints[3]),
edge3d(xAxisPoints[4], xAxisPoints[5]),
edge3d(xAxisPoints[6], xAxisPoints[7]),
edge3d(xAxisPoints[8], xAxisPoints[9]),
edge3d(xAxisPoints[10], xAxisPoints[11]),
edge3d(xAxisPoints[12], xAxisPoints[13]),
edge3d(xAxisPoints[14], xAxisPoints[15]),
edge3d(xAxisPoints[16], xAxisPoints[17]),
edge3d(xAxisPoints[18], xAxisPoints[19]),
edge3d(xAxisPoints[20], xAxisPoints[21]),
});
scene.addObject(Object3D(xAxisPoints, xAxisEdges));
/// Y Axis
vector<point3d> yAxisPoints({
point3d(0, 0, 0),
point3d(0, 10, 0),
point3d(0, 1, 0),
point3d(0.1, 1, 0),
point3d(0, 2, 0),
point3d(0.1, 2, 0),
point3d(0, 3, 0),
point3d(0.1, 3, 0),
point3d(0, 4, 0),
point3d(0.1, 4, 0),
point3d(0, 5, 0),
point3d(0.1, 5, 0),
point3d(0, 6, 0),
point3d(0.1, 6, 0),
point3d(0, 7, 0),
point3d(0.1, 7, 0),
point3d(0, 8, 0),
point3d(0.1, 8, 0),
point3d(0, 9, 0),
point3d(0.1, 9, 0),
point3d(0, 10, 0),
point3d(0.1, 10, 0),
});
vector<edge3d> yAxisEdges({
edge3d(yAxisPoints[0], yAxisPoints[1]),
edge3d(yAxisPoints[2], yAxisPoints[3]),
edge3d(yAxisPoints[4], yAxisPoints[5]),
edge3d(yAxisPoints[6], yAxisPoints[7]),
edge3d(yAxisPoints[8], yAxisPoints[9]),
edge3d(yAxisPoints[10], yAxisPoints[11]),
edge3d(yAxisPoints[12], yAxisPoints[13]),
edge3d(yAxisPoints[14], yAxisPoints[15]),
edge3d(yAxisPoints[16], yAxisPoints[17]),
edge3d(yAxisPoints[18], yAxisPoints[19]),
edge3d(yAxisPoints[20], yAxisPoints[21]),
});
scene.addObject(Object3D(yAxisPoints, yAxisEdges));
/// Z Axis
vector<point3d> zAxisPoints({
point3d(0, 0, 0),
point3d(0, 0, 10),
point3d(0, 0, 1),
point3d(0.1, 0, 1),
point3d(0, 0, 2),
point3d(0.1, 0, 2),
point3d(0, 0, 3),
point3d(0.1, 0, 3),
point3d(0, 0, 4),
point3d(0.1, 0, 4),
point3d(0, 0, 5),
point3d(0.1, 0, 5),
point3d(0, 0, 6),
point3d(0.1, 0, 6),
point3d(0, 0, 7),
point3d(0.1, 0, 7),
point3d(0, 0, 8),
point3d(0.1, 0, 8),
point3d(0, 0, 9),
point3d(0.1, 0, 9),
point3d(0, 0, 10),
point3d(0.1, 0, 10),
});
vector<edge3d> zAxisEdges({
edge3d(zAxisPoints[0], zAxisPoints[1]),
edge3d(zAxisPoints[2], zAxisPoints[3]),
edge3d(zAxisPoints[4], zAxisPoints[5]),
edge3d(zAxisPoints[6], zAxisPoints[7]),
edge3d(zAxisPoints[8], zAxisPoints[9]),
edge3d(zAxisPoints[10], zAxisPoints[11]),
edge3d(zAxisPoints[12], zAxisPoints[13]),
edge3d(zAxisPoints[14], zAxisPoints[15]),
edge3d(zAxisPoints[16], zAxisPoints[17]),
edge3d(zAxisPoints[18], zAxisPoints[19]),
edge3d(zAxisPoints[20], zAxisPoints[21]),
});
scene.addObject(Object3D(zAxisPoints, zAxisEdges));
/// Create 3d Cube
double CUBE_SIZE = 1;
vector<point3d> cube3dPoints({
point3d(1 * CUBE_SIZE, 1 * CUBE_SIZE, 1 * CUBE_SIZE),
point3d(1 * CUBE_SIZE, 1 * CUBE_SIZE, -1 * CUBE_SIZE),
point3d(1 * CUBE_SIZE, -1 * CUBE_SIZE, 1 * CUBE_SIZE),
point3d(1 * CUBE_SIZE, -1 * CUBE_SIZE, -1 * CUBE_SIZE),
point3d(-1 * CUBE_SIZE, 1 * CUBE_SIZE, 1 * CUBE_SIZE),
point3d(-1 * CUBE_SIZE, 1 * CUBE_SIZE, -1 * CUBE_SIZE),
point3d(-1 * CUBE_SIZE, -1 * CUBE_SIZE, 1 * CUBE_SIZE),
point3d(-1 * CUBE_SIZE, -1 * CUBE_SIZE, -1 * CUBE_SIZE)
});
vector<edge3d> cube3dEdges({
edge3d(cube3dPoints[0], cube3dPoints[1]),
edge3d(cube3dPoints[0], cube3dPoints[2]),
edge3d(cube3dPoints[0], cube3dPoints[4]),
edge3d(cube3dPoints[1], cube3dPoints[3]),
edge3d(cube3dPoints[1], cube3dPoints[5]),
edge3d(cube3dPoints[2], cube3dPoints[3]),
edge3d(cube3dPoints[2], cube3dPoints[6]),
edge3d(cube3dPoints[3], cube3dPoints[7]),
edge3d(cube3dPoints[4], cube3dPoints[5]),
edge3d(cube3dPoints[4], cube3dPoints[6]),
edge3d(cube3dPoints[5], cube3dPoints[7]),
edge3d(cube3dPoints[6], cube3dPoints[7])
});
scene.addObject(Object3D(cube3dPoints, cube3dEdges));
/// Create 4d Cube
vector<point4d> cube4dPoints({
point4d(1 * CUBE_SIZE, 5 * CUBE_SIZE, 1 * CUBE_SIZE, 1),
point4d(1 * CUBE_SIZE, 5 * CUBE_SIZE, -1 * CUBE_SIZE, 1),
point4d(1 * CUBE_SIZE, 7 * CUBE_SIZE, 1 * CUBE_SIZE, 1),
point4d(1 * CUBE_SIZE, 7 * CUBE_SIZE, -1 * CUBE_SIZE, 1),
point4d(-1 * CUBE_SIZE, 5 * CUBE_SIZE, 1 * CUBE_SIZE, 1),
point4d(-1 * CUBE_SIZE, 5 * CUBE_SIZE, -1 * CUBE_SIZE, 1),
point4d(-1 * CUBE_SIZE, 7 * CUBE_SIZE, 1 * CUBE_SIZE, 1),
point4d(-1 * CUBE_SIZE, 7 * CUBE_SIZE, -1 * CUBE_SIZE, 1),
point4d(1 * CUBE_SIZE, 5 * CUBE_SIZE, 1 * CUBE_SIZE, -1),
point4d(1 * CUBE_SIZE, 5 * CUBE_SIZE, -1 * CUBE_SIZE, -1),
point4d(1 * CUBE_SIZE, 7 * CUBE_SIZE, 1 * CUBE_SIZE, -1),
point4d(1 * CUBE_SIZE, 7 * CUBE_SIZE, -1 * CUBE_SIZE, -1),
point4d(-1 * CUBE_SIZE, 5 * CUBE_SIZE, 1 * CUBE_SIZE, -1),
point4d(-1 * CUBE_SIZE, 5 * CUBE_SIZE, -1 * CUBE_SIZE, -1),
point4d(-1 * CUBE_SIZE, 7 * CUBE_SIZE, 1 * CUBE_SIZE, -1),
point4d(-1 * CUBE_SIZE, 7 * CUBE_SIZE, -1 * CUBE_SIZE, -1),
});
vector<edge4d> cube4dEdges({
edge4d(cube4dPoints[0], cube4dPoints[1]),
edge4d(cube4dPoints[0], cube4dPoints[2]),
edge4d(cube4dPoints[0], cube4dPoints[4]),
edge4d(cube4dPoints[0], cube4dPoints[8]),
edge4d(cube4dPoints[1], cube4dPoints[3]),
edge4d(cube4dPoints[1], cube4dPoints[5]),
edge4d(cube4dPoints[1], cube4dPoints[9]),
edge4d(cube4dPoints[2], cube4dPoints[3]),
edge4d(cube4dPoints[2], cube4dPoints[10]),
edge4d(cube4dPoints[2], cube4dPoints[6]),
edge4d(cube4dPoints[3], cube4dPoints[7]),
edge4d(cube4dPoints[3], cube4dPoints[11]),
edge4d(cube4dPoints[4], cube4dPoints[5]),
edge4d(cube4dPoints[4], cube4dPoints[6]),
edge4d(cube4dPoints[4], cube4dPoints[12]),
edge4d(cube4dPoints[5], cube4dPoints[7]),
edge4d(cube4dPoints[5], cube4dPoints[13]),
edge4d(cube4dPoints[6], cube4dPoints[7]),
edge4d(cube4dPoints[6], cube4dPoints[14]),
edge4d(cube4dPoints[7], cube4dPoints[15]),
edge4d(cube4dPoints[8], cube4dPoints[9]),
edge4d(cube4dPoints[8], cube4dPoints[10]),
edge4d(cube4dPoints[8], cube4dPoints[12]),
edge4d(cube4dPoints[9], cube4dPoints[11]),
edge4d(cube4dPoints[9], cube4dPoints[13]),
edge4d(cube4dPoints[10], cube4dPoints[11]),
edge4d(cube4dPoints[10], cube4dPoints[14]),
edge4d(cube4dPoints[11], cube4dPoints[15]),
edge4d(cube4dPoints[12], cube4dPoints[13]),
edge4d(cube4dPoints[12], cube4dPoints[14]),
edge4d(cube4dPoints[13], cube4dPoints[15]),
edge4d(cube4dPoints[14], cube4dPoints[15]),
});
scene.addObject(Object4D(cube4dPoints, cube4dEdges));
}
/* Initialize OpenGL Graphics */
void initGL() {
// Set "clearing" or background color 0, 113, 85, 1
glClearColor(0.9f, 0.9f, 0.86f, 1.0f); // Black and opaque
}
/* Handler for window-repaint event. Call back when the window first appears and
whenever the window needs to be re-painted. */
void display() {
// Tell OpenGL to use the whole window for drawing
glViewport(0, 0, width, height);
// glViewport(0, 0, width, height);
// Do an orthographic parallel projection with the coordinate
// system set to center, limited by screen/window size
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// glOrtho(-ORTHO_ZOOM/width/2, ORTHO_ZOOM/width/2, -ORTHO_ZOOM/height/2,
// ORTHO_ZOOM/height/2, 1.f, -1.f);
glOrtho(-width/2, width/2, -height/2, height/2, 1.f, -1.f);
// Clear the color buffer with current clearing color
glClear(GL_COLOR_BUFFER_BIT);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
scene.draw();
// Render now
glFlush();
}
// http://www.theasciicode.com.ar/ascii-control-characters/escape-ascii-code-27.html
void kbd(unsigned char key, int x, int y)
{
// Get state of shift/ctrl/alt etc.
glutGetModifiers();
switch(key) {
case 27:
// escape
glutDestroyWindow(wd);
exit(0);
break;
case ROT_UP_KEY:
if (scene.getActiveCamera()){
scene.getCamera3D().rotateUp();
} else {
scene.getCamera4D().rotateUp();
}
break;
case ROT_DOWN_KEY:
if (scene.getActiveCamera()){
scene.getCamera3D().rotateDown();
} else {
scene.getCamera4D().rotateDown();
}
break;
case ROT_RIGHT_KEY:
if (scene.getActiveCamera()){
scene.getCamera3D().rotateRight();
} else {
scene.getCamera4D().rotateRight();
}
break;
case ROT_LEFT_KEY:
if (scene.getActiveCamera()){
scene.getCamera3D().rotateLeft();
} else {
scene.getCamera4D().rotateLeft();
}
break;
case ROT_OUT_KEY:
// Only can rotate out if 4d camera is selected
if (!scene.getActiveCamera()){
scene.getCamera4D().rotateOut();
}
break;
case ROT_IN_KEY:
// Only can rotate in if 4d camera is selected
if (!scene.getActiveCamera()){
scene.getCamera4D().rotateIn();
}
break;
case UP_KEY:
if (scene.getActiveCamera()){
scene.getCamera3D().up();
} else {
scene.getCamera4D().up();
}
break;
case DOWN_KEY:
if (scene.getActiveCamera()){
scene.getCamera3D().down();
} else {
scene.getCamera4D().down();
}
break;
case RIGHT_KEY:
if (scene.getActiveCamera()){
scene.getCamera3D().right();
} else {
scene.getCamera4D().right();
}
break;
case LEFT_KEY:
if (scene.getActiveCamera()){
scene.getCamera3D().left();
} else {
scene.getCamera4D().left();
}
break;
case FORWARD_KEY:
if (scene.getActiveCamera()){
scene.getCamera3D().forward();
} else {
scene.getCamera4D().forward();
}
break;
case BACKWARD_KEY:
if (scene.getActiveCamera()){
scene.getCamera3D().back();
} else {
scene.getCamera4D().back();
}
break;
case OUT_KEY:
// Only can move outward if 4d camera is selected
if (!scene.getActiveCamera()){
scene.getCamera4D().out();
}
break;
case IN_KEY:
// Only can move outward if 4d camera is selected
if (!scene.getActiveCamera()){
scene.getCamera4D().in();
}
break;
case TOGGLE_ACTIVE_CAMERA_KEY:
scene.toggleActiveCamera();
break;
}
glutPostRedisplay();
}
void kbdS(int key, int x, int y) {
glutGetModifiers();
if (GLUT_ACTIVE_SHIFT) { // DOWN_KEY
if (scene.getActiveCamera()){
scene.getCamera3D().down();
} else {
scene.getCamera4D().down();
}
}
glutPostRedisplay();
}
void cursor(int x, int y) {
glutPostRedisplay();
}
// button will be GLUT_LEFT_BUTTON or GLUT_RIGHT_BUTTON
// state will be GLUT_UP or GLUT_DOWN
void mouse(int button, int state, int x, int y) {
glutPostRedisplay();
}
void timer(int dummy) {
glutPostRedisplay();
glutTimerFunc(30, timer, dummy);
}
/* Main function: GLUT runs as a console application starting at main() */
int main(int argc, char** argv) {
// /** Create and Initialize Scene */
// vector<point3d> cube3dPoints({
// point3d(1, 5, 1),
// point3d(1, 5, -1),
// point3d(1, 7, 1),
// point3d(1, 7, -1),
// point3d(-1, 5, 1),
// point3d(-1, 5, -1),
// point3d(-1, 7, 1),
// point3d(-1, 7, -1),
// });
// vector<edge3d> cube3dEdges({
// edge3d(cube3dPoints[0], cube3dPoints[1]),
// edge3d(cube3dPoints[0], cube3dPoints[2]),
// edge3d(cube3dPoints[0], cube3dPoints[4]),
// edge3d(cube3dPoints[1], cube3dPoints[3]),
// edge3d(cube3dPoints[1], cube3dPoints[5]),
// edge3d(cube3dPoints[2], cube3dPoints[3]),
// edge3d(cube3dPoints[2], cube3dPoints[6]),
// edge3d(cube3dPoints[3], cube3dPoints[7]),
// edge3d(cube3dPoints[4], cube3dPoints[5]),
// edge3d(cube3dPoints[4], cube3dPoints[6]),
// edge3d(cube3dPoints[5], cube3dPoints[7]),
// edge3d(cube3dPoints[6], cube3dPoints[7]),
//
// });
//
// Object3D testCube(cube3dPoints, cube3dEdges);
//
// // Extrude single edge twice into a cube
//// testCube.extrude(spatialVector(std::vector<double>({
//// 0, 2, 0
//// })));
//// testCube.extrude(spatialVector(std::vector<double>({
//// 0, 0, 2
//// })));
// scene.addObject(testCube);
/** GLUT Processes */
init();
// Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize((int)width, (int)height);
// Position the window's initial top-left corner
glutInitWindowPosition( 100, 100);
/* create the window and store the handle to it */
wd = glutCreateWindow(/* Fun with Drawing! */ "/* title */");
// Register callback handler for window re-paint event
glutDisplayFunc(display);
// Our own OpenGL initialization
initGL();
// register keyboard press event processing function
// works for numbers, letters, spacebar, etc.
glutKeyboardFunc(kbd);
// register special event: function keys, arrows, etc.
glutSpecialFunc(kbdS);
// handles mouse movement
glutPassiveMotionFunc(cursor);
// handles mouse click
glutMouseFunc(mouse);
// handles timer
glutTimerFunc(0, timer, 0);
// Enter the event-processing loop
glutMainLoop();
return 0;
}