-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextureLoader.cpp
33 lines (30 loc) · 923 Bytes
/
TextureLoader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#define STB_IMAGE_IMPLEMENTATION
#include "TextureLoader.h"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include "stb_image.h"
void TextureLoader::loadNewAsset(std::string name, std::string fileDir)
{
int width, height, nrChannels;
unsigned char* data = stbi_load(fileDir.c_str(), &width, &height, &nrChannels, STBI_rgb_alpha);
if (!data) {
std::cout << "Failed to load texture: " << name << std::endl;
}
glGenTextures(1, &assetMap[name]);
glBindTexture(GL_TEXTURE_2D, assetMap[name]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
// Éú³ÉMipmap
glGenerateMipmap(GL_TEXTURE_2D);
// ÊÍ·ÅdataÄÚ´æ
stbi_image_free(data);
std::cout << "Load " << name << " texture Successfully." << std::endl;
};
unsigned int TextureLoader::getAsset(std::string name)
{
return assetMap[name];
}
int TextureLoader::getNumOfAsset()
{
return assetMap.size();
}