如何使用FBO存储整数VEC2并通过GlreadPixels读取纹理
我想生成一个带有两个频道的整数纹理,我希望第一个存储每个顶点的索引和第二个存储模型索引的索引的频道,因此此纹理中的每个像素都像(vertex_id,dertex_id, model_id)
,它们都是整数。 纹理绑定:
glBindTexture(GL_TEXTURE_2D, id_tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RG32UI, width, height, 0, GL_RG_INTEGER, GL_UNSIGNED_INT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, id_tex, 0);
glBindTexture(GL_TEXTURE_2D, 0);
阅读像素:
double x, y;
glfwGetCursorPos(window, &x, &y);
unsigned rg[2];
glReadBuffer(GL_COLOR_ATTACHMENT1);
glReadPixels(x, height - y - 1, 1, 1, GL_RG_INTEGER, GL_UNSIGNED_INT, rg);
glReadBuffer(GL_NONE);
cout << rg[0] << ' ' << rg[1] << endl;
着色器:
#version 450 core
layout( location = 0 ) out vec4 scene_color;
layout( location = 1 ) out vec2 vid_mid;
flat in int index;
in vec3 FragPos;
in vec3 Position;
in vec3 Normal;
uniform int mid;
void main(){
scene_color = vec4(Normal, 1.0);
vid_mid = vec2(index, mid);
}
这是我得到的而不是模型索引,所以红色是合理的):
但是,当我使用glreadpixels
以获取(x,y)
,我得到了:
,但是我实际想要的是(vertex_id,model_id)
<< << /strong>
I want to generate an integer texture with two channels, and I want the first channel to store the index of each vertex and the second channel to store the index of the model, so each pixel in this texture is like (vertex_id, model_id)
and they are both integers.
Texture binding:
glBindTexture(GL_TEXTURE_2D, id_tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RG32UI, width, height, 0, GL_RG_INTEGER, GL_UNSIGNED_INT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, id_tex, 0);
glBindTexture(GL_TEXTURE_2D, 0);
Read pixels:
double x, y;
glfwGetCursorPos(window, &x, &y);
unsigned rg[2];
glReadBuffer(GL_COLOR_ATTACHMENT1);
glReadPixels(x, height - y - 1, 1, 1, GL_RG_INTEGER, GL_UNSIGNED_INT, rg);
glReadBuffer(GL_NONE);
cout << rg[0] << ' ' << rg[1] << endl;
shader:
#version 450 core
layout( location = 0 ) out vec4 scene_color;
layout( location = 1 ) out vec2 vid_mid;
flat in int index;
in vec3 FragPos;
in vec3 Position;
in vec3 Normal;
uniform int mid;
void main(){
scene_color = vec4(Normal, 1.0);
vid_mid = vec2(index, mid);
}
This is what I got(since the vertex index is greater than the model index, so the red color is reasonable):
But when I use glReadPixels
to get the pixel color at (x, y)
, I got:
But what I actually want is the (vertex_id, model_id)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您将纹理创建为 GL_RG32UI,您还应该确保以正确的格式存储它。
假设你输出为浮点格式,如果你希望你的着色器输出unsigned int,它应该是:
和
If you create your texture as GL_RG32UI, you should also make sure to store it in the correct format.
Assumes that you output to a float format, if you want your shader to output unsigned int, it should be :
and