glGetTexImage不适用于CVOpenGLTextureRef

时间:2011-11-22 21:49:54

标签: opengl textures pixel core-video glteximage2d

我正在使用OpenGL 2.1为Mac OS X编写应用程序

我有一个CVOpenGLTextureRef,它保存了我使用GL_QUADS渲染的纹理,一切正常。

我现在需要确定纹理的哪些像素是黑色的,因此我编写了这段代码来从纹理中读取原始数据:

//"image" is the CVOpenGLTextureRef

GLenum textureTarget = CVOpenGLTextureGetTarget(image);
GLuint textureName = CVOpenGLTextureGetName(image);

glEnable(textureTarget);
glBindTexture(textureTarget, textureName);

GLint textureWidth, textureHeight;
int bytes;
glGetTexLevelParameteriv(textureTarget, 0, GL_TEXTURE_WIDTH, &textureWidth);
glGetTexLevelParameteriv(textureTarget, 0, GL_TEXTURE_HEIGHT, &textureHeight);
bytes = textureWidth*textureHeight;

GLfloat buffer[bytes];
glGetTexImage(textureTarget, 0, GL_LUMINANCE, GL_FLOAT, &buffer);
GLenum error = glGetError();

glGetError()报告GL_NO_ERROR但是在调用glGetTexImage()后缓冲区保持不变...它仍然是空白的。

我做错了吗?

请注意,我无法使用glReadPixels(),因为我在渲染之前修改了纹理,我需要获取未修改纹理的原始数据。

编辑:我尝试使用后续方法,但我仍然没有缓冲区作为输出

unsigned char *buffer = (unsigned char *)malloc(textureWidth * textureHeight * sizeof(unsigned char));
glGetTexImage(textureTarget, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, buffer);

EDIT2:报告了同一问题herehere

2 个答案:

答案 0 :(得分:2)

试试这个:

glGetTexImage(textureTarget, 0, GL_LUMINANCE, GL_FLOAT, buffer);

也许你在想这个成语:

vector< GLfloat > buffer( bytes );
glGetTexImage(textureTarget, 0, GL_LUMINANCE, GL_FLOAT, &buffer[0]);

编辑:在回读之前设置包装对齐也是值得的:

glPixelStorei(GL_PACK_ALIGNMENT, 1);

答案 1 :(得分:0)

我发现这是glGetTexImage()相对于CVOpenGLTextureRef的允许行为。唯一可靠的方法是将纹理绘制到FBO中,然后使用glReadPixels()

从中读取