Skip to content

Commit 613a125

Browse files
authored
Python visualizer OpenGL improvements (#15)
* Python visualizer OpenGL improvements * Remove comment
1 parent 62117f5 commit 613a125

File tree

2 files changed

+23
-48
lines changed

2 files changed

+23
-48
lines changed

python/cli/visualization/visualizer.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,6 @@ def __init__(self, args=VisualizerArgs()):
248248
self.outputQueue = []
249249
self.outputQueueMutex = Lock()
250250
self.clock = pygame.time.Clock()
251-
self.projectionMatrix = None
252-
self.viewMatrix = None
253251

254252
# Window
255253
self.fullScreen = args.fullScreen
@@ -376,18 +374,20 @@ def __render(self, cameraPose, width, height, image=None, colorFormat=None):
376374
top = 25.0 * self.cameraControls2D.zoom / self.aspectRatio
377375
projectionMatrix = getOrthographicProjectionMatrixOpenGL(left, right, bottom, top, -1000.0, 1000.0)
378376

379-
self.projectionMatrix = projectionMatrix
380-
self.viewMatrix = viewMatrix
377+
glMatrixMode(GL_PROJECTION)
378+
glLoadMatrixf(projectionMatrix.transpose())
379+
glMatrixMode(GL_MODELVIEW)
380+
glLoadMatrixf(viewMatrix.transpose())
381381

382382
self.map.render(cameraPose.getPosition(), viewMatrix, projectionMatrix)
383-
if self.showGrid: self.grid.render(viewMatrix, projectionMatrix)
384-
if self.showPoseTrail: self.poseTrail.render(viewMatrix, projectionMatrix)
383+
if self.showGrid: self.grid.render()
384+
if self.showPoseTrail: self.poseTrail.render()
385385
if self.args.customRenderCallback: self.args.customRenderCallback()
386386

387387
if self.cameraMode in [CameraMode.THIRD_PERSON, CameraMode.TOP_VIEW]:
388388
modelMatrix = cameraPose.getCameraToWorldMatrix()
389-
if self.showCameraModel: self.cameraModelRenderer.render(modelMatrix, viewMatrix, projectionMatrix)
390-
if self.showCameraFrustum: self.cameraFrustumRenderer.render(modelMatrix, viewMatrix, projectionMatrix, self.cameraMode is CameraMode.TOP_VIEW)
389+
if self.showCameraModel: self.cameraModelRenderer.render(modelMatrix)
390+
if self.showCameraFrustum: self.cameraFrustumRenderer.render(modelMatrix, self.cameraMode is CameraMode.TOP_VIEW)
391391

392392
if self.recorder: self.recorder.recordFrame()
393393
pygame.display.flip()
@@ -548,12 +548,6 @@ def run(self):
548548

549549
self.__close()
550550

551-
def getProjectionMatrix(self):
552-
return self.projectionMatrix
553-
554-
def getViewMatrix(self):
555-
return self.viewMatrix
556-
557551
def printHelp(self):
558552
print("Control using the keyboard:")
559553
print("* Q: Quit")

python/cli/visualization/visualizer_renderers/renderers.py

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ def __init__(self, color=np.array(DEFAULT_CAMERA_RGBA), scale=0.05, lineWidth=2)
3333
[4, 1],
3434
]
3535

36-
def render(self, modelMatrix, viewMatrix, projectionMatrix):
36+
def render(self, modelMatrix):
37+
glPushMatrix()
38+
glMultMatrixf(modelMatrix.transpose())
39+
3740
glLineWidth(self.lineWidth)
3841
glEnable(GL_BLEND)
3942
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
@@ -42,12 +45,6 @@ def render(self, modelMatrix, viewMatrix, projectionMatrix):
4245
glEnable(GL_DEPTH_TEST)
4346
glColor4fv(self.color)
4447

45-
modelView = viewMatrix @ modelMatrix
46-
glMatrixMode(GL_MODELVIEW)
47-
glLoadMatrixf(modelView.transpose())
48-
glMatrixMode(GL_PROJECTION)
49-
glLoadMatrixf(projectionMatrix.transpose())
50-
5148
glBegin(GL_LINES)
5249
for edge in self.edges:
5350
p0 = self.vertices[edge[0]]
@@ -59,6 +56,7 @@ def render(self, modelMatrix, viewMatrix, projectionMatrix):
5956
glDisable(GL_BLEND)
6057
glDisable(GL_LINE_SMOOTH)
6158
glDisable(GL_DEPTH_TEST)
59+
glPopMatrix()
6260

6361
class KeyFrameRenderer:
6462
def __init__(self, color=np.array(DEFAULT_KEYFRAME_RGBA), scale=0.05, lineWidth=2):
@@ -75,11 +73,9 @@ def render(self, keyFrameCameraToWorldMatrices, viewMatrix, projectionMatrix):
7573

7674
for kfId in keyFrameCameraToWorldMatrices:
7775
modelMatrix = keyFrameCameraToWorldMatrices[kfId]
78-
modelView = viewMatrix @ modelMatrix
79-
glMatrixMode(GL_MODELVIEW)
80-
glLoadMatrixf(modelView.transpose())
81-
glMatrixMode(GL_PROJECTION)
82-
glLoadMatrixf(projectionMatrix.transpose())
76+
77+
glPushMatrix()
78+
glMultMatrixf(modelMatrix.transpose())
8379

8480
glBegin(GL_LINES)
8581
for edge in self.cameraWireframe.edges:
@@ -88,6 +84,7 @@ def render(self, keyFrameCameraToWorldMatrices, viewMatrix, projectionMatrix):
8884
glVertex3f(p0[0], p0[1], p0[2])
8985
glVertex3f(p1[0], p1[1], p1[2])
9086
glEnd()
87+
glPopMatrix()
9188

9289
glDisable(GL_BLEND)
9390
glDisable(GL_LINE_SMOOTH)
@@ -255,15 +252,8 @@ def append(self, position):
255252
if self.maxLength is not None and len(self.poseTrail) > self.maxLength:
256253
self.poseTrail.pop(0)
257254

258-
def render(self, viewMatrix, projectionMatrix):
259-
modelView = viewMatrix # pose trail is already in world coordinates -> model matrix is identity
260-
255+
def render(self):
261256
glLineWidth(self.lineWidth)
262-
glMatrixMode(GL_MODELVIEW)
263-
glLoadMatrixf(modelView.transpose())
264-
glMatrixMode(GL_PROJECTION)
265-
glLoadMatrixf(projectionMatrix.transpose())
266-
267257
glEnable(GL_BLEND)
268258
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
269259
glEnable(GL_LINE_SMOOTH)
@@ -289,13 +279,7 @@ def __init__(self, radius=20, length=1.0, color=np.array(DEFAULT_GRID_RGBA), ori
289279
self.lineWidth = lineWidth
290280
self.bounds = [-radius * length, radius * length]
291281

292-
def render(self, viewMatrix, projectionMatrix):
293-
modelView = viewMatrix # grid is defined in world coordinates
294-
glMatrixMode(GL_MODELVIEW)
295-
glLoadMatrixf(modelView.transpose())
296-
glMatrixMode(GL_PROJECTION)
297-
glLoadMatrixf(projectionMatrix.transpose())
298-
282+
def render(self):
299283
glLineWidth(self.lineWidth)
300284
glEnable(GL_BLEND)
301285
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
@@ -389,15 +373,11 @@ def __init__(self, projectionMatrix, color=np.array(DEFAULT_FRUSTUM_RGBA)):
389373
]
390374
]
391375

392-
def render(self, modelMatrix, viewMatrix, projectionMatrix, render2d):
393-
modelView = viewMatrix @ modelMatrix
376+
def render(self, modelMatrix, render2d):
377+
glPushMatrix()
378+
glMultMatrixf(modelMatrix.transpose())
394379

395380
glLineWidth(1)
396-
glMatrixMode(GL_MODELVIEW)
397-
glLoadMatrixf(modelView.transpose())
398-
glMatrixMode(GL_PROJECTION)
399-
glLoadMatrixf(projectionMatrix.transpose())
400-
401381
glEnable(GL_DEPTH_TEST)
402382
glEnable(GL_BLEND)
403383
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
@@ -413,6 +393,7 @@ def render(self, modelMatrix, viewMatrix, projectionMatrix, render2d):
413393
glEnd()
414394

415395
glDisable(GL_DEPTH_TEST)
396+
glPopMatrix()
416397

417398
def createPlaneMesh(scale, position, color):
418399
# 3 ---- 2

0 commit comments

Comments
 (0)