Extra2D/include/resource/texture.h

153 lines
3.6 KiB
C
Raw Normal View History

#pragma once
#include <resource/resource.h>
#include <glad/glad.h>
#include <types/base/types.h>
namespace extra2d {
/**
* @brief
*/
enum class TextureFormat : uint8 {
RGB8, // RGB 8位每通道
RGBA8, // RGBA 8位每通道
R8, // 单通道(字体用)
Depth, // 深度缓冲
DepthStencil // 深度模板缓冲
};
/**
* @brief
*/
enum class TextureFilter : uint8 {
Nearest, // 最近邻
Linear, // 线性
MipmapNearest, // 最近邻 Mipmap
MipmapLinear // 线性 Mipmap
};
/**
* @brief
*/
enum class TextureWrap : uint8 {
Repeat, // 重复
Clamp, // 边缘钳制
Mirror // 镜像重复
};
/**
* @brief
*
* OpenGL ES 3.2
*/
class Texture : public Resource {
public:
Texture();
~Texture() override;
/**
* @brief
*/
ResourceType getType() const override { return ResourceType::Texture; }
/**
* @brief
* @param width
* @param height
* @param format
* @return
*/
bool create(uint32 width, uint32 height, TextureFormat format);
/**
* @brief
* @param path
* @return
*/
bool loadFromFile(const std::string& path);
/**
* @brief
* @param data
* @param width
* @param height
* @param format
* @return
*/
bool loadFromMemory(const uint8* data, uint32 width, uint32 height, TextureFormat format);
/**
* @brief
* @param x X坐标
* @param y Y坐标
* @param width
* @param height
* @param data
*/
void updateRegion(uint32 x, uint32 y, uint32 width, uint32 height, const uint8* data);
/**
* @brief
* @param slot 0-31
*/
void bind(uint32 slot = 0) const;
/**
* @brief
*/
void unbind() const;
/**
* @brief
* @param minFilter
* @param magFilter
*/
void setFilter(TextureFilter minFilter, TextureFilter magFilter);
/**
* @brief
* @param wrapS S轴环绕模式
* @param wrapT T轴环绕模式
*/
void setWrap(TextureWrap wrapS, TextureWrap wrapT);
/**
* @brief Mipmap
*/
void generateMipmap();
/**
* @brief
*/
uint32 getWidth() const { return width_; }
/**
* @brief
*/
uint32 getHeight() const { return height_; }
/**
* @brief
*/
TextureFormat getFormat() const { return format_; }
/**
* @brief OpenGL
*/
GLuint getHandle() const { return handle_; }
private:
GLuint handle_ = 0; // OpenGL 纹理句柄
uint32 width_ = 0; // 纹理宽度
uint32 height_ = 0; // 纹理高度
TextureFormat format_ = TextureFormat::RGBA8; // 纹理格式
// 将 TextureFormat 转换为 OpenGL 格式
static GLint getGLInternalFormat(TextureFormat format);
static GLenum getGLFormat(TextureFormat format);
static GLenum getGLType(TextureFormat format);
};
} // namespace extra2d