stbi_load()什么都不返回,但它不返回null

时间:2017-07-02 18:17:37

标签: c++ image opengl

我尝试使用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);
}

1 个答案:

答案 0 :(得分:0)

if (data = NULL)
         ^ perhaps you meant '=='?

不要用NULL覆盖data所有的内容。

Yoda conditionals你可能想要。