Repository navigation
Expand file tree
/
Copy pathopengl_test.cpp
More file actions
1750 lines (1527 loc) · 58.5 KB
/
Copy pathopengl_test.cpp
File metadata and controls
1750 lines (1527 loc) · 58.5 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
#include<windows.h>
#include <GL/glut.h>
#include <cmath>
#include <iostream>
#include<cstdlib>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define MAX_PARTICLES 500
#define VACUUM_INTAKE_START 0
#define VACUUM_INTAKE_END 99
#define VACUUM_EXHAUST_START 100
#define VACUUM_EXHAUST_END 199
#define DEHUMID_INTAKE_START 200
#define DEHUMID_INTAKE_END 349
#define DEHUMID_EXHAUST_START 350
#define DEHUMID_EXHAUST_END 499
float particleLifetime = 2.0f;
bool airFlowEnabled = true; // Airflow Animation Switch
struct Particle {
float pos[3];
float vel[3];
float color[4]; // RGBA
float size;
float life;
bool active;
int type; // 0=air, 1=dust, 2=dirt
};
Particle particles[MAX_PARTICLES];
int lastUsedParticle = 0;
// Cylinder parameters
const float RADIUS = 3.0f;
const int SLICES = 64;
// Mouse control variables
bool mouseLeftDown = false;
int mouseLastX = 0;
int mouseLastY = 0;
float cameraAngleX = 0.0f; // Vertical camera angle
float cameraAngleY = 0.0f; // Horizontal angle (A/D to adjust)
float cameraZ = 3.0f; // Camera height (W/S to adjust)
float cameraDistance = 10.0f;// Distance to origin (Q/E to adjust)
float brushRotation = 0.0f;
float wheelRotation = 0.0f;
float mopOscillation = 0.0f;
bool animationEnabled = true;
bool showText = true;
// Control point angles and related parameters for partial cylinder
float a0 = 3 * M_PI / 4.0f - M_PI / 10.0f;
float a1 = M_PI;
float a2 = 5 * M_PI / 4.0f + M_PI / 10.0f;
float r = 2.8f; // Radius
float width = 0.12f; // Band width
// Variables for bumper (collision barrier)
const float start1 = M_PI / 4.0f;
const float end1 = 5.0f * M_PI / 6.0f;
const float start2 = 7.0f * M_PI / 6.0f;
const float end2 = 7.0f * M_PI / 4.0f;
// Caster wheel rotation
float casterAngle = 0.0f;
// Show ground
bool showGround = true;
// Robot vacuum movement parameters
float vacuumPosX = 0.0f;
float vacuumPosY = 0.0f;
float vacuumAngle = 0.0f;
//texture
GLuint tex_floor;
GLuint tex_wall;
void drawPartialCylinder(float r, float h, float angleStart, float angleEnd) {
int slices = SLICES * (angleEnd - angleStart) / (2.0f * M_PI);
glBegin(GL_QUAD_STRIP);
for (int i = 0; i <= slices; ++i) {
float t = (float)i / slices;
float angle = angleStart + t * (angleEnd - angleStart);
float x = r * cos(angle);
float y = r * sin(angle);
// Normal vector
glNormal3f(cos(angle), sin(angle), 0.0f);
glVertex3f(x, y, 0); // Point on the bottom edge
glVertex3f(x, y, h); // Point on the top edge
}
glEnd();
}
void drawDisk(float r, float z, float angleStart = 0.0f, float angleEnd = 2.0f * M_PI, int segments = SLICES) {
// Normal vector (up or down)
if (z >= 0.0f)
glNormal3f(0.0f, 0.0f, 1.0f);
else
glNormal3f(0.0f, 0.0f, -1.0f);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.0f, 0.0f, z); // Center point
for (int i = 0; i <= segments; ++i) {
float angle = angleStart + (float)i / segments * (angleEnd - angleStart);
float x = r * cos(angle);
float y = r * sin(angle);
glVertex3f(x, y, z);
}
glEnd();
}
// Convert polar coordinates (angle and radius) to Cartesian (x, y) coordinates
void polarToXY(float angle, float radius, float* out) {
out[0] = radius * cos(angle);
out[1] = radius * sin(angle);
}
// Quadratic Bézier interpolation to get the polar angle
void bezierAngle(float t, float* outAngle) {
float u = 1 - t;
*outAngle = u * u * a0 + 2 * u * t * a1 + t * t * a2;
}
// Quadratic Bézier interpolation, output the derivative of the angle (tangent direction)
void bezierAngleTangent(float t, float* outTangent) {
*outTangent = 2 * (1 - t) * (a1 - a0) + 2 * t * (a2 - a1);
}
void drawWidePolarBezierWithHoles() {
int n = 100;
float holeSpacing = 0.08f; // Spacing between holes
// Draw the main band along the Bézier curve
glColor3f(0.3f, 0.3f, 0.3f);
glBegin(GL_QUAD_STRIP);
for (int i = 0; i <= n; ++i) {
float t = i / (float)n;
float angle, dAngle;
bezierAngle(t, &angle);// Calculate the polar angle at t
bezierAngleTangent(t, &dAngle);// Calculate the derivative (tangent direction) at t
float pos[2];
polarToXY(angle, r, pos);// Convert to (x, y) coordinates
// Compute tangent vector at this point
float tangent[2];
tangent[0] = -r * sin(angle) * dAngle;
tangent[1] = r * cos(angle) * dAngle;
// Normalize tangent and get normal direction
float len = sqrt(tangent[0] * tangent[0] + tangent[1] * tangent[1]);
float normal[2] = { -tangent[1] / len, tangent[0] / len };
// Draw the two edges of the band (using the normal direction for offset)
glVertex2f(pos[0] + normal[0] * width / 2, pos[1] + normal[1] * width / 2);
glVertex2f(pos[0] - normal[0] * width / 2, pos[1] - normal[1] * width / 2);
}
glEnd();
// Draw holes on the band
glColor3f(0.05f, 0.05f, 0.05f);
for (int i = 0; i <= n; i += 3) { // Draw a set of holes every 3 points
float t = i / (float)n;
float angle;
bezierAngle(t, &angle);
float pos[2];
polarToXY(angle, r, pos);
// Draw 3 rows of holes at each location
for (int row = 0; row < 3; row++) {
float rowOffset = (row - 1) * width * 0.25f;
glPushMatrix();
glTranslatef(pos[0], pos[1], -0.005f);
// Offset along the normal direction
float tangent[2];
tangent[0] = -r * sin(angle);
tangent[1] = r * cos(angle);
float len = sqrt(tangent[0] * tangent[0] + tangent[1] * tangent[1]);
float normal[2] = { -tangent[1] / len, tangent[0] / len };
glTranslatef(normal[0] * rowOffset, normal[1] * rowOffset, 0);
// Draw a small hole
drawPartialCylinder(0.0125f, 0.01f, 0.0f, 2.0f * M_PI);
glPopMatrix();
}
}
}
void drawVacuumBody() {
glPushMatrix();
// Main body - white plastic shell
GLfloat body1_ambient[] = { 0.3f, 0.3f, 0.3f, 1.0f };
GLfloat body1_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat body1_specular[] = { 1.0f,1.0f,1.0f,1.0f };
GLfloat body1_shininess = 60.0f;
glMaterialfv(GL_FRONT, GL_AMBIENT, body1_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, body1_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, body1_specular);
glMaterialf(GL_FRONT, GL_SHININESS, body1_shininess);
float a0 = 3.0f * M_PI / 4.0f, a1 = 5.0f * M_PI / 4.0f, a2 = 11.0f * M_PI / 4.0f;
drawPartialCylinder(RADIUS, 0.5f, a1, a2);
drawPartialCylinder(RADIUS, -0.1f, 0.0f, 2.0f * M_PI);
glTranslated(0.0f, 0.0f, 0.5f);
drawPartialCylinder(RADIUS, 0.3f, 0.0f, 2.0f * M_PI);
glPopMatrix();
// Internal Water Tank Structure
float outerRadius = RADIUS; // Outer wall radius (transparent wall)
float waterRadius = RADIUS - 0.02f; // Water radius, almost touching the outer wall
float waterLevel = 0.35f; // Water level (can be changed dynamically)
// Draw the inner wall of the sector-shaped water tank
GLfloat inner_wall_ambient[] = { 0.3f, 0.3f, 0.3f, 1.0f };
GLfloat inner_wall_diffuse[] = { 0.7f, 0.7f, 0.7f, 1.0f };
GLfloat inner_wall_specular[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat inner_wall_shininess = 25.0f;
glMaterialfv(GL_FRONT, GL_AMBIENT, inner_wall_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, inner_wall_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, inner_wall_specular);
glMaterialf(GL_FRONT, GL_SHININESS, inner_wall_shininess);
// Calculate the sector angle and center angle of the outer wall
float sectorAngle = a1 - a0; // Angle size of the outer wall sector
float centerAngle = (a0 + a1) / 2.0f; // Center angle of the outer wall sector
// Compute angle range for the inner wall (opposite to the outer wall opening)
float innerA0, innerA1;
innerA0 = centerAngle + M_PI - sectorAngle / 2.0f;
innerA1 = centerAngle + M_PI + sectorAngle / 2.0f;
glPushMatrix();
glTranslatef(-4.2f, 0.0f, 0.0f);
// Draw inner wall arc
drawPartialCylinder(RADIUS, 0.5f, innerA0, innerA1);
glPopMatrix();
// Draw the bottom of water tank
glPushMatrix();
glTranslatef(0.0f, 0.0f, 0.0f);
drawDisk(waterRadius, 0.0f, 0, 2 * M_PI, 32);
glPopMatrix();
// Draw the top of water tank
glPushMatrix();
glTranslatef(0.0f, 0.0f, 0.5f);
drawDisk(waterRadius, 0.0f, 0, 2 * M_PI, 32);
glPopMatrix();
// Draw internal pipes
GLfloat pipe_ambient[] = { 0.1f, 0.1f, 0.1f, 1.0f };
GLfloat pipe_diffuse[] = { 0.4f, 0.4f, 0.4f, 1.0f };
GLfloat pipe_specular[] = { 0.6f, 0.6f, 0.6f, 1.0f };
GLfloat pipe_shininess = 50.0f;
glMaterialfv(GL_FRONT, GL_AMBIENT, pipe_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, pipe_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, pipe_specular);
glMaterialf(GL_FRONT, GL_SHININESS, pipe_shininess);
// Top inlet pipe
glPushMatrix();
glTranslatef(-2.0f, 1.0f, 0.45f);
drawPartialCylinder(0.018f, 0.2f, 0.0f, 2.0f * M_PI);
glPopMatrix();
// Bottom outlet pipe
glPushMatrix();
glTranslatef(-1.5f, 0.0f, 0.0f);
drawPartialCylinder(0.015f, 0.05f, 0.0f, 2.0f * M_PI);
drawPartialCylinder(0.022f, 0.008f, 0.0f, 2.0f * M_PI);
drawDisk(0.022f, 0.0f);
glPopMatrix();
// Draw the water itself
glEnable(GL_BLEND);
glDepthMask(GL_FALSE);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Water material - blue, semi-transparent, high specular reflection
GLfloat water_ambient[] = { 0.1f, 0.3f, 0.4f, 0.8f };
GLfloat water_diffuse[] = { 0.2f, 0.5f, 0.9f, 0.8f };
GLfloat water_specular[] = { 0.8f, 0.9f, 1.0f, 0.8f };
GLfloat water_shininess = 120.0f; // High shininess for water reflection
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, water_ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, water_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, water_specular);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, water_shininess);
// Draw the sector-shaped water volume
glPushMatrix();
glTranslatef(0.0f, 0.0f, 0.0f); // Water starts from the bottom
drawPartialCylinder(waterRadius, waterLevel, a0, a1);
glPopMatrix();
glPushMatrix();
glTranslatef(0.0f, 0.0f, waterLevel);
// Draw water surface
drawDisk(waterRadius, 0.0f, a0, a1);
glPopMatrix();
// draw the transparent outer shell
// Transparent plastic material - more transparent, slightly bluish
GLfloat tank_ambient[] = { 0.05f, 0.08f, 0.1f, 0.15f }; // Very transparent
GLfloat tank_diffuse[] = { 0.1f, 0.2f, 0.3f, 0.15f }; // Very transparent
GLfloat tank_specular[] = { 0.9f, 0.95f, 1.0f, 0.15f }; // High specular reflection
GLfloat tank_shininess = 150.0f; // Very high shininess for plastic
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, tank_ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, tank_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, tank_specular);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, tank_shininess);
drawPartialCylinder(RADIUS, 0.5f, a0, a1);
glDepthMask(GL_TRUE);
glDisable(GL_BLEND);
glPushMatrix();
// Top cover - white plastic
GLfloat body2_ambient[] = { 0.4f, 0.4f, 0.4f, 1.0f };
GLfloat body2_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat body2_specular[] = { 1.0f,1.0f,1.0f,1.0f };
GLfloat body2_shininess = 80.0f;
glMaterialfv(GL_FRONT, GL_AMBIENT, body2_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, body2_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, body2_specular);
glMaterialf(GL_FRONT, GL_SHININESS, body2_shininess);
drawDisk(RADIUS, 0.8f);// Top cover at height 0.8f
// Bottom cover - dark gray plastic
GLfloat body3_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat body3_diffuse[] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat body3_specular[] = { 0.8f,0.8f,0.8f,1.0f };
GLfloat body3_shininess = 40.0f;
glMaterialfv(GL_FRONT, GL_AMBIENT, body3_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, body3_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, body3_specular);
glMaterialf(GL_FRONT, GL_SHININESS, body3_shininess);
drawDisk(RADIUS, -0.1f);// Bottom cover at height -0.1f
glPopMatrix();
}
void drawWheel(float y, float z, float rotation) {
glPushMatrix();
glTranslatef(-0.7f, y, z);
glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
glRotatef(rotation, 0.0f, 0.0f, 1.0f);
// Wheel
GLfloat wheel_ambient[] = { 0.02f, 0.02f, 0.02f, 1.0f };
GLfloat wheel_diffuse[] = { 0.05f, 0.05f, 0.05f, 1.0f };
GLfloat wheel_specular[] = { 0.1f,0.1f,0.1f,1.0f };
GLfloat wheel_shininess = 5.0f;
glMaterialfv(GL_FRONT, GL_AMBIENT, wheel_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, wheel_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, wheel_specular);
glMaterialf(GL_FRONT, GL_SHININESS, wheel_shininess);
glutSolidTorus(0.15, 0.3, 10, 20);
// Hub
GLfloat hub_ambient[] = { 0.4f, 0.4f, 0.4f, 1.0f };
GLfloat hub_diffuse[] = { 0.85f, 0.85f, 0.85f, 1.0f };
GLfloat hub_specular[] = { 0.8f,0.8f,0.8f,1.0f };
GLfloat hub_shininess = 80.0f;
glMaterialfv(GL_FRONT, GL_AMBIENT, hub_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, hub_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, hub_specular);
glMaterialf(GL_FRONT, GL_SHININESS, hub_shininess);
glutSolidSphere(0.2, 10, 10);
glPopMatrix();
}
void drawMoppingPads(float oscillation) {
// Left mopping pad
glPushMatrix();
glColor3f(0.8f, 0.8f, 0.8f);
// Add small translation based on oscillation
glTranslated(-1.0f - oscillation * 0.02f, -1.15f, -0.1f);
drawPartialCylinder(0.1, -0.3f, 0.0f, 2.0f * M_PI);
glPopMatrix();
glPushMatrix();
glColor3f(0.8f, 0.8f, 0.8f);
glTranslatef(-1.0f - oscillation * 0.02f, -1.15f, -0.4f);
glScalef(2.2f, 2.2f, 0.2f);
glutSolidSphere(0.53, 20, 20);
glPopMatrix();
glPushMatrix();
glTranslatef(-1.0f - oscillation * 0.02f, -1.15f, -0.515f);
glScalef(1.0f, 1.0f, 0.02f);
glColor3f(0.0f, 0.0f, 0.0f);
glutSolidSphere(0.15, 20, 20);
glPopMatrix();
// Right mopping pad
glPushMatrix();
glColor3f(0.8f, 0.8f, 0.8f);
// Add small translation based on oscillation (opposite direction)
glTranslated(-1.0f - oscillation * 0.02f, 1.15f, -0.1f);
drawPartialCylinder(0.1, -0.3f, 0.0f, 2.0f * M_PI);
glPopMatrix();
glPushMatrix();
glColor3f(0.8f, 0.8f, 0.8f);
glTranslatef(-1.0f - oscillation * 0.02f, 1.15f, -0.4f);
glScalef(2.2f, 2.2f, 0.2f);
glutSolidSphere(0.53, 20, 20);
glPopMatrix();
glPushMatrix();
glTranslatef(-1.0f - oscillation * 0.02f, 1.15f, -0.515f);
glScalef(1.0f, 1.0f, 0.02f);
glColor3f(0.0f, 0.0f, 0.0f);
glutSolidSphere(0.15, 20, 20);
glPopMatrix();
}
void drawSideBrush(float brushRadius, float bristleLength, float rotation) {
glPushMatrix();
// Apply rotation around Z axis
glRotatef(rotation, 0.0f, 0.0f, 1.0f);
// Draw the central motor base
glColor3f(0.2f, 0.2f, 0.2f);
drawPartialCylinder(brushRadius * 0.3f, brushRadius * 0.15f, 0.0f, 2.0f * M_PI);
drawDisk(brushRadius * 0.3f, 0.0f);
drawDisk(brushRadius * 0.3f, brushRadius * 0.15f);
// Draw the brush fixing plate
glPushMatrix();
glTranslatef(0.0f, 0.0f, brushRadius * 0.15f);
glColor3f(0.1f, 0.1f, 0.1f);
drawPartialCylinder(brushRadius * 0.5f, brushRadius * 0.08f, 0.0f, 2.0f * M_PI);
drawDisk(brushRadius * 0.5f, 0.0f);
drawDisk(brushRadius * 0.5f, brushRadius * 0.08f);
glPopMatrix();
// Draw 3 main bristles (distributed every 120 degrees)
glColor3f(0.3f, 0.3f, 0.3f);
for (int arm = 0; arm < 3; arm++) {
glPushMatrix();
glRotatef(arm * 120.0f, 0.0f, 0.0f, 1.0f);
// Main bristle arm
glTranslatef(brushRadius * 0.25f, 0.0f, brushRadius * 0.2f);
// Draw bristle segments
for (int segment = 0; segment < 8; segment++) {
glPushMatrix();
float segmentRatio = (float)segment / 7.0f;
float x = segmentRatio * bristleLength * 0.8f;
float z = -segmentRatio * bristleLength * 0.3f; // Curve downward
float bendAngle = segmentRatio * 20.0f; // Gradually bend
glTranslatef(x, 0.0f, z);
glRotatef(-bendAngle, 0.0f, 1.0f, 0.0f); // Curve upward (for aesthetics)
// Bristle thickness decreases toward the tip
float thickness = brushRadius * 0.05f * (1.0f - segmentRatio * 0.5f);
glScalef(bristleLength * 0.15f, thickness, thickness);
glutSolidCube(1.0f);
glPopMatrix();
}
glPopMatrix();
}
// Draw additional fine bristles
glColor3f(0.4f, 0.4f, 0.4f);
for (int arm = 0; arm < 3; arm++) {
for (int subBristle = 0; subBristle < 5; subBristle++) {
glPushMatrix();
glRotatef(arm * 120.0f + (subBristle - 2) * 8.0f, 0.0f, 0.0f, 1.0f);
// Sub-bristle
glTranslatef(brushRadius * 0.3f, 0.0f, brushRadius * 0.2f);
for (int segment = 0; segment < 6; segment++) {
glPushMatrix();
float segmentRatio = (float)segment / 5.0f;
float x = segmentRatio * bristleLength * 0.6f;
float z = -segmentRatio * bristleLength * 0.25f;
float bendAngle = segmentRatio * 15.0f;
glTranslatef(x, 0.0f, z);
glRotatef(-bendAngle, 0.0f, 1.0f, 0.0f);
float thickness = brushRadius * 0.03f * (1.0f - segmentRatio * 0.6f);
glScalef(bristleLength * 0.12f, thickness, thickness);
glutSolidCube(1.0f);
glPopMatrix();
}
glPopMatrix();
}
}
glPopMatrix();
}
// Suction inlet (vacuum mouth)
void drawCenterRectangle(float w, float h) {
float hw = w / 2.0f;
float hh = h / 2.0f;
glBegin(GL_QUADS);
glVertex2f(-hw, -hh);
glVertex2f(hw, -hh);
glVertex2f(hw, hh);
glVertex2f(-hw, hh);
glEnd();
}
void drawControlPanel() {
// Control panel on front
glPushMatrix();
glTranslatef(0.7f, 0.0f, 0.8f);
// Panel background
glColor3f(0.2f, 0.2f, 0.2f);
glBegin(GL_QUADS);
glNormal3f(0.0f, 0.0f, 1.0f);
glVertex3f(-0.5f, -0.8f, 0.01f);
glVertex3f(0.5f, -0.8f, 0.01f);
glVertex3f(0.5f, 0.8f, 0.01f);
glVertex3f(-0.5f, 0.8f, 0.01f);
glEnd();
// Display
glColor3f(0.0f, 0.8f, 0.0f);
glBegin(GL_QUADS);
glVertex3f(-0.2f, -0.5f, 0.02f);
glVertex3f(0.2f, -0.5f, 0.02f);
glVertex3f(0.2f, 0.5f, 0.02f);
glVertex3f(-0.2f, 0.5f, 0.02f);
glEnd();
// Mode selector buttons
glColor3f(0.6f, 0.6f, 0.6f);
for (int i = 0; i < 3; i++) {
glPushMatrix();
glTranslatef(0.4f, -0.5f + i * 0.5f, 0.02f);
glutSolidSphere(0.08, 10, 10);
glPopMatrix();
}
glPopMatrix();
}
void drawCasterWheel() {
// Draw the wheel base
GLfloat base_ambient[] = { 0.3f, 0.3f, 0.3f, 1.0f };
GLfloat base_diffuse[] = { 0.6f, 0.6f, 0.6f, 1.0f };
GLfloat base_specular[] = { 0.9f, 0.9f, 0.9f, 1.0f };
GLfloat base_shininess = 50.0f;
glMaterialfv(GL_FRONT, GL_AMBIENT, base_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, base_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, base_specular);
glMaterialf(GL_FRONT, GL_SHININESS, base_shininess);
glPushMatrix();
glTranslatef(2.5f, 0.0f, -0.06f);
glScalef(1.5f, 1.5f, 1.3f);
glutSolidCube(0.1f);
glPopMatrix();
// Draw the left wheel
glPushMatrix();
glTranslatef(2.5f, 0.04f, -0.35f);
glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
// Tire material (dark rubber)
GLfloat tire_ambient[] = { 0.02f, 0.02f, 0.02f, 1.0f };
GLfloat tire_diffuse[] = { 0.05f, 0.05f, 0.05f, 1.0f };
GLfloat tire_specular[] = { 0.1f, 0.1f, 0.1f, 1.0f };
GLfloat tire_shininess = 5.0f;
glMaterialfv(GL_FRONT, GL_AMBIENT, tire_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, tire_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, tire_specular);
glMaterialf(GL_FRONT, GL_SHININESS, tire_shininess);
glutSolidTorus(0.02, 0.08, 8, 16);// Draw tire
// Hub material (gray plastic)
GLfloat hub_ambient[] = { 0.3f, 0.3f, 0.3f, 1.0f };
GLfloat hub_diffuse[] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat hub_specular[] = { 0.8f, 0.8f, 0.8f, 1.0f };
GLfloat hub_shininess = 60.0f;
glMaterialfv(GL_FRONT, GL_AMBIENT, hub_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, hub_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, hub_specular);
glMaterialf(GL_FRONT, GL_SHININESS, hub_shininess);
glScalef(1.0f, 1.0f, 0.08f);
glutSolidSphere(0.07, 10, 10);// Draw hub
glPopMatrix();
// Draw the right wheel
glPushMatrix();
glTranslatef(2.5f, -0.04f, -0.35f);
glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
glMaterialfv(GL_FRONT, GL_AMBIENT, tire_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, tire_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, tire_specular);
glMaterialf(GL_FRONT, GL_SHININESS, tire_shininess);
glutSolidTorus(0.02, 0.08, 8, 16);
glMaterialfv(GL_FRONT, GL_AMBIENT, hub_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, hub_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, hub_specular);
glMaterialf(GL_FRONT, GL_SHININESS, hub_shininess);
glScalef(1.0f, 1.0f, 0.08f);
glutSolidSphere(0.07, 10, 10);
glPopMatrix();
// Draw the axle
glPushMatrix();
GLfloat axle_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat axle_diffuse[] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat axle_specular[] = { 0.7f, 0.7f, 0.7f, 1.0f };
GLfloat axle_shininess = 40.0f;
glMaterialfv(GL_FRONT, GL_AMBIENT, axle_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, axle_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, axle_specular);
glMaterialf(GL_FRONT, GL_SHININESS, axle_shininess);
glTranslatef(2.5f, 0.0f, -0.24f);
glScalef(0.08, 0.08, 0.25);
glutSolidCube(1.0f);// Draw axle
glPopMatrix();
}
void drawSmallEmbeddedCamera(float radius) {
glPushMatrix();
glRotatef(-90, 0, 1, 0); // Rotate 90° around the y-axis
glPushMatrix();
// Outer mounting ring
glColor3f(0.15f, 0.15f, 0.15f);
drawPartialCylinder(radius * 1.3f, 0.025f, 0.0f, 2.0f * M_PI);
drawDisk(radius * 1.3f, 0.0f);
drawDisk(radius * 1.3f, 0.025f);
// Camera body
glPushMatrix();
glTranslatef(0.0f, 0.0f, -0.012f);
glColor3f(0.05f, 0.05f, 0.05f);
drawPartialCylinder(radius, 0.015f, 0.0f, 2.0f * M_PI);
drawDisk(radius, 0.0f);
drawDisk(radius, 0.015f);
glPopMatrix();
// Camera lens
glPushMatrix();
glTranslatef(0.0f, 0.0f, -0.02f);
glColor3f(0.1f, 0.1f, 0.4f);
drawPartialCylinder(radius * 0.5f, 0.008f, 0.0f, 2.0f * M_PI);
drawDisk(radius * 0.5f, 0.008f);
glPopMatrix();
// Central reflection highlight
glPushMatrix();
glTranslatef(0.0f, 0.0f, -0.025f);
glColor3f(0.3f, 0.5f, 0.8f);
glutSolidSphere(radius * 0.15f, 6, 6);
glPopMatrix();
glPopMatrix();
glPopMatrix();
}
// Infrared sensor
void drawIRSensor(float radius) {
glPushMatrix();
glRotatef(-90, 0, 1, 0); // Rotate 90° around the y-axis
glPushMatrix();
// Outer mounting ring
glColor3f(0.2f, 0.2f, 0.2f);
drawPartialCylinder(radius * 1.1f, 0.02f, 0.0f, 2.0f * M_PI);
drawDisk(radius * 1.1f, 0.0f);
drawDisk(radius * 1.1f, 0.02f);
// Sensor body
glPushMatrix();
glTranslatef(0.0f, 0.0f, -0.01f);
glColor3f(0.1f, 0.1f, 0.1f);
drawPartialCylinder(radius, 0.012f, 0.0f, 2.0f * M_PI);
drawDisk(radius, 0.0f);
drawDisk(radius, 0.012f);
glPopMatrix();
// Infrared emitter/receiver window
glPushMatrix();
glTranslatef(0.0f, 0.0f, -0.018f);
glColor3f(0.3f, 0.1f, 0.1f);
drawPartialCylinder(radius * 0.6f, 0.005f, 0.0f, 2.0f * M_PI);
drawDisk(radius * 0.6f, 0.005f);
glPopMatrix();
// Central reflection highlight
glPushMatrix();
glTranslatef(0.0f, 0.0f, -0.02f);
glColor3f(0.8f, 0.3f, 0.3f);
glutSolidSphere(radius * 0.12f, 6, 6);
glPopMatrix();
glPopMatrix();
glPopMatrix();
}
// Dehumidifier air intake vent
void drawIntakeVent(float width, float height) {
glPushMatrix();
// Main frame of the vent
glColor3f(0.2f, 0.2f, 0.2f);
glBegin(GL_QUADS);
glVertex3f(-width / 2, -height / 2, 0.0f);
glVertex3f(width / 2, -height / 2, 0.0f);
glVertex3f(width / 2, height / 2, 0.0f);
glVertex3f(-width / 2, height / 2, 0.0f);
glEnd();
// Vent grilles
glColor3f(0.1f, 0.1f, 0.1f);
int barCount = 8;
float barHeight = 0.06f * height;// Thickness of each grille bar
float gap = (height - barCount * barHeight) / (barCount + 1);
for (int i = 0; i < barCount; i++) {
float y = -height / 2 + gap + i * (barHeight + gap) + barHeight / 2;
glPushMatrix();
glTranslatef(0.0f, y, 0.01f);
glBegin(GL_QUADS);
glVertex3f(-0.88f * width / 2, -barHeight / 2, 0.0f);
glVertex3f(0.88f * width / 2, -barHeight / 2, 0.0f);
glVertex3f(0.88f * width / 2, barHeight / 2, 0.0f);
glVertex3f(-0.88f * width / 2, barHeight / 2, 0.0f);
glEnd();
glPopMatrix();
}
glPopMatrix();
}
void drawVentSlots(float width, float height) {
// Set black material
GLfloat black_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
GLfloat black_diffuse[] = { 0.1f, 0.1f, 0.1f, 1.0f };
GLfloat black_specular[] = { 0.0f, 0.0f, 0.0f, 1.0f };
GLfloat black_shininess = 5.0f;
glMaterialfv(GL_FRONT, GL_AMBIENT, black_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, black_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, black_specular);
glMaterialf(GL_FRONT, GL_SHININESS, black_shininess);
int slotCount = 6; // Number of horizontal bars
float slotThickness = 0.02f; // Thickness of each bar
float spacing = height / slotCount; // Spacing between bars
for (int i = 0; i < slotCount; i++) {
float z = -height / 2 + i * spacing + spacing / 2;
glPushMatrix();
glTranslatef(0.0f, 0.0f, z);
drawPartialCylinder(RADIUS + 0.01, slotThickness, 0 - M_PI / 8, M_PI / 8);
glPopMatrix();
}
}
// Bumper (collision protection panel)
// Close the top and bottom (draw ring sector caps)
void drawRingEndCap(float innerR, float outerR, float z, float angleStart, float angleEnd, int segments = SLICES) {
glBegin(GL_TRIANGLE_STRIP);
for (int i = 0; i <= segments; ++i) {
float angle = angleStart + (float)i / segments * (angleEnd - angleStart);
float xOuter = outerR * cos(angle);
float yOuter = outerR * sin(angle);
float xInner = innerR * cos(angle);
float yInner = innerR * sin(angle);
glNormal3f(0.0f, 0.0f, 1.0f); /// Facing up or down
glVertex3f(xOuter, yOuter, z);
glVertex3f(xInner, yInner, z);
}
glEnd();
}
// Close the front and back (draw sector side walls)
void drawEndCapWall(float innerR, float outerR, float height, float angle) {
float x1 = innerR * cos(angle);
float y1 = innerR * sin(angle);
float x2 = outerR * cos(angle);
float y2 = outerR * sin(angle);
glBegin(GL_QUADS);
// Set normal
float nx = cos(angle);
float ny = sin(angle);
glNormal3f(nx, ny, 0.0f);
// From bottom to top
glVertex3f(x1, y1, 0.0f);
glVertex3f(x2, y2, 0.0f);
glVertex3f(x2, y2, height);
glVertex3f(x1, y1, height);
glEnd();
}
// Main part: connector between bumper and robot body
void drawConnector(float angle, float outerRadius, float innerRadius, float width, float height) {
float midRadius = (outerRadius + innerRadius) / 2.0f;
float length = outerRadius - innerRadius;
// Start position: on the outer arc
float x = midRadius * cos(angle);
float y = midRadius * sin(angle);
glPushMatrix();
glTranslatef(x, y, height / 2.0f); // Place at the middle, center in height
glRotatef(angle * 180.0f / M_PI, 0, 0, 1); // Point toward the circle center
glScalef(length, width, height); // Stretch to a bar shape
glutSolidCube(1.0f);
glPopMatrix();
}
void drawBumper() {
float outerRadius = 3.3f;
float innerRadius = 3.2f;
float height = 0.6f;
float start1 = M_PI / 4.0f;
float end1 = 5 * M_PI / 6.0f;
float start2 = 7 * M_PI / 6.0f;
float end2 = 7 * M_PI / 4.0f;
drawPartialCylinder(outerRadius, height, start1, end1); // Outer wall
drawPartialCylinder(innerRadius, height, start1, end1); // Inner wall
drawRingEndCap(innerRadius, outerRadius, 0.0f, start1, end1);// Bottom cap
drawRingEndCap(innerRadius, outerRadius, height, start1, end1);// Top cap
drawEndCapWall(innerRadius, outerRadius, height, start1);// Front cap
drawEndCapWall(innerRadius, outerRadius, height, end1);// Back cap
// Draw the other sector (second bumper segment)
drawPartialCylinder(outerRadius, height, start2, end2);
drawPartialCylinder(innerRadius, height, start2, end2);
drawRingEndCap(innerRadius, outerRadius, 0.0f, start2, end2);
drawRingEndCap(innerRadius, outerRadius, height, start2, end2);
drawEndCapWall(innerRadius, outerRadius, height, start2);
drawEndCapWall(innerRadius, outerRadius, height, end2);
// Connectors between the bumper and the robot main body
float mid1_1 = start1 + (end1 - start1) / 3.0f;
float mid1_2 = start1 + 2.0f * (end1 - start1) / 3.0f;
float mid2_1 = start2 + (end2 - start2) / 3.0f;
float mid2_2 = start2 + 2.0f * (end2 - start2) / 3.0f;
float length1 = 0.5f; // Length toward inside
float width1 = 0.08f; // Width
float height1 = 0.6f; // Same height as bumper
drawConnector(mid1_1, outerRadius, length1, width1, height1);
drawConnector(mid1_2, outerRadius, length1, width1, height1);
drawConnector(mid2_1, outerRadius, length1, width1, height1);
drawConnector(mid2_2, outerRadius, length1, width1, height1);
}
// Initialize the particle system
void initParticles() {
for (int i = 0; i < MAX_PARTICLES; i++) {
particles[i].active = false;
particles[i].life = 0.0f;
}
}
// Modified search function to specify a range for each type
int findUnusedParticleInRange(int start, int end) {
static int lastUsed[4] = { 0, 100, 200, 350 };// Last used position for each pool
int poolIndex = (start == 0) ? 0 : (start == 100) ? 1 : (start == 200) ? 2 : 3;
// Start searching from last used position
for (int i = lastUsed[poolIndex]; i <= end; i++) {
if (particles[i].life <= 0.0f) {
lastUsed[poolIndex] = i;
return i;
}
}
// If not found, search from start position
for (int i = start; i < lastUsed[poolIndex]; i++) {
if (particles[i].life <= 0.0f) {
lastUsed[poolIndex] = i;
return i;
}
}
// If still not found, forcibly reuse the particle with the lowest life value
int bestIndex = start;
float minLife = particles[start].life;
for (int i = start; i <= end; i++) {
if (particles[i].life < minLife) {
minLife = particles[i].life;
bestIndex = i;
}
}
particles[bestIndex].life = 0.0f; // Forcefully reset
particles[bestIndex].active = false;
lastUsed[poolIndex] = bestIndex;
return bestIndex;
}
// Modified vacuum intake particle generation function
void generateVacuumIntake(float deltaTime) {
// Intake area
float intakeX = 1.5f;
float intakeWidth = 1.0f;
float intakeDepth = 2.0f;
float intakeHeight = -0.1f;
int newParticles = 12;
for (int i = 0; i < newParticles; i++) {
int index = findUnusedParticleInRange(VACUUM_INTAKE_START, VACUUM_INTAKE_END);
// Randomly select particle type
int particleType = rand() % 10;
particles[index].type = (particleType < 6) ? 0 : // 60% air
(particleType < 9) ? 1 : // 30% dust
2; // 10% dirt
// Randomly generate position within the intake area
particles[index].pos[0] = intakeX + ((float)rand() / RAND_MAX - 0.5f) * intakeWidth * 0.5f;
particles[index].pos[1] = ((float)rand() / RAND_MAX - 0.5f) * intakeDepth;
particles[index].pos[2] = intakeHeight - 0.3f;
// Set particle color and size based on type
if (particles[index].type == 0) { // Air - gray
particles[index].color[0] = 0.7f;
particles[index].color[1] = 0.7f;
particles[index].color[2] = 0.7f;
particles[index].color[3] = 0.3f + ((float)rand() / RAND_MAX) * 0.2f;
particles[index].size = 0.03f + ((float)rand() / RAND_MAX) * 0.02f;
}
else if (particles[index].type == 1) { // Dust - dark gray
particles[index].color[0] = 0.5f;
particles[index].color[1] = 0.5f;
particles[index].color[2] = 0.5f;
particles[index].color[3] = 0.4f + ((float)rand() / RAND_MAX) * 0.3f;
particles[index].size = 0.04f + ((float)rand() / RAND_MAX) * 0.03f;
}
else { // Dirt - black
particles[index].color[0] = 0.2f;
particles[index].color[1] = 0.2f;
particles[index].color[2] = 0.2f;
particles[index].color[3] = 0.5f + ((float)rand() / RAND_MAX) * 0.3f;
particles[index].size = 0.05f + ((float)rand() / RAND_MAX) * 0.04f;
}
// Upward velocity
particles[index].vel[0] = -0.2f;
particles[index].vel[1] = ((float)rand() / RAND_MAX - 0.5f) * 0.2f;
particles[index].vel[2] = 1.3f + ((float)rand() / RAND_MAX) * 0.3f;
particles[index].life = 1.3f;
particles[index].active = true;
}
}
// Modified function for generating vacuum exhaust particles
void generateVacuumExhaust(float deltaTime) {
float slotCount = 6;
float slotThickness = 0.02f;
float ventHeight = 0.3f;
float spacing = ventHeight / slotCount;
float ventX = 0.0f;
float ventY = 0.0f;
float ventZ = 0.3f;
int newParticles = 12;
for (int i = 0; i < newParticles; i++) {
int index = findUnusedParticleInRange(VACUUM_EXHAUST_START, VACUUM_EXHAUST_END);
// Randomly select a slot position
int slotIndex = rand() % (int)slotCount;
float slotZ = ventZ + (-ventHeight / 2 + slotIndex * spacing + spacing / 2);
// Randomly select a position on the arc-shaped bar