最后更新:2025-10-12
版本:v0.0.1
| API 函数 | 状态 | 优先级 | 说明 |
|---|---|---|---|
drawTriangle() |
✅ 已实现 | ⭐⭐⭐ | 三角形渲染,支持顶点颜色 |
drawRectangle() |
✅ 已实现 | ⭐⭐⭐ | 矩形渲染,支持颜色和旋转 |
drawCircle() |
✅ 已实现 | ⭐⭐⭐ | 圆形渲染,使用三角扇形 |
drawLine() |
✅ 已实现 | ⭐⭐ | 线段渲染(矩形转换法) |
drawText() |
✅ 已实现 | ⭐⭐⭐ | 文本渲染(简单位图字体) |
drawTexture() |
❌ 未实现 | ⭐⭐ | 纹理渲染 |
setClearColor() |
✅ 已实现 | ⭐⭐⭐ | 设置清屏颜色 |
beginFrame() |
✅ 已实现 | ⭐⭐⭐ | 开始渲染帧 |
endFrame() |
✅ 已实现 | ⭐⭐⭐ | 结束渲染帧 |
文件: native/src/runtime/runtime_impl.h:259
状态: ✅ 完全实现
void drawTriangle(const float* vertices, const float* colors) override {
// 实现了完整的三角形绘制
// 支持顶点颜色插值
}支持的组件:
- ✅ Triangle(三角形)
文件: native/src/runtime/runtime_impl.h:273
状态: ✅ 完全实现
void drawRectangle(float x, float y, float w, float h, const float* color) override {
// 实现了矩形绘制
// 支持位置、大小、颜色
}支持的组件:
- ✅ Rectangle(矩形)
- ✅ Button(按钮)
- ✅ Switch(开关)
- ✅ ProgressBar(进度条)
- ✅ Checkbox(复选框)
- ✅ Slider(滑动条)
- ✅ Input(输入框)
文件: native/src/runtime/runtime_impl.h:299
状态: ✅ 完全实现
void drawCircle(float x, float y, float radius, const float* color) override {
// 使用三角扇形(triangle fan)绘制圆形
const int segments = 32; // 圆形分段数
// ... 实现细节
}支持的组件:
- ✅ Circle(圆形)
- ✅ Radio(单选框)
- ✅ Slider(滑块的圆形部分)
文件: native/src/runtime/runtime_impl.h:295
状态: ✅ 完全实现
void drawLine(float x1, float y1, float x2, float y2, const float* color, float width) override {
// 将线段转换为旋转矩形绘制
// 使用矩阵变换计算矩形顶点
}实现方式: 矩形转换法
- 计算线段长度和角度
- 生成旋转的矩形(两个三角形)
- 支持任意宽度
支持的组件:
- ✅ Line(线段渲染)
- ✅ 所有需要绘制线条的组件
文件: native/src/runtime/runtime_impl.h:447
状态: ✅ 基本实现(简单位图字体)
void drawText(const char* text, float x, float y, const float* color, float size) override {
// 简单的位图字体实现
// 使用矩形绘制基本字符
}实现方式: 简单位图字体
- 使用矩形组合绘制字符
- 支持数字 0-9
- 支持常用字母 A-Z
- 支持特殊字符 (. : - / [ ] ( ) ~)
- 自动转换小写为大写
支持的组件:
- ✅ Text(文本渲染)
- ✅ Line(线段渲染)- 标题和提示
- ✅ 所有需要显示文字的组件
限制:
- 仅支持基本字符
- 字符样式简化
- 未来可升级为 FreeType
文件: native/src/runtime/runtime_impl.h:675
状态: ❌ 未实现
void drawTexture(void* texture, float x, float y, float w, float h) override {
// TODO: 实现纹理绘制
}技术挑战:
- 需要纹理加载和管理
- 需要UV坐标映射
- 需要纹理采样器配置
影响的组件:
- 图像组件(规划中)
- UI 纹理装饰(规划中)
优先级: ⭐⭐ 中等
- ✅ 基础图形绘制(Triangle, Rectangle, Circle)
- ✅ 文本渲染系统(drawText)- 简单位图字体
- ✅ 线段渲染(drawLine)- 矩形转换法
- 📝 纹理系统(drawTexture)
- 📝 高级渲染效果(阴影、模糊)
- 📝 动画系统优化
- 📝 完整的字体系统(多字体、多语言)
- 📝 高级图形(贝塞尔曲线、路径)
- 📝 3D 渲染支持
方案 A:几何着色器
// geometry shader
layout(lines) in;
layout(triangle_strip, max_vertices = 4) out;
void main() {
// 根据线段端点生成矩形
vec2 p1 = gl_in[0].gl_Position.xy;
vec2 p2 = gl_in[1].gl_Position.xy;
vec2 dir = normalize(p2 - p1);
vec2 normal = vec2(-dir.y, dir.x);
float width = line_width * 0.5;
// 生成4个顶点
EmitVertex(p1 + normal * width);
EmitVertex(p1 - normal * width);
EmitVertex(p2 + normal * width);
EmitVertex(p2 - normal * width);
EndPrimitive();
}方案 B:CPU 端转换
void drawLine(float x1, float y1, float x2, float y2,
const float* color, float width) {
// 计算线段方向和法线
float dx = x2 - x1;
float dy = y2 - y1;
float length = std::sqrt(dx * dx + dy * dy);
float nx = -dy / length * width * 0.5f;
float ny = dx / length * width * 0.5f;
// 转换为矩形绘制
float centerX = (x1 + x2) * 0.5f;
float centerY = (y1 + y2) * 0.5f;
float angle = std::atan2(dy, dx);
drawRectangle(centerX, centerY, length, width, color, angle);
}推荐: 方案 B(CPU 端转换)- 更简单,兼容性更好
方案 A:FreeType + 纹理图集
// 1. 使用 FreeType 加载字体
FT_Library ft;
FT_Init_FreeType(&ft);
FT_Face face;
FT_New_Face(ft, "font.ttf", 0, &face);
// 2. 生成字形纹理图集
for (char c = 0; c < 128; c++) {
FT_Load_Char(face, c, FT_LOAD_RENDER);
// 将字形渲染到纹理图集
}
// 3. 渲染文本
void drawText(const char* text, float x, float y,
const float* color, float size) {
for (const char* p = text; *p; p++) {
// 从图集获取字形纹理
// 使用 drawTexture 渲染
}
}方案 B:位图字体
// 预渲染的位图字体(简化版)
struct BitmapChar {
unsigned char pixels[16][16];
int width, height;
};
void drawText(const char* text, float x, float y,
const float* color, float size) {
for (const char* p = text; *p; p++) {
// 使用矩形近似字符
drawCharAsRects(*p, x, y, color);
x += char_width;
}
}推荐: 方案 A(FreeType)- 专业,质量高
如果你想帮助实现这些 API:
- 查看
native/src/runtime/runtime_impl.h - 找到 TODO 标记的函数
- 实现功能并添加测试
- 更新本文档状态
- 提交 Pull Request
- Native API 定义:
native/include/runtime_api.h - Runtime 实现:
native/src/runtime/runtime_impl.h - Vulkan 渲染:
native/src/vk/instance.cpp - FreeType 文档: https://freetype.org/
- Vulkan 教程: https://vulkan-tutorial.com/
维护者:Bit Project 团队