-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model.h
69 lines (56 loc) · 2.06 KB
/
Model.h
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
//
// Created by Marrony Neris on 11/10/15.
//
#ifndef MODEL_H
#define MODEL_H
struct Mesh {
CommandBuffer* draw;
int offset;
int count;
static void create(HeapAllocator& allocator, Mesh* mesh, int offset, int count, bool useIndex = true) {
mesh->offset = offset;
mesh->count = count;
mesh->draw = CommandBuffer::create(allocator, 1);
if (useIndex)
DrawTriangles::create(mesh->draw, offset, count);
else
DrawArrays::create(mesh->draw, GL_TRIANGLES, offset, count);
}
static void destroy(HeapAllocator& allocator, Mesh* mesh) {
CommandBuffer::destroy(allocator, mesh->draw);
}
};
struct Model {
CommandBuffer* state;
bool hasIndices;
int meshCount;
Mesh meshes[];
static Model* create(HeapAllocator& allocator, VertexArray vertexArray, int meshCount, bool hasIndices = true) {
Model* model = (Model*) allocator.allocate(sizeof(Model) + meshCount * sizeof(Mesh));
model->state = CommandBuffer::create(allocator, 1);
BindVertexArray::create(model->state, vertexArray);
model->hasIndices = hasIndices;
model->meshCount = meshCount;
return model;
}
static void addMesh(HeapAllocator& allocator, Model* model, int index, int offset, int count) {
Mesh::create(allocator, &model->meshes[index], offset, count, model->hasIndices);
}
static void destroy(HeapAllocator& allocator, Model* model) {
allocator.deallocate(model->state);
for(int i = 0; i < model->meshCount; i++)
Mesh::destroy(allocator, &model->meshes[i]);
allocator.deallocate(model);
}
static void draw(Model* model, uint64_t key, RenderQueue& renderQueue, CommandBuffer* globalState) {
for (int i = 0; i < model->meshCount; i++) {
CommandBuffer* commandBuffers[] = {
globalState,
model->state,
model->meshes[i].draw
};
renderQueue.submit(key, commandBuffers, 3);
}
}
};
#endif //MODEL_H