-
Notifications
You must be signed in to change notification settings - Fork 2
/
ImageLoader.cpp
42 lines (31 loc) · 1.14 KB
/
ImageLoader.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
34
35
36
37
38
39
40
41
42
#include "ImageLoader.h"
#include "picoPNG.h"
#include "IOManager.h"
#include "Errors.h"
namespace NeroEngine{
GLTexture ImageLoader::loadPNG(std::string filePath){
GLTexture texture = {};
std::vector<unsigned char> in;
std::vector<unsigned char> out;
unsigned long width, height;
if (IOManager::readFileToBuffer(filePath, in) == false){
fatalError("Îļþ" + filePath + "¶Áȡʧ°Ü!");
}
int errorCode = decodePNG(out, width, height, &(in[0]), in.size());
if (errorCode != 0){
fatalError("decodePNG ʧ°Ü" + std::to_string(errorCode));
}
glGenTextures(1, &(texture.id));
glBindTexture(GL_TEXTURE_2D, texture.id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &(out[0]));
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_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
texture.width = width;
texture.height = height;
return texture;
}
}