-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtexture.cpp
More file actions
150 lines (118 loc) · 3.87 KB
/
texture.cpp
File metadata and controls
150 lines (118 loc) · 3.87 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "texture.h"
#include <cassert>
// TODO
#include <iostream>
#include <IL/ilu.h>
Texture::Texture() : m_isValid(false)
{
}
Texture::~Texture()
{
}
bool Texture::Load(const std::string& key, const OptionList& options)
{
return Load(key, options.GetBoolean("mipmap", true));
}
bool Texture::Release()
{
if(IsValid())
glDeleteTextures(1, &m_textureId);
return true;
}
bool Texture::Load(const std::string& filename, bool mipmap)
{
m_textureName = filename;
// Initialize Devil only once:
static bool alreadyInitialized = false;
if(!alreadyInitialized)
{
ilInit();
iluInit();
alreadyInitialized = true;
}
// Use Devil library to load image data in memory
ILuint texid;
ilGenImages(1, &texid);
ilBindImage(texid);
ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
ilEnable(IL_ORIGIN_SET);
// Si ilLoadImage retourne false ici, c'est à cause que j'utiliser aussi un sf::Image (sfml)
// au moins à une place dans le programme. Ca semble faire un conflit, qui entraine que Devil
// n'est plus capable de charger d'images au format PNG
if (!ilLoadImage((const ILstring)filename.c_str()))
{
ILenum Error;
while ((Error = ilGetError()) != IL_NO_ERROR)
printf("Error ilLoadImage #%d: %s\n", Error, iluErrorString(Error));
return false;
}
if (!ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE))
return false;
glGenTextures(1, &m_textureId);
glBindTexture(GL_TEXTURE_2D, m_textureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
if(mipmap)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
}
else
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData());
ilDeleteImages(1, &texid);
m_isValid = true;
return true;
}
bool Texture::LoadFromMemoryRGBA(const std::string& name, const uint8* data, int width, int height, bool mipmap)
{
std::cout << "WARNING: [Texture::LoadFromMemoryRGBA]: This texture resource will not have a sequential key set, as it's not loaded using the resource manager..." << std::endl;
m_textureName = name;
glGenTextures(1, &m_textureId);
glBindTexture(GL_TEXTURE_2D, m_textureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
if(mipmap)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
}
else
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
m_isValid = true;
return true;
}
bool Texture::IsValid() const
{
return m_isValid;
}
void Texture::Bind() const
{
assert(IsValid());
glBindTexture(GL_TEXTURE_2D, m_textureId);
}
uint Texture::GetTextureId() const
{
return m_textureId;
}
std::string Texture::GetTextureName() const
{
return m_textureName;
}
bool Texture::operator==(const Texture& texture) const
{
return (m_textureId == texture.m_textureId);
}
bool Texture::operator!=(const Texture& texture) const
{
return !operator==(texture);
}