gpt4 book ai didi

c++ - OpenGL 灰度纹理作为浮点格式错误

转载 作者:行者123 更新时间:2023-11-30 03:20:49 25 4
gpt4 key购买 nike

我得到了一个浮点值在 0.0 - 1.0 范围内的数组(高度图),但纹理返回时要么是黑色,要么只是红色 我已经尝试了内部格式和源格式的多种组合,但似乎没有任何效果。我当前的代码只是给我一个红色纹理:

glGenTextures(1, &texure);
glBindTexture(GL_TEXTURE_2D, texure);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, w, h, 0, GL_RED, GL_FLOAT, data);

[编辑] 图像输出: enter image description here任何想法出了什么问题?

最佳答案

您必须设置纹理混合参数,以便在查找纹理时将 OpenGL 从红色 channel 读取绿色和蓝色 channel - 参见 glTexParameteri :

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_RED );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED );

参见 OpenGL 4.6 API Compatibility Profile Specification; 16.1 Texture Environments and Texture Functions; page 595 :

They are derived by computing the base color Cb and Ab from the filtered texture values Rt, Gt, Bt, At, Lt, and It as shown in table 16.1, ...

Texture Base Texture base color Internal Format    Cb              Ab
...
RED (Rt, 0, 0) 1
RG (Rt, Gt, 0) 1
RGB (Rt, Gt, Bt) 1
RGBA (Rt, Gt, Bt) At

Table 16.1: Correspondence of filtered texture components to texture base components.

...followed by swizzling the components of Cb, controlled by the values of the texture parameters TEXTURE_SWIZZLE_R, TEXTURE_SWIZZLE_G, TEXTURE_SWIZZLE_B, and TEXTURE_SWIZZLE_A. If the value of TEXTURE_SWIZZLE_R is denoted by swizzler, swizzling computes the first component of Cs according to

if (swizzler == RED)
Cs[0] = Cb[0];
else if (swizzler == GREEN)
Cs[0] = Cb[1];
else if (swizzler == BLUE)
Cs[0] = Cb[2];
else if (swizzler == ALPHA)
Cs[0] = Ab;
else if (swizzler == ZERO)
Cs[0] = 0;
else if (swizzler == ONE)
Cs[0] = 1; // float or int depending on texture component type

如果您使用着色器程序,则可以在片段着色器中执行类似的操作,作者 Swizzling :

in vec3 uv;

out vec4 frag_color;

uniform sampler2D u_texture;

void main()
{
frag_color = texture(u_texture, uv).rrra;
}

关于c++ - OpenGL 灰度纹理作为浮点格式错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52531345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com