/
lab3.c
590 lines (509 loc) · 16 KB
/
lab3.c
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
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <stack>
#include <math.h>
#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
#ifdef __APPLE__
#include <GLUT/glut.h>
#include <OpenGL/gl.h>
#else
#include <GL/glut.h>
#include <GL/gl.h>
#endif
typedef struct
{
float LL[4];
float color[4];
} Vertex;
Vertex verts[6]; // triangle vertices
GLubyte tindices[6]; // triangle vertex indices
GLuint vboHandle; // a VBO that contains interleaved positions and colors
GLuint indexVBO;
int press_x, press_y;
int release_x, release_y;
float x_angle = 0.0;
float y_angle = 0.0;
float scale_size = 1;
//Matrices
glm::mat4 modelM = glm::mat4();
glm::mat4 transform[3] = {glm::mat4(),glm::mat4(),glm::mat4()};
glm::mat4 nothing = glm::mat4();
std::stack<glm::mat4> stacks;
int xform_mode = 0;
//colors
float c1[4]={0.7,0,0,1};
float c2[4]={0,0.7,0,1};
float c3[4]={0,0,0.7,1};
float c4[4]={0.8,1,0,1};
float c5[4]={1,0,0.8,1};
float c6[4]={0,0.8,1,1};
float c7[4]={0.4,0.4,1,1};
float brown[4] = {0.5,0.25,0.1};
#define XFORM_NONE 0
#define XFORM_ROTATE 1
#define XFORM_SCALE 2
#define PI 3.14159265
#define KRED "\x1B[31m"
#define RESET "\033[0m"
void InitGeometry()
{
verts[0].LL[0] = -0.5; verts[0].LL[1] = -0.5; verts[0].LL[2] = 0; verts[0].LL[3] = 1;
verts[1].LL[0] = -0.5; verts[1].LL[1] = 0.5; verts[1].LL[2] = 0; verts[1].LL[3] = 1;
verts[2].LL[0] = 0.5; verts[2].LL[1] = 0.5; verts[2].LL[2] = 0; verts[2].LL[3] = 1;
verts[3].LL[0] = 0.5; verts[3].LL[1] = 0.5; verts[3].LL[2] = 0; verts[3].LL[3] = 1;
verts[4].LL[0] = 0.5; verts[4].LL[1] = -0.5; verts[4].LL[2] = 0; verts[4].LL[3] = 1;
verts[5].LL[0] = -0.5; verts[5].LL[1] = -0.5; verts[5].LL[2] = 0; verts[5].LL[3] = 1;
verts[0].color[0] = 1; verts[0].color[1] = 1; verts[0].color[2] = 0; verts[0].color[3] = 1;
verts[1].color[0] = 1; verts[1].color[1] = 1; verts[1].color[2] = 0; verts[1].color[3] = 1;
verts[2].color[0] = 1; verts[2].color[1] = 1; verts[2].color[2] = 0; verts[2].color[3] = 1;
verts[3].color[0] = 1; verts[3].color[1] = 0; verts[3].color[2] = 0; verts[3].color[3] = 1;
verts[4].color[0] = 1; verts[4].color[1] = 0; verts[4].color[2] = 0; verts[4].color[3] = 1;
verts[5].color[0] = 1; verts[5].color[1] = 0; verts[5].color[2] = 0; verts[5].color[3] = 1;
// create triangle vertex indices.
tindices[0] = 0; tindices[1] = 1; tindices[2] = 2;
tindices[3] = 3; tindices[4] = 4; tindices[5] = 5;
}
void InitVBO()
{
glGenBuffers(1, &vboHandle); // create two VBO handles, one position, one color handle
glBindBuffer(GL_ARRAY_BUFFER, vboHandle); // bind the first handle
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*6, verts, GL_STATIC_DRAW); // allocate space and copy the
// position data over
glBindBuffer(GL_ARRAY_BUFFER, 0); // clean up
glGenBuffers(1, &indexVBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLubyte)*6, tindices, GL_STATIC_DRAW); // load the
// index data
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0); // clean up
// by now, we moved the position and color data over to the graphics card. There will be no redundant data
// copy at drawing time
}
//Implement translate
glm::mat4 Mtranslate (float x, float y, float z){
glm::mat4 output = glm::mat4();
output[3][0]=x;
output[3][1]=y;
output[3][2]=z;
return output;
}
//Implement scaling
glm::mat4 Mscale(float x, float y, float z){
glm::mat4 output = glm::mat4();
output[0][0]=x;
output[1][1]=y;
output[2][2]=z;
return output;
}
//inplement rotation
glm::mat4 Mrotate(float angle, bool x, bool y, bool z){
glm::mat4 output = glm::mat4();
if(x){
output[1][1]=cos(angle*PI/180);
output[2][2]=cos(angle*PI/180);
output[2][1]=-sin(angle*PI/180);
output[1][2]=sin(angle*PI/180);
// std::cout<<output[1][1]<<","<<output[2][3]<<std::endl;
return output;
}else if (y){
output[0][0]=cos(angle*PI/180);
output[2][2]=cos(angle*PI/180);
output[0][2]=-sin(angle*PI/180);
output[2][0]=sin(angle*PI/180);
return output;
}else if (z){
output[0][0]=cos(angle*PI/180);
output[1][1]=cos(angle*PI/180);
output[1][0]=-sin(angle*PI/180);
output[0][1]=sin(angle*PI/180);
return output;
}
}
//basic draw square
void draw_square(float color[3]){
glColor3f(color[0],color[1],color[2]);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, (char*) NULL+0);
}
//not used, for debugging
void draw_square2(glm::mat4 m,float color[3]){
//glLoadMatrixf(&m[0][0]);
glColor3f(color[0],color[1],color[2]);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, (char*) NULL+0);
}
//not used, for debugging
void draw_square3(float color[3]){
glColor3f(color[0],color[1],color[2]);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, (char*) NULL+0);
}
//draw triangle by given points A,B,and C
void draw_triangle(float A1,float A2,float A3,float B1,float B2,float B3,float C1,float C2, float C3){
glPushMatrix();
verts[0].LL[0] = A1; verts[0].LL[1] = A2; verts[0].LL[2] = A3; verts[0].LL[3] = 1;
verts[1].LL[0] = B1; verts[1].LL[1] = B2; verts[1].LL[2] = B3; verts[1].LL[3] = 1;
verts[2].LL[0] = C1; verts[2].LL[1] = C2; verts[2].LL[2] = C3; verts[2].LL[3] = 1;
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*6, verts, GL_STATIC_DRAW);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_BYTE, (char*) NULL+0);
InitGeometry();
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*6, verts, GL_STATIC_DRAW);
glPopMatrix();
}
//draw circle implementation (this draws circle but not as good as draw_circle2)
void draw_circle (float r,float color[3]){
glPushMatrix();
glScalef(r*1.414,r*1.414,0);
for (int i = 0; i < 360; ++i)
{
draw_square(color);
glRotatef(1,0,0,1);
}
glPopMatrix();
}
//draw circle implementation (This is the correct way of drawing circles, but fps is too low)
void draw_circle2 (float r,float color[3]){
glPushMatrix();
int cut =24;
glColor3f(color[0],color[1],color[2]);
//glScalef(r*1.414,r*1.414,0);
for (int i = 0; i < cut; ++i)
{
draw_triangle(r*cos(i*2*PI/cut),r*sin(i*2*PI/cut),0,
r*cos((i+1)*2*PI/cut),r*sin((i+1)*2*PI/cut),0,
0,0,0);
}
glPopMatrix();
}
//draw cylinder based on two circles and multiple triangles
void draw_cylinder (float baser, float topr, float h, int slices, float color[3],bool center,glm::mat4 M )
{
glPushMatrix();
glMultMatrixf(&M[0][0]);
if(center) glTranslatef(0,0,h/2);
glTranslatef(0,0,h/2);
draw_circle(topr,color);
glTranslatef(0,0,-h);
draw_circle(baser,color);
glTranslatef(0,0,h/2);
for (int i = 0; i < slices; ++i)
{
//sida = 360/slices
draw_triangle(baser*cos(i*2*PI/slices),baser*sin(i*2*PI/slices),-h/2,
baser*cos((i+1)*2*PI/slices),baser*sin((i+1)*2*PI/slices),-h/2,
topr*cos(i*2*PI/slices),topr*sin(i*2*PI/slices),h/2);
draw_triangle(topr*cos(i*2*PI/slices),topr*sin(i*2*PI/slices),h/2,
topr*cos((i+1)*2*PI/slices),topr*sin((i+1)*2*PI/slices),h/2,
baser*cos((i+1)*2*PI/slices),baser*sin((i+1)*2*PI/slices),-h/2);
}
//draw_triangle(base,-1,-h/2, -1,-1,-h/2, -1,1,-h/2);
glPopMatrix();
}
//draw sphere based on cylinders
void draw_sphere(float r, int slices, int stacks, float color[3],glm::mat4 M){
glPushMatrix();
//glMultMatrixf(&M[0][0]);
glm::mat4 buf = M;
for (int i = 0; i < stacks/2; ++i)
{
// std::cout<<r*sin((i+1)*(PI)/stacks)<<std::endl;
draw_cylinder(r*cos(i*(PI)/stacks),r*cos((i+1)*(PI)/stacks),
r*sin((i+1)*(PI)/stacks)-r*sin((i)*(PI)/stacks),slices,color,true,buf);
//glTranslatef(0,0,r*sin((i+1)*(PI)/stacks)-r*sin((i)*(PI)/stacks));
buf = buf * Mtranslate(0,0,r*sin((i+1)*(PI)/stacks)-r*sin((i)*(PI)/stacks));
}
// glRotatef(180,1,0,0);
buf = M * Mrotate(180,1,0,0);
for (int i = 0; i < stacks/2; ++i)
{
// std::cout<<r*sin((i+1)*(PI)/stacks)<<std::endl;
draw_cylinder(r*cos(i*(PI)/stacks),r*cos((i+1)*(PI)/stacks),
r*sin((i+1)*(PI)/stacks)-r*sin((i)*(PI)/stacks),slices,color,true,buf);
// glTranslatef(0,0,r*sin((i+1)*(PI)/stacks)-r*sin((i)*(PI)/stacks));
buf = buf * Mtranslate(0,0,r*sin((i+1)*(PI)/stacks)-r*sin((i)*(PI)/stacks));
}
glPopMatrix();
}
//draw cube based on squares
void draw_cube(float size, float color[3],glm::mat4 M){
//glLoadMatrixf
glPushMatrix();
glMultMatrixf(&M[0][0]);
glPushMatrix();
glPushMatrix();
glTranslatef(0,0,-0.5);
draw_square(color);
glTranslatef(0,0,1);
draw_square(color);
glPopMatrix();
glRotatef(90,1,0,0);
glTranslatef(0,0,-0.5);
draw_square(color);
glTranslatef(0,0,1);
draw_square(color);
glPopMatrix();
glRotatef(90,0,1,0);
glTranslatef(0,0,-0.5);
draw_square(color);
glTranslatef(0,0,1);
draw_square(color);
glPopMatrix();
}
//draw tree based on cylinders
void draw_tree(glm::mat4 M){
draw_cylinder(0.3,0.3,2,12,brown,false,M);
M *= Mtranslate(0,0,2);
draw_cylinder(2,1,2,12,c2,false,M);
M *= Mtranslate(0,0,2);
draw_cylinder(2,1,2,12,c2,false,M);
M *= Mtranslate(0,0,2);
draw_cylinder(2,0,2,12,c2,false,M);
}
//draw floor
void draw_floor(){
glPushMatrix();
stacks.push(nothing);
nothing = nothing * Mtranslate(0,0,-4);
nothing = nothing * Mscale(20,20,0.2);
draw_cube(1,c7,nothing);
//tree1
nothing = stacks.top();
nothing *=Mtranslate(5,5,-3);
draw_tree(nothing);
//tree2
nothing = stacks.top();
nothing *=Mtranslate(-5,5,-3);
nothing *=Mscale(0.8,0.8,0.8);
draw_tree(nothing);
//tree3
nothing = stacks.top();
nothing *=Mtranslate(0,5,-3);
nothing *=Mscale(0.4,0.4,0.4);
draw_tree(nothing);
nothing = stacks.top();
stacks.pop();
glPopMatrix();
}
float angle_whole=0,
angle_head=0,
angle_nose=0,
angle_larm=0,
angle_lhand=0,
angle_rarm=0,
angle_rhand=0;
float ii =0;
//draw the main object
void draw_man(){
stacks.push(modelM);
// modelM *= Mtranslate(0,-ii,0);
modelM *= Mrotate(angle_whole,0,0,1);
// modelM *= Mtranslate(0,ii,0);
//modelM *= Mtranslate(0,-ii,0);
//modelM = modelM*Mrotate(angle_whole,0,0,1);
//modelM *= Mtranslate(0,ii,0);
//ii=0;
stacks.push(modelM);
//Head and nose
modelM = modelM*Mrotate(angle_head,0,0,1);
modelM = modelM*Mtranslate(0,0,5);
draw_sphere(1.5,12,12,c1,modelM);//head
modelM = modelM*Mtranslate(0,-2,0);
modelM = modelM*Mtranslate(0,1,0);
modelM = modelM*Mrotate(angle_nose,0,0,1);
modelM = modelM*Mtranslate(0,-1,0);
modelM = modelM*Mrotate(90,1,0,0);
draw_cylinder(0.2,0.1,1,12,c4,false,modelM);//nose
//draw_cube(1,c2,modelM);
//Left arm and body
modelM=stacks.top();
modelM = modelM * Mtranslate(0,0,2);
draw_cylinder(2,1,4,12,c6,false,modelM);//Body
modelM = modelM * Mtranslate(-2,0,1);
modelM = modelM*Mrotate(90,0,1,0);
modelM = modelM*Mtranslate(0,0,1);
modelM = modelM*Mrotate(angle_larm,0,1,0);
modelM = modelM*Mtranslate(0,0,-1);
draw_cylinder(0.5,0.8,2,12,c3,false,modelM);//L Arm
modelM = modelM * Mtranslate(0,0,-1);
draw_sphere(0.5,12,12,c1,modelM); // L connector
// modelM = modelM*Mtranslate(0,0,1);
modelM = modelM*Mrotate(angle_lhand,0,1,0);
// modelM = modelM*Mtranslate(0,0,-1);
modelM = modelM * Mtranslate(0,0,-1);
draw_cylinder(0.1,0.5,2,12,c5,false,modelM);
//Right arm
modelM=stacks.top();
modelM = modelM*Mtranslate(2,0,3);
modelM = modelM*Mrotate(-90,0,1,0);
modelM = modelM*Mtranslate(0,0,1);
modelM = modelM*Mrotate(angle_rarm,0,1,0);
modelM = modelM*Mtranslate(0,0,-1);
draw_cylinder(0.5,0.8,2,12,c3,false,modelM);//R Arm
modelM = modelM * Mtranslate(0,0,-1);
draw_sphere(0.5,12,12,c1,modelM); // R connector
// modelM = modelM*Mtranslate(0,0,1);
modelM = modelM*Mrotate(angle_rhand,0,1,0);
// modelM = modelM*Mtranslate(0,0,-1);
modelM = modelM * Mtranslate(0,0,-1);
draw_cylinder(0.1,0.5,2,12,c5,false,modelM);
//legs
modelM=stacks.top();
modelM = modelM*Mtranslate(1,0,-2);
modelM = modelM*Mscale(0.7,0.7,4);
draw_cube(1,c3,modelM);
modelM=stacks.top();
modelM = modelM*Mtranslate(-1,0,-2);
modelM = modelM*Mscale(0.7,0.7,4);
draw_cube(1,c3,modelM);
stacks.pop();
modelM=stacks.top();
stacks.pop();
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, 1, 0.1, 100);
// glLoadMatrixf(&modelM[0][0]);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,15 , 0,0,0, 0,1,0);
glRotatef(x_angle, 0,1,0);
glRotatef(y_angle, 1,0,0);
glScalef(scale_size, scale_size, scale_size);
glBindBuffer(GL_ARRAY_BUFFER, vboHandle);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBO);
glEnableClientState(GL_VERTEX_ARRAY); // enable the vertex array on the client side
//glEnableClientState(GL_COLOR_ARRAY); // enable the color array on the client side
// number of coordinates per vertex (4 here), type of the coordinates,
// stride between consecutive vertices, and pointers to the first coordinate
// glColorPointer(4, GL_FLOAT, sizeof(Vertex), (char*) NULL+ sizeof(float)*4);
glVertexPointer(4,GL_FLOAT, sizeof(Vertex), (char*) NULL+ 0);
// glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, (char*) NULL+0);
//draw_cube(1,c1);
//draw_cylinder(1.0f,3.0f,5.0f,12,c2);
//draw_sphere(1,12,12,c3);
draw_floor();
draw_man();
//draw_square2(modelM,cc);
glDisableClientState(GL_VERTEX_ARRAY);
//glDisableClientState(GL_COLOR_ARRAY);
glutSwapBuffers();
}
void mymouse(int button, int state, int x, int y)
{
if (state == GLUT_DOWN) {
press_x = x; press_y = y;
if (button == GLUT_LEFT_BUTTON)
xform_mode = XFORM_ROTATE;
else if (button == GLUT_RIGHT_BUTTON)
xform_mode = XFORM_SCALE;
}
else if (state == GLUT_UP) {
xform_mode = XFORM_NONE;
}
}
/////////////////////////////////////////////////////////
void mymotion(int x, int y)
{
if (xform_mode==XFORM_ROTATE) {
x_angle += (x - press_x)/5.0;
if (x_angle > 180) x_angle -= 360;
else if (x_angle <-180) x_angle += 360;
press_x = x;
y_angle += (y - press_y)/5.0;
if (y_angle > 180) y_angle -= 360;
else if (y_angle <-180) y_angle += 360;
press_y = y;
}
else if (xform_mode == XFORM_SCALE){
float old_size = scale_size;
scale_size *= (1+ (y - press_y)/60.0);
if (scale_size <0) scale_size = old_size;
press_y = y;
}
glutPostRedisplay();
}
///////////////////////////////////////////////////////////////
void mykey(unsigned char key, int x, int y)
{
switch(key) {
case 'q':
exit(1);
break;
case 'f':
// modelM = modelM*Mrotate(angle_whole,0,0,1);
// modelM = stacks.top();
modelM = modelM * Mtranslate(0,-0.5,0);
// ii+=1;
display();
break;
case 'b':
modelM = modelM * Mtranslate(0,0.5,0);
display();
break;
case 't':
modelM *= Mrotate(15,0,0,1);
//angle_whole+=15;
display();
break;
case 'T':
modelM *= Mrotate(-15,0,0,1);
//angle_whole-=15;
display();
break;
case 'y':
angle_nose+=15;
display();
break;
case 'Y':
angle_nose-=15;
display();
break;
case 'l':
angle_larm+=15;
display();
break;
case 'L':
angle_lhand-=15;
display();
break;
case 'r':
angle_rarm+=15;
display();
break;
case 'R':
angle_rhand-=15;
display();
break;
case 'h':
angle_head+=15;
display();
break;
case 'H':
angle_head-=15;
display();
break;
}
}
///////////////////////////////////////////////////////////////
int main(int argc, char** argv) {
InitGeometry();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("Lab3 Kevin Yen");
glutDisplayFunc(display);
glutMouseFunc(mymouse);
glutMotionFunc(mymotion);
glutKeyboardFunc(mykey);
printf("%s\n","please see readme.txt for control detail");
#ifdef __APPLE__
#else
glewInit();
#endif
InitVBO();
glutMainLoop();
}