FCT
载入中...
搜索中...
未找到
Primitives.h
浏览该文件的文档.
1//
2// Created by Administrator on 2025/5/11.
3//
4
5#ifndef PRIMITIVES_H
6#define PRIMITIVES_H
7namespace FCT
8{
9 namespace Primitives
10 {
11 enum class VertexMode {
15 };
16
22 template<typename IndexType = uint16_t>
23static StaticMesh<IndexType>* createCube(Context* ctx, float size = 1.0f,
24 const VertexLayout& layout = VertexLayout{
28 })
29 {
30 StaticMesh<IndexType>* mesh = new StaticMesh<IndexType>(ctx, layout);
31
32 float halfSize = size * 0.5f;
33
34 Vec3 vertices[8] = {
35 Vec3(-halfSize, -halfSize, -halfSize),
36 Vec3(halfSize, -halfSize, -halfSize),
37 Vec3(halfSize, halfSize, -halfSize),
38 Vec3(-halfSize, halfSize, -halfSize),
39 Vec3(-halfSize, -halfSize, halfSize),
40 Vec3(halfSize, -halfSize, halfSize),
41 Vec3(halfSize, halfSize, halfSize),
42 Vec3(-halfSize, halfSize, halfSize)
43 };
44
45 Vec4 colors[6] = {
46 Vec4(1.0f, 0.0f, 0.0f, 1.0f),
47 Vec4(0.0f, 1.0f, 0.0f, 1.0f),
48 Vec4(0.0f, 0.0f, 1.0f, 1.0f),
49 Vec4(1.0f, 1.0f, 0.0f, 1.0f),
50 Vec4(1.0f, 0.0f, 1.0f, 1.0f),
51 Vec4(0.0f, 1.0f, 1.0f, 1.0f)
52 };
53
54 Vec2 texCoords[4] = {
55 Vec2(0.0f, 1.0f),
56 Vec2(1.0f, 1.0f),
57 Vec2(1.0f, 0.0f),
58 Vec2(0.0f, 0.0f)
59 };
60
61 mesh->addVertex(vertices[4], colors[0], texCoords[0]);
62 mesh->addVertex(vertices[5], colors[0], texCoords[1]);
63 mesh->addVertex(vertices[6], colors[0], texCoords[2]);
64 mesh->addVertex(vertices[7], colors[0], texCoords[3]);
65
66 mesh->addVertex(vertices[0], colors[1], texCoords[1]);
67 mesh->addVertex(vertices[1], colors[1], texCoords[0]);
68 mesh->addVertex(vertices[2], colors[1], texCoords[3]);
69 mesh->addVertex(vertices[3], colors[1], texCoords[2]);
70
71 mesh->addVertex(vertices[3], colors[2], texCoords[0]);
72 mesh->addVertex(vertices[2], colors[2], texCoords[1]);
73 mesh->addVertex(vertices[6], colors[2], texCoords[2]);
74 mesh->addVertex(vertices[7], colors[2], texCoords[3]);
75
76 mesh->addVertex(vertices[0], colors[3], texCoords[0]);
77 mesh->addVertex(vertices[1], colors[3], texCoords[1]);
78 mesh->addVertex(vertices[5], colors[3], texCoords[2]);
79 mesh->addVertex(vertices[4], colors[3], texCoords[3]);
80
81 mesh->addVertex(vertices[1], colors[4], texCoords[0]);
82 mesh->addVertex(vertices[2], colors[4], texCoords[1]);
83 mesh->addVertex(vertices[6], colors[4], texCoords[2]);
84 mesh->addVertex(vertices[5], colors[4], texCoords[3]);
85
86 mesh->addVertex(vertices[0], colors[5], texCoords[0]);
87 mesh->addVertex(vertices[3], colors[5], texCoords[1]);
88 mesh->addVertex(vertices[7], colors[5], texCoords[2]);
89 mesh->addVertex(vertices[4], colors[5], texCoords[3]);
90
91 mesh->setIndices({
92 0, 1, 2, 2, 3, 0,
93 4, 5, 6, 6, 7, 4,
94 8, 9, 10, 10, 11, 8,
95 12, 13, 14, 14, 15, 12,
96 16, 17, 18, 18, 19, 16,
97 20, 21, 22, 22, 23, 20
98 });
99
100 mesh->create();
101
102 return mesh;
103 }
104
105 template<typename IndexType = uint16_t>
106 static StaticMesh<IndexType>* createSphere(Context* ctx, float radius = 1.0f, int segments = 16,
107 const VertexLayout& layout = VertexLayout{
111 })
112 {
113 StaticMesh<IndexType>* mesh = new StaticMesh<IndexType>(ctx, layout);
114
115 segments = std::max(3, segments);
116
117 std::vector<Vec3> positions;
118 std::vector<Vec4> colors;
119 std::vector<Vec2> texCoords;
120 std::vector<IndexType> indices;
121
122 for (int y = 0; y <= segments; y++) {
123 for (int x = 0; x <= segments; x++) {
124 float xSegment = (float)x / (float)segments;
125 float ySegment = (float)y / (float)segments;
126 float xPos = std::cos(xSegment * 2.0f * M_PI) * std::sin(ySegment * M_PI);
127 float yPos = std::cos(ySegment * M_PI);
128 float zPos = std::sin(xSegment * 2.0f * M_PI) * std::sin(ySegment * M_PI);
129
130 positions.push_back(Vec3(xPos, yPos, zPos) * radius);
131 colors.push_back(Vec4(xSegment, ySegment, 1.0f - ySegment, 1.0f));
132 texCoords.push_back(Vec2(xSegment, ySegment));
133 }
134 }
135
136 for (int y = 0; y < segments; y++) {
137 for (int x = 0; x < segments; x++) {
138 IndexType first = (y * (segments + 1)) + x;
139 IndexType second = first + segments + 1;
140
141 indices.push_back(first);
142 indices.push_back(second);
143 indices.push_back(first + 1);
144
145 indices.push_back(second);
146 indices.push_back(second + 1);
147 indices.push_back(first + 1);
148 }
149 }
150
151 for (size_t i = 0; i < positions.size(); i++) {
152 mesh->addVertex(positions[i], colors[i], texCoords[i]);
153 }
154
155 mesh->setIndices(indices);
156 mesh->create();
157
158 return mesh;
159 }
160 }
161}
162#endif //PRIMITIVES_H
void addVertex(Args &&... args)
定义 Mesh.h:38
void setIndices(const std::vector< IndexType > &indices)
定义 Mesh.h:43
static StaticMesh< IndexType > * createCube(Context *ctx, float size=1.0f, const VertexLayout &layout=VertexLayout{ VertexElement{VtxType::Position3f}, VertexElement{VtxType::Color4f}, VertexElement{VtxType::TexCoord2f} })
static StaticMesh< IndexType > * createSphere(Context *ctx, float radius=1.0f, int segments=16, const VertexLayout &layout=VertexLayout{ VertexElement{VtxType::Position3f}, VertexElement{VtxType::Color4f}, VertexElement{VtxType::TexCoord2f} })