gpt4 book ai didi

c++ - 使用 stb 加载图像时出错

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

我正在尝试加载以下图片:

enter image description here

作为斯坦福龙的纹理。然而结果如下: enter image description here

我读到其他人遇到过这个问题,因为要么没有正确绑定(bind)纹理,要么在加载纹理时使用了错误数量的组件。我认为我没有这些问题中的任何一个,因为我正在检查图像的格式并绑定(bind)纹理。我已经设法让其他图像正确加载,所以这似乎是该图像特有的问题(我并不是说该图像已损坏,而是该图像与我试过的其他图像略有不同) .

我用来初始化纹理的代码如下:

//Main constructor
Texture::Texture(string file_path, GLuint t_target)
{
//Change the coordinate system of the image
stbi_set_flip_vertically_on_load(true);
int numComponents;
//Load the pixel data of the image
void *data = stbi_load(file_path.c_str(), &width, &height, &numComponents, 0);
if (data == nullptr)//Error check
{
cerr << "Error when loading texture from file: " + file_path << endl;

Log::record_log(
string(80, '!') +
"\nError when loading texture from file: " + file_path + "\n" +
string(80, '!')
);
exit(EXIT_FAILURE);
}
//Create the texture OpenGL object
target = t_target;
glGenTextures(1, &textureID);
glBindTexture(target, textureID);
//Name the texture
glObjectLabel(GL_TEXTURE, textureID, -1,
("\"" + extract_name(file_path) +"\"").c_str());
//Set the color format
color_format = numComponents == 3 ? GL_RGB : GL_RGBA;

glTexImage2D(target, 0, color_format, width, height, 0,
color_format, GL_UNSIGNED_BYTE, data);
//Set the texture parameters of the image
glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
//free the memory
stbi_image_free(data);
//Create a debug notification event
char name[100];
glGetObjectLabel(GL_TEXTURE, textureID, 100, NULL, name);
string message = "Succesfully created texture: " + string(name) +
". Bound to target: " + textureTargetEnumToString(target);
glDebugMessageInsert(GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_OTHER, 0,
GL_DEBUG_SEVERITY_NOTIFICATION, message.size(), message.c_str());
}

最佳答案

JPEG 是吧?那时可能没有 alpha channel 。 894 像素宽完全不能被 4 整除。

仔细检查您是否遇到了 numComponents == 3 情况,如果是,请确保 GL_UNPACK_ALIGNMENT 设置为 1(默认 4) 在 glTexImage2D() 调用之前使用 glPixelStorei()

关于c++ - 使用 stb 加载图像时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50127730/

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