-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobo.cpp
More file actions
1315 lines (1094 loc) · 41.4 KB
/
Robo.cpp
File metadata and controls
1315 lines (1094 loc) · 41.4 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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//Aluno: Juan Pablo Costa Silva / Wagner Serafim dos Santos Junior
//Matrícula: 202010353011 / 202010059711
//Período: 2024.2
#define PI 3.141592654f
#include <GL/freeglut.h>
#include "RgbImage.h"
//TEXTURAS
//Controle
bool textureOn = true;
//Cabeça e Pinça
const char* filenameRusted = "./texture_rusted_metal.bmp";
const char* filenameGolden = "./texture_golden_metal.bmp";
const char* filenameGlass = "./texture_glass.bmp";
//Tronco
const char* filenameMetal = "./metalTexture1.bmp";
const char* filenameGradeF = "./gradeFTexture.bmp";
const char* filenameGradeB = "./gradeBTexture.bmp";
const char* filenameblack = "./blackTexture.bmp";
const char* filenameR = "./buttonRTexture.bmp";
const char* filenameG = "./buttonGTexture.bmp";
const char* filenameB = "./buttonBTexture.bmp";
//Chão
const char* filenameFloor = "./floorTexture.bmp";
//Roda
const char* filenameWheels = "./wheelsTexture.bmp";
//VARIÁVEIS
//Globais
float viewAngleX = 0.0f, viewAngleZ = 0.0f, eyeDistance = 20.0f;
//Cabeça
float headAngleX = 0.0f, headAngleY = 90.0f;
float stretchFactor = 1.0f; // Ajuste este valor dinamicamente para alongar/comprimir
float neckStretch = 1.0f; // Fator de estiramento do pescoço
float neckStretchSpeed = 0.01f; // Velocidade de alteração do estiramento
bool showTransparentSphere = true; // Inicialmente ativa
//Braço
float sizeArm = 1.5f;
float sizeForearm = 3.0f;
float angleForearm = 90.0f; // Ângulo do antebraço
float armRetract[2] = { 1.0f, 1.0f }; // Fator de retração para os dois braços (1.0 = sem retração)
float maxRetract = 0.2f; // Fator mínimo de retração
float minRetract = 1.0f; // Fator máximo de retração
float armAngle[2] = { 0.0f, 0.0f };
float armSpeed[2] = { 0.1f, 0.1f }; // Velocidade dos braços (subindo e descendo)
bool isManualControl = false; // Indica se o movimento está sendo controlado manualmente
int manualControlTimer = 0; // Contador para desligar o controle manual após um tempo
const int manualControlDelay = 300; // Quantidade de ciclos para retornar ao automático (~5 segundos)
//Pinça
float sizeClampPart = 1.0f;
float angleClampZ = 0.0f; // Rotação da garra no eixo Z
float angleClampY = 0.0f; // Ângulo de abertura da garra
bool isPinchaEsquerdaPressed = false;
bool isPinchaDireitaPressed = false;
float angleClampLeftY = 0.0f; // Ângulo da pinça esquerda
float angleClampRightY = 0.0f; // Ângulo da pinça direita
//Tronco
float trunkAngle = -90.0f;
float trunkPositionX = 0.0f; // Posição do robô
float trunkPositionY = 0.0f;
//Base e Rodas
float wheelRotation = 0.0f;
float baseAngle = -90.0f;
float wheelRotationAngle = 0.0f; // Ângulo de rotação das rodas
//Objetos OpenGL
GLfloat angle, rot_x, rot_y;
GLuint _textureIdRusted, _textureIdGolden, _textureIdGlass, _textureIdMetal, _textureIdFloor;
GLuint _textureIdWheels, _textureIdGradeF, _textureIdGradeB, _blackTextureId, _redTextureId, _greenTextureId, _blueTextureId;
GLUquadric* quadric;
void reshape(int width, int height) {
if (height == 0) height = 1; // Prevenir divisão por zero
float aspect = (float)width / (float)height;
glViewport(0, 0, width, height); // Configura o viewport
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, aspect, 1.0f, 100.0f); // Configura a projeção perspectiva
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); // Reseta a matriz de modelo/visualização
}
float mySin(float x) {
// Ajusta x para o intervalo [-PI, PI]
x = x - (int)(x / (2.0f * PI)) * (2.0f * PI);
if (x > PI) x -= 2.0f * PI;
if (x < -PI) x += 2.0f * PI;
float term = x;
float result = x;
float xSquared = x * x;
int sign = -1;
// Série de Taylor para sen(x)
for (int i = 3; i <= 13; i += 2) {
term *= xSquared / (i * (i - 1.0f));
result += sign * term;
sign *= -1;
if (term < 1e-6f && term > -1e-6f) break;
}
return result;
}
float myCos(float x) {
// Ajusta x para o intervalo [-PI, PI]
x = x - (int)(x / (2.0f * PI)) * (2.0f * PI);
if (x > PI) x -= 2.0f * PI;
if (x < -PI) x += 2.0f * PI;
float term = 1.0f;
float result = 1.0f;
float xSquared = x * x;
int sign = -1;
// Série de Taylor para cos(x)
for (int i = 2; i <= 12; i += 2) {
term *= xSquared / (i * (i - 1.0f));
result += sign * term;
sign *= -1;
if (term < 1e-6f && term > -1e-6f) break;
}
return result;
}
template <typename T>
T myMin(T a, T b) {
return (a < b) ? a : b;
}
template <typename T>
T myMax(T a, T b) {
return (a > b) ? a : b;
}
GLuint loadTexture(const char* filename) {
printf("Carregando textura: %s\n", filename);
RgbImage theTexMap(filename);
if (!theTexMap.ImageLoaded()) {
printf("Erro ao carregar a textura: %s\n", filename);
return 0; // Retorna 0 em caso de erro
}
GLuint textureId;
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
// Define a textura para uso no OpenGL
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
theTexMap.GetNumCols(),
theTexMap.GetNumRows(),
0, GL_RGB, GL_UNSIGNED_BYTE,
theTexMap.ImageData());
// Configura filtros de textura
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
return textureId;
}
void initTextures() {
// Carrega texturas usadas no programa
_textureIdRusted = loadTexture(filenameRusted);
_textureIdGolden = loadTexture(filenameGolden);
_textureIdGlass = loadTexture(filenameGlass);
_textureIdMetal = loadTexture(filenameMetal);
_textureIdFloor = loadTexture(filenameFloor);
_textureIdWheels = loadTexture(filenameWheels);
_textureIdGradeF = loadTexture(filenameGradeF);
_textureIdGradeB = loadTexture(filenameGradeB);
_blackTextureId = loadTexture(filenameblack);
_redTextureId = loadTexture(filenameR);
_greenTextureId = loadTexture(filenameG);
_blueTextureId = loadTexture(filenameB);
}
void setupLighting() {
// Configura os parâmetros de iluminação
GLfloat luzAmbiente0[4] = { 0.6f, 0.6f, 0.6f, 1.0f };
GLfloat luzDifusa0[4] = { 0.9f, 0.9f, 0.9f, 1.0f };
GLfloat posicaoLuz0[4] = { 0.0f, 500.0f, 0.0f, 0.0f };
glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
glShadeModel(GL_SMOOTH);
// Define a iluminação ambiente e as fontes de luz
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, luzAmbiente0);
glLightfv(GL_LIGHT0, GL_AMBIENT, luzAmbiente0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, luzDifusa0);
glLightfv(GL_LIGHT0, GL_POSITION, posicaoLuz0);
// Ativa iluminação
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
// Habilita o uso de cor no material
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
//Ativaprofundidade e blending
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Inicializa ângulos
angle = 45;
rot_x = 0;
rot_y = 0;
}
void initRendering() {
printf("Iniciando Rendering...\n");
glEnable(GL_TEXTURE_2D); // Ativa texturas 2D
//glEnable(GL_BLEND);
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
quadric = gluNewQuadric(); // Cria objeto GLUquadric
if (!quadric) {
printf("Erro ao criar objeto GLUquadric!\n");
exit(1);
}
printf("Iniciando Texturas...\n");
initTextures(); // Carrega texturas
printf("Rendering inicializado com sucesso!\n");
}
void drawCube(GLuint _textureId, float lenghtX, float lenghtY, float height) {
// Define vértices e texturas para as faces do cubo
GLfloat vertices[6][4][3] = {
{ {-lenghtX, -lenghtY, -height}, {-lenghtX, lenghtY, -height}, { lenghtX, lenghtY, -height}, { lenghtX, -lenghtY, -height} }, // Posterior
{ {-lenghtX, -lenghtY, height}, { lenghtX, -lenghtY, height}, { lenghtX, lenghtY, height}, {-lenghtX, lenghtY, height} }, // Frontal
{ {-lenghtX, -lenghtY, -height}, {-lenghtX, -lenghtY, height}, {-lenghtX, lenghtY, height}, {-lenghtX, lenghtY, -height} }, // Esquerda
{ { lenghtX, -lenghtY, -height}, { lenghtX, lenghtY, -height}, { lenghtX, lenghtY, height}, { lenghtX, -lenghtY, height} }, // Direita
{ {-lenghtX, lenghtY, -height}, {-lenghtX, lenghtY, height}, { lenghtX, lenghtY, height}, { lenghtX, lenghtY, -height} }, // Superior
{ {-lenghtX, -lenghtY, -height}, { lenghtX, -lenghtY, -height}, { lenghtX, -lenghtY, height}, {-lenghtX, -lenghtY, height} } // Inferior
};
GLfloat texCoords[4][2] = {
{1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}, {0.0f, 0.0f}
};
glBindTexture(GL_TEXTURE_2D, _textureId); // Vincula a textura ao cubo
for (int i = 0; i < 6; i++) { // Para cada face do cubo
glBegin(GL_QUADS);
for (int j = 0; j < 4; j++) {
glTexCoord2f(texCoords[j][0], texCoords[j][1]); // Define coordenadas de textura
glVertex3f(vertices[i][j][0], vertices[i][j][1], vertices[i][j][2]); // Define vértices
}
glEnd();
}
}
void DesenhaBastao(float diam_start, float diam_end, float lenght, float radius, int color) {
glPushMatrix(); // Salva o estado atual da matriz
// Configura cor e textura do cilindro
glColor4f(0.3f, 0.3f, 0.3f, 1.0f);
glBindTexture(GL_TEXTURE_2D, _textureIdGolden);
gluQuadricTexture(quadric, GL_TRUE);
// Desenha o cilindro
gluCylinder(quadric, diam_start, diam_end, lenght, 72, 72);
// Posiciona a esfera no topo do cilindro
glTranslatef(0.0f, 0.0f, lenght + radius);
// Define a cor da esfera
switch (color) {
case 1: glColor4f(1.0f, 0.0f, 0.0f, 1.0f); break; // Vermelho
case 2: glColor4f(0.0f, 1.0f, 0.0f, 1.0f); break; // Verde
case 3: glColor4f(0.0f, 0.0f, 1.0f, 1.0f); break; // Azul
case 4: glColor4f(1.0f, 1.0f, 0.0f, 1.0f); break; // Amarelo
}
// Configura textura e desenha a esfera
glBindTexture(GL_TEXTURE_2D, _textureIdGlass);
gluQuadricTexture(quadric, GL_TRUE);
gluSphere(quadric, radius, 72, 72);
glPopMatrix(); // Restaura o estado salvo da matriz
}
void DesenhaParteCoroa(float base, float top, float length, float height) {
glPushMatrix(); // Salva o estado atual da matriz
glColor4f(0.3f, 0.3f, 0.3f, 1.0f); // Define a cor
glBindTexture(GL_TEXTURE_2D, _textureIdGolden); // Vincula a textura
// Desenha o quadrilátero com textura
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-base, 0.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-top, length, height);
glTexCoord2f(0.0f, 1.0f); glVertex3f(top, length, height);
glTexCoord2f(0.0f, 0.0f); glVertex3f(base, 0.0f, 0.0f);
glEnd();
glPopMatrix(); // Restaura o estado salvo da matriz
}
void drawConeFole(float baseRadius, float topRadius, float segmentHeight, int segments, float stretchFactor) {
glPushMatrix(); // Salva o estado atual da matriz
for (int i = 0; i < segments; i++) {
float progress = (float)i / (float)(segments - 1); // Progresso de 0 a 1
float currentRadius = baseRadius * (1.0f - progress) + topRadius * progress; // Raio interpolado
glPushMatrix(); // Salva o estado antes do segmento
glTranslatef(0.0f, 0.0f, i * segmentHeight * stretchFactor); // Ajusta a posição do segmento
gluCylinder(quadric, currentRadius, currentRadius, segmentHeight * stretchFactor, 32, 1); // Desenha o segmento
glPopMatrix(); // Restaura o estado após o segmento
}
glPopMatrix(); // Restaura o estado inicial da matriz
}
// Desenha a Cabeça com Olhos Luminosos
void drawHead() {
glPushMatrix();
// Aplica rotações (pan)
glRotatef(headAngleY, 0, 0, 1);
// Desenha o pescoço como fole cônico
glColor4f(0.3f, 0.3f, 0.3f, 1.0f);
glPushMatrix();
glTranslatef(0.0f, 0.0f, -1.2f);
glBindTexture(GL_TEXTURE_2D, _blackTextureId);
gluQuadricTexture(quadric, GL_TRUE);
drawConeFole(0.5f, 0.2f, 0.1f, 5, neckStretch);
glPopMatrix();
// Parte superior do pescoço
glPushMatrix();
float basePosition = -1.2f;
float neckOffset = neckStretch * 0.5f;
glTranslatef(0.0f, 0.0f, basePosition + neckOffset);
glColor4f(0.3f, 0.3f, 0.3f, 1.0f);
glBindTexture(GL_TEXTURE_2D, _textureIdRusted);
gluQuadricTexture(quadric, GL_TRUE);
gluCylinder(quadric, 0.15f, 0.1f, 0.25f, 72, 72);
glPopMatrix();
// Cabeça principal
glPushMatrix();
// Calcula a posição base da cabeça
float baseHeadPosition = -1.05f;
float neckTopOffset = neckStretch * 0.5f;
float headPosition = baseHeadPosition + neckTopOffset + 0.25f;
glTranslatef(0.0f, 0.0f, headPosition);
glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
// Desenha haste para os olhos (paralelepipedo)
glColor4f(0.3f, 0.3f, 0.3f, 1.0f);
glTranslatef(0.0f, -0.0f, -0.0f);
drawCube(_textureIdGolden, 0.9f, 0.06f, 0.06f);
// Desenha olhos (esferas)
glColor4f(1.0f, 1.0f, 0.3f, 0.8f);
glPushMatrix();
glTranslatef(-0.8f, 0.0f, 0.1f);
glBindTexture(GL_TEXTURE_2D, _textureIdGlass);
gluQuadricTexture(quadric, GL_TRUE);
gluSphere(quadric, 0.07f, 72, 72);
glPopMatrix();
glPushMatrix();
glTranslatef(0.8f, 0.0f, 0.1f);
glBindTexture(GL_TEXTURE_2D, _textureIdGlass);
gluQuadricTexture(quadric, GL_TRUE);
gluSphere(quadric, 0.07f, 72, 72);
glPopMatrix();
// Desenha bastões (cilindros e esferas)
for (int i = 0; i < 8; i++) {
glPushMatrix();
glTranslatef(0.0f, -0.23f, 0.0f);
glRotatef(30.0f + i * 45.0f, 0.0f, 1.0f, 0.0f);
glRotatef((i % 2 == 0 ? 3.0f : -3.0f), 1.0f, 0.0f, 0.0f);
DesenhaBastao(0.06f, 0.035f, 0.45f, 0.08f, (i % 4) + 1);
glPopMatrix();
}
// Bastões laterais (fixos)
glPushMatrix();
glTranslatef(0.6f, 0.05f, 0.0f);
glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
DesenhaBastao(0.05f, 0.025f, 0.15f, 0.05f, 1);
glPopMatrix();
glPushMatrix();
glTranslatef(-0.6f, 0.05f, 0.0f);
glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
DesenhaBastao(0.05f, 0.025f, 0.15f, 0.05f, 4);
glPopMatrix();
// Desenha coroa
glPushMatrix();
glColor4f(0.3f, 0.3f, 0.3f, 1.0f);
glTranslatef(0.0f, -0.0f, 0.0f);
glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
glBindTexture(GL_TEXTURE_2D, _textureIdRusted);
gluQuadricTexture(quadric, GL_TRUE);
gluCylinder(quadric, 0.01f, 0.1f, 0.2f, 72, 72);
// Desenha o disco no topo do cilindro
glTranslatef(0.0f, 0.0f, -0.08f);
glBindTexture(GL_TEXTURE_2D, _textureIdGolden);
gluQuadricTexture(quadric, GL_TRUE);
gluDisk(quadric, 0.0f, 0.40f, 72, 72);
glPopMatrix(); // Finaliza as transformações
// Desenha as partes da coroa
for (int i = 0; i < 8; i++) {
glPushMatrix();
glRotatef(i * 45.0f, 0.0f, 1.0f, 0.0f);
glTranslatef(0.0f, 0.08f, 0.35f);
DesenhaParteCoroa(0.03f, 0.15f, 0.2f, 0.15f);
glPopMatrix();
}
// Desenha cabeça (elipsoide transparente)
if (showTransparentSphere) {
glEnable(GL_CULL_FACE);
glColor4f(0.4f, 0.4f, 0.4f, 0.5f);
glPushMatrix();
glScalef(1.0f, 0.4f, 1.0f);
glBindTexture(GL_TEXTURE_2D, _textureIdGlass);
gluQuadricTexture(quadric, GL_TRUE);
gluSphere(quadric, 1.05f, 72, 72);
glPopMatrix();
glDisable(GL_CULL_FACE);
}
glPopMatrix();
glPopMatrix();
}
// Desenha um Cilindro
void drawCylinder(float diameter, float length) {
if (textureOn) {
gluQuadricTexture(quadric, GL_TRUE);
}
else {
gluQuadricTexture(quadric, GL_FALSE);
}
// Converte o comprimento para um número inteiro para usar como "stacks"
int stacks = (int)(length * 30);
gluCylinder(quadric, diameter, diameter, length, 40, stacks);
}
// Desenha uma Esfera
void drawSphere(float radius) {
if (textureOn) {
gluQuadricTexture(quadric, GL_TRUE);
}
else {
gluQuadricTexture(quadric, GL_FALSE);
}
gluSphere(quadric, radius, 40, 40);
}
// Desenha um Cone
void drawCone(float baseRadius, float length) {
if (!quadric) {
printf("Erro: quadCylinder não foi inicializado!\n");
return; // Retorna se o quadric não estiver inicializado
}
if (textureOn) {
gluQuadricTexture(quadric, GL_TRUE);
}
else {
gluQuadricTexture(quadric, GL_FALSE);
}
// Converte o comprimento para um número inteiro para usar como "stacks"
int stacks = (int)(length * 30); // Ajuste proporcional ao comprimento
gluCylinder(quadric, baseRadius, 0.0f, length, 40, stacks);
}
// Desenha um Disco
void drawDisk(float innerRadius, float outerRadius) {
gluQuadricTexture(quadric, textureOn ? GL_TRUE : GL_FALSE);
if (textureOn) {
glBindTexture(GL_TEXTURE_2D, _textureIdMetal);
gluQuadricTexture(quadric, GL_TRUE);
}
gluDisk(quadric, innerRadius, outerRadius, 40, 30);
}
// Desenha forma dos Braços
void drawFormArm(float diameter, float segmentHeight, int segments, float stretchFactor) {
glPushMatrix();
// Ativa a textura preta
glBindTexture(GL_TEXTURE_2D, _blackTextureId);
gluQuadricTexture(quadric, GL_TRUE);
// Cada segmento do fole é um cilindro
for (int i = 0; i < segments; i++) {
glPushMatrix();
glTranslatef(0.0f, 0.0f, i * segmentHeight * stretchFactor);
gluCylinder(quadric, diameter, diameter, segmentHeight * stretchFactor, 32, 1);
glPopMatrix();
}
glPopMatrix();
}
// Desenhar a garra
void drawClamp(bool isLeft) {
float scaleFactor = 0.91f;
float angleClampY = isLeft ? angleClampLeftY : angleClampRightY;
float diameterSphere = 1.0f;
float diameterCylinder = 0.3f;
glPushMatrix();
glScalef(scaleFactor, scaleFactor, scaleFactor);
// Move para o referencial da garra
glTranslatef(0.0f, 0.0f, -0.2f);
glRotatef(angleClampZ, 0.0f, 0.0f, 1.0f);
// Desenha a esfera da garra
glBindTexture(GL_TEXTURE_2D, _blackTextureId);
drawSphere(diameterSphere);
glTranslatef(0.0f, 0.0f, diameterSphere / 2);
// Parte superior da garra
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, _redTextureId);
glRotatef(angleClampY + 60, 0.0f, 1.0f, 0.0f);
drawCylinder(diameterCylinder / 3, sizeClampPart);
glTranslatef(0.0f, 0.0f, sizeClampPart + diameterSphere / 15);
drawSphere(diameterSphere / 8);
glTranslatef(0.0f, 0.0f, diameterSphere / 15);
glRotatef(-60, 0.0f, 1.0f, 0.0f);
drawCylinder(diameterCylinder / 3, sizeClampPart);
glTranslatef(0.0f, 0.0f, sizeClampPart + diameterSphere / 15);
drawSphere(diameterSphere / 8);
glTranslatef(0.0f, 0.0f, diameterSphere / 15);
glRotatef(-60, 0.0f, 1.0f, 0.0f);
drawCone(diameterCylinder / 3, sizeClampPart);
glPopMatrix();
// Parte inferior da garra
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, _redTextureId); // Aplica a textura Vermelha
glRotatef(-angleClampY - 60, 0.0f, 1.0f, 0.0f);
drawCylinder(diameterCylinder / 3, sizeClampPart);
glTranslatef(0.0f, 0.0f, sizeClampPart + diameterSphere / 15);
drawSphere(diameterSphere / 8);
glTranslatef(0.0f, 0.0f, diameterSphere / 15);
glRotatef(60, 0.0f, 1.0f, 0.0f);
drawCylinder(diameterCylinder / 3, sizeClampPart);
glTranslatef(0.0f, 0.0f, sizeClampPart + diameterSphere / 15);
drawSphere(diameterSphere /8);
glTranslatef(0.0f, 0.0f, diameterSphere / 15);
glRotatef(60, 0.0f, 1.0f, 0.0f);
drawCone(diameterCylinder / 3, sizeClampPart);
glPopMatrix();
glPopMatrix();
}
//Desenha os braços
void drawArm(int index, bool isLeft) {
float scaleFactor = 0.3f;
int segments = 7;
float enlargedRadius = 1.0f;
float diameterSphere = 0.98f;
// Fator de estiramento do braço baseado no fator de retração
float stretchFactor = armRetract[index];
glPushMatrix();
glScalef(scaleFactor, scaleFactor, scaleFactor);
glBindTexture(GL_TEXTURE_2D, _blackTextureId);
glColor3f(1.0f, 1.0f, 1.0f);
// Move para o referencial do braço
glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
glRotatef(armAngle[index], 1.0f, 0.0f, 0.0f);
// Desenha o braço com o estilo de fole e raio aumentado
drawFormArm(enlargedRadius, sizeArm / segments, segments, stretchFactor);
// Move para o referencial da esfera no meio do braço
glPushMatrix();
glTranslatef(0.0f, 0.0f, (sizeArm * stretchFactor) + diameterSphere / 5);
glBindTexture(GL_TEXTURE_2D, _blackTextureId);
drawSphere(diameterSphere);
glPopMatrix();
// Move para o referencial do antebraço
glTranslatef(0.0f, 0.0f, (sizeArm * stretchFactor) + diameterSphere / 5);
glRotatef(-90.0f, 0.0f, 1.0f, 0.0f);
glRotatef(angleForearm, 0.0f, 1.0f, 0.0f);
// Move para o referencial da esfera no final do antebraço
glPushMatrix();
glTranslatef(0.0f, 0.0f, (sizeForearm * stretchFactor) + diameterSphere / 5);
glBindTexture(GL_TEXTURE_2D, _blackTextureId);
drawSphere(diameterSphere);
glPopMatrix();
// Desenha o antebraço com o estilo de fole e raio aumentado
glTranslatef(0.0f, 0.0f, diameterSphere / 5);
drawFormArm(enlargedRadius, sizeForearm / segments, segments, stretchFactor);
// Desenha a garra correspondente
glPushMatrix();
glTranslatef(0.0f, 0.0f, (sizeForearm * stretchFactor) + diameterSphere / 5);
drawClamp(isLeft);
glPopMatrix();
glPopMatrix();
}
// Desenha os braços e as garras nos soquetes
void drawArmsAndClamps() {
for (int i = 0; i < 2; i++) {
glPushMatrix();
glTranslatef((i == 0 ? -1.1f : 1.1f), 1.0f, 0.8f);
drawArm(i, i == 0);
glPopMatrix();
}
}
// Desenha as inicio das pernas
void drawBaseSegment(float baseRadius, float topRadius, float height) {
glPushMatrix();
glColor4f(0.5f, 0.5f, 0.5f, 1.0f); // Cor prateada
glBindTexture(GL_TEXTURE_2D, _blackTextureId);
gluQuadricTexture(quadric, GL_TRUE);
gluCylinder(quadric, baseRadius, topRadius, height, 30, 1);
glPopMatrix();
}
//Desenha as pernas
void drawLegSegment(float baseRadius, float topRadius, float height) {
glPushMatrix();
glColor4f(0.5f, 0.5f, 0.5f, 1.0f); // Cor prateada
glBindTexture(GL_TEXTURE_2D, _blackTextureId);
gluQuadricTexture(quadric, GL_TRUE);
gluCylinder(quadric, baseRadius, topRadius, height, 30, 1);
glPopMatrix();
}
//Desenha a forma do pé
void drawfoot(float topWidth, float bottomWidth, float height, float depth) {
GLfloat vertices[8][3] = {
{-bottomWidth / 2, -depth / 2, 0.0f}, // Inferior esquerdo traseiro
{bottomWidth / 2, -depth / 2, 0.0f}, // Inferior direito traseiro
{bottomWidth / 2, depth / 2, 0.0f}, // Inferior direito frontal
{-bottomWidth / 2, depth / 2, 0.0f}, // Inferior esquerdo frontal
{-topWidth / 2, -depth / 2, height}, // Superior esquerdo traseiro
{topWidth / 2, -depth / 2, height}, // Superior direito traseiro
{topWidth / 2, depth / 2, height}, // Superior direito frontal
{-topWidth / 2, depth / 2, height} // Superior esquerdo frontal
};
GLuint faces[6][4] = {
{0, 1, 5, 4}, // Traseira
{1, 2, 6, 5}, // Direita
{2, 3, 7, 6}, // Frontal
{3, 0, 4, 7}, // Esquerda
{4, 5, 6, 7}, // Superior
{0, 3, 2, 1} // Inferior
};
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, _textureIdMetal);
glBegin(GL_QUADS);
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 4; j++) {
// Adiciona textura proporcional
if (j == 0) glTexCoord2f(0.0f, 0.0f);
if (j == 1) glTexCoord2f(1.0f, 0.0f);
if (j == 2) glTexCoord2f(1.0f, 1.0f);
if (j == 3) glTexCoord2f(0.0f, 1.0f);
glVertex3fv(vertices[faces[i][j]]);
}
}
glEnd();
glPopMatrix();
}
//Desenha as Rodas
void drawWheel() {
glPushMatrix();
glRotatef(wheelRotationAngle, 0.0f, 0.0f, 1.0f);
glColor4f(0.2f, 0.2f, 0.2f, 1.0f);
// Parte central (eixo da roda)
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, _textureIdWheels);
gluQuadricTexture(quadric, GL_TRUE);
gluDisk(quadric, 0.0f, 0.7f, 30, 1); // Parte interna (lado esquerdo)
glTranslatef(0.0f, 0.0f, 0.1f);
gluDisk(quadric, 0.0f, 0.7f, 30, 1); // Parte interna (lado direito)
glPopMatrix();
// Bordas laterais
glPushMatrix();
glTranslatef(0.0f, 0.0f, -0.02f); // Lado esquerdo da roda
gluDisk(quadric, 0.7f, 0.8f, 30, 1); // Borda externa (esquerda)
glPopMatrix();
glPushMatrix();
glTranslatef(0.0f, 0.0f, 0.12f); // Lado direito da roda
gluDisk(quadric, 0.7f, 0.8f, 30, 1); // Borda externa (direita)
glPopMatrix();
// Faixa externa do pneu
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, _textureIdWheels); // Textura da parte externa
glTranslatef(0.0f, 0.0f, -0.02f); // Alinha com a borda externa
gluCylinder(quadric, 0.8f, 0.8f, 0.14f, 30, 1); // Largura da borda
glPopMatrix();
glPopMatrix();
}
//Desenha toda Base/Perna
void drawLowerBody() {
glPushMatrix();
glColor4f(0.5f, 0.5f, 0.5f, 1.0f); // Cor prateada
glTranslatef(0.0f, 0.0f, -0.85f); // Ajuste de altura geral para base
glRotatef(baseAngle, 0, 0, 1);
// Base das pernas segmentada
for (int i = 0; i < 3; i++) {
glPushMatrix();
glTranslatef(0.0f, 0.0f, -i * 0.2f);
drawBaseSegment(0.9f, 1.2f, 0.5f);
glPopMatrix();
}
// Pernas segmentadas
for (int i = 0; i < 3; i++) {
// Perna esquerda
glPushMatrix();
glTranslatef(-0.8f, 0.0f, -i * 0.5f);
drawLegSegment(0.5f, 0.6f, 0.5f);
glPopMatrix();
// Perna direita
glPushMatrix();
glTranslatef(0.8f, 0.0f, -i * 0.5f);
drawLegSegment(0.5f, 0.6f, 0.5f);
glPopMatrix();
}
// Pé esquerdo
glPushMatrix();
glTranslatef(-0.82f, 0.0f, -2.4f);
drawfoot(1.2f, 1.6f, 1.4f, 1.9f);
glPopMatrix();
// Pé direito
glPushMatrix();
glTranslatef(0.82f, 0.0f, -2.4f);
drawfoot(1.2f, 1.6f, 1.4f, 1.9f);
glPopMatrix();
// Rodas da perna esquerda
glPushMatrix();
glTranslatef(-1.3f, 0.0f, -2.0f);
glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
drawWheel(); // Primeira roda
glPopMatrix();
glPushMatrix();
glTranslatef(-0.35f, 0.0f, -2.0f);
glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
drawWheel(); // Segunda roda
glPopMatrix();
// Rodas da perna direita
glPushMatrix();
glTranslatef(1.3f, 0.0f, -2.0f);
glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
drawWheel(); // Primeira roda
glPopMatrix();
glPushMatrix();
glTranslatef(0.35f, 0.0f, -2.0f);
glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
drawWheel(); // Segunda roda
glPopMatrix();
glPopMatrix();
}
//Desenha Grade frontal Superior
void drawUpperFrontGrid() {
glPushMatrix();
glTranslatef(0.0f, 1.27f, 1.07f);
glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
glBindTexture(GL_TEXTURE_2D, _textureIdGradeF);
glBegin(GL_QUADS);
glVertex3f(-0.4f, -0.2f, 0.0f);
glVertex3f(0.4f, -0.2f, 0.0f);
glVertex3f(0.5f, 0.2f, 0.0f);
glVertex3f(-0.5f, 0.2f, 0.0f);
glEnd();
glPopMatrix();
}
//Desenha Grade frontal e traseiro inferior
void drawBackFrontGrid(float x, float y, float z) {
glPushMatrix();
glTranslatef(x, y, z);
glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
glBindTexture(GL_TEXTURE_2D, _textureIdGradeB);
glBegin(GL_QUADS);
glVertex3f(-0.3f, -0.3f, 0.0f);
glVertex3f(0.3f, -0.3f, 0.0f);
glVertex3f(0.4f, 0.2f, 0.0f);
glVertex3f(-0.4f, 0.2f, 0.0f);
glEnd();
glPopMatrix();
}
//Desenha Grades Laterais inferiores
void drawSidesGrid(float x, float y, float z) {
glPushMatrix();
glTranslatef(x, y, z);
glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
glRotatef(-90.0f, 0.0f, 0.0f, 1.0f);
glBindTexture(GL_TEXTURE_2D, _textureIdGradeB);
glBegin(GL_QUADS);
glVertex3f(-0.4f, -0.2f, 0.0f);
glVertex3f(-0.3f, 0.3f, -0.0f);
glVertex3f(0.3f, 0.3f, 0.0f);
glVertex3f(0.4f, -0.2f, 0.0f);
glEnd();
glPopMatrix();
}
// Desenha o 2º anel conectada ao tronco e ao pescoço
void drawRanhurasAnel(float radius, float height, int slices) {
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, _textureIdGlass);
gluQuadricTexture(quadric, GL_TRUE);
for (int i = 0; i < slices; i++) {
glPushMatrix();
glRotatef(i * (360.0f / slices), 0.0f, 0.0f, 1.0f);
glTranslatef(radius, 0.0f, 0.0f);
gluCylinder(quadric, 0.1f, 0.1f, height, 10, 10);
glPopMatrix();
}
glPopMatrix();
}
//Desenha o 1º Anel conectada ao tronco e pescoço
void drawAnelSuperior(float outerRadius, float innerRadius, float height, int slices) {
glPushMatrix();
// Base do anel
glBindTexture(GL_TEXTURE_2D, _textureIdMetal);
gluQuadricTexture(quadric, GL_TRUE);
gluCylinder(quadric, outerRadius, outerRadius, height, slices, 1);
// Adicionando os dentes do anel
for (int i = 0; i < slices; i++) {
glPushMatrix();
glRotatef(i * (360.0f / slices), 0.0f, 0.0f, 1.0f);
glTranslatef(outerRadius, 0.0f, height);
drawCube(_textureIdMetal, 0.05f, 0.2f, 0.1f);
glPopMatrix();
}
glPopMatrix();
}
// Desenha o Tronco
void drawTrunk() {
glPushMatrix();
GLuint buttonTextures[3] = { _redTextureId, _greenTextureId, _blueTextureId };
glRotatef(trunkAngle, 0, 0, 1);
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, _textureIdMetal);
gluQuadricTexture(quadric, GL_TRUE);
// Desenha camadas ovais segmentadas para o tronco
for (float i = 0.0f; i < 1.5f; i += 0.5f) {
glPushMatrix();
glTranslatef(0.0f, 0.0f, i);
glScalef(1.6f, 1.4f, 0.8f);
drawSphere(0.9f);
glPopMatrix();
}
glPopMatrix();
// Botões coloridos (vermelho, verde, azul)
glPushMatrix();
glTranslatef(0.0f, 1.3f, 0.52f);
glRotatef(90.0f, 1.0f, -0.0f, 0.0f);
// Dimensão e espaçamento
float buttonSize = 0.08f;
float buttonSpacing = 0.2f;
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
glPushMatrix();
// Posiciona cada botão na matriz
glTranslatef((col - 1) * buttonSpacing, (1 - row) * buttonSpacing, 0.0f);
// Aplica textura de vidro como base
glBindTexture(GL_TEXTURE_2D, _textureIdGlass);
gluQuadricTexture(quadric, GL_TRUE);
// Aplica a textura do botão sobre a textura de vidro
glBindTexture(GL_TEXTURE_2D, buttonTextures[(row + col) % 3]);
gluQuadricTexture(quadric, GL_TRUE);
// Desenha o botão como uma esfera
drawSphere(buttonSize);
glPopMatrix();
}
}
glPopMatrix();
// Grade Frontal Superior
drawUpperFrontGrid();
// Grade Frontal Inferior
drawBackFrontGrid(0.0f, 1.27f, -0.0f);
// Grade Lateral Esquerda
drawSidesGrid(-1.45f, 0.0f, 0.0f);
// Grade Lateral Direita
drawSidesGrid(1.45f, 0.0f, 0.0f);
// Grade Traseira Inferior
drawBackFrontGrid(0.0f, -1.27f, -0.0f);
// Soquetes para os braços
for (int i = -1; i <= 1; i += 2) { // Desenha soquete esquerdo e direito
glPushMatrix();
glTranslatef(i * 1.1f, 1.0f, 0.8f);
glBindTexture(GL_TEXTURE_2D, _blackTextureId);
gluQuadricTexture(quadric, GL_TRUE);
drawSphere(0.3f);
glPopMatrix();
}
// Posicione o anel logo acima do tronco
glPushMatrix();
glTranslatef(0.0f, 0.0f, 1.5f);
drawRanhurasAnel(0.8f, 0.2f, 36);
glPopMatrix();
// Desenha os braços e pinças