Implementing render-to-vertex-array, glReadPixels fails (invalid operation)

跟風遠走 提交于 2019-12-06 08:37:15

问题


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?

回答1:


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!


来源:https://stackoverflow.com/questions/5755478/implementing-render-to-vertex-array-glreadpixels-fails-invalid-operation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!