our_dick/include/graphics/texture.hpp
2022-01-15 11:36:42 -08:00

155 lines
4.6 KiB
C++

/**
This file is a part of our_dick
Copyright (C) 2020-2022 rexy712
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OUR_DICK_GRAPHICS_TEXTURE_HPP
#define OUR_DICK_GRAPHICS_TEXTURE_HPP
#include "engine/image.hpp"
#include "gl_include.hpp"
#include "math/vec.hpp"
namespace gfx{
class texture;
class weak_texture_handle
{
private:
const texture* m_texture;
public:
weak_texture_handle(const texture& tex);
weak_texture_handle(const weak_texture_handle&) = default;
weak_texture_handle(weak_texture_handle&&) = default;
~weak_texture_handle(void) = default;
weak_texture_handle& operator=(const weak_texture_handle&) = default;
weak_texture_handle& operator=(weak_texture_handle&&) = default;
GLuint raw(void)const;
GLsizei get_width(void)const;
GLsizei get_height(void)const;
void bind(void)const;
void bind_unit(GLuint tunit)const;
};
//class representing an opengl 2D texture
class texture
{
public:
//all available wrapping styles as a strongly typed enum
enum class wrap : GLint{
CLAMP_EDGE = GL_CLAMP_TO_EDGE,
CLAMP_BORDER = GL_CLAMP_TO_BORDER,
MIRROR_REPEAT = GL_MIRRORED_REPEAT,
REPEAT = GL_REPEAT,
MIRROR_CLAMP_EDGE = GL_MIRROR_CLAMP_TO_EDGE,
};
//all available minfilter styles as a strongly typed enum
enum class minfilter : GLint{
NEAREST = GL_NEAREST,
LINEAR = GL_LINEAR,
NMIPMAP_NEAREST = GL_NEAREST_MIPMAP_NEAREST,
LMIPMAP_NEAREST = GL_LINEAR_MIPMAP_NEAREST,
NMIPMAP_LINEAR = GL_NEAREST_MIPMAP_LINEAR,
LMIPMAP_LINEAR = GL_LINEAR_MIPMAP_LINEAR,
};
//all available magfilter styles as a strongly typed enum
enum class magfilter : GLint{
NEAREST = GL_NEAREST,
LINEAR = GL_LINEAR,
};
private:
GLuint m_tex_id = 0; //handle to texture object
GLsizei m_width = 0;
GLsizei m_height = 0;
GLenum m_format = 0;
GLenum m_type = GL_UNSIGNED_BYTE;
bool m_mipmapped = true;
public:
//create the texture with no image data
texture(void);
texture(const unsigned char* data, GLenum format, GLsizei w, GLsizei h, GLenum type, bool mipmap = true);
texture(GLenum format, GLsizei w, GLsizei h, GLenum type, bool mipmap = true);
//create the texture with image data from 'i'
texture(const egn::image& i, bool mipmap = true);
texture(const egn::deferred_image& i, bool mipmap = true);
texture(const texture&);
texture(texture&&);
~texture(void);
texture& operator=(const texture&);
texture& operator=(texture&&);
GLuint raw(void)const;
//overwrite the current image data with 'i'
bool set_image(const unsigned char* data, GLenum format, GLsizei w, GLsizei h, GLenum type);
bool set_image(const egn::image& i);
bool set_image(const egn::deferred_image& i);
bool set_subimage(const unsigned char* data, GLenum format, GLenum type,
GLsizei xoffset, GLsizei yoffset, GLsizei w, GLsizei h);
bool set_subimage(const egn::image& i, GLsizei xoffset, GLsizei yoffset);
bool set_subimage(const egn::deferred_image& i, GLsizei xoffset, GLsizei yoffset);
//change wrap mode for both x and y
void set_wrap_mode(wrap w);
void set_wrap_mode(wrap w, wrap h);
//change wrap mode for one direction
void set_wrap_x(wrap w);
void set_wrap_y(wrap h);
void set_border_color(GLfloat r, GLfloat g, GLfloat b, GLfloat a = 1.0f);
void set_filter(minfilter min, magfilter mag);
void set_mag_filter(magfilter m);
void set_min_filter(minfilter m);
magfilter get_mag_filter(void)const;
minfilter get_min_filter(void)const;
math::vec4<GLfloat> get_border_color(void)const;
wrap get_wrap_x(void)const;
wrap get_wrap_y(void)const;
GLsizei get_width(void)const;
GLsizei get_height(void)const;
void enable_auto_mipmap(bool enable);
void generate_mipmap(void);
//release ownership of this texture object
GLuint release(void);
//bind to GL_TEXTURE_2D
void bind(void)const;
//bind to given texture unit and load into given program uniform location
void bind_unit(GLuint tunit)const;
weak_texture_handle create_handle(void)const;
private:
bool create_texture_storage_(void);
};
}
#endif