-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTexture.cpp
88 lines (62 loc) · 1.49 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#ifdef WIN32
#include <windows.h>
#endif
#include <GL/glew.h>
#include "Texture.h"
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include "ImageTools.h"
bool Texture::s_bGenerateMipmaps = true;
bool Texture::LoadFile(GLenum target, const std::string& name)
{
unsigned int w, h, d;
GLubyte* ptr = ImageTools::OpenImage(name, w, h, d);
if(!ptr) {
std::cerr << "[Error] Impossible de charger la texture " << name << std::endl;
return false;
}
LoadData(target, ptr, w, h, d);
delete[] ptr;
return true;
}
void Texture::LoadData(GLenum target, GLubyte* ptr, unsigned int w, unsigned int h, unsigned int d)
{
glTexImage2D(target, 0, d==3?GL_RGB:GL_RGBA, w, h, 0, d==3?GL_RGB:GL_RGBA, GL_UNSIGNED_BYTE, ptr);
}
void Texture::Gen()
{
Destroy();
glGenTextures(1, &m_nHandle);
}
bool Texture::Load(const std::string& name)
{
Gen();
if(m_nHandle == 0){
std::cerr << "Identifiant de texture incorrect" << std::endl;
return false;
}
return true;
}
void Texture::Destroy()
{
glDeleteTextures(1, &m_nHandle);
m_nHandle = 0;
}
void Texture::Bind() const {
glBindTexture(getTextureType(), m_nHandle);
}
void Texture::Bind(GLuint slot) const {
glActiveTexture(GL_TEXTURE0+slot);
glEnable(getTextureType());
glBindTexture(getTextureType(), m_nHandle);
}
void Texture::Unbind() const {
glBindTexture(getTextureType(), 0);
}
void Texture::Unbind(GLuint slot) const {
glActiveTexture(GL_TEXTURE0+slot);
glBindTexture(getTextureType(), 0);
}