Repository navigation
Expand file tree
/
Copy pathtask6.cpp
More file actions
1070 lines (956 loc) · 46 KB
/
Copy pathtask6.cpp
File metadata and controls
1070 lines (956 loc) · 46 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 "task6.h"
#include "task1.h"
#include "task3.h"
#include "config.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <cmath>
#include <vector>
#include <string>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
// ═════════════════════════════════════════════════════════════════════════════
// SECTION 1 — WIREFRAME GEOMETRY
// ═════════════════════════════════════════════════════════════════════════════
// ─── Helper: push a line with colour ────────────────────────────────────────
static void pushLine(std::vector<Line3D>& L,
cv::Point3f a, cv::Point3f b,
cv::Scalar col, int thick = 1)
{
L.push_back({a, b, col, thick});
}
// ─────────────────────────────────────────────────────────────────────────────
// Shape 1 — EIFFEL TOWER FRAME
// A leaning lattice tower: wide square base tapering to a spire.
// The "lean" is introduced by offsetting the top apex in X and Y.
// ─────────────────────────────────────────────────────────────────────────────
std::vector<Line3D> buildEiffelTower(float cx, float cy, float scale)
{
// Z convention: z = 0 → on the board, negative = above camera
const float zBase = scale * -0.05f;
const float zFloor1 = scale * -1.0f;
const float zFloor2 = scale * -1.9f;
const float zFloor3 = scale * -2.7f;
const float zSpire = scale * -3.6f;
// Lean: the apex drifts slightly in X and Y for a non-symmetric look
const float leanX = 0.25f, leanY = 0.15f;
// Foot width at each tier (half-extents)
const float w0 = scale * 1.20f; // base
const float w1 = scale * 0.68f; // 1st floor
const float w2 = scale * 0.34f; // 2nd floor
const float w3 = scale * 0.14f; // 3rd floor / top platform
// Helper: 4 corners of a square tier at (cx+dx, cy+dy, z) with half-width w
auto tier = [&](float dx, float dy, float z, float w) {
return std::array<cv::Point3f, 4>{{
{cx + dx - w, cy + dy - w, z},
{cx + dx + w, cy + dy - w, z},
{cx + dx + w, cy + dy + w, z},
{cx + dx - w, cy + dy + w, z}
}};
};
// Colour palette — warm iron tones with gradient by height
cv::Scalar cBase (100, 130, 200); // cool grey-blue at feet
cv::Scalar cLower(80, 160, 220);
cv::Scalar cMid (60, 200, 230);
cv::Scalar cUpper(40, 230, 200);
cv::Scalar cTop (20, 255, 160); // bright teal at apex
cv::Scalar cBrace(160, 200, 255); // lighter for cross-bracing
cv::Scalar cSpire(200, 240, 255); // near-white spire
std::vector<Line3D> L;
// ── Tier rings (horizontal squares) ─────────────────────────────────────
auto drawRing = [&](const std::array<cv::Point3f,4>& t, cv::Scalar c, int th = 2) {
for (int i = 0; i < 4; ++i) pushLine(L, t[i], t[(i+1)%4], c, th);
};
auto t0 = tier(0.f, 0.f, zBase, w0);
auto t1 = tier(leanX*.25f, leanY*.25f, zFloor1, w1);
auto t2 = tier(leanX*.55f, leanY*.55f, zFloor2, w2);
auto t3 = tier(leanX*.80f, leanY*.80f, zFloor3, w3);
cv::Point3f apex{cx + leanX, cy + leanY, zSpire};
drawRing(t0, cBase, 2);
drawRing(t1, cLower, 2);
drawRing(t2, cMid, 1);
drawRing(t3, cUpper, 1);
// ── Legs: base → floor 1 (4 corner legs + mid bracing) ──────────────────
for (int i = 0; i < 4; ++i) {
pushLine(L, t0[i], t1[i], cBase, 2);
// mid-point X-braces on each face
cv::Point3f m0 = {(t0[i].x+t0[(i+1)%4].x)*.5f,
(t0[i].y+t0[(i+1)%4].y)*.5f, zBase};
cv::Point3f m1 = {(t1[i].x+t1[(i+1)%4].x)*.5f,
(t1[i].y+t1[(i+1)%4].y)*.5f, zFloor1};
pushLine(L, t0[i], m1, cBrace, 1);
pushLine(L, t0[(i+1)%4], m1, cBrace, 1);
pushLine(L, m0, t1[i], cBrace, 1);
}
// ── Columns: floor 1 → floor 2 ───────────────────────────────────────────
for (int i = 0; i < 4; ++i) {
pushLine(L, t1[i], t2[i], cLower, 2);
cv::Point3f m1 = {(t1[i].x+t1[(i+1)%4].x)*.5f,
(t1[i].y+t1[(i+1)%4].y)*.5f, zFloor1};
cv::Point3f m2 = {(t2[i].x+t2[(i+1)%4].x)*.5f,
(t2[i].y+t2[(i+1)%4].y)*.5f, zFloor2};
pushLine(L, t1[i], m2, cBrace, 1);
pushLine(L, t1[(i+1)%4], m2, cBrace, 1);
pushLine(L, m1, t2[i], cBrace, 1);
}
// ── Columns: floor 2 → floor 3 ───────────────────────────────────────────
for (int i = 0; i < 4; ++i) {
pushLine(L, t2[i], t3[i], cMid, 2);
pushLine(L, t2[i], t3[(i+1)%4], cBrace, 1);
}
// ── Spire: floor 3 → apex ────────────────────────────────────────────────
for (int i = 0; i < 4; ++i) pushLine(L, t3[i], apex, cTop, 2);
// small antenna cap
cv::Point3f antennaTop{apex.x, apex.y, apex.z - scale * 0.8f};
pushLine(L, apex, antennaTop, cSpire, 2);
// ── Diagonal bracing bands between rings ─────────────────────────────────
// floor1 ↔ floor2 diagonal crosses
for (int i = 0; i < 4; ++i) {
pushLine(L, t1[i], t2[(i+1)%4], cBrace, 1);
pushLine(L, t1[(i+1)%4], t2[i], cBrace, 1);
}
return L;
}
// ─────────────────────────────────────────────────────────────────────────────
// Shape 2 (was Sphere) — DNA DOUBLE HELIX / SPIRAL LADDER
// Two helices wound around a vertical axis, connected by rungs.
// ─────────────────────────────────────────────────────────────────────────────
std::vector<Line3D> buildDNA(float cx, float cy)
{
const float zCenter = -3.5f; // midpoint depth — centred higher above board
const float height = 6.5f; // tall enough to be clearly visible
const float radius = 1.30f; // wider helix for visibility
const int turns = 4; // number of full twists
const int steps = 60; // resolution
std::vector<Line3D> L;
auto helix = [&](float phaseOffset) -> std::vector<cv::Point3f> {
std::vector<cv::Point3f> pts;
for (int i = 0; i <= steps; ++i) {
float t = float(i) / steps; // 0..1 along height
float ang = 2.f * float(M_PI) * turns * t + phaseOffset;
float z = zCenter + height * (t - 0.5f);
pts.push_back({ cx + radius * std::cos(ang),
cy + radius * std::sin(ang),
z });
}
return pts;
};
auto hA = helix(0.f);
auto hB = helix(float(M_PI)); // π offset = opposite strand
// Colour gradient along height (blue → cyan → green → yellow)
auto strandColor = [&](int i, bool isA) -> cv::Scalar {
float t = float(i) / steps;
if (isA) {
// strand A: electric blue → cyan
return cv::Scalar(int(255*(1.f-t)), int(180+75*t), 255);
} else {
// strand B: magenta → gold
return cv::Scalar(int(60+195*t), int(40+200*t), int(255*(1.f-t)));
}
};
// Draw strands
for (int i = 0; i < steps; ++i) {
pushLine(L, hA[i], hA[i+1], strandColor(i, true), 2);
pushLine(L, hB[i], hB[i+1], strandColor(i, false), 2);
}
// Draw rungs every ~1/turns of a turn
const int rungEvery = steps / (turns * 5);
for (int i = 0; i <= steps; i += rungEvery) {
float t = float(i) / steps;
// Rung colour: white-teal gradient
cv::Scalar rc(int(80+175*t), int(220-60*t), int(180+75*(1.f-t)));
pushLine(L, hA[i], hB[i], rc, 1);
// Small tick marks at rung midpoints to suggest base pairs
cv::Point3f mid{(hA[i].x+hB[i].x)*.5f,
(hA[i].y+hB[i].y)*.5f,
(hA[i].z+hB[i].z)*.5f};
cv::Point3f perp{mid.x, mid.y, mid.z - 0.12f};
pushLine(L, mid, perp, cv::Scalar(200,230,255), 1);
}
// Floating bounding helix ring at top and bottom for visual anchor
for (int i = 0; i < 20; ++i) {
float a0 = 2.f*float(M_PI)*i/20.f;
float a1 = 2.f*float(M_PI)*(i+1)/20.f;
float zt = zCenter + height*0.5f;
float zb = zCenter - height*0.5f;
cv::Point3f pT0{cx+radius*1.25f*std::cos(a0), cy+radius*1.25f*std::sin(a0), zt};
cv::Point3f pT1{cx+radius*1.25f*std::cos(a1), cy+radius*1.25f*std::sin(a1), zt};
cv::Point3f pB0{cx+radius*1.25f*std::cos(a0), cy+radius*1.25f*std::sin(a0), zb};
cv::Point3f pB1{cx+radius*1.25f*std::cos(a1), cy+radius*1.25f*std::sin(a1), zb};
pushLine(L, pT0, pT1, cv::Scalar(80,220,255), 1);
pushLine(L, pB0, pB1, cv::Scalar(80,220,255), 1);
}
return L;
}
// ─────────────────────────────────────────────────────────────────────────────
// Shape 3 — ICOSAHEDRON (kept, with richer multi-tone colouring)
// Edges are grouped by their angular "longitude band" so each structural
// belt gets a distinct colour: cold blues at poles, warm at equator.
// ─────────────────────────────────────────────────────────────────────────────
std::vector<Line3D> buildIcosahedron(float cx, float cy, float radius)
{
float zc = -radius - 0.2f;
float phi = (1.f + std::sqrt(5.f)) / 2.f;
std::vector<cv::Point3f> v;
auto add = [&](float x, float y, float z) {
float l = std::sqrt(x*x + y*y + z*z);
v.push_back({cx + radius*x/l, cy + radius*y/l, zc + radius*z/l});
};
add( 0, 1, phi); add( 0,-1, phi); add( 0, 1,-phi); add( 0,-1,-phi);
add( 1, phi, 0); add(-1, phi, 0); add( 1,-phi, 0); add(-1,-phi, 0);
add( phi, 0, 1); add(-phi, 0, 1); add( phi, 0,-1); add(-phi, 0,-1);
// 30 edges, grouped by face-zone for colour variation
// Zone 0 (top cap): edges around top vertex 0
// Zone 1 (upper band)
// Zone 2 (equatorial)
// Zone 3 (lower band)
// Zone 4 (bottom cap)
const std::vector<std::pair<int,int>> E = {
{0,1},{0,4},{0,5},{0,8},{0,9}, // zone 0 – top cap
{1,6},{1,7},{1,8},{1,9}, // zone 1
{4,5},{4,8},{4,10},{5,9},{5,11}, // zone 2 equatorial
{8,10},{9,11},{6,8},{6,10},{7,9},{7,11}, // zone 3
{2,3},{2,4},{2,5},{2,10},{2,11}, // zone 4
{3,6},{3,7},{3,10},{3,11},{6,7} // zone 5 – bottom cap
};
// Colour by zone
const cv::Scalar zoneCol[] = {
cv::Scalar(255, 80, 60), // zone 0 – hot red-orange top
cv::Scalar(255,160, 40), // zone 1 – amber
cv::Scalar(80, 255, 120), // zone 2 – bright green equator
cv::Scalar(60, 180, 255), // zone 3 – sky blue
cv::Scalar(140, 80, 255), // zone 4 – violet
cv::Scalar(220, 60, 200), // zone 5 – magenta bottom
};
const int zoneBoundary[] = {5, 9, 14, 20, 25, 30};
std::vector<Line3D> L;
for (int i = 0; i < (int)E.size(); ++i) {
int zone = 0;
for (int z = 0; z < 6; ++z) if (i < zoneBoundary[z]) { zone = z; break; }
auto [a, b] = E[i];
// blend with neighbour zone for smooth gradient
float frac = (i - (zone>0?zoneBoundary[zone-1]:0)) /
float(zoneBoundary[zone] - (zone>0?zoneBoundary[zone-1]:0));
cv::Scalar c0 = zoneCol[zone];
cv::Scalar c1 = zoneCol[std::min(zone+1, 5)];
cv::Scalar blended(c0[0]*(1-frac)+c1[0]*frac,
c0[1]*(1-frac)+c1[1]*frac,
c0[2]*(1-frac)+c1[2]*frac);
L.push_back({v[a], v[b], blended, 2});
}
return L;
}
// ─────────────────────────────────────────────────────────────────────────────
// Shape 4 (was Octahedron) — UFO / FLYING SAUCER
// Lens-shaped hull + rotating rim rings + landing legs + cockpit dome.
// ─────────────────────────────────────────────────────────────────────────────
std::vector<Line3D> buildUFO(float cx, float cy, float scale)
{
const float zCenter = scale * -1.6f;
const float hullR = scale * 1.6f; // equatorial radius of hull
const float hullH = scale * 0.55f; // half-height of hull (top & bottom)
const int hullSeg = scale * 20; // segments around hull
std::vector<Line3D> L;
// ── Hull profile: two ellipse rings (top & bottom) + meridian arcs ───────
// Top ring at z = zCenter - hullH
// Equator ring at z = zCenter
// Bottom ring at z = zCenter + hullH (inverted below board plane = deeper)
auto ringPt = [&](int i, int n, float r, float z) -> cv::Point3f {
float a = 2.f * float(M_PI) * i / n;
return { cx + r * std::cos(a), cy + r * std::sin(a), z };
};
// Equator (widest) — yellow-gold to match rim band
cv::Scalar cEq(0, 215, 255); // BGR: yellow-gold
for (int i = 0; i < hullSeg; ++i)
pushLine(L, ringPt(i, hullSeg, hullR, zCenter),
ringPt(i+1, hullSeg, hullR, zCenter), cEq, 2);
// Top dome ring — acid green to match upper disc
const float domeR = hullR * 0.55f;
const float domeZ = zCenter - hullH * 1.1f;
cv::Scalar cDome(40, 240, 50); // BGR: acid green
for (int i = 0; i < hullSeg; ++i)
pushLine(L, ringPt(i, hullSeg, domeR, domeZ),
ringPt(i+1, hullSeg, domeR, domeZ), cDome, 1);
// Bottom ring — orange-red to match hot underbelly
const float botR = hullR * 0.60f;
const float botZ = zCenter + hullH * 0.7f;
cv::Scalar cBot(20, 75, 240); // BGR: orange-red
for (int i = 0; i < hullSeg; ++i)
pushLine(L, ringPt(i, hullSeg, botR, botZ),
ringPt(i+1, hullSeg, botR, botZ), cBot, 1);
// Dome apex — electric blue
cv::Point3f apex{cx, cy, domeZ - hullH * 0.9f};
cv::Scalar cApex(255, 115, 15); // BGR: electric blue
for (int i = 0; i < hullSeg; ++i)
pushLine(L, ringPt(i, hullSeg, domeR, domeZ), apex, cApex, 1);
// ── Meridian ribs: equator → top dome (purple), equator → bottom (violet) ─
const int ribCount = 10;
for (int i = 0; i < ribCount; ++i) {
float t = float(i) / ribCount;
// Upper ribs: purple gradient matching lower hull zone
cv::Scalar cRibUp(int(190-60*t), int(20+20*t), int(140+60*t));
// Lower ribs: orange-red matching bottom zone
cv::Scalar cRibDn(int(20+10*t), int(80+40*t), int(220+35*t));
int idx = int(i * hullSeg / ribCount);
pushLine(L, ringPt(idx, hullSeg, hullR, zCenter),
ringPt(idx, hullSeg, domeR, domeZ), cRibUp, 1);
pushLine(L, ringPt(idx, hullSeg, hullR, zCenter),
ringPt(idx, hullSeg, botR, botZ), cRibDn, 1);
}
// ── Rim glow rings — blazing gold, matching shaded rim ────────────────────
const float rimR1 = hullR * 1.05f;
const float rimR2 = hullR * 1.12f;
cv::Scalar cRim1(0, 215, 255); // BGR: gold
cv::Scalar cRim2(0, 170, 220); // slightly darker gold
for (int i = 0; i < hullSeg; ++i) {
pushLine(L, ringPt(i, hullSeg, rimR1, zCenter - 0.05f),
ringPt(i+1, hullSeg, rimR1, zCenter - 0.05f), cRim1, 2);
pushLine(L, ringPt(i, hullSeg, rimR2, zCenter + 0.05f),
ringPt(i+1, hullSeg, rimR2, zCenter + 0.05f), cRim2, 1);
}
// ── Landing legs — coral-red matching shaded legs ─────────────────────────
const int legCount = 3;
const float legLen = scale * 0.80f;
cv::Scalar cLeg(50, 50, 240); // BGR: coral-red
cv::Scalar cPad(40, 80, 255); // slightly brighter red for pad
for (int i = 0; i < legCount; ++i) {
float ang = 2.f * float(M_PI) * i / legCount + float(M_PI)/6.f;
cv::Point3f hip { cx + botR * std::cos(ang), cy + botR * std::sin(ang), botZ };
cv::Point3f foot{ cx + (botR + legLen*0.6f) * std::cos(ang),
cy + (botR + legLen*0.6f) * std::sin(ang),
botZ + legLen };
pushLine(L, hip, foot, cLeg, 2);
// Foot pad — small cross
float pw = 0.18f;
cv::Point3f padA{ foot.x - pw*std::sin(ang), foot.y + pw*std::cos(ang), foot.z };
cv::Point3f padB{ foot.x + pw*std::sin(ang), foot.y - pw*std::cos(ang), foot.z };
pushLine(L, padA, padB, cPad, 2);
// Strut from equator to foot
cv::Point3f shoulder = ringPt(int(i * hullSeg/legCount), hullSeg, hullR*0.85f, zCenter);
pushLine(L, shoulder, foot, cLeg, 1);
}
// ── Tractor beam (downward cone of dashed lines) ──────────────────────────
const float beamZ = botZ + scale * 1.4f;
const float beamR = scale * 0.55f;
const int beamRays = 8;
cv::Scalar cBeam(255, 255, 120); // BGR: bright cyan-white beam
cv::Point3f beamSrc{cx, cy, botZ + 0.1f};
for (int i = 0; i < beamRays; ++i) {
float a = 2.f * float(M_PI) * i / beamRays;
cv::Point3f tip{ cx + beamR * std::cos(a), cy + beamR * std::sin(a), beamZ };
pushLine(L, beamSrc, tip, cBeam, 1);
}
// Beam base ring
for (int i = 0; i < beamRays; ++i) {
float a0 = 2.f * float(M_PI) * i / beamRays;
float a1 = 2.f * float(M_PI) * (i+1) / beamRays;
cv::Point3f p0{cx+beamR*std::cos(a0), cy+beamR*std::sin(a0), beamZ};
cv::Point3f p1{cx+beamR*std::cos(a1), cy+beamR*std::sin(a1), beamZ};
pushLine(L, p0, p1, cBeam, 1);
}
return L;
}
void drawVirtualObject(cv::Mat& frame,
const std::vector<Line3D>& lines,
const cv::Mat& K, const cv::Mat& D,
const cv::Mat& rvec, const cv::Mat& tvec)
{
if (lines.empty()) return;
std::vector<cv::Point3f> pts3d;
pts3d.reserve(lines.size() * 2);
for (const auto& s : lines) { pts3d.push_back(s.a); pts3d.push_back(s.b); }
std::vector<cv::Point2f> pts2d;
cv::projectPoints(pts3d, rvec, tvec, K, D, pts2d);
for (size_t i = 0; i < lines.size(); ++i)
cv::line(frame, pts2d[i*2], pts2d[i*2+1],
lines[i].color, lines[i].thick, cv::LINE_AA);
}
// ═════════════════════════════════════════════════════════════════════════════
// SECTION 2 — OPENGL SHADED RENDERING HELPERS
// ═════════════════════════════════════════════════════════════════════════════
static void buildGLProjection(const cv::Mat& K, int w, int h,
float nearP, float farP)
{
double fx=K.at<double>(0,0), fy=K.at<double>(1,1);
double cx=K.at<double>(0,2), cy=K.at<double>(1,2);
double m[16]={
2.0*fx/w, 0, 0, 0,
0, 2.0*fy/h, 0, 0,
1.0-2.0*cx/w, 2.0*cy/h-1.0, -(farP+nearP)/(farP-nearP), -1,
0, 0, -2.0*farP*nearP/(farP-nearP), 0};
glMatrixMode(GL_PROJECTION); glLoadMatrixd(m);
}
static void buildGLModelView(const cv::Mat& rvec, const cv::Mat& tvec)
{
cv::Mat R; cv::Rodrigues(rvec,R);
double mv[16]={
R.at<double>(0,0), -R.at<double>(1,0), -R.at<double>(2,0), 0,
R.at<double>(0,1), -R.at<double>(1,1), -R.at<double>(2,1), 0,
R.at<double>(0,2), -R.at<double>(1,2), -R.at<double>(2,2), 0,
tvec.at<double>(0),-tvec.at<double>(1),-tvec.at<double>(2), 1};
glMatrixMode(GL_MODELVIEW); glLoadMatrixd(mv);
}
static GLuint g_bgTex = 0;
static void initBgTex()
{
glGenTextures(1,&g_bgTex);
glBindTexture(GL_TEXTURE_2D,g_bgTex);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
}
static void uploadBg(const cv::Mat& frame)
{
cv::Mat rgb,flipped;
cv::cvtColor(frame,rgb,cv::COLOR_BGR2RGB);
cv::flip(rgb,flipped,0);
glBindTexture(GL_TEXTURE_2D,g_bgTex);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,flipped.cols,flipped.rows,0,
GL_RGB,GL_UNSIGNED_BYTE,flipped.data);
}
static void drawBg(int w, int h)
{
glPushAttrib(GL_ALL_ATTRIB_BITS);
glDisable(GL_DEPTH_TEST); glDisable(GL_LIGHTING); glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,g_bgTex);
glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glOrtho(0,w,0,h,-1,1);
glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity();
glColor3f(1,1,1);
glBegin(GL_QUADS);
glTexCoord2f(0,0);glVertex2i(0,0); glTexCoord2f(1,0);glVertex2i(w,0);
glTexCoord2f(1,1);glVertex2i(w,h); glTexCoord2f(0,1);glVertex2i(0,h);
glEnd();
glMatrixMode(GL_PROJECTION);glPopMatrix();
glMatrixMode(GL_MODELVIEW); glPopMatrix();
glPopAttrib();
}
static void setupLight()
{
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1); // second fill light from below/opposite side
glEnable(GL_DEPTH_TEST); glEnable(GL_NORMALIZE);
// Key light — upper-left front
GLfloat lp0[]={1.f,2.f,-3.f,0.f};
GLfloat la0[]={0.10f,0.10f,0.12f,1.f};
GLfloat ld0[]={0.90f,0.88f,0.85f,1.f};
GLfloat ls0[]={1.f,1.f,1.f,1.f};
glLightfv(GL_LIGHT0,GL_POSITION,lp0);
glLightfv(GL_LIGHT0,GL_AMBIENT, la0);
glLightfv(GL_LIGHT0,GL_DIFFUSE, ld0);
glLightfv(GL_LIGHT0,GL_SPECULAR,ls0);
// Fill light — lower-right back, softer, warm tint
GLfloat lp1[]={-1.5f,-1.f,2.f,0.f};
GLfloat la1[]={0.f,0.f,0.f,1.f};
GLfloat ld1[]={0.40f,0.35f,0.30f,1.f};
GLfloat ls1[]={0.f,0.f,0.f,1.f};
glLightfv(GL_LIGHT1,GL_POSITION,lp1);
glLightfv(GL_LIGHT1,GL_AMBIENT, la1);
glLightfv(GL_LIGHT1,GL_DIFFUSE, ld1);
glLightfv(GL_LIGHT1,GL_SPECULAR,ls1);
}
static void setMat(float r,float g,float b,float sh=48.f)
{
GLfloat a[]={r*.25f,g*.25f,b*.25f,1.f}, d[]={r,g,b,1.f}, s[]={.6f,.6f,.6f,1.f};
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,a);
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,d);
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,s);
glMaterialf (GL_FRONT_AND_BACK,GL_SHININESS,sh);
}
// Flat-normal triangle helper
static void glTri(const float* a,const float* b,const float* c)
{
float u[3]={b[0]-a[0],b[1]-a[1],b[2]-a[2]};
float v[3]={c[0]-a[0],c[1]-a[1],c[2]-a[2]};
float n[3]={u[1]*v[2]-u[2]*v[1], u[2]*v[0]-u[0]*v[2], u[0]*v[1]-u[1]*v[0]};
float l=std::sqrt(n[0]*n[0]+n[1]*n[1]+n[2]*n[2]);
if(l>1e-6f){n[0]/=l;n[1]/=l;n[2]/=l;}
glNormal3fv(n); glVertex3fv(a); glVertex3fv(b); glVertex3fv(c);
}
// ── Shaded Eiffel Tower — mirrors buildEiffelTower wireframe geometry exactly ─
// IMPORTANT: buildGLModelView negates the Z column of the rotation matrix.
// This means local +Z maps toward the camera (same as OpenCV's -Z world).
// So we use POSITIVE Z values here, mirroring the wireframe's negative Z values
// in magnitude (wireframe zFloor1=-2.0 → GL zFloor1=+2.0, etc.).
static void glSolidEiffel()
{
// Mirror wireframe Z magnitudes but positive (GL convention after negation)
const float zBase = 0.05f;
const float zFloor1 = 2.0f;
const float zFloor2 = 3.8f;
const float zFloor3 = 5.4f;
const float zSpire = 7.2f;
const float leanX = 0.55f, leanY = 0.30f;
const float w0 = 2.50f, w1 = 1.40f, w2 = 0.70f, w3 = 0.28f;
// Lean offsets per tier (same fractions as wireframe)
const float dx1 = leanX*0.25f, dy1 = leanY*0.25f;
const float dx2 = leanX*0.55f, dy2 = leanY*0.55f;
const float dx3 = leanX*0.80f, dy3 = leanY*0.80f;
// Draw a tapered panel between two square tiers with a lean offset on top.
auto panel = [&](float wb, float wt,
float zb, float zt,
float dxt, float dyt) {
float hb = wb*.5f, ht = wt*.5f;
float B[4][2] = {{-hb,-hb},{hb,-hb},{hb,hb},{-hb,hb}};
float T[4][2] = {{dxt-ht,dyt-ht},{dxt+ht,dyt-ht},
{dxt+ht,dyt+ht},{dxt-ht,dyt+ht}};
for (int f = 0; f < 4; ++f) {
int n = (f+1)%4;
float a[3]={B[f][0],B[f][1],zb}, b[3]={B[n][0],B[n][1],zb};
float c[3]={T[n][0],T[n][1],zt}, d[3]={T[f][0],T[f][1],zt};
glBegin(GL_TRIANGLES);
glTri(a,b,c); glTri(a,c,d);
glEnd();
}
};
// Base → floor1
setMat(0.45f, 0.35f, 0.22f, 32.f);
panel(w0, w1, zBase, zFloor1, dx1, dy1);
// floor1 → floor2
setMat(0.58f, 0.48f, 0.28f, 48.f);
panel(w1, w2, zFloor1, zFloor2, dx2-dx1, dy2-dy1);
// floor2 → floor3
setMat(0.72f, 0.62f, 0.36f, 64.f);
panel(w2, w3, zFloor2, zFloor3, dx3-dx2, dy3-dy2);
// floor3 → spire apex
setMat(0.92f, 0.88f, 0.72f, 96.f);
float ht = w3*.5f;
float spireApex[3] = {leanX, leanY, zSpire};
float sp[4][3] = {
{dx3-ht, dy3-ht, zFloor3}, {dx3+ht, dy3-ht, zFloor3},
{dx3+ht, dy3+ht, zFloor3}, {dx3-ht, dy3+ht, zFloor3}
};
glBegin(GL_TRIANGLES);
for (int i = 0; i < 4; ++i) glTri(sp[i], sp[(i+1)%4], spireApex);
glEnd();
}
static void glSolidDNA(float scale)
{
const int turns = 4, steps = 60;
const float r = scale * 1.30f; // matches wireframe radius
const float h = scale * 6.5f; // matches wireframe height
auto helixPt = [&](int i, float phase) -> std::array<float,3> {
float t = float(i)/steps;
float ang = 2.f*float(M_PI)*turns*t + phase;
float z = h*(t-0.5f);
return {r*std::cos(ang), r*std::sin(ang), z};
};
// Strand half-tube width
const float pw = scale * 0.10f;
// Strand A – cobalt blue, each segment its own Begin/End for correct normals
setMat(0.10f, 0.45f, 1.00f, 72.f);
for (int i = 0; i < steps; ++i) {
auto p0 = helixPt(i, 0.f);
auto p1 = helixPt(i+1, 0.f);
float a[3]={p0[0]-pw, p0[1], p0[2]};
float b[3]={p0[0]+pw, p0[1], p0[2]};
float c[3]={p1[0]+pw, p1[1], p1[2]};
float d[3]={p1[0]-pw, p1[1], p1[2]};
glBegin(GL_TRIANGLES);
glTri(a,b,c); glTri(a,c,d);
glEnd();
}
// Strand B – vivid magenta
setMat(0.90f, 0.15f, 0.75f, 72.f);
for (int i = 0; i < steps; ++i) {
auto p0 = helixPt(i, float(M_PI));
auto p1 = helixPt(i+1, float(M_PI));
float a[3]={p0[0]-pw, p0[1], p0[2]};
float b[3]={p0[0]+pw, p0[1], p0[2]};
float c[3]={p1[0]+pw, p1[1], p1[2]};
float d[3]={p1[0]-pw, p1[1], p1[2]};
glBegin(GL_TRIANGLES);
glTri(a,b,c); glTri(a,c,d);
glEnd();
}
// Rungs – pearl white
setMat(0.85f, 0.90f, 0.95f, 40.f);
const int rungEvery = steps / (turns * 5);
for (int i = 0; i <= steps; i += rungEvery) {
auto pA = helixPt(i, 0.f);
auto pB = helixPt(i, float(M_PI));
float rpw = scale * 0.07f;
float a[3]={pA[0],pA[1]-rpw,pA[2]};
float b[3]={pA[0],pA[1]+rpw,pA[2]};
float c[3]={pB[0],pB[1]+rpw,pB[2]};
float d[3]={pB[0],pB[1]-rpw,pB[2]};
glBegin(GL_TRIANGLES);
glTri(a,b,c); glTri(a,c,d);
glEnd();
}
}
// ── Shaded Icosahedron — each face gets its own colour band ─────────────────
// CRITICAL: setMat() changes GL state and MUST be called outside glBegin/glEnd.
// Each face is drawn in its own glBegin(GL_TRIANGLES) block.
static void glSolidIco(float r)
{
const float phi=(1.f+std::sqrt(5.f))/2.f;
auto N=[](float x,float y,float z,float* o){
float l=std::sqrt(x*x+y*y+z*z);o[0]=x/l;o[1]=y/l;o[2]=z/l;};
float raw[12][3]={{0,1,phi},{0,-1,phi},{0,1,-phi},{0,-1,-phi},
{1,phi,0},{-1,phi,0},{1,-phi,0},{-1,-phi,0},
{phi,0,1},{-phi,0,1},{phi,0,-1},{-phi,0,-1}};
float v[12][3]; for(int i=0;i<12;++i)N(raw[i][0],raw[i][1],raw[i][2],v[i]);
// 20 faces — each given a distinct colour so every face is visually unique.
// Adjacent faces intentionally use contrasting hues so edges are clear.
const int F[20][3]={{0,1,8},{0,8,4},{0,4,5},{0,5,9},{0,9,1},
{1,6,8},{8,6,10},{8,10,4},{4,10,2},{4,2,5},
{5,2,11},{5,11,9},{9,11,7},{9,7,1},{1,7,6},
{3,6,7},{3,10,6},{3,2,10},{3,11,2},{3,7,11}};
// 20 distinct colours — alternating warm/cool to maximise adjacency contrast
const float fc[20][3] = {
{1.00f,0.15f,0.10f}, {0.10f,0.65f,1.00f}, {1.00f,0.55f,0.05f}, {0.15f,0.85f,0.40f},
{0.85f,0.10f,0.75f}, {1.00f,0.85f,0.05f}, {0.05f,0.75f,0.80f}, {0.90f,0.30f,0.10f},
{0.20f,0.40f,1.00f}, {0.75f,0.90f,0.10f}, {0.60f,0.10f,0.90f}, {0.10f,0.90f,0.55f},
{1.00f,0.40f,0.20f}, {0.10f,0.55f,0.90f}, {0.95f,0.15f,0.50f}, {0.40f,0.95f,0.20f},
{0.20f,0.20f,1.00f}, {1.00f,0.70f,0.10f}, {0.80f,0.10f,0.40f}, {0.10f,0.85f,0.75f}
};
for (int fi = 0; fi < 20; ++fi) {
// setMat MUST be outside glBegin — this is the fix for uniform shading
setMat(fc[fi][0], fc[fi][1], fc[fi][2], 48.f);
float a[3]={r*v[F[fi][0]][0],r*v[F[fi][0]][1],r*v[F[fi][0]][2]};
float b[3]={r*v[F[fi][1]][0],r*v[F[fi][1]][1],r*v[F[fi][1]][2]};
float c[3]={r*v[F[fi][2]][0],r*v[F[fi][2]][1],r*v[F[fi][2]][2]};
glBegin(GL_TRIANGLES);
glTri(a, b, c);
glEnd();
}
}
// ── Shaded UFO — each zone drawn in its own Begin/End with distinct material ──
// CRITICAL: setMat() MUST be outside glBegin/glEnd for material to take effect.
static void glSolidUFO(float scale)
{
const int seg = 24;
const float hullR = scale * 1.6f;
const float hullH = scale * 0.55f;
const float domeR = hullR * 0.55f;
const float domeH = hullH * 1.1f;
const float botR = hullR * 0.60f;
const float botH = hullH * 0.7f;
auto pt = [&](int i, float r, float z) -> std::array<float,3> {
float a = 2.f*float(M_PI)*i/seg;
return {r*std::cos(a), r*std::sin(a), z};
};
// ── 1. Lower hull (equator → bottom ring): deep purple-violet ────────────
setMat(0.55f, 0.08f, 0.75f, 40.f);
for (int i = 0; i < seg; ++i) {
auto eq0 = pt(i, hullR, 0.f);
auto eq1 = pt(i+1, hullR, 0.f);
auto bt0 = pt(i, botR, botH);
auto bt1 = pt(i+1, botR, botH);
float a[3]={eq0[0],eq0[1],0.f}, b[3]={eq1[0],eq1[1],0.f};
float c[3]={bt1[0],bt1[1],botH}, d[3]={bt0[0],bt0[1],botH};
glBegin(GL_TRIANGLES);
glTri(a,b,c); glTri(a,c,d);
glEnd();
}
// ── 2. Upper disc (flat annular ring, equator inward to dome base): acid green
setMat(0.20f, 0.95f, 0.25f, 72.f);
for (int i = 0; i < seg; ++i) {
auto eq0 = pt(i, hullR, 0.f);
auto eq1 = pt(i+1, hullR, 0.f);
auto dr0 = pt(i, domeR, 0.f);
auto dr1 = pt(i+1, domeR, 0.f);
float a[3]={eq0[0],eq0[1],0.f}, b[3]={eq1[0],eq1[1],0.f};
float c[3]={dr1[0],dr1[1],0.f}, d[3]={dr0[0],dr0[1],0.f};
glBegin(GL_TRIANGLES);
glNormal3f(0.f,0.f,-1.f);
glVertex3fv(a); glVertex3fv(b); glVertex3fv(c);
glNormal3f(0.f,0.f,-1.f);
glVertex3fv(a); glVertex3fv(c); glVertex3fv(d);
glEnd();
}
// ── 3. Bottom disc (flat underside): hot orange-red ───────────────────────
setMat(0.95f, 0.30f, 0.05f, 24.f);
{
float ctr[3] = {0.f, 0.f, botH};
for (int i = 0; i < seg; ++i) {
auto r0 = pt(i, botR, botH);
auto r1 = pt(i+1, botR, botH);
float a[3]={r0[0],r0[1],botH}, b[3]={r1[0],r1[1],botH};
glBegin(GL_TRIANGLES);
glNormal3f(0.f,0.f,1.f);
glVertex3fv(ctr); glVertex3fv(b); glVertex3fv(a);
glEnd();
}
}
// ── 4. Dome surface (cone to apex): vivid electric blue ───────────────────
setMat(0.05f, 0.45f, 1.00f, 110.f);
float apex[3] = {0.f, 0.f, -domeH};
for (int i = 0; i < seg; ++i) {
auto d0 = pt(i, domeR, 0.f);
auto d1 = pt(i+1, domeR, 0.f);
float a[3]={d0[0],d0[1],0.f}, b[3]={d1[0],d1[1],0.f};
glBegin(GL_TRIANGLES);
glTri(a, b, apex);
glEnd();
}
// ── 5. Rim band (outer ring): blazing yellow-gold ─────────────────────────
setMat(1.00f, 0.85f, 0.00f, 128.f);
for (int i = 0; i < seg; ++i) {
auto r0 = pt(i, hullR*1.10f, -0.08f*scale);
auto r1 = pt(i+1, hullR*1.10f, -0.08f*scale);
auto r2 = pt(i+1, hullR*1.10f, 0.08f*scale);
auto r3 = pt(i, hullR*1.10f, 0.08f*scale);
float a[3]={r0[0],r0[1],r0[2]}, b[3]={r1[0],r1[1],r1[2]};
float c[3]={r2[0],r2[1],r2[2]}, d[3]={r3[0],r3[1],r3[2]};
glBegin(GL_TRIANGLES);
glTri(a,b,c); glTri(a,c,d);
glEnd();
}
// ── 6. Landing legs: bright coral-red ─────────────────────────────────────
setMat(0.95f, 0.20f, 0.20f, 32.f);
const int legCount = 3;
const float legLen = scale * 0.80f;
for (int i = 0; i < legCount; ++i) {
float ang = 2.f*float(M_PI)*i/legCount + float(M_PI)/6.f;
float hip[3] = { botR*std::cos(ang), botR*std::sin(ang), botH };
float foot[3] = { (botR+legLen*0.6f)*std::cos(ang),
(botR+legLen*0.6f)*std::sin(ang), botH+legLen };
// Leg as a thin flat quad
float pw = scale*0.05f;
float sn = std::sin(ang), cs = std::cos(ang);
float h0[3]={hip[0]-pw*sn, hip[1]+pw*cs, hip[2]};
float h1[3]={hip[0]+pw*sn, hip[1]-pw*cs, hip[2]};
float f0[3]={foot[0]-pw*sn, foot[1]+pw*cs, foot[2]};
float f1[3]={foot[0]+pw*sn, foot[1]-pw*cs, foot[2]};
glBegin(GL_TRIANGLES);
glTri(h0,h1,f1); glTri(h0,f1,f0);
glEnd();
}
}
static void drawShadedShape(Shape shape, float cx, float cy)
{
glPushMatrix();
switch (shape) {
case Shape::CUBE: // Eiffel Tower — origin at (cx,cy,0) = board surface
glTranslatef(cx, cy, 0.f);
glSolidEiffel();
break;
case Shape::SPHERE: // DNA
glTranslatef(cx, cy, -2.0f);
glSolidDNA(1.35f);
break;
case Shape::ICOSAHEDRON:
glTranslatef(cx, cy, -2.0f);
glRotatef(20.f, 1.f, .5f, 0.f);
glSolidIco(1.6f);
break;
case Shape::OCTAHEDRON: // UFO
glTranslatef(cx, cy, -2.0f);
glSolidUFO(0.85f);
break;
}
glPopMatrix();
}
// ═════════════════════════════════════════════════════════════════════════════
// SECTION 3 — SHARED UI (shape panel + shade toggle button)
// ═════════════════════════════════════════════════════════════════════════════
static Shape g_shape = Shape::CUBE;
static bool g_shaded = false;
static cv::Point g_mouse6(-1,-1);
// Layout constants
static const int SP_X=10, SP_Y=10, SP_W=170;
static const int SP_BTN_H=26, SP_PAD=4, SP_NUM=4;
static const int TOG_H = 28;
static const int SP_H = 26 + SP_NUM*(SP_BTN_H+SP_PAD) + SP_PAD + SP_PAD + TOG_H + SP_PAD;
// Updated shape names
static const char* SHAPE_NAMES[4] = {
"1 Eiffel Tower",
"2 DNA Helix",
"3 Icosahedron",
"4 UFO"
};
static cv::Rect shapeBtn(int i)
{
return{SP_X+SP_PAD, SP_Y+26+i*(SP_BTN_H+SP_PAD), SP_W-SP_PAD*2, SP_BTN_H};
}
static cv::Rect toggleBtn()
{
int y = SP_Y+26+SP_NUM*(SP_BTN_H+SP_PAD)+SP_PAD;
return{SP_X+SP_PAD, y, SP_W-SP_PAD*2, TOG_H};
}
static void onMouse6(int event,int x,int y,int,void*)
{
g_mouse6={x,y};
if(event!=cv::EVENT_LBUTTONDOWN) return;
for(int i=0;i<SP_NUM;++i)
if(shapeBtn(i).contains({x,y})) g_shape=static_cast<Shape>(i);
if(toggleBtn().contains({x,y})) g_shaded=!g_shaded;
}
// Drawing helpers
static void fillRR(cv::Mat& img,cv::Rect r,int rad,cv::Scalar col,double a=1.0)
{
cv::Rect safe=r&cv::Rect(0,0,img.cols,img.rows);
if(safe.area()<=0) return;
cv::Mat roi=img(safe),over=roi.clone();
cv::rectangle(over,{rad,0,r.width-2*rad,r.height},col,cv::FILLED);
cv::rectangle(over,{0,rad,r.width,r.height-2*rad},col,cv::FILLED);
for(auto[cx,cy]:std::vector<std::pair<int,int>>{{rad,rad},{r.width-rad,rad},{rad,r.height-rad},{r.width-rad,r.height-rad}})
cv::circle(over,{cx,cy},rad,col,cv::FILLED,cv::LINE_AA);
cv::addWeighted(over,a,roi,1.0-a,0,roi);
}
static void strokeRR(cv::Mat& img,cv::Rect r,int rad,cv::Scalar col,int t=1)
{
int x=r.x,y=r.y,w=r.width,h=r.height;
cv::line(img,{x+rad,y},{x+w-rad,y},col,t,cv::LINE_AA);
cv::line(img,{x+rad,y+h},{x+w-rad,y+h},col,t,cv::LINE_AA);
cv::line(img,{x,y+rad},{x,y+h-rad},col,t,cv::LINE_AA);
cv::line(img,{x+w,y+rad},{x+w,y+h-rad},col,t,cv::LINE_AA);
cv::ellipse(img,{x+rad,y+rad},{rad,rad},180,0,90,col,t,cv::LINE_AA);
cv::ellipse(img,{x+w-rad,y+rad},{rad,rad},270,0,90,col,t,cv::LINE_AA);
cv::ellipse(img,{x+rad,y+h-rad},{rad,rad},90,0,90,col,t,cv::LINE_AA);
cv::ellipse(img,{x+w-rad,y+h-rad},{rad,rad},0,0,90,col,t,cv::LINE_AA);
}
static cv::Scalar accent(int i)
{
// Updated accent colours per shape
const cv::Scalar A[4]={
{120, 200, 255}, // Eiffel – steel blue
{100, 255, 200}, // DNA – teal
{255, 160, 40}, // Icosahedron – amber
{60, 255, 120} // UFO – alien green
};
return A[i];
}
static void drawPanel(cv::Mat& frame)
{
// Panel bg
{
cv::Rect safe=cv::Rect(SP_X,SP_Y,SP_W,SP_H)&cv::Rect(0,0,frame.cols,frame.rows);
cv::Mat roi=frame(safe),bg(safe.height,safe.width,CV_8UC3,cv::Scalar(18,18,26));
cv::addWeighted(roi,0.40,bg,0.60,0,roi);
}
strokeRR(frame,{SP_X,SP_Y,SP_W,SP_H},7,cv::Scalar(70,130,200),1);
cv::putText(frame,"SHAPE",{SP_X+8,SP_Y+17},
cv::FONT_HERSHEY_SIMPLEX,0.38,cv::Scalar(220,230,255),1,cv::LINE_AA);
cv::circle(frame,{SP_X+SP_W-10,SP_Y+13},4,cv::Scalar(70,130,200),cv::FILLED,cv::LINE_AA);
cv::line(frame,{SP_X,SP_Y+22},{SP_X+SP_W,SP_Y+22},cv::Scalar(45,45,60),1);
for(int i=0;i<SP_NUM;++i){
cv::Rect br=shapeBtn(i);
bool active=(static_cast<int>(g_shape)==i), hov=br.contains(g_mouse6);
cv::Scalar ac=accent(i);
if(active) { fillRR(frame,br,5,cv::Scalar(ac[0]*.35,ac[1]*.35,ac[2]*.35),0.85); strokeRR(frame,br,5,ac,1); }
else if(hov) { fillRR(frame,br,5,cv::Scalar(40,50,70),0.80); strokeRR(frame,br,5,cv::Scalar(90,110,150),1); }
else { fillRR(frame,br,5,cv::Scalar(28,28,40),0.65); strokeRR(frame,br,5,cv::Scalar(55,55,72),1); }
fillRR(frame,{br.x,br.y+3,3,br.height-6},1,active?ac:cv::Scalar(55,55,72));
cv::Scalar tc=active?cv::Scalar(240,250,255):cv::Scalar(150,160,175);
cv::putText(frame,SHAPE_NAMES[i],{br.x+10,br.y+br.height/2+5},
cv::FONT_HERSHEY_SIMPLEX,0.34,tc,1,cv::LINE_AA);
}
cv::Rect tb=toggleBtn();
bool thov=tb.contains(g_mouse6);
if(g_shaded){ fillRR(frame,tb,5,cv::Scalar(20,60,20),0.90); strokeRR(frame,tb,5,cv::Scalar(60,200,80),1); }
else { fillRR(frame,tb,5,cv::Scalar(28,28,40),0.75); strokeRR(frame,tb,5,cv::Scalar(80,80,120),1); }
if(thov) fillRR(frame,tb,5,cv::Scalar(40,80,60),0.40);
const char* label=g_shaded?"[Tab] Wireframe":"[Tab] Shade";
cv::Scalar lc=g_shaded?cv::Scalar(80,255,120):cv::Scalar(160,170,185);
cv::putText(frame,label,{tb.x+8,tb.y+tb.height/2+5},
cv::FONT_HERSHEY_SIMPLEX,0.36,lc,1,cv::LINE_AA);
}
// ═════════════════════════════════════════════════════════════════════════════
// SECTION 4 — MAIN LOOP
// ═════════════════════════════════════════════════════════════════════════════
void runTask6(int argc, char* argv[])
{
cv::Mat K, D;
if(!loadCalibration("calibration.yml",K,D)) return;
cv::VideoCapture cap=openVideoSource(argc,argv);
if(!cap.isOpened()){ std::cerr<<"[Task6][ERROR] Cannot open video source.\n"; return; }
cv::Mat firstFrame; cap>>firstFrame;
if(firstFrame.empty()){ std::cerr<<"[Task6][ERROR] Empty first frame.\n"; return; }
const int W=firstFrame.cols, H=firstFrame.rows;
bool glOK=false;
GLFWwindow* glfwWin=nullptr;
if(glfwInit()){
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWin=glfwCreateWindow(W,H,"GL Context",nullptr,nullptr);
if(glfwWin){
glfwMakeContextCurrent(glfwWin);
glfwSwapInterval(0);
glViewport(0,0,W,H);
glClearColor(0,0,0,1);
glShadeModel(GL_SMOOTH);
initBgTex();
glOK=true;
}
}
if(!glOK)
std::cout<<"[Task6] GLFW unavailable — shaded mode disabled.\n";
// Build wireframe geometries
const float cx=(BOARD_COLS-1)/2.f, cy=-float(BOARD_ROWS-1)/2.f;
std::vector<Line3D> wf[4];
wf[0] = buildEiffelTower(cx, cy);
wf[1] = buildDNA(cx, cy);
wf[2] = buildIcosahedron(cx, cy, 1.8f);
wf[3] = buildUFO(cx, cy);
const std::string WIN="Task 6 - Virtual Shapes";
cv::namedWindow(WIN,cv::WINDOW_AUTOSIZE);
cv::setMouseCallback(WIN,onMouse6);
cv::Mat glReadback(H,W,CV_8UC3);
std::cout<<"=== Task 6: Virtual Shapes ===\n"