-
Notifications
You must be signed in to change notification settings - Fork 0
/
gearVRCViewer.py
1279 lines (1063 loc) · 54.7 KB
/
gearVRCViewer.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
#!/usr/bin/python3
###############################################################################
# Samsung gearVR Controller Viewer
###############################################################################
# Renders the controller as a 3D object based on the controllers pose.
# Receives pose through ZMQ from gearVRC.py
# Highlights the buttons based on user input.
#
# Urs Utzinger 2023
###############################################################################
# This work uses ideas from Gear VRC reverse engineering:
# https://github.com/jsyang/gearvr-controller-webbluetooth
# In particular the idea of updating the texture based on user
# pressing buttons.
#
# chat.openai.com was extensively used for OpenGL programming
# Code autocompletion was provided by Github Copilot
###############################################################################
# Prerequisite:
# -python packages
# pyqt5, pyopengl, pywavefront, pyglet, zmq, msgpack, numpy
# -some data structures and methods from gearVRC.py
###############################################################################
# Various
from gearVRC import gearFusionData, gearButtonData, gearMotionData, dict2obj
import sys, os, pathlib, math
import logging, argparse
import zmq, msgpack
import numpy as np
import ctypes
# PyQT5
from PyQt5.QtCore import QThread, QObject, pyqtSignal, pyqtSlot
from PyQt5.QtWidgets import QApplication, QMainWindow, QOpenGLWidget, QWidget, \
QVBoxLayout, QSizePolicy, QHBoxLayout
from PyQt5.QtGui import QIcon, QOpenGLShader, QOpenGLShaderProgram
# OpenGL
from OpenGL.GL import glClearColor, glEnable, glClear, glViewport, glMatrixMode, \
glLoadIdentity, glTranslatef, glShadeModel, glRotatef, glColor4f, glGenTextures, \
glBindTexture, glTexImage2D, glDisable, glBlendFunc, glMaterialfv, \
glDrawArrays, glPushClientAttrib, glPushAttrib, glInterleavedArrays, \
glPopAttrib, glPopClientAttrib, glMaterialf, glTexParameteri, glCullFace, \
glGenFramebuffers, glBindFramebuffer, glFramebufferTexture2D, glCheckFramebufferStatus, \
glUseProgram, glGetUniformLocation, glBindVertexArray, \
glBufferData, glUniform4f, glBlendEquation, glCreateShader, glShaderSource, \
glCompileShader, glCreateProgram, glAttachShader, glLinkProgram, glGenBuffers, \
glBindBuffer, glEnableVertexAttribArray, glVertexAttribPointer, glGenVertexArrays, \
glDrawBuffers, glOrtho, glBegin, glEnd, glTexCoord2f, glVertex2f, glGetShaderiv, \
glGetShaderInfoLog, glDeleteShader, glGetProgramiv, glGetProgramInfoLog, \
glGetTexLevelParameteriv, glGetTexImage, glGetError, glFlush, glDrawBuffer, \
glActiveTexture, glUniform1i, glGetIntegerv, glPopMatrix, glPushMatrix, \
glCopyImageSubData, glEnableClientState, glDisableClientState, glVertexPointer, \
glDeleteBuffers, glDeleteVertexArrays, glLightfv
from OpenGL.GLU import gluLookAt, gluPerspective
from OpenGL.GL import GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, GL_TEXTURE_2D, \
GL_LIGHTING, GL_LIGHT0, GL_PROJECTION, GL_MODELVIEW, GL_DEPTH_TEST, \
GL_COLOR_MATERIAL, GL_SMOOTH, GL_POLYGON, GLfloat, GL_TRIANGLES, \
GL_UNSIGNED_BYTE, GL_RGBA, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE, \
GL_TEXTURE_WRAP_T, GL_BLEND, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, \
GL_DIFFUSE, GL_AMBIENT, GL_SPECULAR, GL_REPEAT, \
GL_BACK, GL_FRONT_AND_BACK, GL_CLIENT_VERTEX_ARRAY_BIT, GL_CURRENT_BIT, \
GL_ENABLE_BIT, GL_LIGHTING_BIT, GL_CULL_FACE, \
GL_EMISSION, GL_SHININESS, GL_FRAMEBUFFER, GL_RGBA8, GL_FRAMEBUFFER_COMPLETE, \
GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, \
GL_COLOR_ATTACHMENT3, GL_COLOR_ATTACHMENT4, GL_COLOR_ATTACHMENT5, \
GL_COLOR_ATTACHMENT6, GL_COLOR_ATTACHMENT7, \
GL_TEXTURE_MIN_FILTER, GL_LINEAR, \
GL_TEXTURE_MAG_FILTER, GL_VERTEX_SHADER, GL_FRAGMENT_SHADER, \
GL_ARRAY_BUFFER, GL_DYNAMIC_DRAW, GL_FUNC_ADD, GL_FLOAT, GL_FALSE, \
GL_TRUE, GL_COMPILE_STATUS, GL_LINK_STATUS, GL_TEXTURE_WIDTH, \
GL_TEXTURE_HEIGHT, GL_TEXTURE_INTERNAL_FORMAT, GL_ALL_ATTRIB_BITS, \
GL_CLIENT_ALL_ATTRIB_BITS, GL_VIEWPORT_BIT, GL_TRANSFORM_BIT, \
GL_ACCUM_BUFFER_BIT, GL_EVAL_BIT, GL_LINE_BIT, GL_LIST_BIT, \
GL_PIXEL_MODE_BIT, GL_POINT_BIT, GL_POLYGON_BIT, GL_TEXTURE_BIT, \
GL_TEXTURE1, GL_TEXTURE0, GL_TEXTURE_BINDING_2D, \
GL_FRAMEBUFFER_BINDING, GL_CURRENT_PROGRAM, GL_VERTEX_ARRAY, \
GL_POSITION, GL_LIGHT1, GL_POSITION, GL_DIFFUSE
# Pywavefront is used to load the 3D model
from pywavefront.visualization import VERTEX_FORMATS
from pywavefront import Wavefront
# pyglet is used to load the texture image
from pyglet import image
# sys.path.append('..')
###############################################################################
# GLOBALS
###############################################################################
ZMQTIMEOUT = 10000 # in milliseconds
DEG2RAD = math.pi / 180.0
TWOPI = 2.0 * math.pi
FIELDOFVIEW = 20.0 # in degrees
###############################################################################
# Shaders
###############################################################################
# Vertex shader code for drawing polygons on texture
VERTEX_SHADER_DRAW = """
#version 330 core
// vertex position input
in vec2 position;
void main()
{
// Pass the vertex position and color to the fragment shader
gl_Position = vec4(position, 0.0, 1.0);
}
"""
# Fragment shader code for drawing polygons on texture
FRAGMENT_SHADER_DRAW = """
#version 330 core
// Output color of the fragment shader
out vec4 FragColor;
// Input color from the vertex shader
uniform vec4 color;
void main()
{
// Pass the vertex color to the output
FragColor = color;
}
"""
###################################################################
# Utility functions
###################################################################
# Function to create vertices for a circle
# input: (as measured with imageJ app)
# - center coordinates cx,cy
# 0/0 is upper left
# - radius rx, ry in absolute coordinates
# - scale is maximum vale for x and y
# output: (as used by OpenGL)
# - relative coordinates from -1 ... 1,
# 0/0 center, -1/-1 lower left, 1/1 upper right
def circle_vertices(cx, cy, rx, ry, num_segments, scalex, scaley):
angle_step = 2 * math.pi / num_segments
vertices = np.empty((num_segments, 2), dtype=np.float32)
cx2, cy2, rx2, ry2 = cx * 2.0, cy * 2.0, rx * 2.0, ry * 2.0
for i in range(num_segments ):
angle = i * angle_step
x = (cx2 + rx2 * math.cos(angle)) / scalex - 1.0
y = 1.0 - (cy2 + ry2 * math.sin(angle)) / scaley
vertices[i, 0] = x
vertices[i, 1] = y
return vertices
def getTextureParameters(texture_id):
glBindTexture(GL_TEXTURE_2D, texture_id)
width = glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH)
height = glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT)
internal_format = glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT)
if width > 0 or height > 0:
pixels = glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE)
else:
pixels = None
glBindTexture(GL_TEXTURE_2D, 0)
return width, height, internal_format, pixels
##############################################################################
# ZMQWorker
###############################################################################
class zmqWorker(QObject):
dataReady = pyqtSignal(object)
BLEstatus = pyqtSignal(bool)
finished = pyqtSignal()
def __init__(self, logger, parent=None):
super(zmqWorker, self).__init__(parent)
self.logger = logger
self.running = False
self.paused = False
self.zmqPort = 'tcp://localhost:5556'
self.new_fusion = False
self.new_button = False
self.new_motion = False
self.timeout = False
self.zmqTimeout = ZMQTIMEOUT
self.logger.log(logging.INFO, 'zmqWorker initialized')
def start(self):
self.running = True
self.paused = False
self.new_fusion = False
self.new_button = False
self.new_motion = False
context = zmq.Context()
poller = zmq.Poller()
data_motion = None
data_button = None
data_fusion = None
socket = context.socket(zmq.SUB)
socket.setsockopt(zmq.SUBSCRIBE, b"fusion")
socket.setsockopt(zmq.SUBSCRIBE, b"button")
socket.setsockopt(zmq.SUBSCRIBE, b"motion")
socket.connect(self.zmqPort)
poller.register(socket, zmq.POLLIN)
self.logger.log(logging.INFO, 'zmqWorker started on {}'.format(self.zmqPort))
while self.running:
try:
events = dict(poller.poll(timeout=self.zmqTimeout))
if socket in events and events[socket] == zmq.POLLIN:
response = socket.recv_multipart()
if len(response) == 2:
[topic, msg_packed] = response
if topic == b"fusion":
msg_dict = msgpack.unpackb(msg_packed)
data_fusion = dict2obj(msg_dict)
if hasattr(data_fusion, 'q') and \
hasattr(data_fusion, 'rpy') and \
hasattr(data_fusion, 'heading') and\
hasattr(data_fusion, 'time'):
self.new_fusion = True
elif topic == b"button":
msg_dict = msgpack.unpackb(msg_packed)
data_button = dict2obj(msg_dict)
if hasattr(data_button, 'trigger') and \
hasattr(data_button, 'back') and \
hasattr(data_button, 'home') and \
hasattr(data_button, 'volume_up') and \
hasattr(data_button, 'volume_down') and \
hasattr(data_button, 'touch') and \
hasattr(data_button, 'noButton') and \
hasattr(data_button, 'touchX') and \
hasattr(data_button, 'touchY') and\
hasattr(data_button, 'time'):
self.new_button = True
elif topic == b"motion":
msg_dict = msgpack.unpackb(msg_packed)
data_motion = dict2obj(msg_dict)
if hasattr(data_motion, 'time') and \
hasattr(data_motion, 'residuals') and \
hasattr(data_motion, 'velocity') and \
hasattr(data_motion, 'position') and \
hasattr(data_motion, 'accBias') and \
hasattr(data_motion, 'velocityBias') and \
hasattr(data_motion, 'dtmotion'):
self.new_motion = True
else:
pass # not a topic we need
else:
self.logger.log(
logging.ERROR, 'zmqWorker malformed message')
else: # ZMQ TIMEOUT
self.logger.log(logging.ERROR, 'zmqWorker timed out')
poller.unregister(socket)
socket.close()
socket = context.socket(zmq.SUB)
socket.setsockopt(zmq.SUBSCRIBE, b"fusion")
socket.setsockopt(zmq.SUBSCRIBE, b"button")
socket.setsockopt(zmq.SUBSCRIBE, b"motion")
socket.connect(self.zmqPort)
poller.register(socket, zmq.POLLIN)
self.new_fusion = False
self.new_button = False
self.new_motion = False
if (self.new_fusion and self.new_button and self.new_motion):
if not self.paused:
self.dataReady.emit([data_button, data_fusion, data_motion])
self.BLEstatus.emit(True)
self.new_fusion = False
self.new_button = False
self.new_motion = False
else:
if not self.paused:
self.BLEstatus.emit(False)
except:
self.logger.log(logging.ERROR, 'zmqWorker error')
poller.unregister(socket)
socket.close()
socket = context.socket(zmq.SUB)
socket.setsockopt(zmq.SUBSCRIBE, b"fusion")
socket.setsockopt(zmq.SUBSCRIBE, b"button")
socket.setsockopt(zmq.SUBSCRIBE, b"motion")
socket.connect(self.zmqPort)
poller.register(socket, zmq.POLLIN)
self.new_fusion = False
self.new_button = False
self.new_motion = False
self.logger.log(logging.DEBUG, 'zmqWorker finished')
socket.close()
context.term()
self.finished.emit()
def stop(self):
self.running = False
self.paused = False
def set_zmqPort(self, port):
self.zmqPort = port
def pause(self):
self.paused = not self.paused
###############################################################################
# Texture Display Widget for Debugging OpenGL
###############################################################################
class ImageDisplayWidget(QOpenGLWidget): # QWidget
def __init__(self, logger, parent=None):
super(ImageDisplayWidget, self).__init__(parent)
self.logger = logger
self.imageTextureID = None
self.imageTextureWidth = 256
self.imageTextureHeight = 256
self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
self.setFixedSize(self.imageTextureWidth, self.imageTextureHeight)
self.logger.log(logging.INFO, 'Image Display Widget is setup' )
@pyqtSlot(object)
def receiveTexture(self, pixelData):
# Unpack the data
self.imageTextureWidth, self.imageTextureHeight, internal_format, pixels = pixelData
if pixels is not None:
self.logger.log(logging.DEBUG, 'Received image {}x{} with has internal format {}: pixels in RGBA {}..'.format(\
self.imageTextureWidth, self.imageTextureHeight, internal_format, pixels[0:5]) )
else:
self.logger.log(logging.DEBUG, 'Received image {}x{} with has internal format {}'.format(\
self.imageTextureWidth, self.imageTextureHeight, internal_format) )
# Check for valid texture ID
if self.imageTextureID is not None:
# Bind existing texture ID for update
glBindTexture(GL_TEXTURE_2D, self.imageTextureID)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, self.imageTextureWidth, self.imageTextureHeight, \
0, GL_RGBA, GL_UNSIGNED_BYTE, pixels)
# Set texture parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
# Unbind the texture after update
# glBindTexture(GL_TEXTURE_2D, 0)
# Trigger a repaint of the widget to reflect the changes
self.update()
self.logger.log(logging.DEBUG, 'Image display texture is updated' )
def initializeGL(self):
# Generate texture ID
self.imageTextureID = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, self.imageTextureID)
# Allocate memory for texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, self.imageTextureWidth, self.imageTextureHeight, \
0, GL_RGBA, GL_UNSIGNED_BYTE, None)
# Set properties of texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
#######################################################################
# Create vertex coordinates to render texture to image display widget
#######################################################################
self.fullTextureVertices = np.array([
# Triangle 1
[0.0, 0.0],
[self.imageTextureWidth, 0.0],
[self.imageTextureWidth, self.imageTextureHeight],
# Triangle 2
[0.0, 0.0],
[self.imageTextureWidth, self.imageTextureHeight],
[0.0, self.imageTextureHeight],
], dtype=np.float32)
self.fullTextureVerticesData = self.fullTextureVertices.astype(np.float32).tobytes()
self.fullTextureVerticesBufferSize = ctypes.sizeof(ctypes.c_float) * self.fullTextureVertices.size
self.fullTextureVerticesNumVertices = len(self.fullTextureVertices)
glClearColor(0.0, 0.0, 0.0, 0.0)
glEnable(GL_TEXTURE_2D)
self.logger.log(logging.INFO, 'Image Display Widget is initialized' )
def resizeGL(self, width, height):
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0, width, 0, height, -1, 1)
glMatrixMode(GL_MODELVIEW)
glEnable(GL_TEXTURE_2D)
self.logger.log(logging.INFO, 'Image Display Widget is resized' )
def paintGL(self):
if self.imageTextureID is None:
self.logger.log(logging.DEBUG, 'No Texture, Image Display Scene not painted' )
return
width, height, internal_format, pixels = getTextureParameters(self.imageTextureID)
if pixels is not None:
self.logger.log(logging.DEBUG, 'Displaying image {} {}x{} with internal format {}: pixels in RGBA {}..'.format(\
self.imageTextureID, width, height, internal_format, pixels[0:5]) )
else:
self.logger.log(logging.DEBUG, 'Displaying image {} {}x{} with internal format {}'.format(\
self.imageTextureID, width, height, internal_format) )
#Setup OpenGL settings
glViewport(0, 0, self.width(), self.height())
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # type: ignore
glLoadIdentity()
if self.imageTextureID is not None:
# Bind the texture and draw it on a rectangle
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, self.imageTextureID)
glBegin(GL_TRIANGLES)
# Triangle 1
glTexCoord2f(0, 0); glVertex2f( 0, 0) # Vertex 1 (top-left) - Triangle 1
glTexCoord2f(1, 0); glVertex2f( self.width(), 0) # Vertex 2 (top-right) - Triangle 1
glTexCoord2f(1, 1); glVertex2f( self.width(), self.height()) # Vertex 3 (bottom-right) - Triangle 1
# Triangle 2
glTexCoord2f(0, 0); glVertex2f( 0, 0) # Repeated Vertex 1 (top-left) - Triangle 2
glTexCoord2f(1, 1); glVertex2f( self.width(), self.height()) # Repeated Vertex 3 (bottom-right) - Triangle 2
glTexCoord2f(0, 1); glVertex2f( 0, self.height()) # Vertex 4 (bottom-left) - Triangle 2
glEnd()
glFlush()
glBindTexture(GL_TEXTURE_2D, 0)
glDisable(GL_TEXTURE_2D)
er= glGetError()
if er == 0:
self.logger.log(logging.DEBUG, 'Image display Scene is painted')
else:
self.logger.log(logging.DEBUG, 'Image display Scene is painted with error {}'.format(glGetError()))
###############################################################################
# Main Display Widget
###############################################################################
class ObjectRenderingWidget(QOpenGLWidget):
newTextureAvailable = pyqtSignal(object)
def __init__(self, logger, position=False, debug=False, renderButtons=False, parent=None):
super(ObjectRenderingWidget, self).__init__(parent)
self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.logger = logger
self.usePosition = position # move the object based on positional data
self.debug = debug # displays texture rendering
self.renderButtons = renderButtons
# Initialize variables
self.object = None
self.renderingObject = None
self.originalTextureID = None
self.originalTextureWidth = 0
self.originalTextureHeight = 0
self.bluetooth_on = False
# Rendering parameters
self.backgroundColor = (200./255, 200./255, 205./255)
self.fusion = gearFusionData()
self.button = gearButtonData()
self.motion = gearMotionData()
self.q = None
self.position = None
# Shader parameters
self.blendingFBO = None
self.displayTextureID = None
self.originalTextureID = None
self.originalTextureWidth = 0
self.originalTextureHeight = 0
self.originalTextureClamp = False
self.logger.log(logging.INFO, 'Main Display Widget is setup')
def initializeGL(self):
#######################################################################
# Load the 3D object
#######################################################################
modelFileName = os.path.join(os.path.dirname(__file__), 'model/gear_vr_controller.obj')
# PyWavefront objects internal structure is complex and texture data is
# not automatically loaded.
# The example visualization code uses pyglet image to load the texture image, which
# creates a data structure containing texture pixel data and texture ID.
# For rendering an array of vertices is created.
self.object = Wavefront(modelFileName, collect_faces=True, create_materials=True, strict=False)
#######################################################################
# Calculate the camera position and distance based on the object size
#######################################################################
if self.object is not None:
min_bound = np.min(self.object.vertices, axis=0)
max_bound = np.max(self.object.vertices, axis=0)
self.object_center = (min_bound + max_bound) / 2.0
object_size = np.linalg.norm(max_bound - min_bound)
camera_distance = 2.5 * object_size
self.camera_position = self.object_center + \
camera_distance*np.array([0.0, 1.0, 1.0])
# x is the axis pointing to the right
# y is the axis pointing up
# z is the axis pointing towards the user
else:
self.object_center = np.array([0.0, 0.0, 0.0])
self.camera_position = np.array([0.0, 0.0, 0.0])
#######################################################################
# Convert the vertices to OpenGL GLfloat array
#######################################################################
if self.object is not None:
# Converting the vertices to GLfloat array
self.renderingObject = self.object.materials['controller'] # Object
if self.renderingObject.gl_floats is None:
self.renderingObject.gl_floats = (GLfloat * len(self.renderingObject.vertices))(*self.renderingObject.vertices)
self.renderingObject.triangle_count = len(self.renderingObject.vertices) / self.renderingObject.vertex_size
#
self.vertex_format = VERTEX_FORMATS.get(self.renderingObject.vertex_format)
else:
self.renderingObject = None
self.vertex_format = None
#######################################################################
# Load textures
#######################################################################
if self.renderingObject is not None:
self.originalTextureID, \
self.originalTextureWidth, \
self.originalTextureHeight, \
self.originalTextureClamp = \
self.load_object_texture(self.renderingObject)
else:
self.originalTextureID = None
self.originalTextureWidth = 0
self.originalTextureHeight = 0
self.originalTextureClamp = False
#######################################################################
# If we want to render device button highlights we need to create:
# - a frame buffer and attached to it the original texture and a
# texture to merge original texture with button highlights
# - a vertex array object and vertex buffer object
# - a vertex and fragment shaders
#######################################################################
# Define pre-made vertices
# ------------------------
# This creates circles and polygons for the device buttons
self.create_button_vertices()
# Create frame buffer with two texture,
# original texture (static) originalTextureID
# display texture (modified) displayTextureID
# -----------------------------------------------
# Create and bind new FBO
# -----------------------
self.blendingFBO = glGenFramebuffers(1)
glBindFramebuffer(GL_FRAMEBUFFER, self.blendingFBO)
# displayTextureID
# ----------------
# Generate new display texture and attach it to the FBO
self.displayTextureID = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, self.displayTextureID)
# Allocate memory for the texture and specify its format size, use original texture dimensions
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, self.originalTextureWidth, self.originalTextureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, None)
# Set the display texture parameters
if self.originalTextureClamp:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
else:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
# Attach the display texture to the FBO
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, self.displayTextureID, 0)
# originalTextureID
# ------------------
# Attach original texture to the FBO
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, self.originalTextureID, 0)
# We only want to draw to the display texture
# -------------------------------------------
glDrawBuffer(GL_COLOR_ATTACHMENT0)
# Check if the frame buffer is complete
if glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE:
raise RuntimeError("Texture frame buffer is not complete!")
# Cleanup: unbind the FBOs and textures
glBindFramebuffer(GL_FRAMEBUFFER, 0)
glBindTexture(GL_TEXTURE_2D, 0)
#######################################################################
# Create vertex drawing shaders to render polygons onto texture
#######################################################################
# Create and compile vertex and fragment shaders
self.drawShaderProgram = QOpenGLShaderProgram()
self.drawShaderProgram.addShaderFromSourceCode(QOpenGLShader.Vertex, VERTEX_SHADER_DRAW)
self.drawShaderProgram.addShaderFromSourceCode(QOpenGLShader.Fragment, FRAGMENT_SHADER_DRAW)
self.drawShaderProgram.link()
self.lightPosition0 = [15.0, 15.0, -15.0, 1.0]
self.lightDiffuse0 = [ 0.67, 0.67, 0.67, 1.0]
self.lightPosition1 = [15.0, -15.0, 15.0, 1.0]
self.lightDiffuse1 = [ 0.8, 0.8, 0.8, 1.0]
self.logger.log(logging.INFO, 'Main GL Widget is initialized')
def resizeGL(self, width, height):
# Adjust the viewport and projection matrix
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
fov = FIELDOFVIEW
aspect_ratio = float(width) / height
near = 0.1
far = 100.0
gluPerspective(fov, aspect_ratio, near, far)
glEnable(GL_DEPTH_TEST)
glMatrixMode(GL_MODELVIEW)
self.logger.log(logging.DEBUG, 'Scene resized')
def paintGL(self):
#######################################################################
# Update the display texture based on user interaction with the device
#######################################################################
if self.renderButtons:
# Copy original texture to display texture
glCopyImageSubData(
self.originalTextureID, GL_TEXTURE_2D, 0, 0, 0, 0, # Source
self.displayTextureID, GL_TEXTURE_2D, 0, 0, 0, 0, # Destination
self.originalTextureWidth, self.originalTextureHeight, 1 # Size
)
if self.button is not None:
# Update display texture with button highlights
self.render_buttons_to_display(self.button)
# Update texture in image display widget for debugging
if self.displayTextureID is not None:
if self.debug:
width, height, internal_format, pixels = getTextureParameters(self.displayTextureID)
self.newTextureAvailable.emit([width,height, internal_format, pixels])
else:
# Update texture in image display widget for debugging
if self.debug:
if self.originalTextureID is not None:
width,height, internal_format, pixels = getTextureParameters(self.originalTextureID)
self.newTextureAvailable.emit([width,height, internal_format, pixels])
#######################################################################
# Prepare System for Rendering Object
#######################################################################
# set view port
# set matrices:
# FinalVertex = ProjectionMatrix * ViewMatrix * ModelMatrix * Vertex
# - projection matrix (perspective or orthographic projection)
# - view matrix (camera position and orientation)
# - model matrix (position & scale & orientation of model)
# activating textures and materials
# render object
# Clear
# -----
# Set up general OpenGL settings
glClearColor(*self.backgroundColor, 1.0) # glClearColor(0.2, 0.2, 0.2, 1.0)
# Clear the display buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # type: ignore
glShadeModel(GL_SMOOTH)
# Set view port to match widgets size
# ------------------------------------
glViewport(0,0,self.width(),self.height())
# Setup the projection matrix (perspective or orthographic projection)
#----------------------------
# Set up perspective or orthographic projection using glFrustum, gluPerspective, or glOrtho
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
fov = FIELDOFVIEW
aspect_ratio = float(self.width()) / self.height()
near, far = 0.1, 100.0
gluPerspective(fov, aspect_ratio, near, far)
# Set up view matrix
# ------------------
# Perform camera transformations (e.g., translation, rotation) using glTranslate* and glRotate*
gluLookAt(self.camera_position[0], self.camera_position[1], self.camera_position[2],
self.object_center[0], self.object_center[1], self.object_center[2],
0, 1, 0)
# Set up the model matrix (object's position, scale, and orientation)
# -------------------------------------------------------------------
# Perform object transformations (e.g., translation, rotation) using glTranslate* and glRotate*
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
if self.fusion is not None:
self.q = self.fusion.q
# Move the controller in space based on computed position
# this likely has large drift and sensor might run out of window
if self.usePosition:
if self.motion is not None:
self.position = self.motion.position
if self.position is not None:
glTranslatef(self.position.y, -self.position.z, -self.position.x)
# Rotating the controller with Quaternion
if self.q is not None:
angle = 2 * math.acos(self.q.w) * 180.0 / math.pi
scale = math.sqrt(1 - self.q.x * self.q.x)
axis_x = self.q.x / scale
axis_y = self.q.y / scale
axis_z = self.q.z / scale
glRotatef(angle, axis_y, -axis_z, -axis_x)
#######################################################################
# Render the Object
#######################################################################
# - bind texture for 3D object
# - set material properties
# - (bind VAO containing objects vertex data), not used
# - draw the 3D object
# - (unbind VAO and texture), not used
if self.renderingObject is not None:
# Store current rendering environment
glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT)
glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT) # type: ignore
glEnable(GL_CULL_FACE)
glEnable(GL_DEPTH_TEST)
glCullFace(GL_BACK)
# Texture
# -------
glEnable(GL_TEXTURE_2D)
if self.renderButtons:
glBindTexture(GL_TEXTURE_2D, self.displayTextureID)
else:
glBindTexture(GL_TEXTURE_2D, self.originalTextureID)
if self.originalTextureClamp:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
else:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
# Material
# --------
glEnable(GL_COLOR_MATERIAL)
if self.renderingObject.has_normals:
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, (GLfloat * 4)(*self.renderingObject.diffuse))
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, (GLfloat * 4)(*self.renderingObject.ambient))
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, (GLfloat * 4)(*self.renderingObject.specular))
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, (GLfloat * 4)(*self.renderingObject.emissive))
glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, min(128.0, self.renderingObject.shininess))
glLightfv(GL_LIGHT0, GL_POSITION, self.lightPosition0)
glLightfv(GL_LIGHT1, GL_POSITION, self.lightPosition1)
glLightfv(GL_LIGHT0, GL_DIFFUSE, self.lightDiffuse0)
glLightfv(GL_LIGHT1, GL_DIFFUSE, self.lightDiffuse1)
glEnable(GL_LIGHT0)
glEnable(GL_LIGHT1)
glEnable(GL_LIGHTING)
else:
glDisable(GL_LIGHTING)
glColor4f(*self.renderingObject.ambient)
# Render the object
# -----------------
glInterleavedArrays(self.vertex_format, 0, self.renderingObject.gl_floats)
glDrawArrays(GL_TRIANGLES, 0, int(self.renderingObject.triangle_count))
# Restore previous rendering environment
glPopAttrib()
glPopClientAttrib()
self.logger.log(logging.DEBUG, 'Object scene painted')
else:
self.logger.log(logging.DEBUG, 'No object, scene not painted')
def update_system(self, data):
# Trigger the paintGL routine to update the graphics
[self.button, self.fusion, self.motion] = data
self.update()
self.logger.log(logging.DEBUG, 'Buttons, fusion and motion updated')
def update_BLEstatus(self, data):
self.bluetooth_on = data
self.logger.log(logging.DEBUG, 'Bluetooth updated')
def load_object_texture(self, renderingObject):
'''
Load object texture
'''
#######################################################################
# Load the texture from file
#######################################################################
# This is code from pywavefront.visualization.py.
# pyglet loads the texture image
texture = renderingObject.texture or renderingObject.texture_ambient
if texture and renderingObject.has_uvs:
textureFileName = renderingObject.texture.path
img = image.load(textureFileName)
texture.image = img.get_texture()
glEnable(texture.image.target)
glBindTexture(texture.image.target, texture.image.id)
if texture.options.clamp == "on":
glTexParameteri(texture.image.target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
glTexParameteri(texture.image.target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
else:
glTexParameteri(texture.image.target, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(texture.image.target, GL_TEXTURE_WRAP_T, GL_REPEAT)
textureWidth = texture.image.width
textureHeight = texture.image.height
textureID = texture.image.id
textureClamp = True if texture.options.clamp == "on" else False
else:
textureID = None
textureWidth = 0
textureHeight = 0
textureClamp = False
return textureID, textureWidth, textureHeight, textureClamp
def render_buttons_to_display(self, button):
'''
paintGL function to render all texture polygons to display texture
'''
###################################################
# Prepare the buffers and projections
###################################################
# Generate the Vertex Array Object (VAO)
self.vao = glGenVertexArrays(1)
# Generate and bind a Vertex Buffer Object (VBO)
self.vbo = glGenBuffers(1)
# Set up the viewport and clear the screen
# x/y is lower left corner, width/height is size of viewport
glViewport(0, 0, self.originalTextureWidth, self.originalTextureHeight)
# Set the draw buffer to the display texture
# This is in initialize GL
# Set up the orthographic projection for rendering the textures
# this is default and does not need to be executed
#glMatrixMode(GL_PROJECTION)
#glLoadIdentity()
# Set up projection matrix using glOrtho to map -1 to 1 to 0 to 1 for texture coordinates
#glOrtho(0, self.originalTextureWidth, 0, self.originalTextureHeight, -1, 1)
# # Set up the modelview matrix
# glMatrixMode(GL_MODELVIEW)
# glLoadIdentity()
# Activate the FBO to make it the current frame buffer
currentFBO = glGetIntegerv(GL_FRAMEBUFFER_BINDING)
glBindFramebuffer(GL_FRAMEBUFFER, self.blendingFBO)
# Enable blending
glEnable(GL_BLEND)
# glBlendEquation(GL_FUNC_ADD) # Use simple addition for blending, is default
# Use standard alpha blending
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
# Activate the shader
self.drawShaderProgram.bind()
###################################################
# Draw highlighted back and home button
###################################################
if button.back:
# Red color for the polygon
self.draw_polygon(self.backVertices, (1.0, 0.0, 0.0, 0.5))
if button.home:
# Red color for the polygon
self.draw_polygon(self.homeVertices, (1.0, 0.0, 0.0, 0.5))
if button.volume_up:
# Red color for the polygon
self.draw_polygon(self.volumeUpVertices, (1.0, 0.0, 0.0, 0.5))
if button.volume_down:
# Red color for the polygon
self.draw_polygon(self.volumeDownVertices, (1.0, 0.0, 0.0, 0.5))
if button.trigger:
# Red color for the polygon
self.draw_polygon(self.trigger1Vertices, (1.0, 0.0, 0.0, 0.5))
self.draw_polygon(self.trigger2Vertices, (1.0, 0.0, 0.0, 0.5))
self.draw_polygon(self.trigger3Vertices, (1.0, 0.0, 0.0, 0.5))
self.draw_polygon(self.trigger4Vertices, (1.0, 0.0, 0.0, 0.5))
if button.touch:
# Red color for the polygon
self.draw_polygon(self.touchVertices, (1.0, 0.0, 0.0, 0.5))
if button.touchX > 0 and button.touchY > 0:
# touch pad coordinates are in the range of 0 to 315
# top left corner is 0,0
# The region of interest enclosing the touchpad is 60x60 pixels
# The center of the touch pad is at self.touchCx and self.touchCy which is 197,60
# We have vertices available with circle at 0/0 and radius 5
#
# Absolute coordinates of the touch locations in the texture image (0/0 is upper left):
tCx = self.touchCx + (button.touchX - 157.5) / 157.5 * 30
tCy = self.touchCy + (button.touchY - 157.5) / 157.5 * 30
# relative coordinates for polygon vertex coordinates in OpenGL coordinates:
offsetX = 2.0 * tCx / self.originalTextureWidth - 1.0
offsetY = 1 - 2.0 * tCy / self.originalTextureHeight
# The pad vertices wer centered around 0/0. Lets update them by the offset.
padVertices = self.padVertices + np.array([offsetX, offsetY])
# Green color for the polygon
self.draw_polygon(padVertices, (0.0, 1.0, 0.0, 0.5))
if self.bluetooth_on:
# Blue color for the polygon
self.draw_polygon(self.ledVertices, (0.0, 0.0, 1.0, 1.0))
###################################################
# Clean up
###################################################
# Unbind the shader program
self.drawShaderProgram.release()
# Unbind frame buffer
glDisable(GL_BLEND)
glBindFramebuffer(GL_FRAMEBUFFER, currentFBO)
# Restore the original viewport and projection matrix
glViewport(0, 0, self.width(), self.height()) # Restore the original viewport size
glDeleteBuffers(1, [self.vbo])
glDeleteVertexArrays(1, [self.vao])
def draw_polygon(self, vertices, color):
'''
Draw the vertices with the given color