pass specialized generate function to generic base function for globject

This commit is contained in:
frank 2021-09-28 02:09:49 -04:00
parent 6adac9806f
commit f03d58dae1
4 changed files with 38 additions and 22 deletions

View File

@ -1,11 +1,17 @@
#include "GLObject.hpp"
/* A deleter function is required for freeing the memory allocated to the object (this could be, for example,
* glDeleteTextures for a Texture object, or a custom function that does something in addition to calling
* the appropriate GL deleter function) */
GLObject::GLObject(std::function<void(GLuint*)> deleter)
/* The deleter function is used for freeing the memory allocated to the object (for example, glDeleteTextures,
* or a custom function that does something in addition to calling the appropriate GL deleter function). */
GLObject::GLObject(deleter_function deleter) : deleter(deleter) {}
/* This generator passed in from the derived class will be called to generate a new ID. It is limited to a
* single ID. For generating and managing multiple IDs with a single class, this method and others will have
* to be overwritten. */
void GLObject::generate(generator_function generator)
{
this->deleter = deleter;
GLuint id;
generator(1, &id);
this->id(id);
}
/* Set the shared pointer to point to a new GLuint with specified ID value */

View File

@ -10,9 +10,13 @@
[GLObject.hpp]
The abstract GLObject class is meant to be inherited by specific types of OpenGL objects,
for example Textures or Buffer objects. It stores the object's ID and declares virtual
functions for generating, binding, and destroying the object.
The abstract GLObject class is meant to be inherited and implemented by more specific
types of OpenGL objects, for example Textures or Buffer objects. It stores the object's
ID and declares virtual functions for generating, binding, and destroying the object.
An appropriate deleter function must be passed in to the constructor from the derived
class. It can be a custom function, or it can be one of the GL deleter functions like
glDeleteTextures.
*/
@ -33,19 +37,25 @@
class GLObject
{
protected:
private:
typedef std::function<void(GLsizei, GLuint*)> generator_function;
typedef std::function<void(GLuint*)> deleter_function;
std::shared_ptr<GLuint> object_id = nullptr;
std::function<void(GLuint*)> deleter = nullptr;
deleter_function deleter = nullptr;
protected:
GLObject(deleter_function);
virtual void bind() const = 0;
void generate(generator_function);
public:
GLObject(std::function<void(GLuint*)>);
virtual void generate() = 0;
virtual void bind() const = 0;
void id(GLuint);
GLuint id() const;
bool generated() const;
virtual void id(GLuint);
virtual GLuint id() const;
virtual bool generated() const;
virtual ~GLObject() = default;
};

View File

@ -16,12 +16,10 @@ void Texture::associate(fs::path path)
this->path = path;
}
/* Generate an ID for this Texture and save it */
/* Forward the GL texture generate function to the base class */
void Texture::generate()
{
GLuint id;
glGenTextures(1, &id);
this->id(id);
GLObject::generate(glGenTextures);
}
/* Generate a GL_TEXTURE_2D texture ID and allocate GL_RGB8 storage for the given size */

View File

@ -13,7 +13,9 @@
The Texture class abstracts the file opening, data loading and binding steps of Open GL
texture creation. Currently it only supports loading GL_TEXTURE_2D with GL_RGB8 pixels
and automatic GL_NEAREST mipmapping. Support may be added for users to pass in their own
pixel data, to customize mipmapping, and more.
pixel data, to customize mipmapping, and more. The class can also be used in a custom
way by passing the Texture ID to GL functions instead of using the class's member
functions.
*/
@ -47,7 +49,7 @@ public:
Texture();
Texture(fs::path);
void associate(fs::path);
void generate() override;
void generate();
void generate(glm::vec2);
void load();
void load(fs::path);