Files
XCEngine/docs/api/rhi/opengl/opengl-texture.md

150 lines
3.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# OpenGLTexture
OpenGL 纹理的实现,封装 OpenGL texture object。
## 头文件
```cpp
#include <XCEngine/RHI/OpenGL/OpenGLTexture.h>
```
## 继承关系
```
RHITexture (interface)
└── OpenGLTexture (implementation)
```
## 枚举
### OpenGLTextureType
| 值 | 描述 |
|----|------|
| `Texture1D` | 1D 纹理 |
| `Texture2D` | 2D 纹理 |
| `Texture2DArray` | 2D 纹理数组 |
| `Texture3D` | 3D 纹理 |
| `TextureCube` | 立方体贴图 |
| `TextureCubeArray` | 立方体贴图数组 |
### OpenGLFormat / OpenGLInternalFormat
| OpenGLFormat | OpenGLInternalFormat | 描述 |
|--------------|---------------------|------|
| `R8` | `1` | 单通道 8-bit |
| `RG8` | `2` | 双通道 8-bit |
| `RGBA8` | `4` | 四通道 8-bit |
| `RGBA16F` | `11` | 四通道 16-bit float |
| `RGBA32F` | `16` | 四通道 32-bit float |
| `Depth24Stencil8` | `38` | 24-bit depth + 8-bit stencil |
| `Depth32F` | `31` | 32-bit float depth |
| `CompressedDXT1` | `21` | DXT1 压缩 |
| `CompressedDXT5` | `22` | DXT5 压缩 |
## 公共成员函数
### 构造函数与析构函数
#### `OpenGLTexture()`
#### `~OpenGLTexture() override`
### 初始化
#### `bool Initialize(OpenGLTextureType type, int width, int height, int depth, int mipLevels, OpenGLFormat format, const void* data = nullptr)`
通用初始化。
#### `bool Initialize2D(int width, int height, int channels, const void* data, bool generateMipmap = true)`
便捷的 2D 纹理初始化。
- `channels`: 通道数 (1-4)
#### `bool InitializeCubeMap(int size, int mipLevels, OpenGLFormat format, const void* data = nullptr)`
初始化立方体贴图。
#### `bool LoadFromFile(const char* path, bool flipVertical = true)`
从图片文件加载纹理(需要 stb_image
#### `void Shutdown() override`
### 绑定操作
#### `void Bind(int slot = 0) const`
绑定纹理到纹理单元。
#### `void Unbind() const`
#### `void BindImage(int slot, bool read, bool write) const`
绑定为 image texture用于 compute shader
### 纹理操作
#### `void GenerateMipmap()`
生成 Mipmap。
#### `void SetFiltering(int minFilter, int magFilter)`
设置过滤模式。
#### `void SetWrapping(int wrapS, int wrapT, int wrapR = -1)`
设置纹理环绕模式。
### 属性
#### `unsigned int GetID() const`
获取 OpenGL texture ID。
#### `OpenGLTextureType GetOpenGLType() const`
获取纹理类型。
#### `uint32_t GetWidth() const override`
#### `uint32_t GetHeight() const override`
#### `uint32_t GetDepth() const override`
#### `uint32_t GetMipLevels() const override`
#### `TextureType GetTextureType() const override`
转换为 RHI TextureType。
#### `void* GetNativeHandle() override`
#### `Format GetFormat() const override / void SetFormat(Format format)`
#### `const std::string& GetName() const override / void SetName(...)`
## 内部成员
| 成员 | 类型 | 描述 |
|------|------|------|
| `m_texture` | `unsigned int` | GL texture ID |
| `m_type` | `OpenGLTextureType` | 纹理类型 |
| `m_width/height/depth` | `int` | 尺寸 |
| `m_mipLevels` | `int` | Mipmap 级别 |
| `m_channels` | `int` | 通道数 |
| `m_format` | `Format` | RHI 格式 |
| `m_name` | `std::string` | 名称 |
## 使用示例
```cpp
// Load from file
OpenGLTexture diffuse;
diffuse.LoadFromFile("textures/diffuse.png");
// Create from data
OpenGLTexture texture;
unsigned char pixels[] = { ... };
texture.Initialize2D(512, 512, 4, pixels);
texture.SetFiltering(GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR);
texture.SetWrapping(GL_REPEAT, GL_REPEAT);
texture.GenerateMipmap();
// Bind
texture.Bind(0);
glBindTextureUnit(0, texture.GetID());
// Compute image binding
texture.BindImage(0, true, false);
```