OpenGL glReadPixels returns 0
OpenGL glReadPixels returns 0
当我渲染到屏幕缓冲区时一切正常,但是当使用 glReadPixels 从 FrameBuffer 读取像素时它总是 returns 0。
伪代码如下:
绑定纹理到FrameBuffer:
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
int width, height;
width = 2;
height = 2;
float texture_data[] = {
1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f
};
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_FLOAT, texture_data);
GLuint framebuffer;
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
GLint texture_coord_attribute = glGetAttribLocation(program, "texture_coord");
glEnableVertexAttribArray(texture_coord_attribute);
glVertexAttribPointer(texture_coord_attribute, 2, GL_FLOAT, GL_FALSE,
sizeof(vertices[0]), (void*)(sizeof(float) * 5));
Fragment/Vertex 着色器:
static const char* vertex_shader_text =
"#version 330\n""
"attribute vec2 vPos;\n"
"attribute vec2 texture_coord;\n"
"varying vec2 Texcoord;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(vPos, 0.0, 1.0);\n"
" Texcoord = texture_coord;\n"
"}\n";
static const char* fragment_shader_text =
"#version 330\n"
"varying vec2 Texcoord;\n"
"uniform sampler2D tex;\n"
"void main()\n"
"{\n"
" gl_FragColor = texture(tex, Texcoord);\n"
"}\n";
在主循环中读取像素:
glViewport(0, 0, 720, 480);
glUseProgram(program);
glDrawArrays(GL_TRIANGLES, 0, 6);
GLubyte pixels[3] = {0};
glReadBuffer(GL_COLOR_ATTACHMENT0);
glReadPixels(360, 240, 1, 1, GL_RGB, GL_FLOAT, pixels);
// Any value returns 0 not only 360 and 240
printf("|%f||%f||%f|\n", pixels[0], pixels[1], pixels[2]);
glfwSwapBuffers(window);
glfwPollEvents();
这是我遵循的管道。这里有什么问题?
谢谢
glReadPixels
的第5和第6个参数(format
和type
)指定target像素数据的格式和数据类型.
由于要读取元素数据类型为 GLubyte
的缓冲区,因此类型必须为 GL_BYTE
.
像这样更改您的代码:
glReadPixels(360, 240, 1, 1, GL_RGB, GL_BYTE, pixels);
或者读取数据到GLfloat
类型的缓冲区:
GLfloat pixels[3];
glReadPixels(360, 240, 1, 1, GL_RGB, GL_FLOAT, pixels);
请注意,您所做的是将 12 个字节 (sizeof(float)*3
) 读取到大小为 3 个字节 (GLubyte pixels[3]
) 的缓冲区。这意味着代表红色通道的浮点值的一部分被存储到缓冲区中。其余的覆盖一些访问错误的内存。
当我渲染到屏幕缓冲区时一切正常,但是当使用 glReadPixels 从 FrameBuffer 读取像素时它总是 returns 0。
伪代码如下:
绑定纹理到FrameBuffer:
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
int width, height;
width = 2;
height = 2;
float texture_data[] = {
1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f
};
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_FLOAT, texture_data);
GLuint framebuffer;
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
GLint texture_coord_attribute = glGetAttribLocation(program, "texture_coord");
glEnableVertexAttribArray(texture_coord_attribute);
glVertexAttribPointer(texture_coord_attribute, 2, GL_FLOAT, GL_FALSE,
sizeof(vertices[0]), (void*)(sizeof(float) * 5));
Fragment/Vertex 着色器:
static const char* vertex_shader_text =
"#version 330\n""
"attribute vec2 vPos;\n"
"attribute vec2 texture_coord;\n"
"varying vec2 Texcoord;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(vPos, 0.0, 1.0);\n"
" Texcoord = texture_coord;\n"
"}\n";
static const char* fragment_shader_text =
"#version 330\n"
"varying vec2 Texcoord;\n"
"uniform sampler2D tex;\n"
"void main()\n"
"{\n"
" gl_FragColor = texture(tex, Texcoord);\n"
"}\n";
在主循环中读取像素:
glViewport(0, 0, 720, 480);
glUseProgram(program);
glDrawArrays(GL_TRIANGLES, 0, 6);
GLubyte pixels[3] = {0};
glReadBuffer(GL_COLOR_ATTACHMENT0);
glReadPixels(360, 240, 1, 1, GL_RGB, GL_FLOAT, pixels);
// Any value returns 0 not only 360 and 240
printf("|%f||%f||%f|\n", pixels[0], pixels[1], pixels[2]);
glfwSwapBuffers(window);
glfwPollEvents();
这是我遵循的管道。这里有什么问题? 谢谢
glReadPixels
的第5和第6个参数(format
和type
)指定target像素数据的格式和数据类型.
由于要读取元素数据类型为 GLubyte
的缓冲区,因此类型必须为 GL_BYTE
.
像这样更改您的代码:
glReadPixels(360, 240, 1, 1, GL_RGB, GL_BYTE, pixels);
或者读取数据到GLfloat
类型的缓冲区:
GLfloat pixels[3];
glReadPixels(360, 240, 1, 1, GL_RGB, GL_FLOAT, pixels);
请注意,您所做的是将 12 个字节 (sizeof(float)*3
) 读取到大小为 3 个字节 (GLubyte pixels[3]
) 的缓冲区。这意味着代表红色通道的浮点值的一部分被存储到缓冲区中。其余的覆盖一些访问错误的内存。