jGL
Loading...
Searching...
No Matches
glTexture.h
Go to the documentation of this file.
1#ifndef GLTEXTURE_H
2#define GLTEXTURE_H
3
4#include <jGL/OpenGL/gl.h>
5#include <jGL/texture.h>
6
7#include <stdexcept>
8#include <filesystem>
9
10namespace jGL::GL
11{
12
13 class glTexture : public Texture
14 {
15
16 public:
17
19 {
20 glGenTextures(1, &id);
21 }
22
24 {
25 glDeleteTextures(1, &id);
26 }
27
28 virtual void upload(std::vector<std::byte> * data) {};
29
30 inline void bind() { glBindTexture(GL_TEXTURE_2D, id); }
31
32 inline void unbind() { glBindTexture(GL_TEXTURE_2D, 0); }
33
34 void bind(unsigned binding)
35 {
36 glActiveTexture(GL_TEXTURE0+binding);
37 unbind();
38 bind();
39 textureUnit = binding;
40 }
41
42 void generateMipMaps() {bind(); glGenerateMipmap(GL_TEXTURE_2D);}
43
44 protected:
45
46 GLuint id;
47 unsigned textureUnit;
48
49 std::vector<std::byte> load_image(std::filesystem::path imageFilePath)
50 {
51 unsigned char * pixels = stbi_load(imageFilePath.generic_string().c_str(), &width, &height, &channels, 0);
52
53 if (!pixels)
54 {
55 throw std::runtime_error("Failed to load texture: "+imageFilePath.generic_string());
56 }
57 std::byte * bytes = reinterpret_cast<std::byte*>(pixels);
58
59 size_t dim = width*height;
60 if (channels > 0) { dim *= channels; }
61 std::vector<std::byte> vdata(bytes, bytes+dim);
62 stbi_image_free(pixels);
63 return vdata;
64 }
65
66 std::vector<std::byte> load_image(std::vector<std::byte> imageFile)
67 {
68 unsigned char * chData = reinterpret_cast<unsigned char*>(imageFile.data());
69 unsigned char * pixels = stbi_load_from_memory(chData, imageFile.size(), &width, &height, &channels, 4);
70 if (!pixels)
71 {
72 throw std::runtime_error("Failed to load texture from memory");
73 }
74 std::byte * bytes = reinterpret_cast<std::byte*>(pixels);
75
76 size_t dim = width*height;
77 if (channels > 0) { dim *= channels; }
78 std::vector<std::byte> vdata(bytes, bytes+dim);
79 stbi_image_free(pixels);
80 return vdata;
81 }
82
83 };
84
86 {
87
88 public:
89
90 glTexture2DRGB(std::filesystem::path imageFile)
91 : glTexture()
92 {
93 std::vector<std::byte> pixels = load_image(imageFile);
95 upload(pixels);
96 }
97
98 glTexture2DRGB(std::vector<std::byte> data)
99 : glTexture()
100 {
101 std::vector<std::byte> pixels = load_image(data);
103 upload(pixels);
104 }
105
106 void create(int width, int height, int channels);
107 void upload(std::vector<std::byte> pixels);
108 };
109
111 {
112
113 public:
114
115 glTexture2DRGBA(std::filesystem::path imageFile)
116 : glTexture()
117 {
118 std::vector<std::byte> pixels = load_image(imageFile);
120 upload(pixels);
121 }
122
123 glTexture2DRGBA(std::vector<std::byte> data)
124 : glTexture()
125 {
126 std::vector<std::byte> pixels = load_image(data);
128 upload(pixels);
129 }
130
131 void create(int width, int height, int channels);
132 void upload(std::vector<std::byte> pixels);
133 };
134
136 {
137
138 public:
139
140 glTexture2DByte(std::filesystem::path imageFile)
141 : glTexture()
142 {
143 std::vector<std::byte> pixels = load_image(imageFile);
145 upload(pixels);
146 }
147
148 glTexture2DByte(std::vector<std::byte> data)
149 : glTexture()
150 {
151 std::vector<std::byte> pixels = load_image(data);
153 upload(pixels);
154 }
155
156 glTexture2DByte(std::vector<std::byte> pixels, int width, int height)
157 : glTexture()
158 {
160 upload(pixels);
161 }
162
163 void create(int width, int height);
164 void upload(std::vector<std::byte> pixels);
165
166 };
167}
168
169#endif /* GLTEXTURE_H */
Definition glTexture.h:136
glTexture2DByte(std::vector< std::byte > data)
Definition glTexture.h:148
glTexture2DByte(std::vector< std::byte > pixels, int width, int height)
Definition glTexture.h:156
glTexture2DByte(std::filesystem::path imageFile)
Definition glTexture.h:140
void create(int width, int height)
Definition glTexture.cpp:133
void upload(std::vector< std::byte > pixels)
Definition glTexture.cpp:181
Definition glTexture.h:111
glTexture2DRGBA(std::filesystem::path imageFile)
Definition glTexture.h:115
void upload(std::vector< std::byte > pixels)
Definition glTexture.cpp:117
glTexture2DRGBA(std::vector< std::byte > data)
Definition glTexture.h:123
void create(int width, int height, int channels)
Definition glTexture.cpp:70
Definition glTexture.h:86
glTexture2DRGB(std::vector< std::byte > data)
Definition glTexture.h:98
void create(int width, int height, int channels)
Definition glTexture.cpp:5
void upload(std::vector< std::byte > pixels)
Definition glTexture.cpp:53
glTexture2DRGB(std::filesystem::path imageFile)
Definition glTexture.h:90
Definition glTexture.h:14
void unbind()
Definition glTexture.h:32
~glTexture()
Definition glTexture.h:23
std::vector< std::byte > load_image(std::vector< std::byte > imageFile)
Definition glTexture.h:66
std::vector< std::byte > load_image(std::filesystem::path imageFilePath)
Definition glTexture.h:49
virtual void upload(std::vector< std::byte > *data)
Definition glTexture.h:28
unsigned textureUnit
Definition glTexture.h:47
void generateMipMaps()
Definition glTexture.h:42
void bind()
Definition glTexture.h:30
GLuint id
Definition glTexture.h:46
void bind(unsigned binding)
Definition glTexture.h:34
glTexture()
Definition glTexture.h:18
Definition texture.h:17
int width
Definition texture.h:39
int height
Definition texture.h:39
int channels
Definition texture.h:39
Definition gl.h:41