#python #opengl #pyopengl
Вопрос:
Я с трудом справляюсь с этой проблемой. Я провел над этим всю ночь, и мне действительно нужна помощь. Я хочу отобразить простой треугольник с привязками PyOpenGL, но мое приложение выходит из строя, плюс ничего не отображается. Я использую для проекта окно glfw — в нем ничего особенного не делается — и это настройки в моей функции InitGL.
glViewport(0, 0, 500, 500) glClearColor(0, 0, 0, 1) glClearDepth(1) glEnable(GL_BLEND); # activates blending mode glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glEnable(GL_DEPTH_TEST) # enables depth testing glEnable(GL_LIGHTING) glEnable(GL_LIGHT0)
Вот как выглядит мой игровой цикл:
while not window.shouldClose window.clear() render() window.swap_buffers() window.poll_events()
Но я думаю, что проблема, которую нужно исправить, находится где-то в следующем коде. Это функция рендеринга, вызываемая в игровом цикле:
def render(self): glUseProgram(self.shader_program) self.mesh_filter.bind() self.mesh_filter.render() lt;- this line make the applicaion crash after few loops self.mesh_filter.unbind() glUseProgram(0)
И код для моего фильтра сетки классов:
class MeshFilter(): def __init__(self, vertices: list, indices: list): self.vaoId = glGenVertexArrays(1) glBindVertexArray(self.vaoId) self.count = len(indices) self.__allocateIndexBuffer(indices) self.vbosId = [] self.__allocateAttributeBuffer(0, 4, vertices) glBindVertexArray(0) def __allocateIndexBuffer(self, indices): self.eboId = glGenBuffers(1) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.eboId) self.count = len(indices) indices = uint32(indices) glBufferData(GL_ELEMENT_ARRAY_BUFFER, ArrayDatatype.arrayByteCount(indices), indices, GL_STATIC_DRAW) def __allocateAttributeBuffer(self, attribute_index, attribute_size, buffer): vboId = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, vboId) self.vbosId.insert(attribute_index, vboId) buffer = float32(buffer) glBufferData(GL_ARRAY_BUFFER, ArrayDatatype.arrayByteCount(buffer), buffer, GL_STATIC_DRAW) glVertexAttribPointer(attribute_index, attribute_size, GL_FLOAT, GL_FALSE, 0, None) glBindBuffer(GL_ARRAY_BUFFER, 0) def bind(self): glBindVertexArray(self.vaoId); for attribute in range(len(self.vbosId)): glEnableVertexAttribArray(attribute) def unbind(self): for attribute in range(len(self.vbosId)): glDisableVertexAttribArray(attribute) glBindVertexArray(0) def render(self): glDrawElements(GL_TRIANGLES, self.count, GL_UNSIGNED_INT, 0) def destroy(self): glDeleteBuffers(1, [self.eboId]) glDeleteBuffers(len(self.vbosId), self.vbosId) glDeleteVertexArrays(1, [self.vaoId])
Сетчатый фильтр вызывается со следующими параметрами
индексы = [0, 1, 2] треугольник = [0.6, 0.6, 0, 1.0, -0.6, 0.6, 0, 1.0, 0.0, -0.6, 0, 1.0]
Не стесняйтесь спрашивать у меня коды или дополнительные объяснения.
Редактировать если я заменю элементы рисования на glDrawArrays(GL_TRIANGLES, 0, 3), это будет отлично работать.. Поэтому я предполагаю, что ошибка связана с хранением индексов.
Ответ №1:
ИЗМЕНИТЬ : РЕШЕНИЕ
glDrawElements
требуется None или nullptr в типах ctypes в качестве последнего аргумента, а не 0, как в моем предыдущем коде Java.
Вот так, glDrawElements(GL_TRIANGLES, self.count, GL_UNSIGNED_INT, None)