Skip to content

Commit d52c56b

Browse files
kaatrasakaatrasa
andauthored
Switch camera mesh to wireframe in visualizer (#9)
Co-authored-by: kaatrasa <[email protected]>
1 parent 8d9c5f7 commit d52c56b

File tree

2 files changed

+39
-46
lines changed

2 files changed

+39
-46
lines changed

python/cli/visualization/visualizer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def __init__(self, args=VisualizerArgs()):
281281
renderMesh=args.showMesh)
282282
self.poseTrail = PoseTrailRenderer(maxLength=args.poseTrailLength)
283283
self.grid = GridRenderer(radius=args.gridRadius, length=args.gridCellLength, origin=args.gridOrigin)
284-
self.cameraModelRenderer = MeshRenderer(createCameraModelMesh(scale=args.visualizationScale / 20.0))
284+
self.cameraModelRenderer = CameraWireframeRenderer()
285285
self.cameraFrustumRenderer = None # initialized later when camera projection matrix is available
286286

287287
# Recording

python/cli/visualization/visualizer_renderers/renderers.py

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
DEFAULT_GRID_RGBA = [0.75, 0.75, 0.75, 0.3]
99
DEFAULT_FRUSTUM_RGBA = [0.75, 0.75, 0.75, 0.4]
1010
DEFAULT_KEYFRAME_RGBA = [0.9, 0.4, 0.0, 0.75]
11-
DEFAULT_CAMERA_MODEL_RGB = [1.0, 0.0, 0.0]
11+
DEFAULT_CAMERA_RGBA = [1.0, 0.0, 0.0, 0.75]
1212

13-
class KeyFrameRenderer:
14-
def __init__(self, color=np.array(DEFAULT_KEYFRAME_RGBA), scale=0.05, lineWidth=2):
13+
class CameraWireframeRenderer:
14+
def __init__(self, color=np.array(DEFAULT_CAMERA_RGBA), scale=0.05, lineWidth=2):
1515
self.color = color
1616
self.lineWidth = lineWidth
1717
self.vertices = np.array([
@@ -33,7 +33,7 @@ def __init__(self, color=np.array(DEFAULT_KEYFRAME_RGBA), scale=0.05, lineWidth=
3333
[4, 1],
3434
]
3535

36-
def render(self, keyFrameCameraToWorldMatrices, viewMatrix, projectionMatrix):
36+
def render(self, modelMatrix, viewMatrix, projectionMatrix):
3737
glLineWidth(self.lineWidth)
3838
glEnable(GL_BLEND)
3939
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
@@ -42,6 +42,37 @@ def render(self, keyFrameCameraToWorldMatrices, viewMatrix, projectionMatrix):
4242
glEnable(GL_DEPTH_TEST)
4343
glColor4fv(self.color)
4444

45+
modelView = viewMatrix @ modelMatrix
46+
glMatrixMode(GL_MODELVIEW)
47+
glLoadMatrixf(modelView.transpose())
48+
glMatrixMode(GL_PROJECTION)
49+
glLoadMatrixf(projectionMatrix.transpose())
50+
51+
glBegin(GL_LINES)
52+
for edge in self.edges:
53+
p0 = self.vertices[edge[0]]
54+
p1 = self.vertices[edge[1]]
55+
glVertex3f(p0[0], p0[1], p0[2])
56+
glVertex3f(p1[0], p1[1], p1[2])
57+
glEnd()
58+
59+
glDisable(GL_BLEND)
60+
glDisable(GL_LINE_SMOOTH)
61+
glDisable(GL_DEPTH_TEST)
62+
63+
class KeyFrameRenderer:
64+
def __init__(self, color=np.array(DEFAULT_KEYFRAME_RGBA), scale=0.05, lineWidth=2):
65+
self.cameraWireframe = CameraWireframeRenderer(color, scale, lineWidth)
66+
67+
def render(self, keyFrameCameraToWorldMatrices, viewMatrix, projectionMatrix):
68+
glLineWidth(self.cameraWireframe.lineWidth)
69+
glEnable(GL_BLEND)
70+
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
71+
glEnable(GL_LINE_SMOOTH)
72+
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST)
73+
glEnable(GL_DEPTH_TEST)
74+
glColor4fv(self.cameraWireframe.color)
75+
4576
for kfId in keyFrameCameraToWorldMatrices:
4677
modelMatrix = keyFrameCameraToWorldMatrices[kfId]
4778
modelView = viewMatrix @ modelMatrix
@@ -51,9 +82,9 @@ def render(self, keyFrameCameraToWorldMatrices, viewMatrix, projectionMatrix):
5182
glLoadMatrixf(projectionMatrix.transpose())
5283

5384
glBegin(GL_LINES)
54-
for edge in self.edges:
55-
p0 = self.vertices[edge[0]]
56-
p1 = self.vertices[edge[1]]
85+
for edge in self.cameraWireframe.edges:
86+
p0 = self.cameraWireframe.vertices[edge[0]]
87+
p1 = self.cameraWireframe.vertices[edge[1]]
5788
glVertex3f(p0[0], p0[1], p0[2])
5889
glVertex3f(p1[0], p1[1], p1[2])
5990
glEnd()
@@ -383,44 +414,6 @@ def render(self, modelMatrix, viewMatrix, projectionMatrix, render2d):
383414

384415
glDisable(GL_DEPTH_TEST)
385416

386-
def createCameraModelMesh(scale=1.0, color=np.array(DEFAULT_CAMERA_MODEL_RGB)):
387-
scale = 0.2 * scale
388-
vertices = np.array([
389-
-0.5, -0.5, 0.0, # Vertex 0: Near top-left
390-
0.5, -0.5, 0.0, # Vertex 1: Near top-right
391-
0.5, 0.5, 0.0, # Vertex 2: Near bottom-right
392-
-0.5, 0.5, 0.0, # Vertex 3: Near bottom-left
393-
-1.0, -1.0, 1.0, # Vertex 4: Far top-left
394-
1.0, -1.0, 1.0, # Vertex 5: Far top-right
395-
1.0, 1.0, 1.0, # Vertex 6: Far bottom-right
396-
-1.0, 1.0, 1.0 # Vertex 7: Far bottom-left
397-
], dtype=np.float32)
398-
399-
vertices *= scale
400-
401-
colors = np.array([
402-
color,
403-
color,
404-
color,
405-
color,
406-
# add some shading
407-
0.5 * color,
408-
0.5 * color,
409-
0.5 * color,
410-
0.5 * color
411-
], dtype=np.float32)
412-
413-
triangles = np.array([
414-
0, 1, 2, 0, 2, 3, # Near face
415-
4, 5, 6, 4, 6, 7, # Far face
416-
0, 1, 5, 0, 5, 4, # Bottom face
417-
1, 2, 6, 1, 6, 5, # Right face
418-
2, 3, 7, 2, 7, 6, # Top face
419-
3, 0, 4, 3, 4, 7 # Left face
420-
], dtype=np.uint32)
421-
422-
return Mesh(vertices, colors, triangles)
423-
424417
def createPlaneMesh(scale, position, color):
425418
# 3 ---- 2
426419
# | |

0 commit comments

Comments
 (0)