-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtexture.cpp
387 lines (347 loc) · 10.5 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// TODO: investigate PBO and integrate
// https://www.songho.ca/opengl/gl_pbo.html
#include "texture.hpp"
#include "image.hpp"
#include "logging.hpp"
#include "utils.hpp"
namespace
{
logger log() { return get_logger("texture"); }
} // namespace
template <>
texture::sampling_mode
gl_convert<texture::sampling_mode, int>(int&& gl_sampling_mode)
{
using sampling_mode = texture::sampling_mode;
switch (gl_sampling_mode)
{
case GL_NEAREST: return sampling_mode::nearest;
case GL_LINEAR: return sampling_mode::linear;
case GL_NEAREST_MIPMAP_NEAREST: return sampling_mode::nearest_nearest;
case GL_NEAREST_MIPMAP_LINEAR: return sampling_mode::nearest_linear;
case GL_LINEAR_MIPMAP_NEAREST: return sampling_mode::linear_nearest;
case GL_LINEAR_MIPMAP_LINEAR: return sampling_mode::linear_linear;
default: return sampling_mode::nearest;
}
}
template <>
texture::wrapping_mode
gl_convert<texture::wrapping_mode, int>(int&& gl_wrapping_mode)
{
using wrapping_mode = texture::wrapping_mode;
switch (gl_wrapping_mode)
{
case GL_REPEAT: return wrapping_mode::repeat;
case GL_MIRRORED_REPEAT: return wrapping_mode::mirrored_repeat;
case GL_CLAMP_TO_EDGE: return wrapping_mode::clamp_to_edge;
case GL_CLAMP_TO_BORDER: return wrapping_mode::clamp_to_border;
default: return wrapping_mode::repeat;
}
}
texture::texture()
{
glGenTextures(1, &_texture_id);
_textures.push_back(this);
log()->info("New texture created {}. Total number of textures {}",
_texture_id,
_textures.size());
}
texture::texture(texture&& other)
{
_texture_id = other._texture_id;
_width = other._width;
_height = other._height;
_format = other._format;
other._texture_id = 0;
_textures.push_back(this);
log()->info("New texture created {}. Total number of textures {}",
_texture_id,
_textures.size());
}
texture& texture::operator=(texture&& other)
{
_texture_id = other._texture_id;
_width = other._width;
_height = other._height;
_format = other._format;
other._texture_id = 0;
return *this;
}
texture::~texture()
{
int old_id = _texture_id;
glDeleteTextures(1, &_texture_id);
_textures.erase(std::find(_textures.begin(), _textures.end(), this));
log()->info("Texture deleted {}. Total number of textures {}",
old_id,
_textures.size());
}
void texture::init(size_t width, size_t height, format texture_format)
{
_width = width;
_height = height;
if (_width == 0 || _height == 0)
{
return;
}
_format = texture_format;
glBindTexture(target(), _texture_id);
if (_samples > 1)
{
glTexImage2DMultisample(target(),
_samples,
convert_to_gl_internal_format(texture_format),
_width,
_height,
GL_TRUE);
}
else
{
glTexImage2D(target(),
0,
convert_to_gl_internal_format(texture_format),
_width,
_height,
0,
convert_to_gl_format(texture_format),
GL_UNSIGNED_BYTE,
nullptr);
}
auto error = glGetError();
if (error != GL_NO_ERROR)
{
log()->error("Error ocurred {}", error);
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
return;
}
void texture::set_samples(int sample_count)
{
_samples = sample_count;
init(_width, _height, _format);
}
glm::uvec2 texture::get_size() const { return { _width, _height }; }
size_t texture::get_width() const { return _width; }
size_t texture::get_height() const { return _height; }
size_t texture::get_channel_count() const
{
switch (_format)
{
case format::GRAYSCALE:
case format::DEPTH: return 1;
case format::RGB: return 3;
case format::RGBA: return 4;
default: return 0;
}
}
void texture::get_data(char* data_ptr)
{
glBindTexture(target(), _texture_id);
glGetTexImage(target(), 0, GL_RGBA, GL_UNSIGNED_BYTE, data_ptr);
glBindTexture(target(), 0);
}
void texture::set_data(const char* data_ptr)
{
set_rect_data({ 0, 0 }, { _width, _height }, data_ptr);
}
void texture::set_rect_data(glm::vec<2, size_t> pos,
glm::vec<2, size_t> size,
const char* data_ptr)
{
glBindTexture(target(), _texture_id);
glTexSubImage2D(target(),
0,
pos.x,
pos.y,
size.x,
size.y,
convert_to_gl_format(_format),
GL_UNSIGNED_BYTE,
data_ptr);
}
void texture::bind() const { static_bind(_texture_id, _samples > 1); }
void texture::unbind() const { static_unbind(_samples > 1); }
void texture::set_active_texture(size_t index) const
{
glActiveTexture(GL_TEXTURE0 + index);
glBindTexture(GL_TEXTURE_2D, _texture_id);
}
void texture::set_wrapping_mode(bool x, bool y, wrapping_mode mode)
{
int wmode = GL_REPEAT;
switch (mode)
{
case wrapping_mode::repeat: break;
case wrapping_mode::clamp_to_border: wmode = GL_CLAMP_TO_BORDER; break;
case wrapping_mode::clamp_to_edge: wmode = GL_CLAMP_TO_EDGE; break;
case wrapping_mode::mirrored_repeat: wmode = GL_MIRRORED_REPEAT; break;
}
if (x)
glTextureParameteri(_texture_id, GL_TEXTURE_WRAP_S, wmode);
if (y)
glTextureParameteri(_texture_id, GL_TEXTURE_WRAP_T, wmode);
}
texture::wrapping_mode texture::get_wrapping_mode_x() const
{
int mode = 0;
glGetTextureParameteriv(_texture_id, GL_TEXTURE_WRAP_S, &mode);
return gl_convert<texture::wrapping_mode, int>(std::move(mode));
}
texture::wrapping_mode texture::get_wrapping_mode_y() const
{
int mode = 0;
glGetTextureParameteriv(_texture_id, GL_TEXTURE_WRAP_T, &mode);
return gl_convert<texture::wrapping_mode, int>(std::move(mode));
}
void texture::set_sampling_mode_min(sampling_mode mode)
{
switch (mode)
{
case sampling_mode::nearest:
glTextureParameteri(_texture_id, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
break;
case sampling_mode::linear:
glTextureParameteri(_texture_id, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
break;
case sampling_mode::nearest_nearest:
glTextureParameteri(
_texture_id, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
break;
case sampling_mode::nearest_linear:
glTextureParameteri(
_texture_id, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
break;
case sampling_mode::linear_nearest:
glTextureParameteri(
_texture_id, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
break;
case sampling_mode::linear_linear:
glTextureParameteri(
_texture_id, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
break;
}
}
texture::sampling_mode texture::get_sampling_mode_min() const
{
int mode = 0;
glGetTextureParameteriv(_texture_id, GL_TEXTURE_MIN_FILTER, &mode);
return gl_convert<texture::sampling_mode, int>(std::move(mode));
}
void texture::set_sampling_mode_mag(sampling_mode mode)
{
switch (mode)
{
case sampling_mode::nearest:
case sampling_mode::nearest_nearest:
case sampling_mode::nearest_linear:
glTextureParameteri(_texture_id, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
break;
case sampling_mode::linear:
case sampling_mode::linear_nearest:
case sampling_mode::linear_linear:
glTextureParameteri(_texture_id, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
break;
}
}
texture::sampling_mode texture::get_sampling_mode_mag() const
{
int mode = 0;
glGetTextureParameteriv(_texture_id, GL_TEXTURE_MAG_FILTER, &mode);
return gl_convert<texture::sampling_mode, int>(std::move(mode));
}
void texture::clone(const texture* other_texture)
{
if (get_width() != other_texture->get_width() ||
get_height() != other_texture->get_height() ||
_format != other_texture->_format)
{
init(other_texture->get_width(),
other_texture->get_height(),
other_texture->_format);
}
glCopyImageSubData(other_texture->_texture_id,
GL_TEXTURE_2D,
0,
0,
0,
0,
_texture_id,
GL_TEXTURE_2D,
0,
0,
0,
0,
get_width(),
get_height(),
1);
}
unsigned texture::native_id() const { return _texture_id; }
texture texture::from_image(image* img)
{
auto md = img->get_metadata();
texture result;
format tformat = format::UNSPECIFIED;
switch (md._color_type)
{
case image::color_type::GRAYSCALE:
{
tformat = format::GRAYSCALE;
break;
}
case image::color_type::RGB:
{
tformat = format::RGB;
break;
}
case image::color_type::RGBA:
{
tformat = format::RGBA;
break;
}
default:
{
break;
}
}
result.init(img->get_width(), img->get_height(), tformat);
result.set_data(img->get_data());
return result;
}
void texture::static_bind(size_t id, bool ms)
{
glBindTexture(ms ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D, id);
}
void texture::static_unbind(bool ms)
{
glBindTexture(ms ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D, 0);
}
int texture::convert_to_gl_internal_format(format f)
{
switch (f)
{
case format::DEPTH: return GL_DEPTH_COMPONENT;
case format::GRAYSCALE: return GL_RED;
case format::RGB: return GL_RGB;
case format::RGBA: return GL_RGBA;
default: return GL_NONE;
}
}
int texture::convert_to_gl_format(format f)
{
switch (f)
{
case format::DEPTH: return GL_DEPTH_COMPONENT;
case format::GRAYSCALE: return GL_RED;
case format::RGB: return GL_RGB;
case format::RGBA: return GL_RGBA;
default: return GL_NONE;
}
}
unsigned texture::target() const
{
return _samples > 1 ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D;
}
std::vector<texture*> texture::_textures;