stbi_load() 不返回任何内容,但不返回 null

stbi_load() returns nothing, but it doesnt return null

本文关键字:返回 null 任何内 stbi load      更新时间:2023-10-16

我正在尝试使用stb_image,当我使用stbi_load时,什么都没有返回。我正在使用Visual Studio 2017,我的图像位于C:/Users/michael/Documents/Visual Studio 2017/Projects/OpenGL Testing/OpenGL Testing/textures/test.png

这是我的代码:

#include "texture.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
Texture::Texture(const std::string& filename,unsigned int unit)
{
//Load the textures
int width, height, numComponents;
;
std::cout << filename << std::endl;
unsigned char* data = stbi_load(filename.c_str(),&width,&height,&numComponents,4);
std::cout << data << std::endl;
//Check for errors
if (data = NULL) {
std::cerr << "Failed to load texture: " + filename;
}
//Generate and bind the textures
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D,texture);
//Set texture parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//Send texture to GPU
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
}
void Texture::Bind(unsigned int unit) 
{
assert(unit >= 0 && unit <= 31);
glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_2D, texture);
}
Texture::~Texture()
{
glDeleteTextures(1, &texture);
}
if (data = NULL)
^ perhaps you meant '=='?

不要用 NULL 覆盖data的任何内容。

你可能想要的尤达条件。