如何使用FBO存储整数VEC2并通过GlreadPixels读取纹理

发布于 2025-01-18 16:17:00 字数 1808 浏览 1 评论 0原文

我想生成一个带有两个频道的整数纹理,我希望第一个存储每个顶点的索引和第二个存储模型索引的索引的频道,因此此纹理中的每个像素都像(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁把谁当真 2025-01-25 16:17:00

如果您将纹理创建为 GL_RG32UI,您还应该确保以正确的格式存储它。

layout( location = 1 ) out vec2 vid_mid;

假设你输出为浮点格式,如果你希望你的着色器输出unsigned int,它应该是:

layout( location = 1 ) out uvec2 vid_mid;

vid_mid = uvec2(index, mid);

If you create your texture as GL_RG32UI, you should also make sure to store it in the correct format.

layout( location = 1 ) out vec2 vid_mid;

Assumes that you output to a float format, if you want your shader to output unsigned int, it should be :

layout( location = 1 ) out uvec2 vid_mid;

and

vid_mid = uvec2(index, mid);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文