-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht3.c
547 lines (430 loc) · 13.1 KB
/
t3.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
#define _USE_MATH_DEFINES // for C
#include <math.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
int width = 800;
int height = 800;
int view_width = 800;
int view_height = 800;
const GLfloat abRadius = 2.0f;
const int abStacks = 20;
const int abSlices = 20;
const GLfloat cefaloRadius = 1.0f;
const int cefaloStacks = 20;
const int cefaloSlices = 20;
const GLfloat STEP_SIZE = 0.5f;
const int REFRESH_DELAY = 200;
typedef struct vertex{
GLfloat x;
GLfloat y;
GLfloat z;
} Vertex;
/**
* funcao para ajudar a fazer vertex mais rapido
*/
Vertex* newVertex(GLfloat x, GLfloat y, GLfloat z) {
Vertex* v = (Vertex*) malloc (1 * sizeof(Vertex));
v -> x = x;
v -> y = y;
v -> z = z;
return v;
}
/**
* funcao para somar vertexes mais rapido
* v recebe a soma de a e b
*/
void addVertexesv(Vertex* v, Vertex* a, Vertex* b) {
v -> x = a -> x + b -> x;
v -> y = a -> y + b -> y;
v -> z = a -> z + b -> z;
}
/**
* funcao para somar vertexes mais rapido
* v recebe a soma de a e outro vertex que corresponde a (x, y, z)
*/
void addVertexes(Vertex* v, Vertex* a, GLfloat x, GLfloat y, GLfloat z) {
v -> x = a -> x + x;
v -> y = a -> y + y;
v -> z = a -> z + z;
}
/**
* funcao para multiplicar vertexes por escalares mais rapido
* v recebe vertex a multiplicado pelo escalar b
*/
void mulVertex(Vertex* v, Vertex* a, float b) {
v -> x = a -> x * b;
v -> y = a -> y * b;
v -> z = a -> z * b;
}
Vertex cefaloCenter;
Vertex abCenter;
Vertex* spiderCenter;
Vertex* spiderFrontDir;
Vertex* origin;
GLint ACTIVE_KEY;
GLint LAST_KEY;
GLfloat ROT; //clockwise!
int STATE;
//PROTOTIPOS DAS FUNCOES DE VISUALIZACAO/////////////////////////////////////////////////////////////////////////////
//DESENHO DA ARANHA
void drawAbdomen(GLfloat center_x, GLfloat center_y, GLfloat center_z);
void drawCefaloTorax(GLfloat center_x, GLfloat center_y, GLfloat center_z);
void drawLegs();
void drawSpider();
void handle_SpecialFunc(GLint key, GLint x, GLint y);
void update(int value);
//DESENHO DO GRID - CODIGOS FORNECIDOS PELO MONITOR DIEGO CINTRA
/**
* @desc Draw grid plane - adapted from Song Ho Ahn matrix projection program (http://www.songho.ca/opengl/gl_transform.html).
* @param {float} size Defines grid size.
* @param {float} step Specifies grid steps, similar to tiles on the floor.
*/
void drawGrid(float size, float step);
//DESENHO DOS EIXOS - CODIGOS FORNECIDOS PELO MONITOR DIEGO CINTRA
/**
* @desc Desenha eixos de um sistema de coordenadas.
* @param {float*} basePoint Ponto de origem de um sistema de coordenadas.
* @param {float*} i Primeiro versor.
* @param {float*} j Segundo versor.
* @param {float*} k Terceiro versor.
*/
void drawAxes(Vertex *basePoint, Vertex *i, Vertex *j, Vertex *k);
/**
* @desc Desenha as coordenadas globais.
*/
void drawWCAxes();
//DEFINICAO DAS VIEWPORTS
void displayCallback();
void reshapeCallback(int w, int h);
//FUNCAO DE INICIALIZACAO DA ESTRUTURA VERTEX
void initializeVertex(Vertex *v, GLfloat x, GLfloat y, GLfloat z);
/////////////////////////////////////////////////////////////////////////////////////////////////////
int main(int argc, char **argv){
initializeVertex(&abCenter, 0.0f, 0.0f, -(abRadius + cefaloRadius)); //inicializacao de abCenter
initializeVertex(&cefaloCenter, 0.0f, 0.0f, 0.0f); //inicializacao de cefaloCenter
spiderCenter = newVertex(0.0f, 0.0f, (abRadius + cefaloRadius));
spiderFrontDir = newVertex(0.0f, 0.0f, 1.0f);
origin = newVertex(0.0f, 0.0f, 0.0f);
STATE = 0;
ROT = 0.0f;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(width, height);
glutCreateWindow("Trabalho 2");
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glutDisplayFunc(displayCallback);
glutReshapeFunc(reshapeCallback);
glutSpecialFunc(handle_SpecialFunc);
glutTimerFunc(REFRESH_DELAY, update, 0);
glutMainLoop();
return 0;
}
/////////////////////FUNCOES DE DESENHO E DE DEFINICAO DE VIEWPORTS//////////////////////////////////
void drawGrid(float size, float step){
float i;
glColor3f(0.3, 0.3, 0.3);
for(i = 0; i < size; i = i + step) {
/** Translate to point in x-axis to draw line parallel to z-axis */
glTranslatef(i, 0.0, 0.0);
glBegin(GL_LINES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, size);
glEnd();
glTranslatef(-i, 0.0, 0.0);
/** Translate to point in z-axis to draw line parallel to x-axis */
glTranslatef(0.0, 0.0, i);
glBegin(GL_LINES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(size, 0.0, 0.0);
glEnd();
glTranslatef(0.0, 0.0, -i);
}
}
void drawAxes(Vertex *basePoint, Vertex *i, Vertex *j, Vertex *k){
float currentColor[4];
/** Armazena cor atual */
glGetFloatv(GL_CURRENT_COLOR, currentColor);
/** Desenha versores */
glColor3f(1.0, 0.0, 0.0); //x-axis
glBegin(GL_LINES);
glVertex3f(basePoint->x, basePoint->y, basePoint->z);
glVertex3f(i->x, i->y, i->z);
glEnd();
glColor3f(0.0, 1.0, 0.0); //y-axis
glBegin(GL_LINES);
glVertex3f(basePoint->x, basePoint->y, basePoint->z);
glVertex3f(j->x, j->y, j->z);
glEnd();
glColor3f(0.0, 0.0, 1.0); //z-axis
glBegin(GL_LINES);
glVertex3f(basePoint->x, basePoint->y, basePoint->z);
glVertex3f(k->x, k->y, k->z);
glEnd();
/** Retorna para cor anterior */
glColor3f(currentColor[0], currentColor[1], currentColor[2]);
}
void initializeVertex(Vertex *v, GLfloat x, GLfloat y, GLfloat z){
v->x = x;
v->y = y;
v->z = z;
}
void drawWCAxes(){
Vertex basePoint, i, j, k;
initializeVertex(&basePoint, 0.0f, 0.0f, 0.0f);
initializeVertex(&i, 5.0f, 0.0f, 0.0f);
initializeVertex(&j, 0.0f, 5.0f, 0.0f);
initializeVertex(&k, 0.0f, 0.0f, 5.0f);
drawAxes(&basePoint, &i, &j, &k);
}
void drawAbdomen(GLfloat center_x, GLfloat center_y, GLfloat center_z){
glPushMatrix();
glColor3f(1.0f, 1.0f, 1.0f);
glTranslatef(center_x, center_y, center_z);
glutSolidSphere(abRadius, abSlices, abStacks); //esfera com centro (0,0,0)
glPopMatrix();
}
void drawCefaloTorax(GLfloat center_x, GLfloat center_y, GLfloat center_z){
glPushMatrix();
glColor3f(1.0f, 0.0f, 0.0f);
glTranslatef(center_x, center_y, center_z);
glutSolidSphere(cefaloRadius, cefaloSlices, cefaloStacks); //esfera com centro (0,0,0)
glPopMatrix();
}
void drawContractedLeg(Vertex* d) {
Vertex v; // vertex to draw lines
Vertex i; // vertex that represent the vector to increment v
glBegin(GL_LINE_STRIP);
glColor3f(0.0f, 1.0f, 0.0f);
mulVertex(&i, d, cefaloRadius);
addVertexesv(&v, origin, &i);
glVertex3f(v.x, v.y, v.z);
addVertexes(&i, d, 0.0f, 1.0f, 0.0f);
mulVertex(&i, &i, cefaloRadius);
addVertexesv(&v, &v, &i);
glVertex3f(v.x, v.y, v.z);
// mulVertex(&i, d, cefaloRadius * 2.0);
// addVertexesv(&v, &v, &i);
// glVertex3f(v.x, v.y, v.z);
addVertexes(&i, d, 0.0f, -1.0f, 0.0f);
mulVertex(&i, &i, cefaloRadius * 2);
addVertexesv(&v, &v, &i);
glVertex3f(v.x, v.y, v.z);
glEnd();
}
void drawExtendedLeg(Vertex* d) {
Vertex v; // vertex to draw lines
Vertex i; // vertex that represent the vector to increment v
glBegin(GL_LINE_STRIP);
glColor3f(0.0f, 1.0f, 0.0f);
mulVertex(&i, d, cefaloRadius);
addVertexesv(&v, origin, &i);
glVertex3f(v.x, v.y, v.z);
mulVertex(&i, d, 4.0);
addVertexes(&v, &i, 0.0f, -1.0f, 0.0f);
glVertex3f(v.x, v.y, v.z);
glEnd();
}
void drawLegs0() {
glRotatef(45.0f, 0.0f, 1.0f, 0.0f);
drawContractedLeg(spiderFrontDir);
glRotatef(30.0f, 0.0f, 1.0f, 0.0f);
drawContractedLeg(spiderFrontDir);
glRotatef(30.0f, 0.0f, 1.0f, 0.0f);
drawContractedLeg(spiderFrontDir);
glRotatef(30.0f, 0.0f, 1.0f, 0.0f);
drawContractedLeg(spiderFrontDir);
glRotatef(-135.0f, 0.0f, 1.0f, 0.0f);
glRotatef(-45.0f, 0.0f, 1.0f, 0.0f);
drawContractedLeg(spiderFrontDir);
glRotatef(-30.0f, 0.0f, 1.0f, 0.0f);
drawContractedLeg(spiderFrontDir);
glRotatef(-30.0f, 0.0f, 1.0f, 0.0f);
drawContractedLeg(spiderFrontDir);
glRotatef(-30.0f, 0.0f, 1.0f, 0.0f);
drawContractedLeg(spiderFrontDir);
glRotatef(135.0f, 0.0f, 1.0f, 0.0f);
}
void drawLegs1() {
glRotatef(45.0f, 0.0f, 1.0f, 0.0f);
drawExtendedLeg(spiderFrontDir);
glRotatef(40.0f, 0.0f, 1.0f, 0.0f);
drawContractedLeg(spiderFrontDir);
glRotatef(10.0f, 0.0f, 1.0f, 0.0f);
drawContractedLeg(spiderFrontDir);
glRotatef(40.0f, 0.0f, 1.0f, 0.0f);
drawExtendedLeg(spiderFrontDir);
glRotatef(-135.0f, 0.0f, 1.0f, 0.0f);
glRotatef(-45.0f, 0.0f, 1.0f, 0.0f);
drawContractedLeg(spiderFrontDir);
glRotatef(-30.0f, 0.0f, 1.0f, 0.0f);
drawContractedLeg(spiderFrontDir);
glRotatef(-30.0f, 0.0f, 1.0f, 0.0f);
drawContractedLeg(spiderFrontDir);
glRotatef(-30.0f, 0.0f, 1.0f, 0.0f);
drawContractedLeg(spiderFrontDir);
glRotatef(135.0f, 0.0f, 1.0f, 0.0f);
}
void drawLegs2() {
glRotatef(45.0f, 0.0f, 1.0f, 0.0f);
drawContractedLeg(spiderFrontDir);
glRotatef(30.0f, 0.0f, 1.0f, 0.0f);
drawContractedLeg(spiderFrontDir);
glRotatef(30.0f, 0.0f, 1.0f, 0.0f);
drawContractedLeg(spiderFrontDir);
glRotatef(30.0f, 0.0f, 1.0f, 0.0f);
drawContractedLeg(spiderFrontDir);
glRotatef(-135.0f, 0.0f, 1.0f, 0.0f);
glRotatef(-45.0f, 0.0f, 1.0f, 0.0f);
drawExtendedLeg(spiderFrontDir);
glRotatef(-40.0f, 0.0f, 1.0f, 0.0f);
drawContractedLeg(spiderFrontDir);
glRotatef(-10.0f, 0.0f, 1.0f, 0.0f);
drawContractedLeg(spiderFrontDir);
glRotatef(-40.0f, 0.0f, 1.0f, 0.0f);
drawExtendedLeg(spiderFrontDir);
glRotatef(135.0f, 0.0f, 1.0f, 0.0f);
}
void drawSpider(){
glTranslatef(spiderCenter->x, spiderCenter->y, spiderCenter->z);
glRotatef(-ROT, 0.0f, 1.0f, 0.0f);
glTranslatef(abCenter.x, abCenter.y, abCenter.z);
drawAbdomen(0.0f, 0.0f, 0.0f);
glTranslatef(-(abCenter.x), -(abCenter.y), -(abCenter.z));
drawCefaloTorax(0.0f, 0.0f, 0.0f);
switch(STATE) {
case 0:
drawLegs0();
break;
case 1:
drawLegs1();
break;
case 2:
drawLegs2();
break;
}
glRotatef(ROT, 0.0f, 1.0f, 0.0f);
glTranslatef(-(spiderCenter->x), -(spiderCenter->y), -(spiderCenter->z));
}
void displayCallback(){
/** Limpa a janela APENAS uma vez */
glClear(GL_COLOR_BUFFER_BIT);
Vertex abPos; // center of the view
Vertex v; // eye pos
// addVertexes(&abPos, spiderCenter,
// cos(M_PI * -ROT / 180.0) * abCenter.x + sin(M_PI * -ROT / 180.0) * abCenter.z,
// abCenter.y,
// -sin(M_PI * -ROT / 180.0) * abCenter.x + cos(M_PI * -ROT / 180.0) * abCenter.z);
addVertexesv(&abPos, spiderCenter, &abCenter);
//bottom left viewport - free view
glLoadIdentity();
addVertexes(&v, &abPos, 3.0, 2.0, 10.0);
gluLookAt(v.x, v.y, v.z,
abPos.x, abPos.y, abPos.z,
0.0, 1.0, 0.0);
glViewport(0, 0, view_width/2, view_height);
drawGrid(100.0, 1.0);
drawWCAxes();
drawSpider();
// //bottom right viewport - z-axis view
// glLoadIdentity();
// addVertexes(&v, &abPos, 0.0, 0.0, 10.0);
// gluLookAt(v.x, v.y, v.z,
// abPos.x, abPos.y, abPos.z,
// 0.0, 1.0, 0.0);
// glViewport(width/2, 0, width/2, height/2);
// drawGrid(100.0, 1.0);
// drawWCAxes();
// drawSpider();
//u left viewport - y-axis view
glLoadIdentity();
addVertexes(&v, &abPos, 0.0, 10.0, 0.0);
gluLookAt(v.x, v.y, v.z,
abPos.x, abPos.y, abPos.z,
0.0, 0.0, 1.0); // up vector becomes k
glViewport(width/2, 0, view_width/2, view_height);
drawGrid(100.0, 1.0);
drawWCAxes();
drawSpider();
// //top right viewport - x-axis view
// glLoadIdentity();
// addVertexes(&v, &abPos, 10.0, 0.0, 0.0);
// gluLookAt(v.x, v.y, v.z,
// abPos.x, abPos.y, abPos.z,
// 0.0, 1.0, 0.0);
// glViewport(width/2, height/2, width/2, height/2);
// drawGrid(100.0, 1.0);
// drawWCAxes();
// drawSpider();
/** Dispara os comandos APENAS uma vez */
glFlush();
}
void reshapeCallback(int w, int h){
/** Atualiza os valores da janela */
width = w;
height = h;
/** Define o volume de vista */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(80.0, (GLfloat) width/(GLfloat) height, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
}
int isArrowKey(GLint key) {
return key == GLUT_KEY_LEFT || key == GLUT_KEY_UP || key == GLUT_KEY_RIGHT || key == GLUT_KEY_DOWN;
}
void update(int value) {
switch (ACTIVE_KEY) {
case GLUT_KEY_LEFT:
addVertexes(spiderCenter, spiderCenter, STEP_SIZE, 0.0f, 0.0f);
ROT = -90.0f;
STATE += 1;
break;
case GLUT_KEY_UP:
addVertexes(spiderCenter, spiderCenter, 0.0f, 0.0f, STEP_SIZE);
ROT = 0.0f;
STATE += 1;
break;
case GLUT_KEY_RIGHT:
addVertexes(spiderCenter, spiderCenter, -STEP_SIZE, 0.0f, 0.0f);
ROT = 90.0f;
STATE += 1;
break;
case GLUT_KEY_DOWN:
addVertexes(spiderCenter, spiderCenter, 0.0f, 0.0f, -STEP_SIZE);
ROT = 180.0f;
STATE += 1;
break;
}
if (STATE == 3)
if (isArrowKey(LAST_KEY)) {
ACTIVE_KEY = LAST_KEY;
STATE = 1;
}
else
STATE = 0;
LAST_KEY = 0;
if (STATE == 0)
ACTIVE_KEY = 0;
glutPostRedisplay();
glutTimerFunc(REFRESH_DELAY, update, 0);
}
void handle_SpecialFunc(GLint key, GLint x, GLint y){
LAST_KEY = key;
if (STATE == 0) { // lock ACTIVE_KEY while spider is moving
switch (key) {
case GLUT_KEY_LEFT:
ACTIVE_KEY = key;
break;
case GLUT_KEY_UP:
ACTIVE_KEY = key;
break;
case GLUT_KEY_RIGHT:
ACTIVE_KEY = key;
break;
case GLUT_KEY_DOWN:
ACTIVE_KEY = key;
break;
}
}
}