-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelManager.h
More file actions
317 lines (250 loc) · 11 KB
/
ModelManager.h
File metadata and controls
317 lines (250 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//
// Created by Marrony Neris on 11/17/15.
//
#ifndef MODEL_MANAGER_H
#define MODEL_MANAGER_H
#include "Shapes.h"
#include "Model.h"
#include "Material.h"
#include "ModelInstance.h"
#include "Wavefront.h"
const int MAX_MODEL_NAME = 8;
class ModelManager {
public:
ModelManager(HeapAllocator& allocator, Device& device) : allocator(allocator), device(device) {
modelCount = 0;
modelAllocated = 16;
models = (Resource*) allocator.allocate(modelAllocated * sizeof(Resource));
}
~ModelManager() {
assert(modelCount == 0);
allocator.deallocate(models);
}
Model* findModel(const char* name) {
for(uint32_t i = 0; i < modelCount; i++) {
if(strncmp(models[i].name, name, MAX_MODEL_NAME) == 0) {
models[i].refs++;
return models[i].model;
}
}
return nullptr;
}
Model* createSphere(const char* name, float size, int numberSlices) {
assert(strlen(name) <= MAX_MODEL_NAME);
uint32_t index = getSlot();
Shape shape;
mnCreateSphere(size, numberSlices, shape);
VertexBuffer vertexBuffer = device.createStaticVertexBuffer(shape.numberVertices*sizeof(Vector3), shape.vertices);
VertexBuffer normalBuffer = device.createStaticVertexBuffer(shape.numberVertices*sizeof(Vector3), shape.normals);
VertexBuffer tangentBuffer = device.createStaticVertexBuffer(shape.numberVertices*sizeof(Vector3), shape.tangent);
VertexBuffer textureBuffer = device.createStaticVertexBuffer(shape.numberVertices*sizeof(Vector2), shape.texture);
IndexBuffer indexBuffer = device.createIndexBuffer(shape.numberIndices*sizeof(uint16_t), shape.indices);
VertexDeclaration vertexDeclaration[4];
vertexDeclaration[0].buffer = vertexBuffer;
vertexDeclaration[0].format = VertexFloat3;
vertexDeclaration[0].offset = 0;
vertexDeclaration[0].stride = 0;
vertexDeclaration[1].buffer = textureBuffer;
vertexDeclaration[1].format = VertexFloat2;
vertexDeclaration[1].offset = 0;
vertexDeclaration[1].stride = 0;
vertexDeclaration[2].buffer = normalBuffer;
vertexDeclaration[2].format = VertexFloat3;
vertexDeclaration[2].offset = 0;
vertexDeclaration[2].stride = 0;
vertexDeclaration[3].buffer = tangentBuffer;
vertexDeclaration[3].format = VertexFloat3;
vertexDeclaration[3].offset = 0;
vertexDeclaration[3].stride = 0;
models[index].vertexBuffer[0] = vertexBuffer;
models[index].vertexBuffer[1] = textureBuffer;
models[index].vertexBuffer[2] = normalBuffer;
models[index].vertexBuffer[3] = tangentBuffer;
models[index].indexBuffer = indexBuffer;
models[index].vertexArray = device.createVertexArray(vertexDeclaration, 4, indexBuffer);
models[index].model = Model::create(allocator, models[index].vertexArray, 1);
strncpy(models[index].name, name, MAX_MODEL_NAME);
models[index].refs = 1;
Model::addMesh(allocator, models[index].model, 0, 0, shape.numberIndices);
mnDestroyShape(shape);
return models[index].model;
}
Model* loadWavefront(const char* filename, bool forceNotIndexed = false) {
uint32_t index = getSlot();
Wavefront obj;
mnLoadWavefront(allocator, filename, obj, forceNotIndexed);
WavefrontObject* currentObj = obj.objects;
VertexBuffer vertexBuffer = device.createStaticVertexBuffer(currentObj->numberVertices*sizeof(Vector3), currentObj->vertices);
VertexBuffer normalBuffer = device.createStaticVertexBuffer(currentObj->numberVertices*sizeof(Vector3), currentObj->normals);
VertexBuffer tangentBuffer = device.createStaticVertexBuffer(currentObj->numberVertices*sizeof(Vector3), currentObj->tangent);
VertexBuffer textureBuffer = device.createStaticVertexBuffer(currentObj->numberVertices*sizeof(Vector2), currentObj->texture);
IndexBuffer indexBuffer = {0};
if (currentObj->numberIndices > 0) {
indexBuffer = device.createIndexBuffer(currentObj->numberIndices*sizeof(uint16_t), currentObj->indices);
}
VertexDeclaration vertexDeclaration[4];
vertexDeclaration[0].buffer = vertexBuffer;
vertexDeclaration[0].format = VertexFloat3;
vertexDeclaration[0].offset = 0;
vertexDeclaration[0].stride = 0;
vertexDeclaration[1].buffer = textureBuffer;
vertexDeclaration[1].format = VertexFloat2;
vertexDeclaration[1].offset = 0;
vertexDeclaration[1].stride = 0;
vertexDeclaration[2].buffer = normalBuffer;
vertexDeclaration[2].format = VertexFloat3;
vertexDeclaration[2].offset = 0;
vertexDeclaration[2].stride = 0;
vertexDeclaration[3].buffer = tangentBuffer;
vertexDeclaration[3].format = VertexFloat3;
vertexDeclaration[3].offset = 0;
vertexDeclaration[3].stride = 0;
models[index].vertexBuffer[0] = vertexBuffer;
models[index].vertexBuffer[1] = textureBuffer;
models[index].vertexBuffer[2] = normalBuffer;
models[index].vertexBuffer[3] = tangentBuffer;
models[index].indexBuffer = indexBuffer;
models[index].vertexArray = device.createVertexArray(vertexDeclaration, 4, indexBuffer);
models[index].model = Model::create(allocator, models[index].vertexArray, currentObj->numberGroups, currentObj->numberIndices > 0);
strncpy(models[index].name, "venus", MAX_MODEL_NAME);
models[index].refs = 1;
for(int i = 0; i < currentObj->numberGroups; i++) {
WavefrontGroup* currentGroup = ¤tObj->groups[i];
Model::addMesh(allocator, models[index].model, i, currentGroup->startIndices, currentGroup->numberIndices);
}
mnDestroyWavefront(allocator, obj);
return models[index].model;
}
Model* createQuad(const char* name) {
assert(strlen(name) <= MAX_MODEL_NAME);
uint32_t index = getSlot();
Vector3 vertex[] = {
-1.0, -1.0, 0.0,
-1.0, +1.0, 0.0,
+1.0, +1.0, 0.0,
+1.0, -1.0, 0.0,
};
Vector2 texture[] = {
0, 0,
0, 1,
1, 1,
1, 0
};
Vector3 normal[] = {
0.0, 0.0, 1.0,
0.0, 0.0, 1.0,
0.0, 0.0, 1.0,
0.0, 0.0, 1.0,
};
Vector3 tangent[] = {
1.0, 0.0, 0.0,
1.0, 0.0, 0.0,
1.0, 0.0, 0.0,
1.0, 0.0, 0.0,
};
uint16_t indices[] = {0, 1, 3, 3, 1, 2};
VertexBuffer vertexBuffer = device.createStaticVertexBuffer(sizeof(vertex), vertex);
VertexBuffer textureBuffer = device.createStaticVertexBuffer(sizeof(texture), texture);
VertexBuffer normalBuffer = device.createStaticVertexBuffer(sizeof(normal), normal);
VertexBuffer tangentBuffer = device.createStaticVertexBuffer(sizeof(tangent), tangent);
IndexBuffer indexBuffer = device.createIndexBuffer(sizeof(indices), indices);
VertexDeclaration vertexDeclaration[4] = {};
vertexDeclaration[0].buffer = vertexBuffer;
vertexDeclaration[0].format = VertexFloat3;
vertexDeclaration[0].offset = 0;
vertexDeclaration[0].stride = 0;
vertexDeclaration[1].buffer = textureBuffer;
vertexDeclaration[1].format = VertexFloat2;
vertexDeclaration[1].offset = 0;
vertexDeclaration[1].stride = 0;
vertexDeclaration[2].buffer = normalBuffer;
vertexDeclaration[2].format = VertexFloat3;
vertexDeclaration[2].offset = 0;
vertexDeclaration[2].stride = 0;
vertexDeclaration[3].buffer = tangentBuffer;
vertexDeclaration[3].format = VertexFloat3;
vertexDeclaration[3].offset = 0;
vertexDeclaration[3].stride = 0;
models[index].vertexBuffer[0] = vertexBuffer;
models[index].vertexBuffer[1] = textureBuffer;
models[index].vertexBuffer[2] = normalBuffer;
models[index].vertexBuffer[3] = tangentBuffer;
models[index].indexBuffer = indexBuffer;
models[index].vertexArray = device.createVertexArray(vertexDeclaration, 4, indexBuffer);
models[index].model = Model::create(allocator, models[index].vertexArray, 1);
strncpy(models[index].name, name, MAX_MODEL_NAME);
models[index].refs = 1;
Model::addMesh(allocator, models[index].model, 0, 0, 6);
return models[index].model;
}
void destroyModel(Model* model) {
uint32_t index;
if (findModel(model, index))
destroy(index);
}
ModelInstance* createModelInstance(Model* model, int instanceCount, ConstantBuffer constantBuffer, int bindingPoint) {
uint32_t index;
if (findModel(model, index)) {
models[index].refs++;
if (instanceCount > 1)
return ModelInstance::createInstanced(allocator, model, instanceCount, constantBuffer, bindingPoint);
return ModelInstance::create(allocator, model, constantBuffer, bindingPoint);
}
return nullptr;
}
void destroyModelInstance(ModelInstance* modelInstance) {
uint32_t index;
if (findModel(modelInstance->model, index)) {
destroy(index);
ModelInstance::destroy(allocator, modelInstance);
}
}
private:
struct Resource {
char name[MAX_MODEL_NAME+1];
VertexBuffer vertexBuffer[4];
IndexBuffer indexBuffer;
VertexArray vertexArray;
Model* model;
uint32_t refs;
};
void destroy(uint32_t index) {
models[index].refs--;
if (models[index].refs == 0) {
destroy(&models[index]);
modelCount--;
std::swap(models[index], models[modelCount]);
}
}
void destroy(Resource* resource) {
device.destroyVertexArray(resource->vertexArray);
device.destroyVertexBuffer(resource->vertexBuffer[0]);
device.destroyVertexBuffer(resource->vertexBuffer[1]);
device.destroyVertexBuffer(resource->vertexBuffer[2]);
device.destroyVertexBuffer(resource->vertexBuffer[3]);
device.destroyIndexBuffer(resource->indexBuffer);
Model::destroy(allocator, resource->model);
}
uint32_t getSlot() {
if(modelCount >= modelAllocated) {
modelAllocated = modelAllocated * 3 / 2;
models = (Resource*) allocator.reallocate(models, modelAllocated * sizeof(Resource));
}
return modelCount++;
}
bool findModel(Model* model, uint32_t& index) {
for (uint32_t i = 0; i < modelCount; i++) {
if (models[i].model == model) {
index = i;
return true;
}
}
return false;
}
HeapAllocator& allocator;
Device& device;
Resource* models;
uint32_t modelCount;
uint32_t modelAllocated;
};
#endif //MODEL_MANAGER_H