AireEngine
A 3D Open-World Game Engine
Loading...
Searching...
No Matches
texture.h
1#pragma once
2#include<glad/glad.h>
3#include <stb_image.h>
4#include<vector>
5#include<memory>
6#include"../../source/res-management/resource.h"
7#define NO_HANDLE -1
8namespace Aire {
10 enum TextureType
11 {
12 DIFFUSE_MAP,
13 SPECULAR_MAP,
14 NORMAL_MAP,
15 HEIGHT_MAP,
16 NO_TEXTURE
17 };
18
20 class Texture: public Resource {
21 protected:
22 GLuint handle;
23 unsigned char* imageData;
24
25 public:
27 Texture();
28
29 int texWidth;
30
31 int texHeight;
32
33 int texChannels;
34
35 std::string dir;
36
39 uint32_t getSize();
40
42 GLuint getHandle() const;
43
45 virtual ~Texture() = 0;//< Keep this pure virtual so that the class remains abstract and cannot be isntantiated.
46
49 void bindTexUnit(GLint textureUnit);
50
55 void setFiltering(GLint minFilter, GLint maxFilter);
56
62 void setWrapping(GLint wrapS, GLint wrapT, GLint wrapR);
63
64 unsigned char* getImageData();
65
66 void setImageData(unsigned char*);
67 };
68
70 class Texture2D : public Texture {
71 public:
79 Texture2D(int width, int height, GLsizei levels, GLenum internalformat);
80
93 void setData(
94 int xOffset,
95 int yOffset,
96 int width,
97 int height,
98 const unsigned char* data,
99 GLenum format = GL_RGBA,
100 GLenum type = GL_UNSIGNED_BYTE,
101 int level = 0
102 );
103
105 void generateMipmaps();
106
108 virtual ~Texture2D() override;
109
110 // Delete copy constructor and assignment operator
111 Texture2D(const Texture2D&) = delete;
112 Texture2D& operator=(const Texture2D&) = delete;
113 };
114
116 class TextureCubemap : public Texture {
117 public:
118
119 std::vector<unsigned char*> texDataList;
120
121 int texWidth[6];
122 int texHeight[6];
123 int texChannels[6];
131 TextureCubemap(int width, int height, GLsizei levels, GLenum internalformat);
132
146 void setData(
147 int face,
148 int xOffset,
149 int yOffset,
150 int width,
151 int height,
152 const unsigned char* data,
153 GLenum format = GL_RGBA,
154 GLenum type = GL_UNSIGNED_BYTE,
155 int level = 0
156 );
157
159 void generateMipmaps();
160
161 // Destroy the Cubemap Texture
162 virtual ~TextureCubemap() override;
163
164 // Delete copy constructor and assignment operator
165 TextureCubemap(const TextureCubemap&) = delete;
166 TextureCubemap& operator=(const TextureCubemap&) = delete;
167 };
168
169 //##########################################//
170 //######## INLINE DEFINITIONS BELOW ########//
171 //##########################################//
172
174 handle(0)
175 {
176 imageData = nullptr;
177
178 texWidth = 0;
179 texHeight = 0;
180 texChannels = 0;
181 }
182
183 inline GLuint Texture::getHandle() const {
184 return handle;
185 }
186
188 // Empty destructor, but body still required
189 if (imageData != nullptr)
190 {
191 //stbi_image_free(imageData);
192 }
193 };
194
196 glGenerateTextureMipmap(handle);
197 }
198
200 if (handle != 0) {
201 glDeleteTextures(1, &handle);
202 }
203 }
204
205 inline TextureCubemap::~TextureCubemap() {
206 if (handle != 0) {
207 glDeleteTextures(1, &handle);
208 }
209 }
210
212 glGenerateTextureMipmap(handle);
213 }
214
215 using TextureHandle = int;
216 using CubeMapHandle = int;
217 using TexturePtr = std::shared_ptr<Texture>;
218 using CubeMapPtr = std::shared_ptr<TextureCubemap>;
219
220}
A base resource class.
Definition resource.h:26
A 2D texture class.
Definition texture.h:70
void setData(int xOffset, int yOffset, int width, int height, const unsigned char *data, GLenum format=GL_RGBA, GLenum type=GL_UNSIGNED_BYTE, int level=0)
Definition texture.cpp:13
Texture2D(int width, int height, GLsizei levels, GLenum internalformat)
Definition texture.cpp:41
virtual ~Texture2D() override
Destroy the texture.
Definition texture.h:199
void generateMipmaps()
Generate mipmaps. It is assumed that there is allocated memory for all levels.
Definition texture.h:195
Cubemap Texture class.
Definition texture.h:116
void generateMipmaps()
Generate mipmaps. It is assumed that there is allocated memory for all levels.
Definition texture.h:211
void setData(int face, int xOffset, int yOffset, int width, int height, const unsigned char *data, GLenum format=GL_RGBA, GLenum type=GL_UNSIGNED_BYTE, int level=0)
Definition texture.cpp:82
TextureCubemap(int width, int height, GLsizei levels, GLenum internalformat)
Definition texture.cpp:64
Texture base class.
Definition texture.h:20
void setFiltering(GLint minFilter, GLint maxFilter)
Definition texture.cpp:20
Texture()
Create the texture.
Definition texture.h:173
uint32_t getSize()
Definition texture.cpp:60
GLuint getHandle() const
Definition texture.h:183
void setWrapping(GLint wrapS, GLint wrapT, GLint wrapR)
Definition texture.cpp:25
virtual ~Texture()=0
Destroy the texture.
Definition texture.h:187
void bindTexUnit(GLint textureUnit)
Definition texture.cpp:6