实现渲染到顶点数组,glReadPixels 失败(无效操作)

发布于 2024-11-03 00:53:49 字数 941 浏览 1 评论 0原文

我正在尝试将顶点数据从纹理复制到顶点缓冲区,然后绘制顶点缓冲区。据我所知,最好的方法是将纹理绑定到 fbo,并使用 glReadPixels 将其复制到 vbo。但是,我似乎无法正常工作:glReadPixels 失败并出现错误“无效操作”。

欢迎更正、示例和替代方法。 :)

这是相关代码:

glEnable(GL_TEXTURE_2D)

w, h = 32, 32

vbo = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glBufferData(GL_ARRAY_BUFFER, sizeof(c_float)*w*h*4, None, GL_STREAM_COPY)
glBindBuffer(GL_ARRAY_BUFFER, 0)

fbo = glGenFramebuffersEXT(1)
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo)

tex = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, tex)
# tex params here
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, w, h, 0, GL_RGBA, GL_FLOAT, None)
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, tex, 0)

assert glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) == 36053

glReadBuffer(GL_COLOR_ATTACHMENT0_EXT)
glBindBuffer(GL_PIXEL_PACK_BUFFER, vbo)
glReadPixels(0, 0, w, h, GL_RGBA, GL_FLOAT, None) # invalid operation?

I'm trying to copy vertex data from a texture to a vertex buffer, and then draw the vertex buffer. As far as I know the best way to do this is to bind the texture to a fbo, and use glReadPixels to copy it to a vbo. However, I can't seem to get this working: glReadPixels fails with the error "invalid operation".

Corrections, examples and alternate methods welcome. :)

Here's the relevant code:

glEnable(GL_TEXTURE_2D)

w, h = 32, 32

vbo = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glBufferData(GL_ARRAY_BUFFER, sizeof(c_float)*w*h*4, None, GL_STREAM_COPY)
glBindBuffer(GL_ARRAY_BUFFER, 0)

fbo = glGenFramebuffersEXT(1)
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo)

tex = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, tex)
# tex params here
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, w, h, 0, GL_RGBA, GL_FLOAT, None)
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, tex, 0)

assert glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) == 36053

glReadBuffer(GL_COLOR_ATTACHMENT0_EXT)
glBindBuffer(GL_PIXEL_PACK_BUFFER, vbo)
glReadPixels(0, 0, w, h, GL_RGBA, GL_FLOAT, None) # invalid operation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

是伱的 2024-11-10 00:53:49

我自己已经解决了这个问题。

在这种情况下,ReadPixels 的最后一个参数用作偏移量而不是指针,并且不会由 pyopengl 自动转换,请使用:

glReadPixels(0, 0, w, h, GL_RGBA, GL_FLOAT, c_void_p(0)) # works!

I've solved the issue myself.

The last argument to ReadPixels is used as an offset instead of a pointer in this case, and is not automatically cast by pyopengl, use:

glReadPixels(0, 0, w, h, GL_RGBA, GL_FLOAT, c_void_p(0)) # works!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文