153 lines
3.6 KiB
C
153 lines
3.6 KiB
C
|
|
#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
|