opengl glBufferSubData不工作,不显示错误

8xiog9wr  于 2022-11-04  发布在  其他
关注(0)|答案(1)|浏览(180)

我正在用glfw用python创建一个基本的体素渲染类。我使用glBufferSubData而不是glBufferData,但它不会渲染。下面是怀疑导致问题的代码:


# imports

import glfw, numpy as np
from OpenGL.GL import *
from ctypes import *
from core.texture_manager import *

glfw.init()

class VBOManager:
    def __init__(self, renderer):
        self.renderer = renderer
        self.run()

    def run(self):
        for i in self.renderer.to_add[:self.renderer.to_add_count]:
            self.renderer.vertices.extend(i[0])
            self.renderer.texCoords.extend(i[1])
            vertices = np.array(i[0], dtype=np.float32)
            texCoords = np.array(i[1], dtype=np.float32)

            # use glBufferSubData
            glBindBuffer(GL_ARRAY_BUFFER, self.renderer.vbo)
            glBufferData(GL_ARRAY_BUFFER, self.renderer.vertices.nbytes, vertices.nbytes, (c_float * len(vertices))(*vertices))
            glFlush()
            glVertexPointer(3, GL_FLOAT, 0, None)
            glTexCoordPointer(3, GL_FLOAT, 0, None)
            glBindBuffer(GL_ARRAY_BUFFER, self.renderer.vbo_1)
            glBufferSubData(GL_ARRAY_BUFFER, self.renderer.texCoords.nbytes, texCoords.nbytes, (c_float * len(texCoords))(*texCoords))
            glFlush()

            self.renderer.to_add.remove(i)

class TerrainRenderer:
    def __init__(self, window):
        self.window = window

        self.vertices = np.array([])
        self.texCoords = np.array([])

        self.to_add = []
        self.to_add_count = 256

        self.vbo, self.vbo_1 = glGenBuffers (2)
        glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
        glBufferData(GL_ARRAY_BUFFER, 12 * 4, None, GL_STATIC_DRAW)
        self.vbo_manager = VBOManager(self)

        self.texture_manager = TextureAtlas()

        glEnable(GL_TEXTURE_2D)
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
        glEnableClientState(GL_TEXTURE_COORD_ARRAY)
        glEnableClientState (GL_VERTEX_ARRAY)

    def render(self):
        try:
            self.vbo_manager.run()
        except RuntimeError:
            pass

        glClear (GL_COLOR_BUFFER_BIT)

        glEnable(GL_TEXTURE_2D)
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
        glBindBuffer (GL_ARRAY_BUFFER, self.vbo)
        glVertexPointer (3, GL_FLOAT, 0, None)
        glBindBuffer(GL_ARRAY_BUFFER, self.vbo_1)
        glTexCoordPointer(2, GL_FLOAT, 0, None)

        glDrawArrays (GL_QUADS, 0, len(self.vertices))
        glDisable(GL_TEXTURE_2D)
        glDisable(GL_BLEND)

    def add(self, posList, texCoords):
        self.to_add.append((numpy.array(posList), numpy.array(texCoords)))

当我启动使用这段代码的应用程序时,它没有显示日志、警告或错误,但是它也根本不渲染顶点,屏幕上什么都看不见。

h9vpoimq

h9vpoimq1#

glBufferSubData只更新缓冲区对象的数据存储,但不创建缓冲区对象的数据存储。您需要使用glBufferData创建缓冲区对象的数据存储,然后可以使用glBufferSubData更新数据。此外,glBufferSubData的第二个参数指定缓冲区对象的数据存储中的偏移量,数据替换将从该偏移量开始。
第一个

相关问题