【发布时间】:2018-01-23 20:09:52
【问题描述】:
使用 Angle 对 UWP 应用程序进行编程以运行 OpenGL ES,我遇到了使用 glReadPixels 从帧缓冲区对象读取基本操作的问题。
从 Visual Studio 模板“OpenglES2 Application (Android,iOS,Windows Universal)”开始,我可以将默认场景渲染检索到内存缓冲区中。
初始化:
void SimpleRenderer::InitFbo()
{
int buf_size = tex_height*tex_width * 4;
mReadBuf = new char[buf_size];
memset(mReadBuf, 123, buf_size); // arbitrary value to detect changes
}
绘制函数:
void SimpleRenderer::Draw()
{
// drawing calls here
// (...)
glReadPixels(0, 0, tex_width, tex_height, GL_RGBA, GL_UNSIGNED_BYTE, mReadBuf);
// success : mReadBuf is updated with pixel values
}
但如果我只是创建一个帧缓冲区对象,尝试在其中绘制并检索结果,glReadPixels 不返回任何值,glError 返回INVALID_FRAMEBUFFER_OPERATION。
初始化:
void SimpleRenderer::InitFbo()
{
glGenFramebuffers(1, &mRenderFbo);
glGenTextures(1, &mRenderTexture);
int buf_size = tex_height*tex_width * 4;
char*buf = new char[buf_size];
memset(buf, 255, buf_size);
glBindTexture(GL_TEXTURE_2D, mRenderTexture);
glTexImage2D(GL_TEXTURE_2D, 0, 4,
tex_width,
tex_height,
0, GL_RGBA, GL_UNSIGNED_BYTE,
buf);
glBindFramebuffer(GL_FRAMEBUFFER, mRenderFbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
mRenderTexture, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
delete[] buf;
mReadBuf = new char[buf_size];
memset(mReadBuf, 123, buf_size);
}
绘制函数:
void SimpleRenderer::Draw()
{
glBindFramebuffer(GL_FRAMEBUFFER, mRenderFbo);
// drawing calls here
// (...)
glReadPixels(0, 0, tex_width, tex_height, GL_RGBA, GL_UNSIGNED_BYTE, mReadBuf);
// failure : mReadBuf is unchanged
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
*edit - 解决了感谢下面的评论。纹理的正确初始化是
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
tex_width,
tex_height,
0, GL_RGBA, GL_UNSIGNED_BYTE,
buf);
作为记录,在解决之前,我使用 glCheckFramebufferStatus 检查帧缓冲区状态,它返回了 GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT。
【问题讨论】:
-
完成;抱歉,我到处找,但找不到验证答案的方法。感谢您的帮助。
标签: c++ uwp opengl-es textures opengl-es-2.0