-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathglViewer.py
5236 lines (3979 loc) · 180 KB
/
glViewer.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
'''
This code has opengl visualization of 3D skeletons, including floor visualization and mouse+keyboard control
See the main function for demo codes.
showSkeleton: visualize 3D human body skeletons. This can handle holden's formation, 3.6m formation, and domeDB
setSpeech: to set speechAnnotation. Should be called before showSkeleton
renderscene: main function to render scenes using openGL
Note: this visualizer is assuming centimeter metric (joint, mesh).
Hanbyul Joo ([email protected])
'''
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import json
import numpy as np
from PIL import Image, ImageOps
import numpy as np
import sys, math
import threading
import time
#from time import time
#import pickle
#import cPickle as pickle
import _pickle as pickle
#-----------
# Global Variables
#-----------
#g_camView_fileName = '/ssd/camInfo/camInfo.pkl'
g_camView_fileName = './camInfo/camInfo.pkl'
g_fViewDistance = 50.
# g_Width = 1280
# g_Height = 720
# g_Width = 1920
# g_Height = 1080
g_Width = 1000
g_Height = 1280
g_nearPlane = 0.01 #original
# g_nearPlane = 1000 #cameramode
g_farPlane = 5000.
g_show_fps = False
g_action = ""
g_xMousePtStart = g_yMousePtStart = 0.
g_xTrans = 0.
g_yTrans = 0.
g_zTrans = 0.
g_zoom = 434
g_xRotate = -44.
g_yRotate = -39.
g_zRotate = 0.
g_xrot = 0.0
g_yrot = 0.0
# the usual screen dimension variables and lighting
# Generic Lighting values and coordinates
g_ambientLight = (0.35, 0.35, 0.35, 1.0)
g_diffuseLight = (0.75, 0.75, 0.75, 0.7)
g_specular = (0.2, 0.2, 0.2, 1.0)
g_specref = (0.5, 0.5, 0.5, 1.0)
# To visualize in Dome View point
#g_viewMode = 'camView'#free' #'camView'
g_viewMode = 'free' #'camView'
g_bOrthoCam = False #If true, draw by ortho camera mode
from collections import deque
#g_camid = deque('00',maxlen=2)
g_camid = deque('27',maxlen=2)
g_onlyDrawHumanIdx = -1
# To save rendered scene into file
g_stopMainLoop = False
g_winID = None
g_bSaveToFile = False
g_bSaveToFile_done = False #If yes, file has been saved
# g_savedImg = None # Keep the save image as a global variable, if ones want to obtain this
# g_haggling_render = False #Automatically Load next seqeucne, when frames are done
g_bSaveOnlyMode = False #If true, load camera turn on save mode and exit after farmes are done
g_saveFolderName = None
"Visualization Options"
g_bApplyRootOffset = False
ROOT_OFFSET_DIST = 160
#ROOT_OFFSET_DIST = 30
""" Mesh Drawing Option """
#A tutorial for VAO and VBO: https://www.khronos.org/opengl/wiki/Tutorial2:_VAOs,_VBOs,_Vertex_and_Fragment_Shaders_(C_/_SDL)
g_vao= None
g_vertex_buffer= None
g_normal_buffer= None
g_tangent_buffer = None
g_index_buffer = None
g_hagglingSeqName= None
# global vts_num
# global SMPL_vts
# global face_num
# global SMPLModel
g_saveFrameIdx = 0
# global MoshParam
g_bGlInitDone = False #To initialize opengl only once
BACKGROUND_IMAGE_PLANE_DEPTH=3000
######################################################
# MTC Camera view
g_camView_K = None
g_camView_K_list = None #if each mesh uses diff K.... very ugly
g_bShowBackground = True
g_backgroundTextureID = None
g_textureData = None
# g_textureImgOriginal = None #Original Size
g_renderOutputSize = None #(width, height) If not None, crop the output to this size
######################################################
# Visualizeation Option
g_bShowFloor = True
g_bShowWiredMesh = False
g_bRotateView = False #Rotate view 360 degrees
g_rotateView_counter = 0
g_rotateInterval = 2
g_bShowSkeleton = True
g_bShowMesh = True
########################################################
# 3D keypoints Visualization Setting
g_skeletons = None # list of np.array. skeNum x (skelDim, skelFrames)
g_skeletons_GT = None
g_trajectory = None # list of np.array. skeNum x (trajDim:3, skelFrames)
HOLDEN_DATA_SCALING = 5
g_faces = None #(faceNum, skelDim, skelFrames)
g_faceNormals = None
g_bodyNormals = None
g_hands = None #(handNum, skelDim, skelFrames)
g_hands_left = None
g_hands_right = None
g_posOnly = None
g_meshes = None
g_cameraPoses = None
g_cameraRots = None
g_ptCloud =None
g_ptCloudColor =None
g_ptSize = 2
g_frameLimit = -1
g_frameIdx = 0
g_lastframetime = g_currenttime = time.time()
g_fps = 0
g_speech = None # list of np.array. humanNum x speechUnit, where elmenet is a dict with 'indicator', 'word', 'root' with a size of (1, skelFrames)
g_speechGT = None # list of np.array. humanNum x speechUnit, , where elmenet is a dict with 'indicator', 'word', 'root' with a size of (1, skelFrames)
# Original
# g_colors = [ (0, 255, 127), (209, 206, 0), (0, 0, 128), (153, 50, 204), (60, 20, 220),
# (0, 128, 0), (180, 130, 70), (147, 20, 255), (128, 128, 240), (154, 250, 0), (128, 0, 0),
# (30, 105, 210), (0, 165, 255), (170, 178, 32), (238, 104, 123)]
# g_colors = [ (0, 255, 127), (170, 170, 0), (0, 0, 128), (153, 50, 204), (60, 20, 220),
# (0, 128, 0), (180, 130, 70), (147, 20, 255), (128, 128, 240), (154, 250, 0), (128, 0, 0),
# (30, 105, 210), (0, 165, 255), (170, 178, 32), (238, 104, 123)]
# g_colors = [ (250,0,0), (255,0, 0 ), (0, 255, 127), (209, 206, 0), (0, 0, 128), (153, 50, 204), (60, 20, 220),
# (0, 128, 0), (180, 130, 70), (147, 20, 255), (128, 128, 240), (154, 250, 0), (128, 0, 0),
# (30, 105, 210), (0, 165, 255), (170, 178, 32), (238, 104, 123)]
#GT visualization (by red)
g_colors = [ (255,0,0), (0, 255, 127), (170, 170, 0), (0, 0, 128), (153, 50, 204), (60, 20, 220),
(0, 128, 0), (180, 130, 70), (147, 20, 255), (128, 128, 240), (154, 250, 0), (128, 0, 0),
(30, 105, 210), (0, 165, 255), (170, 178, 32), (238, 104, 123)]
#Custom (non brl) originalPanoptic data
g_colors = [(0, 255, 127), (255,0,0), (170, 170, 0), (0, 0, 128), (153, 50, 204), (60, 20, 220),
(0, 128, 0), (180, 130, 70), (147, 20, 255), (128, 128, 240), (154, 250, 0), (128, 0, 0),
(30, 105, 210), (0, 165, 255), (170, 178, 32), (238, 104, 123)]
#RGB order!!
g_colors = [ (0,0,255), (255,0,0), (0, 255, 127), (170, 170, 0), (0, 0, 128), (153, 50, 204), (60, 20, 220),
(0, 128, 0), (180, 130, 70), (147, 20, 255), (128, 128, 240), (154, 250, 0), (128, 0, 0),
(30, 105, 210), (0, 165, 255), (170, 178, 32), (238, 104, 123)]
# g_colors = [(0, 255, 127), (255,0,0), (170, 170, 0) , (0, 0, 128), (153, 50, 204), (60, 20, 220),
# (0, 128, 0), (180, 130, 70), (147, 20, 255), (128, 128, 240), (154, 250, 0), (128, 0, 0),
# (30, 105, 210), (0, 165, 255), (170, 178, 32), (238, 104, 123)]
import timeit
#######################################v
# Parametric Mesh Models
g_faceModel = None
########################################################3
# Opengl Setting
def init():
#global width
#global height
glClearColor(1.0, 1.0, 1.0, 1.0)
# Enable depth testing
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LESS)
#glShadeModel(GL_FLAT)
glShadeModel(GL_SMOOTH)
glEnable(GL_LIGHTING)
glLightfv(GL_LIGHT0, GL_AMBIENT, g_ambientLight)
glLightfv(GL_LIGHT0, GL_DIFFUSE, g_diffuseLight)
glLightfv(GL_LIGHT0, GL_SPECULAR, g_specular)
glEnable(GL_LIGHT0)
glEnable(GL_COLOR_MATERIAL)
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE)
glMaterialfv(GL_FRONT, GL_SPECULAR, g_specref)
glMateriali(GL_FRONT, GL_SHININESS, 128)
# # #Mesh Rendering
global g_vao
global g_vertex_buffer
global g_normal_buffer
global g_tangent_buffer
global g_index_buffer
g_vao = glGenVertexArrays(1)
# glBindVertexArray(g_vao)
g_vertex_buffer = glGenBuffers(40)
# #glBindBuffer(GL_ARRAY_BUFFER, g_vertex_buffer)
g_normal_buffer = glGenBuffers(40)
# #glBindBuffer(GL_ARRAY_BUFFER, g_normal_buffer)
g_tangent_buffer = glGenBuffers(40)
# #glBindBuffer(GL_ARRAY_BUFFER, g_tangent_buffer)
g_index_buffer = glGenBuffers(40)
# # #glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_index_buffer)
#Create Background Texture
global g_backgroundTextureID
glPixelStorei(GL_UNPACK_ALIGNMENT, 1) #So that texture doesnt have to be power of 2
g_backgroundTextureID = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, g_backgroundTextureID)
# glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, 1920, 1080, 0, GL_RGB, GL_FLOAT, 0)
#glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
# glEnable(GL_CULL_FACE)
# glCullFace(GL_BACK)
def init_minimum():
#global width
#global height
glClearColor(1.0, 1.0, 1.0, 1.0)
# Enable depth testing
glEnable(GL_DEPTH_TEST)
glEnable(GL_LIGHTING)
glLightfv(GL_LIGHT0, GL_AMBIENT, g_ambientLight)
glLightfv(GL_LIGHT0, GL_DIFFUSE, g_diffuseLight)
glLightfv(GL_LIGHT0, GL_SPECULAR, g_specular)
glEnable(GL_LIGHT0)
glEnable(GL_COLOR_MATERIAL)
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE)
glMaterialfv(GL_FRONT, GL_SPECULAR, g_specref)
glMateriali(GL_FRONT, GL_SHININESS, 128)
"""Render text on bottom left corner"""
def RenderText(testStr):
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glLoadIdentity()
gluOrtho2D(0.0, glutGet(GLUT_WINDOW_WIDTH), 0.0, glutGet(GLUT_WINDOW_HEIGHT))
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glLoadIdentity()
# glRasterPos2i(10, 10)
glPushMatrix()
glColor4f(1,1,1,1)
glClearDepth(1)
if testStr is not None:
glRasterPos2d(5,5)
for c in testStr:
#glutBitmapCharacter(GLUT_BITMAP_8_BY_13, ord(c))
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, ord(c))
raster_pos = glGetIntegerv(GL_CURRENT_RASTER_POSITION)
glPopMatrix()
glClearDepth(0.99)
glColor4f(.3,.3,.3,.8)
glPushMatrix()
glBegin(GL_QUADS)
glVertex2i(0, 25)
glVertex2i(raster_pos[0]+5, 25)
glVertex2i(raster_pos[0]+5, 0)
glVertex2i(0, 0)
glEnd()
glPopMatrix()
glClearDepth(1)
glMatrixMode(GL_PROJECTION)
glPopMatrix()
glMatrixMode(GL_MODELVIEW)
glPopMatrix()
def RenderDomeFloor():
glPolygonMode(GL_FRONT, GL_FILL)
glPolygonMode(GL_BACK, GL_FILL)
# glPolygonMode(GL_FRONT, GL_FILL)
gridNum = 10
width = 200
halfWidth =width/2
# g_floorCenter = np.array([0,0.5,0])
# g_floorCenter = np.array([0,500,0])
g_floorCenter = np.array([0,100,0])
g_floorAxis1 = np.array([1,0,0])
g_floorAxis2 = np.array([0,0,1])
origin = g_floorCenter - g_floorAxis1*(width*gridNum/2 ) - g_floorAxis2*(width*gridNum/2)
axis1 = g_floorAxis1 * width
axis2 = g_floorAxis2 * width
for y in range(gridNum+1):
for x in range(gridNum+1):
if (x+y) % 2 ==0:
glColor(1.0,1.0,1.0,1.0) #white
else:
# glColor(0.95,0.95,0.95,0.3) #grey
glColor(0.3,0.3,0.3,0.5) #grey
p1 = origin + axis1*x + axis2*y
p2 = p1+ axis1
p3 = p1+ axis2
p4 = p1+ axis1 + axis2
glBegin(GL_QUADS)
glVertex3f( p1[0], p1[1], p1[2])
glVertex3f( p2[0], p2[1], p2[2])
glVertex3f( p4[0], p4[1], p4[2])
glVertex3f( p3[0], p3[1], p3[2])
glEnd()
def setFree3DView():
glTranslatef(0,0,g_zoom)
glRotatef( -g_yRotate, 1.0, 0.0, 0.0)
glRotatef( -g_xRotate, 0.0, 1.0, 0.0)
glRotatef( g_zRotate, 0.0, 0.0, 1.0)
glTranslatef( g_xTrans, 0.0, 0.0 )
glTranslatef( 0.0, g_yTrans, 0.0)
glTranslatef( 0.0, 0, g_zTrans)
# g_hdCams = None
# def load_panoptic_cameras():
# global g_hdCams
# with open('/media/posefs3b/Users/xiu/domedb/171204_pose3/calibration_171204_pose3.json') as f:
# rawCalibs = json.load(f)
# cameras = rawCalibs['cameras']
# allPanel = map(lambda x:x['panel'],cameras)
# hdCamIndices = [i for i,x in enumerate(allPanel) if x==0]
# g_hdCams = [cameras[i] for i in hdCamIndices]
# def setPanopticCameraView(camid):
# if g_hdCams==None:
# load_cameras()
# if camid>=len(g_hdCams):
# camid = 0
# cam = g_hdCams[camid]
# invR = np.array(cam['R'])
# invT = np.array(cam['t'])
# camMatrix = np.hstack((invR, invT))
# # denotes camera matrix, [R|t]
# camMatrix = np.vstack((camMatrix, [0, 0, 0, 1]))
# #camMatrix = numpy.linalg.inv(camMatrix)
# K = np.array(cam['K'])
# #K = K.flatten()
# glMatrixMode(GL_PROJECTION)
# glLoadIdentity()
# Kscale = 1920.0/g_Width
# K = K/Kscale
# ProjM = np.zeros((4,4))
# ProjM[0,0] = 2*K[0,0]/g_Width
# ProjM[0,2] = (g_Width - 2*K[0,2])/g_Width
# ProjM[1,1] = 2*K[1,1]/g_Height
# ProjM[1,2] = (-g_Height+2*K[1,2])/g_Height
# ProjM[2,2] = (-g_farPlane-g_nearPlane)/(g_farPlane-g_nearPlane)
# ProjM[2,3] = -2*g_farPlane*g_nearPlane/(g_farPlane-g_nearPlane)
# ProjM[3,2] = -1
# glLoadMatrixd(ProjM.T)
# glMatrixMode(GL_MODELVIEW)
# glLoadIdentity()
# gluLookAt(0, 0, 0, 0, 0, 1, 0, -1, 0)
# glMultMatrixd(camMatrix.T)
# def load_MTC_default_camera():
# global g_Width, g_Height
# camRender_width = 1920
# camRender_height = 1080
# g_Width = camRender_width
# g_Height = camRender_height
# 3x3 intrinsic camera matrix
def setCamView_K(K):
global g_camView_K
g_camView_K = K
# 3x3 intrinsic camera matrix
# Set a default camera matrix used for MTC
def setCamView_K_DefaultForMTC():
global g_camView_K
K = np.array([[2000, 0, 960],[0, 2000, 540],[0,0,1]]) #MTC default camera. for 1920 x 1080 input image
g_camView_K = K
#Show the world in a camera cooridnate (defined by K)
def setCameraView():
# camRender_width = 1920
# camRender_height = 1080
# global g_Width, g_Height
# if camRender_width != g_Width or camRender_height!=g_Height:
# g_Width = camRender_width
# g_Height = camRender_height
# #reshape(g_Width, g_Height)
# glutReshapeWindow(g_Width,g_Height)
# invR = np.array(cam['R'])
# invT = np.array(cam['t'])
invR = np.eye(3)
invT = np.zeros((3,1))
# invT[2] = 400
camMatrix = np.hstack((invR, invT))
# denotes camera matrix, [R|t]
camMatrix = np.vstack((camMatrix, [0, 0, 0, 1]))
#camMatrix = numpy.linalg.inv(camMatrix)
# K = np.array(cam['K'])
# K = np.array(cam['K'])
# K = np.array([[2000, 0, 960],[0, 2000, 540],[0,0,1]]) #MTC default camera
# global g_camView_K
# g_camView_K = K
#K = K.flatten()
if g_camView_K is None:
print("## Warning: no K is set, so I use a default cam parameter defined for MTC")
setCamView_K_DefaultForMTC()
K = g_camView_K.copy()
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
# Kscale = 1920.0/g_Width
Kscale = 1.0 #1920.0/g_Width :: why do we need this?
K = K/Kscale
ProjM = np.zeros((4,4))
ProjM[0,0] = 2*K[0,0]/g_Width
ProjM[0,2] = (g_Width - 2*K[0,2])/g_Width
ProjM[1,1] = 2*K[1,1]/g_Height
ProjM[1,2] = (-g_Height+2*K[1,2])/g_Height
ProjM[2,2] = (-g_farPlane-g_nearPlane)/(g_farPlane-g_nearPlane)
ProjM[2,3] = -2*g_farPlane*g_nearPlane/(g_farPlane-g_nearPlane)
ProjM[3,2] = -1
glLoadMatrixd(ProjM.T)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
gluLookAt(0, 0, 0, 0, 0, 1, 0, -1, 0)
glMultMatrixd(camMatrix.T)
def SetOrthoCamera(bOrtho=True):
global g_bOrthoCam
g_bOrthoCam = bOrtho
#Show the world in a camera cooridnate (defined by K)
def setCameraViewOrth():
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
texHeight,texWidth = g_textureData.shape[:2]
# texHeight,texWidth = 1024, 1024
texHeight*=0.5
texWidth*=0.5
# texHeight *=BACKGROUND_IMAGE_PLANE_DEPTH
# texWidth *=BACKGROUND_IMAGE_PLANE_DEPTH
glOrtho(-texWidth, texWidth, -texHeight, texHeight, -1500, 1500)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
gluLookAt(0, 0, 0, 0, 0, 1, 0, -1, 0)
# glMultMatrixd(camMatrix.T)
def setRenderOutputSize(imWidth, imHeight):
global g_renderOutputSize
g_renderOutputSize = (imWidth, imHeight)
def setWindowSize(new_width, new_height):
global g_Width, g_Height
if new_height>1600: #Max height of screen
new_width = int(new_width *0.7)
new_height = int(new_height *0.7)
if new_width != g_Width or new_height!=g_Height:
g_Width = new_width
g_Height =new_height
#reshape(g_Width, g_Height)
if g_bGlInitDone:
glutReshapeWindow(g_Width,g_Height)
def reshape(width, height):
#lightPos = (-50.0, 50.0, 100.0, 1.0)
nRange = 250.0
global g_Width, g_Height
g_Width = width
g_Height = height
glViewport(0, 0, g_Width, g_Height)
# # Set perspective (also zoom)
# glMatrixMode(GL_PROJECTION)
# glLoadIdentity()
# #gluPerspective(zoom, float(g_Width)/float(g_Height), g_nearPlane, g_farPlane)
# gluPerspective(65, float(g_Width)/float(g_Height), g_nearPlane, g_farPlane)
# print("here: {}".format(float(g_Width)/float(g_Height)))
def SaveScenesToFile():
#global g_Width, g_Height, g_bSaveToFile, g_fameIdx, g_hagglingSeqName
# global g_bSaveToFile
global g_saveFrameIdx
glReadBuffer(GL_FRONT)
img = glReadPixels(0, 0, g_Width, g_Height, GL_RGBA, GL_UNSIGNED_BYTE, outputType=None)
img = Image.frombytes("RGBA", (g_Width, g_Height), img)
img = ImageOps.flip(img)
if False:
pix = np.array(img)
import viewer2D
viewer2D.ImShow(pix)
#Crop and keep the original size
# if g_textureImgOriginal is not None and g_viewMode=='camView':
# width, height = img.size
# img = img.crop( (0,0,g_textureImgOriginal.shape[1], g_textureImgOriginal.shape[0])) #top, left, bottom, right where origin seems top left
if g_renderOutputSize is not None and g_viewMode=='camView':
width, height = g_renderOutputSize
img = img.crop( (0,0,width, height)) #top, left, bottom, right where origin seems top left
#img.save('/ssd/render_general/frame_{}.png'.format(g_frameIdx), 'PNG')
if g_saveFolderName is not None:
folderPath = g_saveFolderName
else:# g_haggling_render == False:
# folderPath = '/home/hjoo//temp/render_general/'
folderPath = '/home/hjoo//temp/render_general/'
# else:
# if g_meshes == None and g_hagglingFileList == None: #Initial loading
# LoadHagglingData_Caller() #Load New Scene
# folderPath = '/hjoo/home/temp/render_mesh/' + g_hagglingSeqName
# if os.path.exists(folderPath) == False:
# os.mkdir(folderPath)
if os.path.exists(folderPath) == False:
os.mkdir(folderPath)
# img.save('{0}/scene_{1:08d}.png'.format(folderPath,g_saveFrameIdx), 'PNG')
img = img.convert("RGB")
fileNameOut = '{0}/scene_{1:08d}.jpg'.format(folderPath,g_saveFrameIdx)
img.save(fileNameOut, "JPEG")
print(fileNameOut)
#Done saving
global g_bSaveToFile_done
g_bSaveToFile_done = True
g_saveFrameIdx+=1
# global g_savedImg = img #save rendered one into gobal variable
# if g_frameIdx+1 >= g_frameLimit:
# if g_haggling_render == False:
# g_bSaveToFile = False
# else:
# bLoaded = LoadHagglingData_Caller() #Load New Scene
# if bLoaded ==False:
# g_bSaveToFile = False
#cur_ind += 1
#print(cur_ind)
#glutPostRedisplay()
def SaveCamViewInfo():
global g_Width, g_Height
global g_nearPlane,g_farPlane
global g_zoom,g_yRotate,g_xRotate, g_zRotate, g_xTrans, g_yTrans, g_zTrans
fileName = '/ssd/camInfo/camInfo.pkl'
if os.path.exists('/ssd/camInfo/') == False:
os.mkdir('/ssd/camInfo/')
if os.path.exists('/ssd/camInfo/archive/') == False:
os.mkdir('/ssd/camInfo/archive/')
if os.path.exists(fileName):
#resave it
for i in range(1000):
newName = '/ssd/camInfo/archive/camInfo_old{}.pkl'.format(i)
if os.path.exists(newName) == False:
import shutil
shutil.copy2(fileName,newName)
break
camInfo = dict()
camInfo['g_Width'] = g_Width
camInfo['g_Height'] =g_Height
camInfo['g_nearPlane'] = g_nearPlane
camInfo['g_farPlane'] = g_farPlane
camInfo['g_zoom'] = g_zoom
camInfo['g_yRotate'] = g_yRotate
camInfo['g_xRotate'] =g_xRotate
camInfo['g_zRotate'] = g_zRotate
camInfo['g_xTrans'] = g_xTrans
camInfo['g_yTrans'] = g_yTrans
pickle.dump( camInfo, open(fileName, "wb" ) )
print('camInfo')
def LoadCamViewInfo():
global g_Width, g_Height
global g_nearPlane,g_farPlane
global g_zoom,g_yRotate,g_xRotate, g_zRotate, g_xTrans, g_yTrans
global g_camView_fileName
fileName = g_camView_fileName
if not os.path.exists(fileName):
print("No cam info: {}".format(fileName))
return
camInfo = pickle.load( open( fileName, "rb" ) , encoding='latin1')
g_yTrans = camInfo['g_Width']
g_Height = camInfo['g_Height']
g_nearPlane = camInfo['g_nearPlane']
g_farPlane = camInfo['g_farPlane']
g_zoom = camInfo['g_zoom']
g_yRotate = camInfo['g_yRotate']
g_xRotate = camInfo['g_xRotate']
g_zRotate = camInfo['g_zRotate']
g_xTrans = camInfo['g_xTrans']
g_yTrans= camInfo['g_yTrans']
reshape(g_Width, g_Height)
def PuttingObjectCenter():
global g_zoom, g_xTrans, g_yTrans, g_zTrans
global g_xRotate, g_yRotate, g_zRotate
if (g_skeletons is not None) and len(g_skeletons)>0:
g_xRotate =0
g_yRotate =0
g_zRotate =0
if g_skeletonType == 'smplcoco':
g_xTrans = -(g_skeletons[0][9,0] + g_skeletons[0][12,0] )*0.5
g_yTrans = -(g_skeletons[0][10,0] + g_skeletons[0][13,0] )*0.5
g_zTrans = -(g_skeletons[0][11,0] + g_skeletons[0][14,0] )*0.5
g_zoom = 300
else: #Adam
g_xTrans = -g_skeletons[0][0,0]
g_yTrans = -g_skeletons[0][1,0]# 0#100
g_zTrans = -g_skeletons[0][2,0]
g_zoom = 300
elif g_meshes is not None and len(g_meshes)>0:
g_xRotate =0
g_yRotate =0
g_zRotate =0
g_xTrans = -g_meshes[0]['ver'][0,1767,0]
g_yTrans = -g_meshes[0]['ver'][0,1767,1]
g_zTrans = -g_meshes[0]['ver'][0,1767,2]
# print("{} {} {}".format(g_xTrans, g_yTrans, g_zTrans))
g_zoom = 300
def keyboard(key, x, y):
global g_stopMainLoop,g_frameIdx,g_show_fps
global g_ptSize
if isinstance(key, bytes):
key = key.decode() #Python3: b'X' -> 'X' (bytes -> str)
if key == chr(27) or key == 'q':
#sys.exit()
g_stopMainLoop= True
g_frameIdx = 0
# glutIdleFunc(0); # Turn off Idle function if used.
# glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_CONTINUE_EXECUTION)
# glutLeaveMainLoop()
# glutDestroyWindow(g_winID) # Close open windows
elif key == 'p':
#global width, height
glReadBuffer(GL_FRONT)
img = glReadPixels(0, 0, g_Width, g_Height, GL_RGBA, GL_UNSIGNED_BYTE, outputType=None)
img = Image.frombytes("RGBA", (g_Width, g_Height), img)
img = ImageOps.flip(img)
img.save('temp.jpg', 'JPG')
elif key == 's':
g_frameIdx = 0
elif key == 't':
global g_xRotate,g_yRotate,g_zRotate
g_xRotate =0
g_yRotate =-90
g_zRotate =0
print('showTopView')
# elif key == 'r':
# global g_bSaveToFile
# g_frameIdx = 0
# g_bSaveToFile = True
elif key == 'w':
global g_bShowWiredMesh
g_bShowWiredMesh = not g_bShowWiredMesh
elif key == 'h': #Load Next Haggling Data
LoadHagglingData_Caller()
elif key =='o':
global g_bApplyRootOffset
g_bApplyRootOffset = not g_bApplyRootOffset
elif key =='f':
global g_bShowFloor
g_bShowFloor = not g_bShowFloor
elif key =='V':
SaveCamViewInfo()
elif key =='v':
LoadCamViewInfo()
# elif key =='c':
# g_xRotate =0
# g_yRotate =0
# g_zRotate =180
elif key =='c': #put the target human in the center
PuttingObjectCenter()
elif key=='+':
g_ptSize +=1
elif key=='-':
# global g_ptSize
if g_ptSize >=2:
g_ptSize -=1
elif key =='R': #rotate cameras
global g_bRotateView, g_rotateView_counter
g_bRotateView = not g_bRotateView
g_rotateView_counter =0
elif key =='j': #Toggle joint
global g_bShowSkeleton
g_bShowSkeleton = not g_bShowSkeleton
elif key =='m':
global g_bShowMesh
g_bShowMesh = not g_bShowMesh
elif key =='b':
global g_bShowBackground
g_bShowBackground = not g_bShowBackground
# elif key =='C':
# g_xRotate =0
# g_yRotate =0
# g_zRotate =0
elif key == 'C':
print('Toggle camview / freeview')
global g_viewMode, g_nearPlane
if g_viewMode=='free':
g_viewMode = 'camView'
g_nearPlane = 500 #cameramode
else:
g_viewMode ='free'
g_nearPlane = 0.01 #original
# elif key>='0' and key<='9':
# g_camid.popleft()
# g_camid.append(key)
# print('camView: CamID:{}'.format(g_camid))
elif key =='0':
glutReshapeWindow(1920,720)
elif key =='z': # Toggle fps printing
g_show_fps = not g_show_fps
glutPostRedisplay()
def mouse(button, state, x, y):
global g_action, g_xMousePtStart, g_yMousePtStart
if (button==GLUT_LEFT_BUTTON):
if (glutGetModifiers() == GLUT_ACTIVE_SHIFT):
g_action = "TRANS"
else:
g_action = "MOVE_EYE"
#elif (button==GLUT_MIDDLE_BUTTON):
# action = "TRANS"
elif (button==GLUT_RIGHT_BUTTON):
g_action = "ZOOM"
g_xMousePtStart = x
g_yMousePtStart = y
def motion(x, y):
global g_zoom, g_xMousePtStart, g_yMousePtStart, g_xRotate, g_yRotate, g_zRotate, g_xTrans, g_zTrans
if (g_action=="MOVE_EYE"):
g_xRotate += x - g_xMousePtStart
g_yRotate -= y - g_yMousePtStart
elif (g_action=="MOVE_EYE_2"):
g_zRotate += y - g_yMousePtStart
elif (g_action=="TRANS"):
g_xTrans += x - g_xMousePtStart
g_zTrans += y - g_yMousePtStart
elif (g_action=="ZOOM"):
g_zoom -= y - g_yMousePtStart
# print(g_zoom)
else:
print("unknown action\n", g_action)
g_xMousePtStart = x
g_yMousePtStart = y
print ('xTrans {}, yTrans {}, zoom {} xRotate{} yRotate {} zRotate {}'.format(g_xTrans, g_yTrans, g_zoom, g_xRotate, g_yRotate, g_zRotate))
glutPostRedisplay()
def setBackgroundTexture(img):
global g_textureData#, g_textureImgOriginal
g_textureData = img
#In MTC, the background should be always 1920x1080
# g_textureData = np.ones( (1080, 1920, 3), dtype=img.dtype)*0 #dtype==np.unit8
# g_textureData[:img.shape[0],:img.shape[1] ] = img
# g_textureImgOriginal = img #keep the original image
# import cv2
# cv2.imshow('here??',img)
# cv2.waitKey(0)
def SetCameraPoses(camRots, camPoses):
global g_cameraPoses,g_cameraRots
g_cameraPoses = camPoses
g_cameraRots = camRots
""" ptCloud: N,3 """
def SetPtCloud(ptCloud, ptCloudColor = None):
global g_ptCloud, g_ptCloudColor
g_ptCloud = ptCloud
g_ptCloudColor = ptCloudColor
def DrawBackgroundOrth():
if g_textureData is None:
return
glDisable(GL_CULL_FACE)
glDisable(GL_DEPTH_TEST)
glDisable(GL_LIGHTING)
glEnable(GL_TEXTURE_2D)
# glUseProgram(0)
glBindTexture(GL_TEXTURE_2D, g_backgroundTextureID)
# glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1920, 1080, 0, GL_RGB, GL_UNSIGNED_BYTE, g_textureData)
# glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 480, 640, 0, GL_RGB, GL_UNSIGNED_BYTE, g_textureData.data)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, g_textureData.shape[1], g_textureData.shape[0], 0, GL_BGR, GL_UNSIGNED_BYTE, g_textureData.data)
texHeight,texWidth = g_textureData.shape[:2]
texHeight*=0.5
texWidth*=0.5
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
glBegin(GL_QUADS)
glColor3f(1.0, 1.0, 1.0)
# d = BACKGROUND_IMAGE_PLANE_DEPTH
d = 10
glTexCoord2f(0, 0)
# #Eigen::Map<Eigen::Matrix<double, 3, 3, Eigen::RowMajor>> K(m_options.m_pK);
P = np.array([-texWidth, -texHeight, d])
# P = np.matmul(K_inv,P)
# P = P / P[2]
glVertex3f(P[0] , P[1] , P[2] ); # K^{-1} [0, 0, 1]^T
glTexCoord2f(1, 0)
# P = [1920, 0, 1]
P = [texWidth, -texHeight, d]
glVertex3f(P[0] , P[1] , P[2] ); # K^{-1} [0, 0, 1]^T
glTexCoord2f(1, 1)
# P = [1920, 1080, 1]
P = [texWidth, texHeight, d]
# P = np.matmul(K_inv,P)
# P = P / P[2]
# glVertex3f(P[0] * d, P[1] * d, P[2] * d)
glVertex3f(P[0] , P[1] , P[2] ); # K^{-1} [0, 0, 1]^T
glTexCoord2f(0, 1)
# P = [0, 1080, 1]
P = [-texWidth, texHeight, d]
# glVertex3f(P[0] * d, P[1] * d, P[2] * d)
glVertex3f(P[0] , P[1] , P[2] ); # K^{-1} [0, 0, 1]^T
glEnd()
glEnable(GL_LIGHTING)
glEnable(GL_CULL_FACE)
glEnable(GL_DEPTH_TEST)
glDisable(GL_TEXTURE_2D)