FCT
载入中...
搜索中...
未找到
Mesh.h
浏览该文件的文档.
1//
2// Created by Administrator on 2025/4/22.
3//
4#ifndef FCT_MESH_H
5#define FCT_MESH_H
6#include "../ThirdParty.h"
7#include "./Vertex.h"
8#include "Context.h"
10#include "../RHI/VertexBuffer.h"
12namespace FCT
13{
14 template<typename IndexType = uint16_t>
16 {
17 public:
18 StaticMesh(Context* context, const VertexLayout& layout)
19 : m_ctx(context), m_vertexLayout(layout), m_gpuIndex(nullptr), m_gpuVertex(nullptr)
20 {
21 m_cpuVertex = new VertexBuffer(layout);
22 }
23
25 {
26 if (m_cpuVertex) {
27 delete m_cpuVertex;
28 }
29 if (m_gpuVertex) {
30 delete m_gpuVertex;
31 }
32 if (m_gpuIndex) {
33 delete m_gpuIndex;
34 }
35 }
36
37 template<typename... Args>
38 void addVertex(Args&&... args)
39 {
40 m_cpuVertex->emplaceBack(std::forward<Args>(args)...);
41 }
42
43 void setIndices(const std::vector<IndexType>& indices)
44 {
45 m_cpuIndices = indices;
46 }
47
48 void create();
49
51 {
52 if (m_gpuVertex) {
53 m_gpuVertex->bind(cmdBuf);
54 }
55
56 if (m_gpuIndex) {
57 m_gpuIndex->bind(cmdBuf);
58 }
59 }
60
61 void draw(RHI::CommandBuffer* cmdBuf, uint32_t instanceCount = 1)
62 {
63 if (m_gpuIndex) {
64 cmdBuf->drawIndex(0, 0, static_cast<uint32_t>(m_cpuIndices.size()), instanceCount);
65 } else {
66 cmdBuf->draw(0, 0, m_cpuVertex->getVertexCount(), instanceCount);
67 }
68 }
69
71 {
72 if (m_gpuVertex) {
73 m_gpuVertex->updataBuffer();
74 }
75 }
76
78 {
79 if (m_gpuIndex) {
80 m_gpuIndex->updataBuffer();
81 }
82 }
83
85
86 size_t getIndexCount() const { return m_cpuIndices.size(); }
87
88 size_t getVertexCount() const { return m_cpuVertex ? m_cpuVertex->getVertexCount() : 0; }
89
90 private:
96 std::vector<IndexType> m_cpuIndices;
97 };
98
99
100 template<typename IndexType = uint16_t>
102 {
103 public:
104 DynamicMesh(Context* context, const VertexLayout& layout)
105 : m_ctx(context), m_vertexLayout(layout), m_gpuIndex(nullptr), m_gpuVertex(nullptr)
106 {
107 m_cpuVertex = new VertexBuffer(layout);
108 }
109
111 {
112 if (m_cpuVertex) {
113 delete m_cpuVertex;
114 }
115 if (m_gpuVertex) {
116 delete m_gpuVertex;
117 }
118 if (m_gpuIndex) {
119 delete m_gpuIndex;
120 }
121 }
122
123 template<typename... Args>
124 void addVertex(Args&&... args)
125 {
126 if (m_gpuVertex && m_cpuVertex->getVertexCount() >= m_vertexCapacity) {
127 size_t newCapacity = m_vertexCapacity == 0 ? 1 : m_vertexCapacity * 2;
128 resizeVertexBuffer(newCapacity);
129 }
130 m_cpuVertex->emplaceBack(std::forward<Args>(args)...);
131 }
132 void addIndex(IndexType index)
133 {
134 if (m_gpuIndex && m_cpuIndices.size() >= m_indexCapacity) {
135 size_t newCapacity = m_indexCapacity == 0 ? 1 : m_indexCapacity * 2;
136 resizeIndexBuffer(newCapacity);
137 }
138 m_cpuIndices.push_back(index);
139 }
140
141 void addIndices(const std::vector<IndexType>& indices)
142 {
143 if (m_gpuIndex && (m_cpuIndices.size() + indices.size()) > m_indexCapacity) {
144 size_t tmp = m_cpuIndices.size() + indices.size();
145 size_t tmp2 = m_indexCapacity * 2;
146 size_t newCapacity = std::max(tmp2, tmp);
147 resizeIndexBuffer(newCapacity);
148 }
149
150 m_cpuIndices.insert(m_cpuIndices.end(), indices.begin(), indices.end());
151 }
152
153 void setIndices(const std::vector<IndexType>& indices)
154 {
155 m_cpuIndices = indices;
156 m_indexCapacity = indices.size();
157
158 if (m_gpuIndex) {
160 }
161 }
162
163 void reserveVertices(size_t capacity)
164 {
165 if (!m_gpuVertex) {
166 m_vertexCapacity = capacity;
167 }
168 }
169
170 void reserveIndices(size_t capacity)
171 {
172 if (!m_gpuIndex) {
173 m_indexCapacity = capacity;
174 }
175 }
176
177 void create();
178
180 {
181 if (m_gpuVertex) {
182 m_gpuVertex->bind(cmdBuf);
183 }
184
185 if (m_gpuIndex) {
186 m_gpuIndex->bind(cmdBuf);
187 }
188 }
189
190 void draw(RHI::CommandBuffer* cmdBuf, uint32_t instanceCount = 1)
191 {
192 if (m_gpuIndex && !m_cpuIndices.empty()) {
193 cmdBuf->drawIndex(0, 0, static_cast<uint32_t>(m_cpuIndices.size()), instanceCount);
194 } else if (m_cpuVertex && m_cpuVertex->getVertexCount() > 0) {
195 cmdBuf->draw(0, 0, m_cpuVertex->getVertexCount(), instanceCount);
196 }
197 }
198
199 void clear()
200 {
201 if (m_cpuVertex) {
202 m_cpuVertex->clear();
203 }
204 m_cpuIndices.clear();
205 }
206
208 {
209 if (m_gpuVertex) {
210 m_gpuVertex->updataBuffer();
211 }
212 }
213
215 {
216 if (m_gpuIndex) {
217 m_gpuIndex->updataBuffer();
218 }
219 }
220 void update()
221 {
224 }
225
226
228
229 size_t getIndexCount() const { return m_cpuIndices.size(); }
230
231 size_t getVertexCount() const { return m_cpuVertex ? m_cpuVertex->getVertexCount() : 0; }
232
233 size_t getVertexCapacity() const { return m_vertexCapacity; }
234
235 size_t getIndexCapacity() const { return m_indexCapacity; }
236 private:
237 void resizeVertexBuffer(size_t newCapacity)
238 {
239 if (m_gpuVertex) {
240 m_gpuVertex->resize(newCapacity);
241 m_vertexCapacity = newCapacity;
242 }
243 }
244
245 void resizeIndexBuffer(size_t newCapacity)
246 {
247 if (m_gpuIndex) {
248 m_gpuIndex->resize(newCapacity);
249 m_indexCapacity = newCapacity;
250 }
251 }
252
253 private:
259 std::vector<IndexType> m_cpuIndices;
260
262 size_t m_indexCapacity = 0;
263 };
264
265}
266#endif //MESH_H
void resizeVertexBuffer(size_t newCapacity)
定义 Mesh.h:237
void setIndices(const std::vector< IndexType > &indices)
定义 Mesh.h:153
VertexBuffer * m_cpuVertex
定义 Mesh.h:256
VertexLayout m_vertexLayout
定义 Mesh.h:255
void updateVertexData()
定义 Mesh.h:207
void updateIndexData()
定义 Mesh.h:214
RHI::IndexBuffer * m_gpuIndex
定义 Mesh.h:258
void draw(RHI::CommandBuffer *cmdBuf, uint32_t instanceCount=1)
定义 Mesh.h:190
std::vector< IndexType > m_cpuIndices
定义 Mesh.h:259
void bind(RHI::CommandBuffer *cmdBuf)
定义 Mesh.h:179
VertexBuffer * getVertexBuffer() const
定义 Mesh.h:227
Context * m_ctx
定义 Mesh.h:254
size_t getIndexCapacity() const
定义 Mesh.h:235
DynamicMesh(Context *context, const VertexLayout &layout)
定义 Mesh.h:104
void reserveVertices(size_t capacity)
定义 Mesh.h:163
void addIndex(IndexType index)
定义 Mesh.h:132
RHI::VertexBuffer * m_gpuVertex
定义 Mesh.h:257
size_t m_indexCapacity
定义 Mesh.h:262
size_t m_vertexCapacity
定义 Mesh.h:261
void addVertex(Args &&... args)
定义 Mesh.h:124
size_t getVertexCount() const
定义 Mesh.h:231
void resizeIndexBuffer(size_t newCapacity)
定义 Mesh.h:245
void addIndices(const std::vector< IndexType > &indices)
定义 Mesh.h:141
size_t getVertexCapacity() const
定义 Mesh.h:233
size_t getIndexCount() const
定义 Mesh.h:229
void reserveIndices(size_t capacity)
定义 Mesh.h:170
virtual void draw(size_t vertexBegin, size_t instanceBegin, size_t vertexSize, size_t instanceSize)=0
virtual void drawIndex(size_t indexBegin, size_t instanceBegin, size_t indexCount, size_t instanceCount, size_t firstVertex=0)=0
std::vector< IndexType > m_cpuIndices
定义 Mesh.h:96
VertexLayout m_vertexLayout
定义 Mesh.h:92
StaticMesh(Context *context, const VertexLayout &layout)
定义 Mesh.h:18
void addVertex(Args &&... args)
定义 Mesh.h:38
size_t getVertexCount() const
定义 Mesh.h:88
size_t getIndexCount() const
定义 Mesh.h:86
void updateIndexData()
定义 Mesh.h:77
void setIndices(const std::vector< IndexType > &indices)
定义 Mesh.h:43
RHI::VertexBuffer * m_gpuVertex
定义 Mesh.h:94
VertexBuffer * getVertexBuffer() const
定义 Mesh.h:84
void updateVertexData()
定义 Mesh.h:70
void draw(RHI::CommandBuffer *cmdBuf, uint32_t instanceCount=1)
定义 Mesh.h:61
RHI::IndexBuffer * m_gpuIndex
定义 Mesh.h:95
void bind(RHI::CommandBuffer *cmdBuf)
定义 Mesh.h:50
VertexBuffer * m_cpuVertex
定义 Mesh.h:93
Context * m_ctx
定义 Mesh.h:91