Game of Ur 0.3.3
This is a computer adaptation of Game of Ur, written in C++ mainly using SDL and OpenGL.
Loading...
Searching...
No Matches
texture.hpp
Go to the documentation of this file.
1
11
12#ifndef FOOLSENGINE_TEXTURE_H
13#define FOOLSENGINE_TEXTURE_H
14
15#include <string>
16
17#include <GL/glew.h>
18#include <glm/glm.hpp>
19#include <nlohmann/json.hpp>
20
22
23namespace ToyMaker {
24
35 enum class Type {
36 TEXTURE_2D, //< A simple 2D texture.
37 CUBEMAP, //< The texture of a cubemap (unused I think?)
38 };
39
44 enum class CubemapLayout: uint8_t {
45 NA, //< This color buffer does not represent a cubemap texture.
46 ROW, //< Subregions of the texture corresponding to each face of the cubemap are laid out in a single row.
47 // COLUMN,
48 // ROW_CROSS,
49 // COLUMN_CROSS,
50 };
51
56 glm::vec2 mDimensions {800, 600};
57
62 CubemapLayout mCubemapLayout { CubemapLayout::NA };
63
68 GLenum mMagFilter { GL_LINEAR };
69
74 GLenum mMinFilter { GL_LINEAR };
75
80 GLenum mWrapS { GL_CLAMP_TO_EDGE };
81
86 GLenum mWrapT { GL_CLAMP_TO_EDGE };
87
92 GLenum mDataType { GL_UNSIGNED_BYTE };
93
98 GLbyte mComponentCount { 4 };
99
108 bool mUsesWebColors { false };
109 };
110
116 class Texture : public Resource<Texture> {
117 public:
125 Texture(
126 GLuint textureID,
127 const ColorBufferDefinition& colorBufferDefinition,
128 const std::string& filepath=""
129 );
130
131 /*Copy construction*/
132 Texture(const Texture& other);
133 /*Copy assignment*/
134 Texture& operator=(const Texture& other);
135
136 /*Move construction*/
137 Texture(Texture&& other) noexcept;
138 /*Move assignment*/
139 Texture& operator=(Texture&& other) noexcept;
140
141 /*Destructor belonging to latest subclass*/
142 virtual ~Texture();
143
144 /* basic deallocate function */
145 virtual void free();
146
152 void bind(GLuint textureUnit) const;
153
154 /* attaches this texture to a (presumably existing and bound) framebuffer*/
155
161 void attachToFramebuffer(GLuint attachmentUnit) const;
162
168 GLuint getTextureID() const;
169
175 GLint getWidth() const;
176
182 GLint getHeight() const;
183
189 inline static std::string getResourceTypeName() { return "Texture"; }
190
197
198 protected:
199 void copyImage(const Texture& other);
200
201
206 GLuint mID {0};
207
212 std::string mFilepath {""};
213
219
224 void generateTexture();
225
231 GLenum internalFormat();
232
238 GLenum externalFormat();
239
244 void destroyResource();
245
249 void releaseResource();
250 };
251
275 class TextureFromFile: public ResourceConstructor<Texture, TextureFromFile> {
276 public:
277 TextureFromFile(): ResourceConstructor<Texture, TextureFromFile> {0} {}
278 inline static std::string getResourceConstructorName() { return "fromFile"; }
279 private:
280 std::shared_ptr<IResource> createResource(const nlohmann::json& methodParameters) override;
281 };
282
313 class TextureFromColorBufferDefinition: public ResourceConstructor<Texture, TextureFromColorBufferDefinition> {
314 public:
315 TextureFromColorBufferDefinition(): ResourceConstructor<Texture, TextureFromColorBufferDefinition> {0} {}
316 inline static std::string getResourceConstructorName() { return "fromDescription"; }
317 private:
318 std::shared_ptr<IResource> createResource(const nlohmann::json& methodParameters) override;
319 };
320
331 inline GLenum deduceInternalFormat(const ColorBufferDefinition& colorBufferDefinition) {
332 GLenum internalFormat;
333
334 if(colorBufferDefinition.mDataType == GL_FLOAT && colorBufferDefinition.mComponentCount == 1){
335 internalFormat = GL_R16F;
336 } else if (colorBufferDefinition.mDataType == GL_FLOAT && colorBufferDefinition.mComponentCount == 4) {
337 internalFormat = GL_RGBA16F;
338 } else if (colorBufferDefinition.mDataType == GL_UNSIGNED_BYTE && colorBufferDefinition.mComponentCount == 1) {
339 internalFormat = GL_RED;
340 } else if (colorBufferDefinition.mDataType == GL_UNSIGNED_BYTE && colorBufferDefinition.mComponentCount == 4) {
341
342 if(colorBufferDefinition.mUsesWebColors) {
343 internalFormat = GL_SRGB_ALPHA;
344 } else {
345 internalFormat = GL_RGBA;
346 }
347
348 } else {
349 throw std::invalid_argument("Invalid data type and component count combination provided in texture constructor");
350 }
351
352 return internalFormat;
353 }
354
365 inline GLenum deduceExternalFormat(const ColorBufferDefinition& colorBufferDefinition) {
366 GLenum externalFormat;
367
368 if(colorBufferDefinition.mDataType == GL_FLOAT && colorBufferDefinition.mComponentCount == 1){
369 externalFormat = GL_RED;
370 } else if (colorBufferDefinition.mDataType == GL_FLOAT && colorBufferDefinition.mComponentCount == 4) {
371 externalFormat = GL_RGBA;
372 } else if (colorBufferDefinition.mDataType == GL_UNSIGNED_BYTE && colorBufferDefinition.mComponentCount == 1) {
373 externalFormat = GL_RED;
374 } else if (colorBufferDefinition.mDataType == GL_UNSIGNED_BYTE && colorBufferDefinition.mComponentCount == 4) {
375 externalFormat = GL_RGBA;
376 } else {
377 throw std::invalid_argument("Invalid data type and component count combination provided in texture constructor");
378 }
379
380 return externalFormat;
381 }
382
383
385 void to_json(nlohmann::json& json, const ColorBufferDefinition& colorBufferDefinition);
386
388 void from_json(const nlohmann::json& json, ColorBufferDefinition& colorBufferDefinition);
389 NLOHMANN_JSON_SERIALIZE_ENUM(ColorBufferDefinition::CubemapLayout, {
390 {ColorBufferDefinition::CubemapLayout::NA, "na"},
391 {ColorBufferDefinition::CubemapLayout::ROW, "row"},
392 });
393}
394
395#endif
ResourceConstructor(int explicitlyInitializeMe)
Definition resource_database.hpp:491
Resource(int explicitlyInitializeMe)
Definition resource_database.hpp:389
std::shared_ptr< IResource > createResource(const nlohmann::json &methodParameters) override
Creates a resource object using the parameters specified in methodParameters.
Definition texture.cpp:268
std::shared_ptr< IResource > createResource(const nlohmann::json &methodParameters) override
Creates a resource object using the parameters specified in methodParameters.
Definition texture.cpp:213
The class representation of textures on this engine, which are a type of Resource used both within an...
Definition texture.hpp:116
void attachToFramebuffer(GLuint attachmentUnit) const
Attaches this texture to a (presumably existing and bound) framebuffer, allowing the user of the fram...
Definition texture.cpp:176
void generateTexture()
Generates a new texture based on the stored color buffer definition.
Definition texture.cpp:186
static std::string getResourceTypeName()
Gets the resource type string associated with this resource.
Definition texture.hpp:189
void destroyResource()
Destroys (OpenGL managed) texture tied to this object.
Definition texture.cpp:161
GLuint mID
The OpenGL ID of this texture.
Definition texture.hpp:206
GLenum internalFormat()
The enum value passed as the "internalFormat" argument of glTexImage2D.
Definition texture.cpp:205
GLuint getTextureID() const
Gets the OpenGL texture ID for this texture.
Definition texture.cpp:101
GLint getHeight() const
Gets the height of this texture (per its color buffer definition).
Definition texture.cpp:108
GLenum externalFormat()
The enum value passed as the "format" argument of glTexImage2D.
Definition texture.cpp:209
Texture(GLuint textureID, const ColorBufferDefinition &colorBufferDefinition, const std::string &filepath="")
Constructs a new texture object which takes ownership of an OpenGL texture handle and engine colorbuf...
Definition texture.cpp:40
void releaseResource()
Removes references to (OpenGL managed) texture tied to this object, so that another object or part of...
Definition texture.cpp:165
std::string mFilepath
The file this texture was loaded from, if any.
Definition texture.hpp:212
ColorBufferDefinition getColorBufferDefinition()
Gets the description of this texture.
Definition texture.hpp:196
GLint getWidth() const
Gets the width of this texture (per its color buffer definition).
Definition texture.cpp:103
ColorBufferDefinition mColorBufferDefinition
The color buffer definition of this texture.
Definition texture.hpp:218
void bind(GLuint textureUnit) const
Binds this texture to a texture unit, making it available for use by a shader.
Definition texture.cpp:170
GLenum deduceExternalFormat(const ColorBufferDefinition &colorBufferDefinition)
A (quite possibly unnecessary) function to fetch the enum corresponding to the "format" argument of g...
Definition texture.hpp:365
GLenum deduceInternalFormat(const ColorBufferDefinition &colorBufferDefinition)
A (quite possibly unnecessary) function to fetch the enum corresponding to the "internalFormat" argum...
Definition texture.hpp:331
Namespace containing all class definitions and functions related to the ToyMaker engine.
Definition camera_system.hpp:20
Headers relating to resources and their management for a given project.
A struct containing the definition of a color buffer, using which similar color buffers can be create...
Definition texture.hpp:30
GLenum mMagFilter
The type of sampling used with the texture when it is zoomed in to.
Definition texture.hpp:68
GLenum mWrapS
Horizontally: for UV coordinates beyond the 0.0-1.0 range, which part of the texture is to be sampled...
Definition texture.hpp:80
GLenum mDataType
The underlying data type representing each channel (also component) of the texture.
Definition texture.hpp:92
CubemapLayout mCubemapLayout
The type of cubemap layout the texture conforms to, if it is a cubemap.
Definition texture.hpp:62
glm::vec2 mDimensions
The dimensions of the 2D texture.
Definition texture.hpp:56
GLbyte mComponentCount
The number of components (or channels) each pixel of the texture contains.
Definition texture.hpp:98
bool mUsesWebColors
Whether the intensity of the color of a component maps linearly or exponentially with the value of th...
Definition texture.hpp:108
GLenum mMinFilter
The type of sampling used with the texture when it is zoomed out from.
Definition texture.hpp:74
Type
The type of texture defined.
Definition texture.hpp:35
CubemapLayout
For a 2D texture - determines the manner in which the Texture should be sampled in order for it to be...
Definition texture.hpp:44
GLenum mWrapT
Vertically: for UV coordinates beyond the 0.0-1.0 range, which part of the texture is to be sampled f...
Definition texture.hpp:86