-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprojectV8.py
1174 lines (954 loc) · 38.7 KB
/
projectV8.py
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
# Import necessary libraries
import pygame as pg
import pyrr.matrix44
from OpenGL.GL import *
import numpy as np
import os
import shaderLoader
import guiV3
import time
from objLoaderV2 import ObjLoader
import cv2
# cvzone is a helpful Computer Vision library from Murtaza Hassan
# Link to the Github documentation for it is in the README
from cvzone.PoseModule import PoseDetector
'====================='
'SET UP CUBEMAP'
'====================='
def load_image(filename, format="RGB", flip=False):
img = pg.image.load(filename)
img_data = pg.image.tobytes(img, format, flip)
w, h = img.get_size()
return img_data, w, h
def create_cubemap_texture(cubemap_paths):
# Generate a texture ID
texture_id = glGenTextures(1)
# Bind the texture as a cubemap
glBindTexture(GL_TEXTURE_CUBE_MAP, texture_id)
# Define texture parameters
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST)
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
# Define the faces of the cubemap
faces = [GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z]
# Load and bind images to the corresponding faces
for i in range(6):
img_data, img_w, img_h = load_image(cubemap_paths[i], format="RGB", flip=False)
glTexImage2D(faces[i], 0, GL_RGB, img_w, img_h, 0, GL_RGB, GL_UNSIGNED_BYTE, img_data)
# Generate mipmaps
glGenerateMipmap(GL_TEXTURE_CUBE_MAP)
# Unbind the texture
glBindTexture(GL_TEXTURE_CUBE_MAP, 0)
return texture_id
'====================='
'SET UP THE WINDOW'
'====================='
# Initialize pygame
pg.init()
# Set up OpenGL context version
pg.display.gl_set_attribute(pg.GL_CONTEXT_MAJOR_VERSION, 3)
pg.display.gl_set_attribute(pg.GL_CONTEXT_MINOR_VERSION, 3)
# Create a window for graphics using OpenGL
width = 480
height = 480
screen = pg.display.set_mode((width, height), pg.OPENGL | pg.DOUBLEBUF)
pg.display.set_caption('Tracking')
# Background color
glClearColor(0.0941, 0.7490, 0.0235, 1.0)
# Enable depth testing and point size
glEnable(GL_DEPTH_TEST)
glEnable(GL_PROGRAM_POINT_SIZE)
# Load in the shader
shader = shaderLoader.compile_shader(
"shaders/vert.glsl", "shaders/frag.glsl")
glUseProgram(shader)
shaderSkybox = shaderLoader.compile_shader("shaders/vert_skybox.glsl",
"shaders/frag_skybox.glsl")
rayman_shader = shaderLoader.compile_shader("shaders/raymanVS.glsl",
"shaders/raymanFS.glsl")
glUseProgram(rayman_shader)
""
"Object 1"
""
obj1 = ObjLoader("objects/raymanHead.obj")
vertices1 = np.array(obj1.vertices, dtype="float32")
center1 = obj1.center
dia1 = obj1.dia
# Sizes
size_position = 3
size_texture = 2
size_normal = 3
float_byte_size = vertices1[0].nbytes
# Byte values
stride = float_byte_size * (size_position + size_texture + size_normal)
offset_position = 0
offset_texture = float_byte_size * size_position
offset_normal = float_byte_size * (size_position + size_texture)
n_vertices1 = len(vertices1) // (size_position + size_texture + size_normal)
# Create the VAO
vao1 = glGenVertexArrays(1)
glBindVertexArray(vao1)
# Create the VBO
vbo1 = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, vbo1)
glBufferData(GL_ARRAY_BUFFER, size=vertices1.nbytes, data=vertices1,
usage=GL_STATIC_DRAW)
# Configure position
position_loc1 = glGetAttribLocation(rayman_shader, "position")
glVertexAttribPointer(index=position_loc1, size=size_position, type=GL_FLOAT,
normalized=GL_FALSE, stride=stride,
pointer=ctypes.c_void_p(offset_position))
glEnableVertexAttribArray(position_loc1)
# Configure normal
normal_loc1 = glGetAttribLocation(rayman_shader, "normal")
glVertexAttribPointer(normal_loc1, size=size_normal, type=GL_FLOAT,
normalized=GL_FALSE, stride=stride,
pointer=ctypes.c_void_p(offset_normal))
glEnableVertexAttribArray(normal_loc1)
# Get variables
scale_loc1 = glGetUniformLocation(rayman_shader, "scale")
# Setting scale to 2 / dia
scale1 = .75 / dia1
glUniform1f(scale_loc1, scale1)
center_loc1 = glGetUniformLocation(rayman_shader, "center")
# Setting center to the model's center.
glUniform3f(center_loc1, center1[0], center1[1], center1[2])
aspect_loc1 = glGetUniformLocation(rayman_shader, "aspect")
# Setting aspect to width / height
aspect = width / height
glUniform1f(aspect_loc1, aspect)
# Model matrix
modelM1 = pyrr.matrix44.create_identity()
# Assignment 4:
transM1 = pyrr.matrix44.create_from_translation([
-center1[0], -center1[1], -center1[2]])
# Get the matrix locations.
model_loc1 = glGetUniformLocation(rayman_shader, "model_matrix")
view_loc1 = glGetUniformLocation(rayman_shader, "view_matrix")
proj_loc1 = glGetUniformLocation(rayman_shader, "proj_matrix")
""
"Object 2"
""
obj2 = ObjLoader("objects/raymanTorso.obj")
vertices2 = np.array(obj2.vertices, dtype="float32")
center2 = obj2.center
dia2 = obj2.dia
float_byte_size = vertices2[0].nbytes
n_vertices2 = len(vertices2) // (size_position + size_texture + size_normal)
# Create the VAO
vao2 = glGenVertexArrays(1)
glBindVertexArray(vao2)
# Create the VBO
vbo2 = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, vbo2)
glBufferData(GL_ARRAY_BUFFER, size=vertices2.nbytes, data=vertices2,
usage=GL_STATIC_DRAW)
# Configure position
position_loc2 = glGetAttribLocation(rayman_shader, "position")
glVertexAttribPointer(index=position_loc2, size=size_position, type=GL_FLOAT,
normalized=GL_FALSE, stride=stride,
pointer=ctypes.c_void_p(offset_position))
glEnableVertexAttribArray(position_loc2)
# Configure normal
normal_loc2 = glGetAttribLocation(rayman_shader, "normal")
glVertexAttribPointer(normal_loc2, size=size_normal, type=GL_FLOAT,
normalized=GL_FALSE, stride=stride,
pointer=ctypes.c_void_p(offset_normal))
glEnableVertexAttribArray(normal_loc2)
# Get variables
scale_loc2 = glGetUniformLocation(rayman_shader, "scale")
# Setting scale to 2 / dia
scale2 = .75 / dia2
glUniform1f(scale_loc2, scale2)
center_loc2 = glGetUniformLocation(rayman_shader, "center")
# Setting center to the model's center.
glUniform3f(center_loc2, center2[0], center2[1], center2[2])
aspect_loc2 = glGetUniformLocation(rayman_shader, "aspect")
# Setting aspect to width / height
aspect = width / height
glUniform1f(aspect_loc2, aspect)
# Model matrix
modelM2 = pyrr.matrix44.create_identity()
# Assignment 4:
transM2 = pyrr.matrix44.create_from_translation([
-center2[0], -center2[1], -center2[2]])
# Get the matrix locations.
model_loc2 = glGetUniformLocation(rayman_shader, "model_matrix")
view_loc2 = glGetUniformLocation(rayman_shader, "view_matrix")
proj_loc2 = glGetUniformLocation(rayman_shader, "proj_matrix")
""
"Object 3"
""
obj3 = ObjLoader("objects/raymanLeftHand.obj")
vertices3 = np.array(obj3.vertices, dtype="float32")
center3 = obj3.center
dia3 = obj3.dia
float_byte_size3 = vertices3[0].nbytes
n_vertices3 = len(vertices3) // (size_position + size_texture + size_normal)
# Create the VAO
vao3 = glGenVertexArrays(1)
glBindVertexArray(vao3)
# Create the VBO
vbo3 = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, vbo3)
glBufferData(GL_ARRAY_BUFFER, size=vertices3.nbytes, data=vertices3,
usage=GL_STATIC_DRAW)
# Configure position
position_loc3 = glGetAttribLocation(rayman_shader, "position")
glVertexAttribPointer(index=position_loc3, size=size_position, type=GL_FLOAT,
normalized=GL_FALSE, stride=stride,
pointer=ctypes.c_void_p(offset_position))
glEnableVertexAttribArray(position_loc3)
# Configure normal
normal_loc3 = glGetAttribLocation(rayman_shader, "normal")
glVertexAttribPointer(normal_loc3, size=size_normal, type=GL_FLOAT,
normalized=GL_FALSE, stride=stride,
pointer=ctypes.c_void_p(offset_normal))
glEnableVertexAttribArray(normal_loc3)
# Get variables
scale_loc3 = glGetUniformLocation(rayman_shader, "scale")
# Setting scale to 2 / dia
scale3 = .75 / dia3
glUniform1f(scale_loc3, scale3)
center_loc3 = glGetUniformLocation(rayman_shader, "center")
# Setting center to the model's center.
glUniform3f(center_loc3, center3[0], center3[1], center3[2])
aspect_loc3 = glGetUniformLocation(rayman_shader, "aspect")
# Setting aspect to width / height
aspect = width / height
glUniform1f(aspect_loc3, aspect)
# Model matrix
modelM3 = pyrr.matrix44.create_identity()
# Assignment 4:
transM3 = pyrr.matrix44.create_from_translation([
-center3[0], -center3[1], -center3[2]])
# Get the matrix locations.
model_loc3 = glGetUniformLocation(rayman_shader, "model_matrix")
view_loc3 = glGetUniformLocation(rayman_shader, "view_matrix")
proj_loc3 = glGetUniformLocation(rayman_shader, "proj_matrix")
""
"Object 4"
""
obj4 = ObjLoader("objects/raymanRightHand.obj")
vertices4 = np.array(obj4.vertices, dtype="float32")
center4 = obj4.center
dia4 = obj4.dia
float_byte_size4 = vertices4[0].nbytes
n_vertices4 = len(vertices4) // (size_position + size_texture + size_normal)
# Create the VAO
vao4 = glGenVertexArrays(1)
glBindVertexArray(vao4)
# Create the VBO
vbo4 = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, vbo4)
glBufferData(GL_ARRAY_BUFFER, size=vertices4.nbytes, data=vertices4,
usage=GL_STATIC_DRAW)
# Configure position
position_loc4 = glGetAttribLocation(rayman_shader, "position")
glVertexAttribPointer(index=position_loc4, size=size_position, type=GL_FLOAT,
normalized=GL_FALSE, stride=stride,
pointer=ctypes.c_void_p(offset_position))
glEnableVertexAttribArray(position_loc4)
# Configure normal
normal_loc4 = glGetAttribLocation(rayman_shader, "normal")
glVertexAttribPointer(normal_loc4, size=size_normal, type=GL_FLOAT,
normalized=GL_FALSE, stride=stride,
pointer=ctypes.c_void_p(offset_normal))
glEnableVertexAttribArray(normal_loc4)
# Get variables
scale_loc4 = glGetUniformLocation(rayman_shader, "scale")
# Setting scale to 2 / dia
scale4 = .75 / dia4
glUniform1f(scale_loc4, scale4)
center_loc4 = glGetUniformLocation(rayman_shader, "center")
# Setting center to the model's center.
glUniform3f(center_loc4, center4[0], center4[1], center4[2])
aspect_loc4 = glGetUniformLocation(rayman_shader, "aspect")
# Setting aspect to width / height
aspect = width / height
glUniform1f(aspect_loc4, aspect)
# Model matrix
modelM4 = pyrr.matrix44.create_identity()
# Assignment 4:
transM4 = pyrr.matrix44.create_from_translation([
-center4[0], -center4[1], -center4[2]])
# Get the matrix locations.
model_loc4 = glGetUniformLocation(rayman_shader, "model_matrix")
view_loc4 = glGetUniformLocation(rayman_shader, "view_matrix")
proj_loc4 = glGetUniformLocation(rayman_shader, "proj_matrix")
""
"Object 5"
""
obj5 = ObjLoader("objects/raymanLeftFoot.obj")
vertices5 = np.array(obj5.vertices, dtype="float32")
center5 = obj5.center
dia5 = obj5.dia
float_byte_size5 = vertices5[0].nbytes
n_vertices5 = len(vertices5) // (size_position + size_texture + size_normal)
# Create the VAO
vao5 = glGenVertexArrays(1)
glBindVertexArray(vao5)
# Create the VBO
vbo5 = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, vbo5)
glBufferData(GL_ARRAY_BUFFER, size=vertices5.nbytes, data=vertices5,
usage=GL_STATIC_DRAW)
# Configure position
position_loc5 = glGetAttribLocation(rayman_shader, "position")
glVertexAttribPointer(index=position_loc5, size=size_position, type=GL_FLOAT,
normalized=GL_FALSE, stride=stride,
pointer=ctypes.c_void_p(offset_position))
glEnableVertexAttribArray(position_loc5)
# Configure normal
normal_loc5 = glGetAttribLocation(rayman_shader, "normal")
glVertexAttribPointer(normal_loc5, size=size_normal, type=GL_FLOAT,
normalized=GL_FALSE, stride=stride,
pointer=ctypes.c_void_p(offset_normal))
glEnableVertexAttribArray(normal_loc5)
# Get variables
scale_loc5 = glGetUniformLocation(rayman_shader, "scale")
# Setting scale to 2 / dia
scale5 = .75 / dia5
glUniform1f(scale_loc5, scale5)
center_loc5 = glGetUniformLocation(rayman_shader, "center")
# Setting center to the model's center.
glUniform3f(center_loc5, center5[0], center5[1], center5[2])
aspect_loc5 = glGetUniformLocation(rayman_shader, "aspect")
# Setting aspect to width / height
aspect = width / height
glUniform1f(aspect_loc5, aspect)
# Model matrix
modelM5 = pyrr.matrix44.create_identity()
# Assignment 4:
transM5 = pyrr.matrix44.create_from_translation([
-center5[0], -center5[1], -center5[2]])
# Get the matrix locations.
model_loc5 = glGetUniformLocation(rayman_shader, "model_matrix")
view_loc5 = glGetUniformLocation(rayman_shader, "view_matrix")
proj_loc5 = glGetUniformLocation(rayman_shader, "proj_matrix")
""
"Object 6"
""
obj6 = ObjLoader("objects/raymanRightFoot.obj")
vertices6 = np.array(obj6.vertices, dtype="float32")
center6 = obj6.center
dia6 = obj6.dia
float_byte_size6 = vertices6[0].nbytes
n_vertices6 = len(vertices6) // (size_position + size_texture + size_normal)
# Create the VAO
vao6 = glGenVertexArrays(1)
glBindVertexArray(vao6)
# Create the VBO
vbo6 = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, vbo6)
glBufferData(GL_ARRAY_BUFFER, size=vertices6.nbytes, data=vertices6,
usage=GL_STATIC_DRAW)
# Configure position
position_loc6 = glGetAttribLocation(rayman_shader, "position")
glVertexAttribPointer(index=position_loc6, size=size_position, type=GL_FLOAT,
normalized=GL_FALSE, stride=stride,
pointer=ctypes.c_void_p(offset_position))
glEnableVertexAttribArray(position_loc6)
# Configure normal
normal_loc6 = glGetAttribLocation(rayman_shader, "normal")
glVertexAttribPointer(normal_loc6, size=size_normal, type=GL_FLOAT,
normalized=GL_FALSE, stride=stride,
pointer=ctypes.c_void_p(offset_normal))
glEnableVertexAttribArray(normal_loc6)
# Get variables
scale_loc6 = glGetUniformLocation(rayman_shader, "scale")
# Setting scale to 2 / dia
scale6 = .75 / dia6
glUniform1f(scale_loc6, scale6)
center_loc6 = glGetUniformLocation(rayman_shader, "center")
# Setting center to the model's center.
glUniform3f(center_loc6, center6[0], center6[1], center6[2])
aspect_loc6 = glGetUniformLocation(rayman_shader, "aspect")
# Setting aspect to width / height
aspect = width / height
glUniform1f(aspect_loc6, aspect)
# Model matrix
modelM6 = pyrr.matrix44.create_identity()
# Assignment 4:
transM6 = pyrr.matrix44.create_from_translation([
-center6[0], -center6[1], -center6[2]])
# Get the matrix locations.
model_loc6 = glGetUniformLocation(rayman_shader, "model_matrix")
view_loc6 = glGetUniformLocation(rayman_shader, "view_matrix")
proj_loc6 = glGetUniformLocation(rayman_shader, "proj_matrix")
'====================='
'SET UP SKYBOX STUFFS'
'====================='
# Ask the user if they want a green screen or the environment.
drawingSkybox = input("Do you want to capture the environment? Yes or "
"No?\n")
if drawingSkybox == "Yes":
drawingSkybox = True
else:
drawingSkybox = False
print("Capturing images shortly...")
time.sleep(2)
#drawingSkybox = 0
cubemap_paths = [
"skybox/left.png",
"skybox/left.png",
"skybox/left.png",
"skybox/left.png",
"skybox/left.png",
"skybox/left.png"
]
cubemap_texture = create_cubemap_texture(cubemap_paths)
size_position = 3
# Define the vertices of the quad.
quad_vertices = (
# Position
-1, -1,
1, -1,
1, 1,
1, 1,
-1, 1,
-1, -1
)
skybox_vertices = np.array(quad_vertices, dtype=np.float32)
skybox_size_position = 2 # x, y, z
skybox_stride = skybox_size_position * 4
skybox_offset_position = 0
quad_n_vertices = len(skybox_vertices) // skybox_size_position # number of vertices
vao_quad = glGenVertexArrays(1)
glBindVertexArray(vao_quad)
vbo_quad = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, vbo_quad)
quad_n_vertices = len(skybox_vertices) // skybox_size_position
glBufferData(GL_ARRAY_BUFFER,
skybox_vertices.nbytes,
skybox_vertices,
GL_STATIC_DRAW)
glVertexAttribPointer(
index=0, # index of normal
size=skybox_size_position, # x, y, z, # number of position components
type=GL_FLOAT, # type of the components
normalized=GL_FALSE, # data is/is not normalized
stride=skybox_stride, # number of bytes between verticies
pointer=ctypes.c_void_p(skybox_offset_position) # pointer of the beginning of the position data
)
glEnableVertexAttribArray(0)
glUseProgram(shaderSkybox)
view_loc = glGetUniformLocation(shader, "view_matrix")
proj_loc = glGetUniformLocation(shader, "proj_matrix")
'====================='
'SET UP RENDERING PIPELINE'
'====================='
# Generate the VAO
vao = glGenVertexArrays(1)
glBindVertexArray(vao)
# Create the VBO
vbo = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, vbo)
# Configure position
position_loc = glGetAttribLocation(shader, "position")
glVertexAttribPointer(index=position_loc, size=2, type=GL_FLOAT,
normalized=GL_FALSE, stride=8,
pointer=ctypes.c_void_p(0))
glEnableVertexAttribArray(position_loc)
# Get the matrix locations.
cube_map_loc = glGetUniformLocation(shaderSkybox, "cubeMapTex")
inv_proj_loc = glGetUniformLocation(shaderSkybox, "invViewProjectionMatrix")
'====================='
'CAPTURE VIDEO'
'====================='
# Getting the video
capture = cv2.VideoCapture(0)
if not capture.isOpened():
print("Cannot open camera")
exit()
# Getting the pose
detector = PoseDetector()
'====================='
'GET THE POINT POSITIONS AND DRAW THE MODEL'
'====================='
'====================='
'VIDEO CAPTURE AND DRAW LOOP'
'====================='
cameraFOV = 130.0
lmString3D = ''
lmString2D = ''
# For storing the calibration data.
calibratedPose = False
takenPicture = False
startedTimer = False
startedTimerPicture = False
inFrame = False
notInFrame = True
timer = time.time_ns()
second = 1
calibList2D = np.array([0]*66, dtype="float32")
calibList3D = np.array([0]*99, dtype="float32")
calibratedHeadDistance = 0
headTurnDir = [width/2, width/2]
headTurning = 0
calibratedBodyDistance = 0
bodyTurnDir = [width/2, width/2]
bodyTurning = 0
calibratedLHDistance = 0
LHTurnDir = [width/2, width/2]
LHTurning = 0
RHTurning = 0
scaleConv = 0
shoulderWidth = 0
turningBody = False
rotY1 = 0
rotY2 = 0
rotY3 = 0
rotZ3 = 0
rotY4 = 0
rotY5 = 0
rotY6 = 0
rotationX = 0
rotationY = 180
rotationZ = 0
# Run a loop to keep the program running
draw = True
while draw:
# Quit out if need be
for event in pg.event.get():
if event.type == pg.QUIT:
draw = False
'---------------------'
'VIDEO CAPTURE'
'---------------------'
# Read in the video and store its data
success, readImg = capture.read()
img = detector.findPose(readImg)
lmList, bboxInfo = detector.findPosition(img)
pointList2D = []
pointList3D = []
# print("Height:", img.shape[0], "Width:", img.shape[0])
if bboxInfo:
lmString3D = ''
lmString2D = ''
for lm in lmList:
lmString3D += f'{lm[0]},{img.shape[0] - lm[1]},{lm[2]},'
lmString2D += f'{lm[0]},{img.shape[0] - lm[1]},'
cv2.imshow("Image", img)
cv2.waitKey(1)
# Once here, we have the position data of a full frame in posList2D.
startI = 0
endI = startI
if lmString2D != "":
while endI < len(lmString2D) and lmString2D[endI] != ',':
endI += 1
if endI < len(lmString2D) and lmString2D[endI] == ',':
pointList2D.append(float(lmString2D[startI:endI]))
startI = endI + 1
endI = startI
# Once here, we have the position data of a full frame in posList3D.
startI = 0
endI = startI
if lmString3D != "":
while endI < len(lmString3D) and lmString3D[endI] != ',':
endI += 1
if endI < len(lmString3D) and lmString3D[endI] == ',':
pointList3D.append(float(lmString3D[startI:endI]))
startI = endI + 1
endI = startI
# Once here, pointList is a list of all points in the given frame.
# 66 elements
fPointList = np.array(pointList2D, dtype="float32")
# 99 elements
fPointList3D = np.array(pointList3D, dtype="float32")
'---------------------'
'CALIBRATION'
'---------------------'
if drawingSkybox:
# Check if frame is empty (no human detected)
frameIsEmpty = len(lmList) == 0
# Taking the initial background picture:
if not takenPicture:
backgroundImg = readImg
# Timer to take picture when frame is empty
if not startedTimerPicture and not takenPicture:
oldTimer = timer
second = 1
print("Initializing actor's environment in...")
startedTimerPicture = True
if startedTimerPicture and not takenPicture:
timer = time.time_ns()
deltaTime = (timer - oldTimer) / 1e9
if deltaTime >= second and second <= 5:
print("0:00:0" + str(6 - second) + "...")
second += 1
if second > 5:
startedTimerPicture = False
if not startedTimerPicture:
if not frameIsEmpty and not startedTimer:
print("Please make sure nobody is visible in frame! Restarting "
"timer...")
second = 1
elif frameIsEmpty and not takenPicture:
print("Picture taken!")
takenPicture = True
# Once this flag flips, the backgroundImg will stay constant.
os.chdir('skybox')
backgroundImg = cv2.flip(backgroundImg, 1)
backgroundImg = backgroundImg[:, 80:560]
cv2.imwrite("left.png", backgroundImg)
cubemap_paths = [
"left.png",
"left.png",
"left.png",
"left.png",
"left.png",
"left.png"
]
# print("Initializing image...")
# time.sleep(3)
# print("Image intialized!")
cubemap_texture = create_cubemap_texture(cubemap_paths)
if not takenPicture:
continue
# Taking the initial calibration picture:
if not calibratedPose:
# Calibration timer
if not startedTimer and not calibratedPose:
oldTimer = timer
second = 1
print("Taking picture of the actor in...")
startedTimer = True
if startedTimer and not calibratedPose:
cutoffTime = 10
timer = time.time_ns()
deltaTime = (timer - oldTimer) / 1e9
if deltaTime >= second and second <= cutoffTime:
print("0:00:0"+str(cutoffTime+1-second)+"...")
second += 1
if second > cutoffTime:
startedTimer = False
if not startedTimer:
if len(fPointList) < 66:
print("No data points found! Make sure you are in frame!")
continue
elif fPointList[63] < 0 and fPointList[65] < 0:
print("Please make sure your entire body is in frame!")
continue
elif fPointList[63] >= 0 and fPointList[65] >= 0:
inFrame = True
#print(fPointList[63], fPointList[65])
if not calibratedPose and inFrame:
calibList2D = fPointList
calibList3D = fPointList3D
calibratedBodyDistance = abs(calibList2D[24] - calibList2D[22])
shoulderWidth = calibratedBodyDistance
print("Calibration complete!")
calibratedPose = True
#prevents from crashing if no data points are found
if len(fPointList) < 66:
continue
# Shift all x coordinates over by 80.
for i in range(len(fPointList)):
if i % 2 == 0:
fPointList[i] -= 80
for i in range(len(fPointList3D)):
if i % 3 == 0:
fPointList3D[i] -= 80
# Some of the turning is experimental and wasn't included in the motion
# of the final product. We left it here for progress' sake.
'---------------'
'Head Angle'
'---------------'
if calibratedPose:
# Using simulated depth to know the turn direction
oldHeadAngle = headTurnDir
headTurnDir = (fPointList3D[23], fPointList3D[26])
# Using shoulder distance to get turning amount.
oldHeadTurning = headTurning
headTurning = np.sqrt((fPointList[16] - fPointList[14]) ** 2 +
(fPointList[17] - fPointList[15]) ** 2)
# If currently turning...
if abs(headTurning - oldHeadTurning) > 1.0:
# If turning left
if (headTurnDir[0] - oldHeadAngle[0] > 0 and headTurnDir[1] -
oldHeadAngle[1] < 0):
rotY1 += 5
# If turning right
elif (headTurnDir[0] - oldHeadAngle[0] < 0 and headTurnDir[1] -
oldHeadAngle[1] > 0):
rotY1 -= 5
# If turned too much:
if rotY1 > 85:
rotY1 = 85
if rotY1 < -85:
rotY1 = -85
'---------------'
'Body Angle'
'---------------'
if calibratedPose:
# Using simulated depth to know the turn direction
oldBodyAngle = bodyTurnDir
bodyTurnDir = (fPointList3D[35], fPointList3D[38])
# Using shoulder distance to get turning amount.
oldBodyTurning = bodyTurning
bodyTurning = np.sqrt((fPointList[24] - fPointList[22]) ** 2 +
(fPointList[25] - fPointList[23]) ** 2)
# Estimated shoulder width
shoulderWidth = abs(fPointList[24] - fPointList[22])# / np.cos(rotY2)
# If currently turning...
if abs(bodyTurning - oldBodyTurning) > 1.5:
# If turning left
if (bodyTurnDir[0] - oldBodyAngle[0] > 0 and bodyTurnDir[1] -
oldBodyAngle[1] < 0):
rotY2 += 5
# If turning right
elif (bodyTurnDir[0] - oldBodyAngle[0] < 0 and bodyTurnDir[1] -
oldBodyAngle[1] > 0):
rotY2 -= 5
# If turned too much:
if rotY2 > 85:
rotY2 = 85
if rotY2 < -85:
rotY2 = -85
'---------------'
'Left Hand Angle'
'---------------'
if calibratedPose:
LHTurning = (fPointList[41] - fPointList[33])
rotY3 = LHTurning
'---------------'
'Right Hand Angle'
'---------------'
if calibratedPose:
RHTurning = (fPointList[39] - fPointList[31])
rotY4 = RHTurning
'---------------'
'Left Foot Angle'
'---------------'
if calibratedPose:
LFTurning = (fPointList[64] - fPointList[30])
rotY5 = -LFTurning
'---------------'
'Right Foot Angle'
'---------------'
if calibratedPose:
RFTurning = (fPointList[62] - fPointList[29])
rotY6 = RFTurning
'==============='
'Drawing'
'==============='
# Clear color buffer and depth buffer before drawing each frame
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glUseProgram(shader)
'---------------'
'Camera'
'---------------'
if shoulderWidth == 0 or calibratedBodyDistance == 0:
calibConv = 1
else:
calibConv = shoulderWidth / calibratedBodyDistance
# Compute camera matrices
eye = [(width/2), height/2, 10 * width/(4.45)] # 4.5
# Change target from center to [0, 0, 0].
view_matrix = pyrr.matrix44.create_look_at(
eye, [width/2, height/2, 0], np.asarray([0, 1, 0]))
glUniformMatrix4fv(view_loc, 1, GL_FALSE, view_matrix)
proj_matrix = pyrr.matrix44.create_perspective_projection_matrix(
cameraFOV, width/height, 0.1, 1000)
glUniformMatrix4fv(proj_loc, 1, GL_FALSE, proj_matrix)
'---------------------'
'Drawing Points'
'---------------------'
# # Draw Points
# glUseProgram(shader)
# glBindVertexArray(vao)
#
# # Using fPointList, aka the list of 2D points, to draw the points.
# glBufferData(GL_ARRAY_BUFFER, size=calibList2D.nbytes,
# data=calibList2D, usage=GL_STATIC_DRAW) # calibList2D
#
#
# glDrawArrays(GL_POINTS, 0, len(fPointList) // 2)
'==============='
'Drawing Rayman'
'==============='
# obj1 is rayman's head
# obj2 is rayman's torso
# obj3 is rayman's left hand
'---------------'
'Drawing Raymans Head'
'---------------'
# Draw Rayman
glUseProgram(rayman_shader)
scale1 = (.75 / dia1) * calibConv
# Compute matrices
rPosC = -center1
rPosC[0] = -(rPosC[0] + (22 * (fPointList[0] / float(width))) - 11) + 1
rPosC[1] = (rPosC[1] + (22 * (fPointList[1] / float(height))) - 11) #20, 10
#rPosC[2] = -(rPosC[2] + 5 * (1 - calibConv))
scaleM1 = pyrr.matrix44.create_from_scale(np.array([scale1,
scale1,
scale1]))
rotateZM1 = pyrr.matrix44.create_from_z_rotation(np.deg2rad(0))
rotateYM1 = pyrr.matrix44.create_from_y_rotation(np.deg2rad(180))# + rotY1))
rotateXM1 = pyrr.matrix44.create_from_x_rotation(np.deg2rad(0))
transM1 = pyrr.matrix44.create_from_translation(rPosC)
model_matrix1 = pyrr.matrix44.multiply(transM1,
pyrr.matrix44.multiply(rotateXM1,
pyrr.matrix44.multiply(rotateYM1,
pyrr.matrix44.multiply(rotateZM1,
scaleM1))))
glUniformMatrix4fv(model_loc1, 1, GL_FALSE, model_matrix1)
glBindVertexArray(vao1)
glDrawArrays(GL_TRIANGLES, 0, n_vertices1)
'---------------'
'Drawing Raymans Body'
'---------------'
scale2 = (.65 / dia2) * calibConv
rPosC2 = -center2
rPosC2[0] = -(rPosC2[0] + (15 * (((fPointList[46] + fPointList[48]) / 2) /
float(width))) - 8) # 10, 5
rPosC2[1] = (rPosC2[1] + (20 * (((fPointList[47] + fPointList[49]) / 2) /
float(height))) - 9) # 10
rPosC2[2] = rPosC2[2] - 1
# Compute matrices
scaleM2 = pyrr.matrix44.create_from_scale(np.array([scale2,
scale2,
scale2]))