-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.cpp
More file actions
659 lines (534 loc) · 18.3 KB
/
main.cpp
File metadata and controls
659 lines (534 loc) · 18.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
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
#include "GL/freeglut.h"
#include "glextload.h"
#include <stdio.h>
#include <math.h> // fabs
static double zoom = .01;
static int width = 0;
static int height = 0;
static float tx=0, ty=0;
static float thetax=0, thetay=0, thetaz=0;
enum DrawType
{
kImmediate,
kVertexArray,
kVBO,
kShaderVBO,
kShaderVAO
};
DrawType drawType = kImmediate;
GLuint* vboIds = NULL;
GLuint* vaoIds = NULL;
GLuint program = 0;
GLuint VERTEX_ATTR_COORDS = 1;
GLuint VERTEX_ATTR_COLOR = 2;
const int nCoordsComponents = 3;
const int nColorComponents = 3;
const int nLines = 3;
const int nVerticesPerLine = 2;
const int nFaces = 6;
const int nVerticesPerFace = 3;
// =========== Axis Data ======================================================
// Y
// | Z
// | /
// | /
// | /
// | /
// | /
// /--------------
// O X
float av[] = { 0.0, 0.0, 0.0, // origin
2.0, 0.0, 0.0, // x-axis
0.0, 2.0, 0.0, // y-axis
0.0, 0.0, 2.0 }; // z-axis
GLubyte avi[] = { 0, 1,
0, 2,
0, 3 };
float ac[] = { 1.0, 0.0, 0.0, // red x-axis
0.0, 1.0, 0.0, // green y-axis
0.0, 0.0, 1.0 }; // blue z-axis
GLubyte aci[] = { 0, 0,
1, 1,
2, 2 };
float ave[nLines*nVerticesPerLine*nCoordsComponents];
void expandAxesVertices()
{
for (int i=0; i<6; i++)
{
ave[i*3+0] = av[avi[i]*3+0];
ave[i*3+1] = av[avi[i]*3+1];
ave[i*3+2] = av[avi[i]*3+2];
}
}
float ace[nLines*nVerticesPerLine*nColorComponents];
void expandAxesColors()
{
for (int i=0; i<6; i++)
{
ace[i*3+0] = ac[aci[i]*3+0];
ace[i*3+1] = ac[aci[i]*3+1];
ace[i*3+2] = ac[aci[i]*3+2];
}
}
// =========== Pyramid Data =================================================
// (3,4,5) (6,7,8)
// 1----------------2
// | \ / |
// | \ / |
// | \ / |
// | 4 | (12,13,14)
// | / \ |
// | / \ |
// | / \ |
// 0 ---------------3
// (0,1,2) (9,10,11)
float pv[] = { 0.5, 0.5, 0.5, // 0
0.5, 1.5, 0.5, // 1
1.5, 1.5, 0.5, // 2
1.5, 0.5, 0.5, // 3
1.0, 1.0, 1.5 }; // 4
GLubyte pvi[] = {0, 1, 2,
2, 3, 0,
0, 3, 4,
3, 2, 4,
2, 1, 4,
1, 0, 4};
float pve[nFaces*nVerticesPerFace*nCoordsComponents];
void expandPyramidVertices()
{
for (int i=0; i<nFaces; i++)
{
for (int j=0; j<nVerticesPerFace; j++)
{
for (int k=0; k<nCoordsComponents; k++)
{
pve[(i*3+j)*3+k] = pv[pvi[i*3+j]*3+k];
}
}
}
}
float pc[] = { 0.3f, 0.30f, 0.3f,
1.0f, 0.70f, 0.0f,
1.0f, 0.62f, 0.0f,
1.0f, 0.40f, 0.0f,
1.0f, 0.48f, 0.0f};
GLubyte pci[] = { 0, 0, 0,
0, 0, 0,
1, 1, 1,
2, 2, 2,
3, 3, 3,
4, 4, 4 };
float pce[nFaces*nVerticesPerFace*nColorComponents];
void expandPyramidColors()
{
for (int i=0; i<nFaces; i++)
{
for (int j=0; j<nVerticesPerFace; j++)
{
for (int k=0; k<nColorComponents; k++)
{
pce[(i*3+j)*3+k] = pc[pci[i*3+j]*3+k];
}
}
}
}
float n[nFaces*nVerticesPerFace*nCoordsComponents];
// ===========================================================================
bool double_equal(double a, double b)
{
if (fabs(a-b) < 1e-3)
return true;
return false;
}
// ---------------------------------------------------------------------------
static void displayCommands()
{
printf("----- Key commands -----\n");
printf("\n");
printf("1 : Immediate\n");
printf("2 : Vertex array\n");
printf("3 : Vertex Buffer Object (VBO)\n");
printf("4 : Shader with Vertex Buffer Object (VBO)\n");
printf("5 : Shader with Vertex Array Object (VBO and VAO)\n");
printf("\n");
printf("+/= : Zoom in\n");
printf("-/_ : Zoom out\n");
printf("\n");
printf("Left arrow : Pan left\n");
printf("Right arrow : Pan left\n");
printf("Up arrow : Pan left\n");
printf("Down arrow : Pan left\n");
printf("\n");
printf("X : Rotate +ve X axis\n");
printf("x : Rotate -ve X axis\n");
printf("Y : Rotate +ve Y axis\n");
printf("X : Rotate +ve Y axis\n");
printf("Z : Rotate +ve Z axis\n");
printf("z : Rotate -ve Z axis\n");
printf("\n");
printf("SPACE : Reset view\n");
printf("Q/q/ESC : Quit\n");
}
// ---------------------------------------------------------------------------
static void onKeyPressed(unsigned char key, int x, int y)
{
switch (key)
{
case 27 :
case 'Q':
case 'q':
glutLeaveMainLoop () ; break;
case '=':
case '+':
zoom=zoom-0.001;
if (double_equal(zoom, 0))
zoom = 0.001;
break;
case '-':
case '_':
zoom = zoom+0.001;
if (double_equal(zoom, 0))
zoom = 0.001;
break;
case ' ':
zoom = 0.01;
tx=0; ty=0;
thetax=0; thetay=0; thetaz=0;
break;
case 'x':
thetax -= 10; break;
case 'X':
thetax += 10; break;
case 'y':
thetay -= 10; break;
case 'Y':
thetay += 10; break;
case 'z':
thetaz -= 10; break;
case 'Z':
thetaz += 10; break;
case '1':
drawType = kImmediate; break;
case '2':
drawType = kVertexArray; break;
case '3':
drawType = kVBO; break;
case '4':
drawType = kShaderVBO; break;
case '5':
drawType = kShaderVAO; break;
default:
break;
}
glutPostRedisplay();
}
// ---------------------------------------------------------------------------
static void onSpecialKeyPressed(int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_LEFT: // left arrow
tx -=0.25; break;
case GLUT_KEY_RIGHT: // right arrow
tx +=0.25; break;
case GLUT_KEY_UP: // up arrow
ty +=0.25; break;
case GLUT_KEY_DOWN: // down arrow
ty -=0.25; break;
}
glutPostRedisplay();
}
// ---------------------------------------------------------------------------
static void onResize(int w, int h)
{
width = w;
height = h;
glViewport(0, 0, width, height);
}
// ---------------------------------------------------------------------------
const char* vertex_shader =
"attribute vec3 aCoords;"
"attribute vec3 aColor;"
"uniform mat4 umvMat;"
"uniform mat4 upMat;"
"varying vec3 vColor;"
"void main () {"
"gl_Position = upMat * umvMat * vec4(aCoords, 1.0);"
"vColor = aColor;"
"}";
const char* fragment_shader =
"varying vec3 vColor;"
"void main () {"
"gl_FragColor = vec4 (vColor, 1.0);"
"}";
// ---------------------------------------------------------------------------
static void initShaders()
{
GLuint vs = glCreateShader (GL_VERTEX_SHADER);
glShaderSource (vs, 1, &vertex_shader, NULL);
glCompileShader (vs);
GLuint fs = glCreateShader (GL_FRAGMENT_SHADER);
glShaderSource (fs, 1, &fragment_shader, NULL);
glCompileShader (fs);
program = glCreateProgram();
glAttachShader (program, fs);
glAttachShader (program, vs);
glBindAttribLocation(program, VERTEX_ATTR_COORDS, "aCoords");
glBindAttribLocation(program, VERTEX_ATTR_COLOR, "aColor");
glLinkProgram (program);
glUseProgram (program);
}
// ---------------------------------------------------------------------------
static void defineVAO()
{
vaoIds = new GLuint[2];
glGenVertexArrays(2, vaoIds);
vboIds = new GLuint[4];
glGenBuffers(4, vboIds);
GLint vertexAttribCoords = glGetAttribLocation(program, "aCoords");
GLint vertexAttribColor = glGetAttribLocation(program, "aColor");
// set current (bind) VAO to define axes data
glBindVertexArray(vaoIds[0]);
glBindBuffer(GL_ARRAY_BUFFER, vboIds[0]); // coordinates
glBufferData(GL_ARRAY_BUFFER, sizeof(ave), ave, GL_STATIC_DRAW);
glVertexAttribPointer(vertexAttribCoords, nCoordsComponents, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(vertexAttribCoords);
glBindBuffer(GL_ARRAY_BUFFER, vboIds[1]); // color
glBufferData(GL_ARRAY_BUFFER, sizeof(ace), ace, GL_STATIC_DRAW);
glVertexAttribPointer(vertexAttribColor, nColorComponents, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(vertexAttribColor);
// Set current (bind) VAO to define pyramid data
glBindVertexArray(vaoIds[1]);
glBindBuffer(GL_ARRAY_BUFFER, vboIds[2]); // coordinates
glBufferData(GL_ARRAY_BUFFER, sizeof(pve), pve, GL_STATIC_DRAW);
glVertexAttribPointer(vertexAttribCoords, nCoordsComponents, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(vertexAttribCoords);
glBindBuffer(GL_ARRAY_BUFFER, vboIds[3]); // color
glBufferData(GL_ARRAY_BUFFER, sizeof(pce), pce, GL_STATIC_DRAW);
glVertexAttribPointer(vertexAttribColor, nColorComponents, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(vertexAttribColor);
// Disable VAO
glBindVertexArray(0);
}
// ---------------------------------------------------------------------------
static void drawShaderWithVertexArrayObject()
{
LoadGLExtensions();
initShaders();
defineVAO();
// Get the variables from the shader to which data will be passed
GLint mvloc = glGetUniformLocation(program, "umvMat");
GLint ploc = glGetUniformLocation(program, "upMat");
// Pass the model-view matrix to the shader
GLfloat mvMat[16];
glGetFloatv(GL_MODELVIEW_MATRIX, mvMat);
glUniformMatrix4fv(mvloc, 1, false, mvMat);
// Pass the projection matrix to the shader
GLfloat pMat[16];
glGetFloatv(GL_PROJECTION_MATRIX, pMat);
glUniformMatrix4fv(ploc, 1, false, pMat);
// Enable VAO to set axes data
glBindVertexArray(vaoIds[0]);
// Draw axes
glDrawArrays(GL_LINES, 0, nLines*nVerticesPerLine);
// Enable VAO to set pyramid data
glBindVertexArray(vaoIds[1]);
// Draw pyramid
glDrawArrays(GL_TRIANGLES, 0, nFaces*nVerticesPerFace);
// Disable VAO
glBindVertexArray(0);
}
// ---------------------------------------------------------------------------
static void drawShaderWithVertexBufferObject()
{
LoadGLExtensions();
initShaders();
// Get the variables from the shader to which data will be passed
GLint mvloc = glGetUniformLocation(program, "umvMat");
GLint ploc = glGetUniformLocation(program, "upMat");
GLint vertexAttribCoords = glGetAttribLocation(program, "aCoords");
GLint vertexAttribColor = glGetAttribLocation(program, "aColor");
// Pass the model-view matrix to the shader
GLfloat mvMat[16];
glGetFloatv(GL_MODELVIEW_MATRIX, mvMat);
glUniformMatrix4fv(mvloc, 1, false, mvMat);
// Pass the projection matrix to the shader
GLfloat pMat[16];
glGetFloatv(GL_PROJECTION_MATRIX, pMat);
glUniformMatrix4fv(ploc, 1, false, pMat);
vboIds = new GLuint[4];
glGenBuffers(4, vboIds);
// Set axes data
glBindBuffer(GL_ARRAY_BUFFER, vboIds[0]); // coordinates
glBufferData(GL_ARRAY_BUFFER, sizeof(ave), ave, GL_STATIC_DRAW);
glVertexAttribPointer(vertexAttribCoords, nCoordsComponents, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(vertexAttribCoords);
glBindBuffer(GL_ARRAY_BUFFER, vboIds[1]); // color
glBufferData(GL_ARRAY_BUFFER, sizeof(ace), ace, GL_STATIC_DRAW);
glVertexAttribPointer(vertexAttribColor, nColorComponents, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(vertexAttribColor);
// Draw axes
glDrawArrays(GL_LINES, 0, nLines*nVerticesPerLine);
// Set pyramid data
glBindBuffer(GL_ARRAY_BUFFER, vboIds[2]); // coordinates
glBufferData(GL_ARRAY_BUFFER, sizeof(pve), pve, GL_STATIC_DRAW);
glVertexAttribPointer(vertexAttribCoords, nCoordsComponents, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(vertexAttribCoords);
glBindBuffer(GL_ARRAY_BUFFER, vboIds[3]); // color
glBufferData(GL_ARRAY_BUFFER, sizeof(pce), pce, GL_STATIC_DRAW);
glVertexAttribPointer(vertexAttribColor, nColorComponents, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(vertexAttribColor);
// Draw pyramid
glDrawArrays(GL_TRIANGLES, 0, nFaces*nVerticesPerFace);
// Disable the VBO
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
// ---------------------------------------------------------------------------
static void drawVertexBufferObject()
{
LoadGLExtensions();
vboIds = new GLuint[3];
glGenBuffers(3, vboIds);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
// Set axes data
glBindBuffer(GL_ARRAY_BUFFER, vboIds[0]); // coordinates
glBufferData(GL_ARRAY_BUFFER, sizeof(ave), ave, GL_STATIC_DRAW);
glVertexPointer(nCoordsComponents, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, vboIds[1]); // color
glBufferData(GL_ARRAY_BUFFER, sizeof(ace), ace, GL_STATIC_DRAW);
glColorPointer(nColorComponents, GL_FLOAT, 0, 0);
// Draw axes
glDrawArrays(GL_LINES, 0, nLines*nVerticesPerLine);
// Set pyramid data
glBindBuffer(GL_ARRAY_BUFFER, vboIds[2]); // coordinates
glBufferData(GL_ARRAY_BUFFER, sizeof(pve), pve, GL_STATIC_DRAW);
glVertexPointer(nCoordsComponents, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, vboIds[3]); // color
glBufferData(GL_ARRAY_BUFFER, sizeof(pce), pce, GL_STATIC_DRAW);
glColorPointer(nColorComponents, GL_FLOAT, 0, 0);
// Draw pyramid
glDrawArrays(GL_TRIANGLES, 0, nFaces*nVerticesPerFace);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
// Disable the VBO
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
// ---------------------------------------------------------------------------
static void drawVertexArray()
{
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
// Set axes data
glVertexPointer(nCoordsComponents, GL_FLOAT, 0, ave);
glColorPointer(nColorComponents, GL_FLOAT, 0, ace);
// Draw axes
glDrawArrays(GL_LINES, 0, nLines*nVerticesPerLine);
// Set pyramid data
glVertexPointer(nCoordsComponents, GL_FLOAT, 0, pve);
glColorPointer(nColorComponents, GL_FLOAT, 0, pce);
// Draw pyramid
glDrawArrays(GL_TRIANGLES, 0, nFaces*nVerticesPerFace);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}
// ---------------------------------------------------------------------------
static void drawImmediate()
{
// Draw x-axis in red
glColor3d(ac[0], ac[1], ac[2]);
glBegin(GL_LINES);
glVertex3f(av[0], av[1], av[2]);
glVertex3f(av[3], av[4], av[5]);
glEnd();
// Draw y-axis in green
glColor3d(ac[3], ac[4], ac[5]);
glBegin(GL_LINES);
glVertex3f(av[0], av[1], av[2]);
glVertex3f(av[6], av[7], av[8]);
glEnd();
// Draw z-axis in blue
glColor3d(ac[6], ac[7], ac[8]);
glBegin(GL_LINES);
glVertex3f(av[0], av[1], av[2]);
glVertex3f(av[9], av[10], av[11]);
glEnd();
// Draw pyramid
glBegin(GL_TRIANGLES);
glColor3d(pc[0], pc[1], pc[2]);
glVertex3f(pv[0], pv[1], pv[2]); // 0
glVertex3f(pv[3], pv[4], pv[5]); // 1
glVertex3f(pv[6], pv[7], pv[8]); // 2
glVertex3f(pv[6], pv[7], pv[8]); // 2
glVertex3f(pv[9], pv[10], pv[11]); // 3
glVertex3f(pv[0], pv[1], pv[2]); // 0
glColor3f(pc[3], pc[4], pc[5]);
glVertex3f(pv[0], pv[1], pv[2]); // 0
glVertex3f(pv[9], pv[10], pv[11]); // 3
glVertex3f(pv[12], pv[13], pv[14]); // 4
glColor3f(pc[6], pc[7], pc[8]);
glVertex3f(pv[9], pv[10], pv[11]); // 3
glVertex3f(pv[6], pv[7], pv[8]); // 2
glVertex3f(pv[12], pv[13], pv[14]); // 4
glColor3f(pc[9], pc[10], pc[11]);
glVertex3f(pv[6], pv[7], pv[8]); // 2
glVertex3f(pv[3], pv[4], pv[5]); // 1
glVertex3f(pv[12], pv[13], pv[14]); // 4
glColor3f(pc[12], pc[13], pc[14]);
glVertex3f(pv[3], pv[4], pv[5]); // 1
glVertex3f(pv[0], pv[1], pv[2]); // 0
glVertex3f(pv[12], pv[13], pv[14]); // 4
glEnd();
}
// ---------------------------------------------------------------------------
static void onDraw(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho((-width/2)*zoom, (width/2)*zoom, (-height/2)*zoom, (height/2)*zoom, -10, 10);
gluLookAt(0, 0, 1, 0, 0, 0, 0, 1, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(thetax, 1, 0, 0);
glRotatef(thetay, 0, 1, 0);
glRotatef(thetaz, 0, 0, 1);
glTranslatef(tx, ty, 0);
if (drawType == kImmediate)
drawImmediate();
else if (drawType == kVertexArray)
drawVertexArray();
else if (drawType == kVBO)
drawVertexBufferObject();
else if (drawType == kShaderVBO)
drawShaderWithVertexBufferObject();
else if (drawType == kShaderVAO)
drawShaderWithVertexArrayObject();
glutSwapBuffers();
}
// ---------------------------------------------------------------------------
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(450,450);
glutInitWindowPosition(40,40);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
glutCreateWindow("Basic OpenGL using FreeGLUT");
glutReshapeFunc(onResize);
glutDisplayFunc(onDraw);
glutKeyboardFunc(onKeyPressed);
glutSpecialFunc(onSpecialKeyPressed);
glClearColor(0,0,0,.5);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST); // z-buffer test
//glDepthFunc(GL_LESS);
glShadeModel(GL_SMOOTH);
expandAxesVertices();
expandAxesColors();
expandPyramidVertices();
expandPyramidColors();
displayCommands();
glutMainLoop();
return 0;
}