-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbgfxh_mesh.h
152 lines (146 loc) · 4.87 KB
/
bgfxh_mesh.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
142
143
144
145
146
147
148
149
150
151
152
// bgfxh_mesh.hh
//
#ifndef LZZ_bgfxh_mesh_hh
#define LZZ_bgfxh_mesh_hh
#define LZZ_INLINE inline
namespace bgfxh
{
struct meshFace
{
uint16_t a;
uint16_t b;
uint16_t c;
meshFace ();
meshFace (uint16_t const _a, uint16_t const _b, uint16_t const _c);
};
}
namespace bgfxh
{
template <typename VERT_TYPE, typename DECL_TYPE, typename INDEX_TYPE>
struct mesh
{
BGFXH_VECTOR <VERT_TYPE> verts;
BGFXH_VECTOR <INDEX_TYPE> indices;
BGFXH_VECTOR <meshFace> faces;
bgfx::VertexBufferHandle m_vbh;
bgfx::IndexBufferHandle m_ibh;
mesh ();
~ mesh ();
static void internal_freeVerts_cb (void * _ptr, void * _userData);
static void internal_freeIndicies_cb (void * _ptr, void * _userData);
void upload (bool freeOnUploadComplete = true);
void bind ();
void destroy ();
};
}
namespace bgfxh
{
typedef mesh <bx::Vec3, PosVertex, uint16_t> simpleMesh;
}
namespace bgfxh
{
LZZ_INLINE meshFace::meshFace ()
{}
}
namespace bgfxh
{
LZZ_INLINE meshFace::meshFace (uint16_t const _a, uint16_t const _b, uint16_t const _c)
: a (_a), b (_b), c (_c)
{}
}
namespace bgfxh
{
template <typename VERT_TYPE, typename DECL_TYPE, typename INDEX_TYPE>
LZZ_INLINE mesh <VERT_TYPE, DECL_TYPE, INDEX_TYPE>::mesh ()
: m_vbh (BGFX_INVALID_HANDLE), m_ibh (BGFX_INVALID_HANDLE)
{}
}
namespace bgfxh
{
template <typename VERT_TYPE, typename DECL_TYPE, typename INDEX_TYPE>
mesh <VERT_TYPE, DECL_TYPE, INDEX_TYPE>::~ mesh ()
{ destroy(); }
}
namespace bgfxh
{
template <typename VERT_TYPE, typename DECL_TYPE, typename INDEX_TYPE>
void mesh <VERT_TYPE, DECL_TYPE, INDEX_TYPE>::internal_freeVerts_cb (void * _ptr, void * _userData)
{
BX_UNUSED(_ptr);
BGFXH_VECTOR<VERT_TYPE>* vertsHeap = (BGFXH_VECTOR<VERT_TYPE>*)_userData;
delete vertsHeap;
}
}
namespace bgfxh
{
template <typename VERT_TYPE, typename DECL_TYPE, typename INDEX_TYPE>
void mesh <VERT_TYPE, DECL_TYPE, INDEX_TYPE>::internal_freeIndicies_cb (void * _ptr, void * _userData)
{
BX_UNUSED(_ptr);
BGFXH_VECTOR<INDEX_TYPE>* indicesHeap = (BGFXH_VECTOR<INDEX_TYPE>*)_userData;
delete indicesHeap;
}
}
namespace bgfxh
{
template <typename VERT_TYPE, typename DECL_TYPE, typename INDEX_TYPE>
void mesh <VERT_TYPE, DECL_TYPE, INDEX_TYPE>::upload (bool freeOnUploadComplete)
{
// Static data can be passed with bgfx::makeRef
if (freeOnUploadComplete) {
faces.clear();
BGFXH_VECTOR<VERT_TYPE>* vertsHeap = new BGFXH_VECTOR<VERT_TYPE>;
BGFXH_VECTOR<INDEX_TYPE>* indicesHeap = new BGFXH_VECTOR<INDEX_TYPE>;
vertsHeap->swap(verts);
indicesHeap->swap(indices);
m_vbh = bgfx::createVertexBuffer(
bgfx::makeRef(vertsHeap->data(), sizeof(VERT_TYPE)*vertsHeap->size(), internal_freeVerts_cb, vertsHeap )
, DECL_TYPE::ms_decl
);
if (sizeof(INDEX_TYPE) == sizeof(uint32_t))
m_ibh = bgfx::createIndexBuffer( bgfx::makeRef(indicesHeap->data(), sizeof(INDEX_TYPE)*indicesHeap->size(), internal_freeIndicies_cb, indicesHeap ), BGFX_BUFFER_INDEX32);
else
m_ibh = bgfx::createIndexBuffer( bgfx::makeRef(indicesHeap->data(), sizeof(INDEX_TYPE)*indicesHeap->size(), internal_freeIndicies_cb, indicesHeap ));
}
else {
m_vbh = bgfx::createVertexBuffer(
bgfx::makeRef(verts.data(), sizeof(VERT_TYPE)*verts.size() )
, DECL_TYPE::ms_decl
);
if (sizeof(INDEX_TYPE) == sizeof(uint32_t))
m_ibh = bgfx::createIndexBuffer( bgfx::makeRef(indices.data(), sizeof(INDEX_TYPE)*indices.size() ), BGFX_BUFFER_INDEX32 );
else
m_ibh = bgfx::createIndexBuffer( bgfx::makeRef(indices.data(), sizeof(INDEX_TYPE)*indices.size() ));
}
}
}
namespace bgfxh
{
template <typename VERT_TYPE, typename DECL_TYPE, typename INDEX_TYPE>
void mesh <VERT_TYPE, DECL_TYPE, INDEX_TYPE>::bind ()
{
bgfx::setVertexBuffer(0, m_vbh);
bgfx::setIndexBuffer(m_ibh);
}
}
namespace bgfxh
{
template <typename VERT_TYPE, typename DECL_TYPE, typename INDEX_TYPE>
void mesh <VERT_TYPE, DECL_TYPE, INDEX_TYPE>::destroy ()
{
bgfxh::destroyHandle (m_vbh);
bgfxh::destroyHandle (m_ibh);
}
}
#undef LZZ_INLINE
#endif
////////////////////////////////////////////////////////////////////////
#ifdef BGFXH_IMPL
#ifndef BGFXH_DOUBLE_GUARD_bgfxh_mesh
#define BGFXH_DOUBLE_GUARD_bgfxh_mesh
// bgfxh_mesh.cpp
//
#define LZZ_INLINE inline
#undef LZZ_INLINE
#endif //BGFXH_DOUBLE_GUARD_bgfxh_mesh
#endif //BGFXH_IMPL