У меня есть сцена, которую нужно быстро отрендерить. Таким образом, я успешно генерирую вершины треугольников как numpy, загружаю их в GPU и очень быстро рендеринг. Пока все хорошо.
Проблема начинается, когда некоторые элементы в сцене должны исчезнуть со сцены. Что я делаю, так это обхожу весь массив и отфильтровываю вершины элементов сцены, которые больше не нужно рисовать, загружаю их снова в графический процессор и затем отрисовываю. Это, очевидно, занимает несколько секунд, что недопустимо. Я ищу быстрый способ изменить массив и перерисовать сцену.
Это упрощенная версия кода, который я использую: (Также был бы признателен за любые комментарии о самом коде)
from OpenGL.GL import *
from OpenGL.GLU import *
import numpy as np
import ctypes
vertex_dtype = [("position", np.float32, 3), ("color", np.float32, 4)]
vertex_array_object = None
vertex_buffer = None
shader_program = .......
def upload_to_gpu(vertices_np):
global vertex_array_object, vertex_buffer
if vertex_array_object is None:
vertex_array_object = glGenVertexArrays(1)
if vertex_buffer is None:
vertex_buffer = glGenBuffers(1)
glBindVertexArray(vertex_array_object)
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer)
glBufferData(GL_ARRAY_BUFFER, vertices_np.nbytes, vertices_np, GL_STATIC_DRAW)
stride = vertices_np.strides[0]
offset = ctypes.c_void_p(0)
position_location = glGetAttribLocation(shader_program, 'a_position')
glEnableVertexAttribArray(position_location)
glVertexAttribPointer(position_location, 3, GL_FLOAT, False, stride, offset)
offset = ctypes.c_void_p(vertices_np.dtype["position"].itemsize)
color_location = glGetAttribLocation(shader_program, "a_color")
glEnableVertexAttribArray(color_location)
glVertexAttribPointer(color_location, 4, GL_FLOAT, False, stride, offset)
glBindVertexArray(0)
glBindBuffer(GL_ARRAY_BUFFER, 0)
def create_np_array(all_vertices):
vertices_np = np.zeros(len(all_vertices), dtype=vertex_dtype)
cur_idx = 0
for vertex in all_vertices:
if ....should_ignore_triangle....:
continue
vertices_np[cur_idx]["position"] = [vertex[0], vertex[1], vertex[2]]
vertices_np[cur_idx]["color"] = ......
cur_idx += 1
vertices_np = vertices_np[:cur_idx]
return vertices_np
def draw_scene(vertices_np):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glUseProgram(shader_program)
# Set view and projection matrix
glBindVertexArray(vertex_array_object)
glDrawArrays(GL_TRIANGLES, 0, vertices_np.shape[0])
def main():
all_vertices = [[0, 0, 0], [1, 0, 0], [0, 1, 0], ......] # long list of ~million vertices
vertices_np = create_np_array(all_vertices)
upload_to_gpu(vertices_np)
# Now we are ready to start rendering
draw_scene(vertices_np) # works fine
# Now something changes and we need to regenerate the vertices_np and upload it again
vertices_np = create_np_array(all_vertices) # <-- THIS TAKES TOO MUCH TIME
upload_to_gpu(vertices_np)
draw_scene(vertices_np) # works fine