使用 stb 加载图像时出错
Error when loading image with stb
我正在尝试加载以下图片:
作为斯坦福龙的纹理。然而结果如下:
我了解到其他人遇到过此问题,因为要么没有正确绑定纹理,要么在加载纹理时使用了错误数量的组件。我认为我没有这些问题中的任何一个,因为我正在检查图像的格式并绑定纹理。我已经设法让其他图像正确加载,所以这似乎是该图像特有的问题(我并不是说该图像已损坏,而是该图像与我试过的其他图像略有不同) .
我用来初始化纹理的代码如下:
//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 通道。 894 像素宽相当 不能被 4 整除。
仔细检查您是否遇到 numComponents == 3
情况,如果是,请确保 GL_UNPACK_ALIGNMENT
设置为 1
(默认 4
)且 glPixelStorei()
在你的 glTexImage2D()
电话之前。
我正在尝试加载以下图片:
作为斯坦福龙的纹理。然而结果如下:
我了解到其他人遇到过此问题,因为要么没有正确绑定纹理,要么在加载纹理时使用了错误数量的组件。我认为我没有这些问题中的任何一个,因为我正在检查图像的格式并绑定纹理。我已经设法让其他图像正确加载,所以这似乎是该图像特有的问题(我并不是说该图像已损坏,而是该图像与我试过的其他图像略有不同) .
我用来初始化纹理的代码如下:
//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 通道。 894 像素宽相当 不能被 4 整除。
仔细检查您是否遇到 numComponents == 3
情况,如果是,请确保 GL_UNPACK_ALIGNMENT
设置为 1
(默认 4
)且 glPixelStorei()
在你的 glTexImage2D()
电话之前。