-
Notifications
You must be signed in to change notification settings - Fork 1
/
MeshBuilder.h
141 lines (124 loc) · 2.96 KB
/
MeshBuilder.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
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
#pragma once
#include "4dm.h"
namespace fdm
{
class Mesh;
class MeshBuilder : public Mesh
{
public:
struct AttrInfo
{
unsigned int type;
int size;
int stride;
};
struct BuffInfo
{
const void* bufferData;
int bufferSize;
PAD(4);
std::vector<AttrInfo> attributes;
};
int vertexCount;
PAD(4);
std::vector<BuffInfo> buffers;
const void* indexBufferData;
int indexBufferSize;
void addBuff(const void* buffData, GLsizei buffSize)
{
buffers.push_back({ buffData, buffSize, {} });
}
void addAttr(GLenum type, GLint attrib_size, GLint stride)
{
buffers.back().attributes.push_back({ type, attrib_size, stride });
}
void addAttr(GLenum type, GLint attrib_size, GLint stride, int buffIndex)
{
buffers[buffIndex].attributes.push_back({ type, attrib_size, stride });
}
void setIndexBuff(const void* indexBuffData, int indexBuffSize)
{
this->indexBufferData = indexBuffData;
this->indexBufferSize = indexBuffSize;
}
MeshBuilder(GLsizei vertexCount = 0)
: vertexCount(vertexCount), indexBufferData(nullptr), indexBufferSize(0)
{
}
MeshBuilder(GLsizei vertexCount, std::initializer_list<BuffInfo> buffers, const void* indexBuffData, GLsizei indexBuffSize)
: vertexCount(vertexCount), buffers(buffers), indexBufferData(indexBuffData), indexBufferSize(indexBuffSize)
{
}
~MeshBuilder()
{
// do not free anything; that is not our responsibility - Mashpoe
}
void move(MeshBuilder& other)
{
buffers = std::move(other.buffers);
vertexCount = other.vertexCount;
other.vertexCount = 0;
indexBufferData = other.indexBufferData;
other.indexBufferData = nullptr;
indexBufferSize = other.indexBufferSize;
other.indexBufferSize = 0;
}
MeshBuilder(MeshBuilder&& other) noexcept
{
move(other);
}
MeshBuilder& operator=(MeshBuilder&& other) noexcept
{
clear();
move(other);
return *this;
}
int buffCount() const override
{
return buffers.size();
}
const void* buffData(int buffIndex) const override
{
return buffers[buffIndex].bufferData;
}
int buffSize(int buffIndex) const override
{
return buffers[buffIndex].bufferSize;
}
int attrCount(int buffIndex) const override
{
return buffers[buffIndex].attributes.size();
}
int vertCount() const override
{
return vertexCount;
}
const void* indexBuffData() const override
{
return indexBufferData;
}
int indexBuffSize() const override
{
return indexBufferSize;
}
unsigned int attrType(int buffIndex, int attrIndex) const override
{
return buffers[buffIndex].attributes[attrIndex].type;
}
int attrSize(int buffIndex, int attrIndex) const override
{
return buffers[buffIndex].attributes[attrIndex].size;
}
int attrStride(int buffIndex, int attrIndex) const override
{
return buffers[buffIndex].attributes[attrIndex].stride;
}
void clear()
{
vertexCount = 0;
buffers.clear();
indexBufferData = nullptr;
indexBufferSize = 0;
}
};
}