BLT-With-Graphics-Template/include/blt/gfx/texture.h

224 lines
7.0 KiB
C
Raw Normal View History

2023-12-17 15:04:57 -05:00
/*
* <Short Description>
* Copyright (C) 2023 Brett Terpstra
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef BLT_TEXTURE_H
#define BLT_TEXTURE_H
2023-12-17 19:37:11 -05:00
#include <blt/gfx/gl_includes.h>
#include <blt/std/logging.h>
#include <string>
#include <vector>
#include <utility>
2023-12-17 15:04:57 -05:00
namespace blt::gfx
{
2023-12-17 19:37:11 -05:00
class texture_file;
class texture_data
{
friend texture_file;
2023-12-17 15:04:57 -05:00
private:
2023-12-17 19:37:11 -05:00
unsigned char* m_data = nullptr;
int m_width = 0, m_height = 0, m_channels = 0;
2023-12-17 15:04:57 -05:00
public:
2023-12-17 19:37:11 -05:00
texture_data(unsigned char* data, int width, int height, int channels):
m_data(data), m_width(width), m_height(height), m_channels(channels)
{}
2023-12-17 15:04:57 -05:00
2023-12-17 19:37:11 -05:00
texture_data(int width, int height, int channels = 4): m_width(width), m_height(height), m_channels(channels)
{
2023-12-28 16:14:12 -05:00
m_data = static_cast<unsigned char*>(std::malloc(width * height * channels));
2023-12-17 15:04:57 -05:00
}
2023-12-17 19:37:11 -05:00
texture_data() = default;
2023-12-17 20:13:28 -05:00
inline unsigned char* data()
2023-12-17 19:37:11 -05:00
{
return m_data;
}
2023-12-26 15:14:24 -05:00
[[nodiscard]] inline const unsigned char* data() const
2023-12-17 19:37:11 -05:00
{
return m_data;
2023-12-17 15:04:57 -05:00
}
2023-12-17 20:13:28 -05:00
[[nodiscard]] inline int width() const
2023-12-17 19:37:11 -05:00
{
return m_width;
2023-12-17 15:04:57 -05:00
}
2023-12-17 20:13:28 -05:00
[[nodiscard]] inline int height() const
2023-12-17 19:37:11 -05:00
{
return m_height;
2023-12-17 15:04:57 -05:00
}
2023-12-17 20:13:28 -05:00
[[nodiscard]] inline int channels() const
2023-12-17 19:37:11 -05:00
{
return m_channels;
2023-12-17 15:04:57 -05:00
}
2023-12-17 19:37:11 -05:00
~texture_data()
{
2023-12-28 16:14:12 -05:00
std::free(m_data);
2023-12-17 15:04:57 -05:00
}
2023-12-17 19:37:11 -05:00
};
class texture_file
{
private:
std::string m_name;
std::string m_path;
mutable texture_data m_texture;
public:
/**
* @param path path to the texture file
* @param name reference name for this texture. If empty the texture will use path as its identifier
*/
explicit texture_file(const std::string& path, const std::string& name = "");
2023-12-17 15:04:57 -05:00
2023-12-17 19:37:11 -05:00
texture_file& resize(int target_width, int target_height);
2023-12-17 20:13:28 -05:00
inline texture_data& texture() const
2023-12-17 19:37:11 -05:00
{
return m_texture;
}
2023-12-17 20:13:28 -05:00
[[nodiscard]] inline int channels() const
2023-12-17 19:37:11 -05:00
{
return m_texture.m_channels;
}
2023-12-17 20:13:28 -05:00
[[nodiscard]] inline int width() const
2023-12-17 19:37:11 -05:00
{
return m_texture.m_width;
}
2023-12-17 20:13:28 -05:00
[[nodiscard]] inline int height() const
2023-12-17 19:37:11 -05:00
{
return m_texture.m_height;
2023-12-17 15:04:57 -05:00
}
2023-12-17 20:13:28 -05:00
[[nodiscard]] inline const std::string& getName() const
2023-12-17 19:37:11 -05:00
{
return m_name;
2023-12-17 15:04:57 -05:00
}
};
2023-12-17 19:37:11 -05:00
struct texture_gl
{
2023-12-17 15:04:57 -05:00
protected:
unsigned int textureID = 0;
GLint textureBindType;
GLint textureColorMode;
int m_width, m_height;
2023-12-28 16:14:12 -05:00
texture_gl(int width, int height, GLint bind_type = GL_TEXTURE_2D, GLint color_mode = GL_RGBA):
textureBindType(bind_type), textureColorMode(color_mode), m_width(width), m_height(height)
2023-12-17 19:37:11 -05:00
{
2023-12-17 15:04:57 -05:00
glGenTextures(1, &textureID);
}
2023-12-17 20:15:12 -05:00
protected:
void setDefaults() const;
inline void generateMipmaps() const
{
glGenerateMipmap(textureBindType);
}
2023-12-28 16:14:12 -05:00
2023-12-17 15:04:57 -05:00
public:
2023-12-17 19:37:11 -05:00
inline void bind() const
{
2023-12-17 15:04:57 -05:00
glBindTexture(textureBindType, textureID);
}
2023-12-17 19:37:11 -05:00
inline void unbind() const
{
2023-12-17 15:04:57 -05:00
glBindTexture(textureBindType, 0);
}
2023-12-17 19:37:11 -05:00
[[nodiscard]] inline unsigned int getTextureID() const
{
2023-12-17 15:04:57 -05:00
return textureID;
}
2023-12-17 19:37:11 -05:00
virtual ~texture_gl()
{
2023-12-17 15:04:57 -05:00
glDeleteTextures(1, &textureID);
}
};
2023-12-17 19:37:11 -05:00
struct texture_gl2D : public texture_gl
{
2023-12-17 15:04:57 -05:00
public:
explicit texture_gl2D(const texture_data& data);
2023-12-28 16:14:12 -05:00
texture_gl2D(int width, int height, GLint colorMode = GL_RGBA8);
2023-12-17 15:04:57 -05:00
2023-12-17 19:37:11 -05:00
void upload(void* data, GLint dataColorMode = GL_RGBA, int level = 0, int x_offset = 0, int y_offset = 0, int sub_width = -1,
2024-01-17 16:03:07 -05:00
int sub_height = -1, GLint dataMode = GL_UNSIGNED_BYTE) const;
2023-12-17 15:04:57 -05:00
2024-01-25 11:03:17 -05:00
void upload(void* data, int width, int height, GLint dataColorMode = GL_RGBA, GLint dataMode = GL_UNSIGNED_BYTE);
void upload(const texture_data& file_data) const;
2023-12-17 15:04:57 -05:00
/**
* Resizes the internal memory for the texture but does NOT resize the texture image stored
*/
2023-12-28 16:14:12 -05:00
inline void resize(int width, int height);
2023-12-17 15:04:57 -05:00
};
2023-12-17 19:37:11 -05:00
struct gl_texture2D_array : public texture_gl
{
2023-12-17 15:04:57 -05:00
protected:
int m_layers;
public:
2023-12-28 16:14:12 -05:00
gl_texture2D_array(int width, int height, int layers, GLint colorMode = GL_RGBA8);
2023-12-17 15:04:57 -05:00
2023-12-17 19:37:11 -05:00
void upload(void* data, int index, GLint dataColorMode = GL_RGBA, int level = 0, int x_offset = 0, int y_offset = 0, int sub_width = -1,
2023-12-28 16:14:12 -05:00
int sub_height = -1) const;
2023-12-17 15:04:57 -05:00
};
2023-12-17 19:37:11 -05:00
class gl_buffer_texture : public texture_gl2D
{
2023-12-17 15:04:57 -05:00
private:
public:
// make sure a framebuffer is bound before constructing!
gl_buffer_texture(
int width, int height, GLint format = GL_RGB32F,
GLint colorAttachment = GL_COLOR_ATTACHMENT0
2023-12-17 19:37:11 -05:00
): texture_gl2D(width, height, format)
{
2023-12-17 15:04:57 -05:00
bind();
// no mipmaping and no interpolation to position textures!
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
//
glFramebufferTexture2D(
GL_FRAMEBUFFER, colorAttachment, GL_TEXTURE_2D, this->textureID, 0
);
}
};
}
#endif //BLT_TEXTURE_H