Skip to content

Instantly share code, notes, and snippets.

@ialhashim
Last active July 18, 2023 20:56
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ialhashim/ca91bac20a72c9c6ab9d8a1126ff7285 to your computer and use it in GitHub Desktop.
Save ialhashim/ca91bac20a72c9c6ab9d8a1126ff7285 to your computer and use it in GitHub Desktop.
Quick python script to draw a dynamic point cloud with changing colors and positions (e.g. RGBD frames)
from OpenGL import GL, GLU
from OpenGL.arrays import vbo
# Prepare point cloud once
def makeCloud(self):
width, height = self.rgb.shape[1], self.rgb.shape[0]
# ...
# Compute 3D positions from depth
# ...
# ...
points = np.dstack((x,y,z)).reshape((height, width, 3)).reshape((height * width, 3))
colors = self.rgb.reshape((height * width, 3))
# Save to self original data
self.pos = points.reshape(height * width, 3).astype('float32')
self.col = colors.reshape(height * width, 3).astype('float32')
# Create VBO
pos_vbo = vbo.VBO(data=self.pos, usage=GL.GL_DYNAMIC_DRAW, target=GL.GL_ARRAY_BUFFER)
col_vbo = vbo.VBO(data=self.col, usage=GL.GL_DYNAMIC_DRAW, target=GL.GL_ARRAY_BUFFER)
# Draw a dynamic point cloud with chaning colors and positions (e.g. RGBD)
def drawPointCloud(self):
# Update data
self.pos_vbo.set_array(self.pos)
self.col_vbo.set_array(self.col)
# Point size
GL.glPointSize(4)
# Position
self.pos_vbo.bind()
GL.glEnableVertexAttribArray(0)
GL.glVertexAttribPointer(0, 3, GL.GL_FLOAT, GL.GL_FALSE, 0, None)
# Color
self.col_vbo.bind()
GL.glEnableVertexAttribArray(3)
GL.glVertexAttribPointer(3, 3, GL.GL_FLOAT, GL.GL_FALSE, 0, None)
# Draw
GL.glDrawArrays(GL.GL_POINTS, 0, (640 * 480))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment