瓶颈是 for 循环和glBegin
/glEnd
顺序。数据是从每一帧的文件中读取的。
请注意,通过使用绘图glBegin/glEnd https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glBegin.xml序列和固定函数矩阵堆栈几十年来已被弃用。
在启动时读取一次文件并创建一个顶点缓冲区对象 https://www.khronos.org/opengl/wiki/Vertex_Specification#Vertex_Buffer_Object。
(阅读更多关于顶点规格 https://www.khronos.org/opengl/wiki/Vertex_Specification and Shader https://www.khronos.org/opengl/wiki/Shader最先进的渲染方式。)
与现有代码最接近的解决方案是使用客户端功能glEnableClientState https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glEnableClientState.xml和固定的功能属性glVertexPointer https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glVertexPointer.xml。使用此解决方案,您不需要任何着色器程序。
在下文中我假设您使用PyOpenGL http://pyopengl.sourceforge.net/.
将顶点坐标加载到数组中
def LoadVertices(pcd_files):
vertices = []
for i in pcd_files:
pc = pypcd.PointCloud.from_path(i)
number_of_points = pc.get_metadata().get('points')
z = pc.pc_data['z']
x = pc.pc_data['x']
y = pc.pc_data['y']
for j in range(number_of_points):
vertices += [x[j], y[j], z[j]]
return vertices
创建一个顶点缓冲区对象 https://www.khronos.org/opengl/wiki/Vertex_Specification#Vertex_Buffer_Object并创建并初始化缓冲区对象的数据存储:
import ctypes
def CreateBuffer(vertices):
bufferdata = (ctypes.c_float*len(vertices))(*vertices) # float buffer
buffersize = len(vertices)*4 # buffer size in bytes
vbo = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glBufferData(GL_ARRAY_BUFFER, buffersize, bufferdata, GL_STATIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER, 0)
return vbo
创建一个可以绘制的函数点基元 https://www.khronos.org/opengl/wiki/Primitive#Point_primitives从缓冲区:
def DrawBuffer(vbo, noOfVertices):
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glEnableClientState(GL_VERTEX_ARRAY)
glVertexPointer(3, GL_FLOAT, 0, None)
glDrawArrays(GL_POINTS, 0, noOfVertices)
glDisableClientState(GL_VERTEX_ARRAY)
glBindBuffer(GL_ARRAY_BUFFER, 0)
在您的程序中使用此函数:
files = glob.glob(os.getcwd() + "\\" + PCD_OutPutDirectory + "\*.pcd")
pygame.init()
display = (1700, 1000)
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
vArray = LoadVertices(files)
noPoints = len(vArray) // 3
bufferObj = CreateBuffer(vArray)
gluPerspective(50, (display[0] / display[1]), 0.1, 5000)
glTranslatef(0, 0, -1000)
Clock = pygame.time.Clock()
while True:
Clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
glRotatef(2, 1, 1, 3)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
DrawBuffer(bufferObj, noPoints)
pygame.display.flip()
如果你想为每个点添加单独的颜色,那么顶点及其属性不仅仅由坐标组成(x, y, z)
,它还必须RGB颜色 https://en.wikipedia.org/wiki/RGB_color_model (x, y, z, r, g, b)
,因此每个属性元组由 6 个分量组成,而不是 3 个。
固定功能颜色属性必须由客户端状态启用GL_COLOR_ARRAY
。添加属性由指定glColorPointer https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glColorPointer.xml.
每个属性元组的大小为 24 字节,因为元组由 6 个组件组成(x, y, z, r, g, b)
每个组件的大小为 4 字节(这是float
).
该大小必须传递给第三个参数(sride
) of glVertexPointer
分别glColorPointer
.
如果绑定了命名缓冲区对象,则最后一个参数glVertexPointer
分别glColorPointer
被视为缓冲区对象缓冲区存储中的字节偏移量。偏移量是属性第一个组成部分的字节数。
的情况下glVertexPointer
偏移量为 0,因为(x, y, z)
是属性元组中的第一个组件。
的情况下glColorPointer
偏移量是 3*4=12 字节,因为(r, g, b)
在3个坐标之后(x, y, z)
每个组件的大小为4。
由于最后一个参数的类型是指针,因此必须将偏移量转换为ctypes.c_void_p
(e.g. ctypes.c_void_p(3*4)
)。对于这个Python内置库ctypes https://stackoverflow.com/questions/56810151/glbuffersubdata-havent-implemented-type-inference-for-lists-yet-instanced-r/56810999#56810999必须进口。如果偏移量为0,None
可以用来代替ctypes.c_void_p(0)
.
顶点属性的规范可能如下所示:
glBindBuffer(GL_ARRAY_BUFFER, vbo)
stride = 6*4 # (24 bates) : [x, y, z, r, g, b] * sizeof(float)
glEnableClientState(GL_VERTEX_ARRAY)
glVertexPointer(3, GL_FLOAT, stride, None)
glEnableClientState(GL_COLOR_ARRAY)
offset = 3*4 # (12 bytes) : the rgb color starts after the 3 coordinates x, y, z
glColorPointer(3, GL_FLOAT, stride, ctypes.c_void_p(offset))
全部一起:
import ctypes
def LoadVertices(pcd_files):
attributes = []
for i in pcd_files:
pc = pypcd.PointCloud.from_path(i)
number_of_points = pc.get_metadata().get('points')
z = pc.pc_data['z']
x = pc.pc_data['x']
y = pc.pc_data['y']
r = # set the RGB color data here
g =
b =
for j in range(number_of_points):
attributes += [x[j], y[j], z[j], r[j], g[j], b[j]]
return attributes
def CreateBuffer(attributes):
bufferdata = (ctypes.c_float*len(attributes))(*attributes) # float buffer
buffersize = len(attributes)*4 # buffer size in bytes
vbo = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glBufferData(GL_ARRAY_BUFFER, buffersize, bufferdata, GL_STATIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER, 0)
return vbo
def DrawBuffer(vbo, noOfVertices):
glBindBuffer(GL_ARRAY_BUFFER, vbo)
stride = 6*4 # (24 bates) : [x, y, z, r, g, b] * sizeof(float)
glEnableClientState(GL_VERTEX_ARRAY)
glVertexPointer(3, GL_FLOAT, stride, None)
glEnableClientState(GL_COLOR_ARRAY)
offset = 3*4 # (12 bytes) : the rgb color starts after the 3 coordinates x, y, z
glColorPointer(3, GL_FLOAT, stride, ctypes.c_void_p(offset))
glDrawArrays(GL_POINTS, 0, noOfVertices)
glDisableClientState(GL_VERTEX_ARRAY)
glDisableClientState(GL_COLOR_ARRAY)
glBindBuffer(GL_ARRAY_BUFFER, 0)
files = glob.glob(os.getcwd() + "\\" + PCD_OutPutDirectory + "\*.pcd")
pygame.init()
display = (800, 600)
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
vArray = LoadVertices(files)
noPoints = len(vArray) // 6 # 6 components per attribute tuple
bufferObj = CreateBuffer(vArray)
gluPerspective(50, (display[0] / display[1]), 0.1, 5000)
glTranslatef(0, 0, -1000)
Clock = pygame.time.Clock()
while True:
Clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
glRotatef(2, 1, 1, 3)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
DrawBuffer(bufferObj, noPoints)
pygame.display.flip()