gpt4 book ai didi

c++ - OpenGL 黑色纹理

转载 作者:行者123 更新时间:2023-11-30 05:29:02 24 4
gpt4 key购买 nike

尝试使用位图纹理渲染 2D 标签时,我的纹理采样返回黑色。我尝试使用 RenderDoc 进行调试,但找不到问题所在。看起来纹理加载正常并存储在正确的寄存器中,但它仍然呈现黑色。

我什至尝试使用全红色纹理来检查纹理坐标,但纹理仍然显示为黑色。

Pipeline texture Texture output output这是我用于加载/渲染纹理的代码。它试图渲染标签。 (genMipMaps 为假)。

void Texture::CreateGLTextureWithData(GLubyte* data, bool genMipMaps) {
if (bitmap)
glDeleteTextures(1, &bitmap);

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

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

if (genMipMaps)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
else
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.x, size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);

if (genMipMaps)
glGenerateMipmap(GL_TEXTURE_2D);
}

自定义采样器:

glGenSamplers(1, &linearSampler);
glSamplerParameteri(linearSampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glSamplerParameteri(linearSampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glSamplerParameteri(linearSampler, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

glSamplerParameteri(linearSampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glSamplerParameteri(linearSampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glSamplerParameteri(linearSampler, GL_TEXTURE_COMPARE_FUNC, GL_NEVER);

glSamplerParameterf(linearSampler, GL_TEXTURE_MIN_LOD, 0);
glSamplerParameterf(linearSampler, GL_TEXTURE_MAX_LOD, GL_TEXTURE_MAX_LOD);
glBindSampler(0, linearSampler);

渲染:

glDisable(GL_DEPTH_TEST);
glDisable(GL_STENCIL_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, texture->getBitmap());
// Set index and vertex buffers
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);


if (glIsBuffer(vertBuffer) && vertBuffer != lastVertBuffer)
glBindBuffer(GL_ARRAY_BUFFER, vertBuffer);

if (glIsBuffer(indexBuffer) && indexBuffer != lastIndexBuffer)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);

shader->updateLayout();

// Draw
const void* firstIndex = reinterpret_cast<const void*>(0);
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_SHORT, firstIndex);

glBindVertexArray(0);

着色器:

对比:

#version 430 core

// Vertex atributes
in vec3 a_position;
in vec2 a_texture;
in vec4 a_color;


// Constant buffers
layout(std140) uniform VertexBlock
{
mat4 u_wvp;
mat4 u_world;
};

// Vertex shader outputs
out vec2 v_texture;
out vec4 v_color;

void main()
{
v_texture = a_texture;
v_color = a_color;
gl_Position = u_wvp * vec4(a_position, 1.0);
}

FS:(我尝试在不使用纹理的情况下将颜色设置为红色,并且它正确地呈现为红色。)

#version 430 core

in vec4 v_color;
in vec2 v_texture;

layout(binding = 0) uniform sampler2D u_texture;

out vec4 fragColor;

void main()
{
fragColor = v_color * texture(u_texture, v_texture);
}

最佳答案

找出问题所在。我没有为 2D 纹理生成 mipmap,但采样器仍在使用它们。

纹理加载:

...
if (genMipMaps)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
else
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.x, size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);

if (genMipMaps)
glGenerateMipmap(GL_TEXTURE_2D);
...

采样器:

...
glSamplerParameteri(linearSampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glSamplerParameteri(linearSampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
...

我所做的修复就是始终为纹理生成 mipmap

关于c++ - OpenGL 黑色纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36574558/

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