C++ 用机顶盒加载图像时出错
我正在尝试加载以下图像: 作为斯坦福龙的纹理。但结果如下: 我曾读到,其他人对此有问题,因为加载纹理时没有正确绑定纹理或使用了错误数量的组件。我想我没有这两个问题,因为我正在检查图像的格式和绑定纹理。我已经设法让其他图像正确加载,因此这似乎是一个特定于此图像的问题(我不是说此图像已损坏,而是说此图像与我尝试的其他图像略有不同) 我用于初始化纹理的代码如下所示:C++ 用机顶盒加载图像时出错,c++,opengl,graphics,textures,stb-image,C++,Opengl,Graphics,Textures,Stb Image,我正在尝试加载以下图像: 作为斯坦福龙的纹理。但结果如下: 我曾读到,其他人对此有问题,因为加载纹理时没有正确绑定纹理或使用了错误数量的组件。我想我没有这两个问题,因为我正在检查图像的格式和绑定纹理。我已经设法让其他图像正确加载,因此这似乎是一个特定于此图像的问题(我不是说此图像已损坏,而是说此图像与我尝试的其他图像略有不同) 我用于初始化纹理的代码如下所示: //Main constructor Texture::Texture(string file_path, GLuint t_tar
//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());
}
//主构造函数
纹理::纹理(字符串文件路径,GLuint t目标)
{
//更改图像的坐标系
stbi设置垂直翻转加载(真);
int numComponents;
//加载图像的像素数据
void*data=stbi_load(文件路径.c_str(),&width,&height,&numComponents,0);
if(data==nullptr)//错误检查
{
cerrJPEG呃?可能没有阿尔法通道。894像素宽并不能被4整除
仔细检查您是否正在点击numComponents==3
案例,如果是这样,请确保在glTexImage2D()之前使用glPixelStorei()
将GL\u UNPACK\u对齐设置为1
(默认4
)
call.您是否尝试过不垂直翻转图像?请删除该调用并查看是否有任何变化。您的回答是正确的。我应该如何处理事情,以便我的代码自己管理这种情况?在其他方面,我应该检查什么,我应该做什么来确保正确加载图像?@Makogan:始终设置GL\u UNPACK\u A对齐
到1
,无论宽度是否为4整除,都可以同时使用3分量图像和4分量图像。@Makogan:或者,只使用一个代码路径。