-
Notifications
You must be signed in to change notification settings - Fork 1
/
Texture.cpp
50 lines (34 loc) · 1.13 KB
/
Texture.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
43
44
45
46
47
48
49
#include "Texture.h"
#include "lodepng.h"
#include "stdio.h"
Texture::Texture(){
glGenTextures(1, &ID);
}
Texture::Texture(const char* path):Texture(){
loadPNG(path);
}
void Texture::bindToUnit(GLenum textureUnir){
glActiveTexture(textureUnir);
glBindTexture(GL_TEXTURE_2D, ID);
}
bool Texture::loadPNG(const char* path){
printf("Loading Texture from %s\n",path);
unsigned int res = lodepng::decode(image, width, height, path);
if(res != 0)
{
printf("error %d : %s\n",res,lodepng_error_text(res));
return false;
}
glBindTexture(GL_TEXTURE_2D, ID);
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &image[0]);
generateMipmap();
return true;
}
void Texture::generateMipmap(){
glBindTexture(GL_TEXTURE_2D, ID);
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);
}