-
Notifications
You must be signed in to change notification settings - Fork 0
/
texture.hxx
46 lines (42 loc) · 1.9 KB
/
texture.hxx
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
#pragma once
#include <cmath>
#include <GL/gl.h>
#include <png++/png.hpp>
using TextureID = GLuint;
static TextureID load_texture(std::string const &filename, GLenum format = GL_RGBA8) {
TextureID texture;
png::image<png::rgba_pixel, png::solid_pixel_buffer<png::rgba_pixel>> image(filename);
glCreateTextures(GL_TEXTURE_2D, 1, &texture);
auto &&data = image.get_pixbuf().get_bytes();
glTextureStorage2D(texture, int(std::log2(image.get_width()) + 1.5), format, image.get_width(), image.get_height());
glTextureSubImage2D(texture, 0, 0, 0, image.get_width(), image.get_height(), GL_RGBA, GL_UNSIGNED_BYTE, data.data());
glGenerateTextureMipmap(texture);
return texture;
}
static TextureID load_cube_texture(std::string const &basename, GLenum format = GL_RGBA8) {
TextureID texture;
glCreateTextures(GL_TEXTURE_CUBE_MAP, 1, &texture);
unsigned size;
for (int k = 0; k < 6; k++) {
png::image<png::rgba_pixel, png::solid_pixel_buffer<png::rgba_pixel>> image(basename + std::to_string(k) + ".png");
auto &&data = image.get_pixbuf().get_bytes();
if (k == 0) {
size = image.get_width();
glTextureStorage2D(texture, int(std::log2(size) + 1.5), format, size, size);
}
if (image.get_width() != size || image.get_height() != size) {
glDeleteTextures(1, &texture);
throw "Invalid cubemap data";
}
glTextureSubImage3D(texture, 0, 0, 0, k, image.get_width(), image.get_height(), 1, GL_RGBA, GL_UNSIGNED_BYTE, data.data());
}
glGenerateTextureMipmap(texture);
return texture;
}
static void load_cube_texture_layer(TextureID texture, int layer, std::string const &basename) {
for (int k = 0; k < 6; k++) {
png::image<png::rgba_pixel, png::solid_pixel_buffer<png::rgba_pixel>> image(basename + std::to_string(k) + ".png");
auto &&data = image.get_pixbuf().get_bytes();
glTextureSubImage3D(texture, 0, 0, 0, 6 * layer + k, image.get_width(), image.get_height(), 1, GL_RGBA, GL_UNSIGNED_BYTE, data.data());
}
}