下载
使用
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
unsigned int textureFromFile(const char *path, const std::string &directory)
{
unsigned int textureID;
std::string filename = std::string(path);
filename = directory + '/' + filename;
int width, height, nrComponents;
unsigned char *data = stbi_load(filename.c_str(), &width, &height, &nrComponents, 0);
if (data)
{
GLenum format = GL_RGB;
if ( nrComponents == 1 )
format = GL_RED;
else if ( nrComponents == 3 )
format = GL_RGB;
else if ( nrComponents == 4 )
format = GL_RGBA;
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
stbi_image_free(data);
}
else
{
printf("error: Texture failed to load with path:%s\n", path);
stbi_image_free(data);
}
}
参考