-
Notifications
You must be signed in to change notification settings - Fork 12
/
Mesh.go
381 lines (326 loc) · 9.48 KB
/
Mesh.go
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package assimp
//#cgo linux LDFLAGS: -L/usr/local/lib -lassimp -lstdc++
//#include <assimp/mesh.h>
//#include <string.h>
import "C"
import "unsafe"
import "reflect"
const (
MaxFaceIndices = C.AI_MAX_FACE_INDICES
MaxBoneWeights = C.AI_MAX_BONE_WEIGHTS
MaxVertices = C.AI_MAX_VERTICES
MaxFaces = C.AI_MAX_FACES
MaxNumberOfColorSets = C.AI_MAX_NUMBER_OF_COLOR_SETS
MaxNumberOfTextureCoords = C.AI_MAX_NUMBER_OF_TEXTURECOORDS
)
type Face C.struct_aiFace
func (f *Face) NumIndices() uint32 {
return uint32(f.mNumIndices)
}
func (f *Face) IndicesPtr() *uint32 {
return (*uint32)(f.mIndices)
}
func (f *Face) CopyIndices() []uint32 {
indices := make([]uint32, f.mNumIndices)
if unsafe.Sizeof(C.uint(0)) != unsafe.Sizeof(uint32(0)) {
panic("wrong size")
}
size := uint32(unsafe.Sizeof(C.uint(0))) * f.NumIndices()
C.memcpy(unsafe.Pointer(&indices[0]), unsafe.Pointer(f.mIndices), C.size_t(size))
return indices
}
type VertexWeight C.struct_aiVertexWeight
func (w VertexWeight) VertexId() uint {
return uint(w.mVertexId)
}
func (w VertexWeight) Weight() float32 {
return float32(w.mWeight)
}
type Bone C.struct_aiBone
func (b *Bone) Name() string {
return C.GoStringN(&b.mName.data[0], C.int(b.mName.length))
}
func (b *Bone) NumWeights() uint {
return uint(b.mNumWeights)
}
func (b *Bone) WeightsPtr() *VertexWeight {
return (*VertexWeight)(b.mWeights)
}
func (b *Bone) Weights() []VertexWeight {
var result []VertexWeight
header := (*reflect.SliceHeader)(unsafe.Pointer(&result))
header.Cap = int(b.NumWeights())
header.Len = int(b.NumWeights())
header.Data = uintptr(unsafe.Pointer(b.mWeights))
return result
}
func (b *Bone) OffsetMatrix() Matrix4x4 {
return Matrix4x4(b.mOffsetMatrix)
}
type PrimitiveType C.enum_aiPrimitiveType
const (
PrimitiveType_Point PrimitiveType = C.aiPrimitiveType_POINT
PrimitiveType_Line PrimitiveType = C.aiPrimitiveType_LINE
PrimitiveType_Triangle PrimitiveType = C.aiPrimitiveType_TRIANGLE
PrimitiveType_Polygon PrimitiveType = C.aiPrimitiveType_POLYGON
)
type AnimMesh C.struct_aiAnimMesh
func (mesh *AnimMesh) VerticesPtr() *Vector3 {
return (*Vector3)(mesh.mVertices)
}
func (mesh *AnimMesh) Vertices() []Vector3 {
if mesh.HasPositions() {
var result []Vector3
header := (*reflect.SliceHeader)(unsafe.Pointer(&result))
header.Cap = int(mesh.mNumVertices)
header.Len = int(mesh.mNumVertices)
header.Data = uintptr(unsafe.Pointer(mesh.mVertices))
return result
} else {
return nil
}
}
func (mesh *AnimMesh) NormalsPtr() *Vector3 {
return (*Vector3)(mesh.mNormals)
}
func (mesh *AnimMesh) Normals() []Vector3 {
if mesh.HasNormals() {
var result []Vector3
header := (*reflect.SliceHeader)(unsafe.Pointer(&result))
header.Cap = int(mesh.mNumVertices)
header.Len = int(mesh.mNumVertices)
header.Data = uintptr(unsafe.Pointer(mesh.mNormals))
return result
} else {
return nil
}
}
func (mesh *AnimMesh) TangentsPtr() *Vector3 {
return (*Vector3)(mesh.mTangents)
}
func (mesh *AnimMesh) Tangents() []Vector3 {
if mesh.HasTangentsAndBitangents() {
var result []Vector3
header := (*reflect.SliceHeader)(unsafe.Pointer(&result))
header.Cap = int(mesh.mNumVertices)
header.Len = int(mesh.mNumVertices)
header.Data = uintptr(unsafe.Pointer(mesh.mTangents))
return result
} else {
return nil
}
}
func (mesh *AnimMesh) BitangentsPtr() *Vector3 {
return (*Vector3)(mesh.mBitangents)
}
func (mesh *AnimMesh) Bitangents() []Vector3 {
if mesh.HasTangentsAndBitangents() {
var result []Vector3
header := (*reflect.SliceHeader)(unsafe.Pointer(&result))
header.Cap = int(mesh.mNumVertices)
header.Len = int(mesh.mNumVertices)
header.Data = uintptr(unsafe.Pointer(mesh.mBitangents))
return result
} else {
return nil
}
}
func (mesh *AnimMesh) ColorsPtr(pIndex int) *Color4 {
return (*Color4)(mesh.mColors[pIndex])
}
func (mesh *AnimMesh) Colors(pIndex int) []Color4 {
if mesh.HasVertexColors(pIndex) {
var result []Color4
header := (*reflect.SliceHeader)(unsafe.Pointer(&result))
header.Cap = int(mesh.mNumVertices)
header.Len = int(mesh.mNumVertices)
header.Data = uintptr(unsafe.Pointer(mesh.mColors[pIndex]))
return result
} else {
return nil
}
}
func (mesh *AnimMesh) TextureCoordsPtr(pIndex int) *Vector3 {
return (*Vector3)(mesh.mTextureCoords[pIndex])
}
func (mesh *AnimMesh) TextureCoords(pIndex int) []Color4 {
if mesh.HasTextureCoords(pIndex) {
var result []Color4
header := (*reflect.SliceHeader)(unsafe.Pointer(&result))
header.Cap = int(mesh.mNumVertices)
header.Len = int(mesh.mNumVertices)
header.Data = uintptr(unsafe.Pointer(mesh.mTextureCoords[pIndex]))
return result
} else {
return nil
}
}
/** Check whether the anim mesh overrides the vertex positions
* of its host mesh*/
func (mesh *AnimMesh) HasPositions() bool {
return mesh.mVertices != nil
}
/** Check whether the anim mesh overrides the vertex normals
* of its host mesh*/
func (mesh *AnimMesh) HasNormals() bool {
return mesh.mNormals != nil
}
/** Check whether the anim mesh overrides the vertex tangents
* and bitangents of its host mesh. As for aiMesh,
* tangents and bitangents always go together. */
func (mesh *AnimMesh) HasTangentsAndBitangents() bool {
return mesh.mTangents != nil
}
/** Check whether the anim mesh overrides a particular
* set of vertex colors on his host mesh.
* @param pIndex 0<index<AI_MAX_NUMBER_OF_COLOR_SETS */
func (mesh *AnimMesh) HasVertexColors(pIndex int) bool {
if pIndex >= MaxNumberOfColorSets {
return false
} else {
return mesh.mColors[pIndex] != nil
}
}
/** Check whether the anim mesh overrides a particular
* set of texture coordinates on his host mesh.
* @param pIndex 0<index<AI_MAX_NUMBER_OF_TEXTURECOORDS */
func (mesh *AnimMesh) HasTextureCoords(pIndex int) bool {
if pIndex >= MaxNumberOfTextureCoords {
return false
} else {
return mesh.mTextureCoords[pIndex] != nil
}
}
type Mesh C.struct_aiMesh
func (mesh *Mesh) PrimitiveTypes() int {
return int(mesh.mPrimitiveTypes)
}
func (mesh *Mesh) NumVertices() int {
return int(mesh.mNumVertices)
}
func (mesh *Mesh) NumFaces() int {
return int(mesh.mNumFaces)
}
func (mesh *Mesh) Vertices() []Vector3 {
if mesh.mVertices != nil {
var result []Vector3
header := (*reflect.SliceHeader)(unsafe.Pointer(&result))
header.Cap = int(mesh.mNumVertices)
header.Len = int(mesh.mNumVertices)
header.Data = uintptr(unsafe.Pointer(mesh.mVertices))
return result
} else {
return nil
}
}
func (mesh *Mesh) Normals() []Vector3 {
if mesh.mNormals != nil {
var result []Vector3
header := (*reflect.SliceHeader)(unsafe.Pointer(&result))
header.Cap = int(mesh.mNumVertices)
header.Len = int(mesh.mNumVertices)
header.Data = uintptr(unsafe.Pointer(mesh.mNormals))
return result
} else {
return nil
}
}
func (mesh *Mesh) Tangents() []Vector3 {
if mesh.mTangents != nil {
var result []Vector3
header := (*reflect.SliceHeader)(unsafe.Pointer(&result))
header.Cap = int(mesh.mNumVertices)
header.Len = int(mesh.mNumVertices)
header.Data = uintptr(unsafe.Pointer(mesh.mTangents))
return result
} else {
return nil
}
}
func (mesh *Mesh) Bitangents() []Vector3 {
if mesh.mBitangents != nil {
var result []Vector3
header := (*reflect.SliceHeader)(unsafe.Pointer(&result))
header.Cap = int(mesh.mNumVertices)
header.Len = int(mesh.mNumVertices)
header.Data = uintptr(unsafe.Pointer(mesh.mBitangents))
return result
} else {
return nil
}
}
func (mesh *Mesh) TextureCoords(pIndex int) []Vector3 {
if pIndex < MaxNumberOfTextureCoords && mesh.mTextureCoords[pIndex] != nil {
var result []Vector3
header := (*reflect.SliceHeader)(unsafe.Pointer(&result))
header.Cap = int(mesh.mNumVertices)
header.Len = int(mesh.mNumVertices)
header.Data = uintptr(unsafe.Pointer(mesh.mTextureCoords[pIndex]))
return result
} else {
return nil
}
}
func (mesh *Mesh) Colors(pIndex int) []Color4 {
if pIndex < MaxNumberOfColorSets && mesh.mColors[pIndex] != nil {
var result []Color4
header := (*reflect.SliceHeader)(unsafe.Pointer(&result))
header.Cap = int(mesh.mNumVertices)
header.Len = int(mesh.mNumVertices)
header.Data = uintptr(unsafe.Pointer(mesh.mColors[pIndex]))
return result
} else {
return nil
}
}
func (mesh *Mesh) NumUvComponents(pIndex int) int {
return int(mesh.mNumUVComponents[pIndex])
}
func (mesh *Mesh) Faces() []Face {
if mesh.mFaces != nil {
var result []Face
header := (*reflect.SliceHeader)(unsafe.Pointer(&result))
header.Cap = int(mesh.mNumFaces)
header.Len = int(mesh.mNumFaces)
header.Data = uintptr(unsafe.Pointer(mesh.mFaces))
return result
} else {
return nil
}
}
func (mesh *Mesh) NumBones() int {
return int(mesh.mNumBones)
}
func (mesh *Mesh) Bones() []*Bone {
if mesh.mBones != nil {
var result []*Bone
header := (*reflect.SliceHeader)(unsafe.Pointer(&result))
header.Cap = int(mesh.mNumBones)
header.Len = int(mesh.mNumBones)
header.Data = uintptr(unsafe.Pointer(mesh.mBones))
return result
} else {
return nil
}
}
func (mesh *Mesh) MaterialIndex() int {
return int(mesh.mMaterialIndex)
}
func (mesh *Mesh) Name() string {
return C.GoStringN(&mesh.mName.data[0], C.int(mesh.mName.length))
}
func (mesh *Mesh) NumAnimMeshes() int {
return int(mesh.mNumAnimMeshes)
}
func (mesh *Mesh) AnimMeshes() []*AnimMesh {
if mesh.mAnimMeshes != nil {
var result []*AnimMesh
header := (*reflect.SliceHeader)(unsafe.Pointer(&result))
header.Cap = int(mesh.mNumAnimMeshes)
header.Len = int(mesh.mNumAnimMeshes)
header.Data = uintptr(unsafe.Pointer(mesh.mAnimMeshes))
return result
} else {
return nil
}
}