forked from raedatoui/assimp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Import.go
276 lines (233 loc) · 9.05 KB
/
Import.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
package assimp
//#cgo pkg-config: assimp
//#include <stdlib.h>
//#include "Import.h"
//#include "assimp/postprocess.h"
import "C"
import (
"reflect"
"unsafe"
)
//export goLogStreamCallback
func goLogStreamCallback(message *C.char, user *C.char) {
ls := *(*LogStream)(unsafe.Pointer(user))
str := C.GoString(message)
ls.Log(str)
}
type LogStream interface {
Log(message string)
}
type PropertyStore C.struct_aiPropertyStore
func ImportFile(file string, flags uint) *Scene {
pFile := C.CString(file)
defer C.free(unsafe.Pointer(pFile))
return (*Scene)(C.aiImportFile(pFile, C.uint(flags)))
}
//func ImportFileEx(file string, flags uint)
//garbage collection prevention
type logStream C.struct_aiLogStream
var logStreams []*logStream
func (log *logStream) Log(message string) {
str := C.CString(message)
defer C.free(unsafe.Pointer(str))
C.CallLogStream((*C.struct_aiLogStream)(log), str)
}
func ImportFileFromMemory(data []byte, flags uint, hint string) *Scene {
pBuffer := (*C.char)(unsafe.Pointer(((*reflect.SliceHeader)(unsafe.Pointer(&data))).Data))
pLength := C.uint(len(data))
pFlags := C.uint(flags)
pHint := C.CString(hint)
defer C.free(unsafe.Pointer(pHint))
return (*Scene)(C.aiImportFileFromMemory(pBuffer, pLength, pFlags, pHint))
}
func ImportFileFromMemoryWithProperties(data []byte, flags uint, hint string, props *PropertyStore) *Scene {
pBuffer := (*C.char)(unsafe.Pointer(((*reflect.SliceHeader)(unsafe.Pointer(&data))).Data))
pLength := C.uint(len(data))
pFlags := C.uint(flags)
pHint := C.CString(hint)
defer C.free(unsafe.Pointer(pHint))
pProps := (*C.struct_aiPropertyStore)(props)
return (*Scene)(C.aiImportFileFromMemoryWithProperties(pBuffer, pLength, pFlags, pHint, pProps))
}
type PostProcessSteps C.enum_aiPostProcessSteps
const (
Process_CalcTangentSpace PostProcessSteps = C.aiProcess_CalcTangentSpace
Process_JoinIdenticalVertices PostProcessSteps = C.aiProcess_JoinIdenticalVertices
Process_MakeLeftHanded PostProcessSteps = C.aiProcess_MakeLeftHanded
Process_Triangulate PostProcessSteps = C.aiProcess_Triangulate
Process_RemoveComponent PostProcessSteps = C.aiProcess_RemoveComponent
Process_GenNormals PostProcessSteps = C.aiProcess_GenNormals
Process_GenSmoothNormals PostProcessSteps = C.aiProcess_GenSmoothNormals
Process_SplitLargeMeshes PostProcessSteps = C.aiProcess_SplitLargeMeshes
Process_PreTransformVertices PostProcessSteps = C.aiProcess_PreTransformVertices
Process_LimitBoneWeights PostProcessSteps = C.aiProcess_LimitBoneWeights
Process_ValidateDataStructure PostProcessSteps = C.aiProcess_ValidateDataStructure
Process_ImproveCacheLocality PostProcessSteps = C.aiProcess_ImproveCacheLocality
Process_RemoveRedundantMaterials PostProcessSteps = C.aiProcess_RemoveRedundantMaterials
Process_FixInfacingNormals PostProcessSteps = C.aiProcess_FixInfacingNormals
Process_SortByPType PostProcessSteps = C.aiProcess_SortByPType
Process_FindDegenerates PostProcessSteps = C.aiProcess_FindDegenerates
Process_FindInvalidData PostProcessSteps = C.aiProcess_FindInvalidData
Process_GenUVCoords PostProcessSteps = C.aiProcess_GenUVCoords
Process_TransformUVCoords PostProcessSteps = C.aiProcess_TransformUVCoords
Process_FindInstances PostProcessSteps = C.aiProcess_FindInstances
Process_OptimizeMeshes PostProcessSteps = C.aiProcess_OptimizeMeshes
Process_OptimizeGraph PostProcessSteps = C.aiProcess_OptimizeGraph
Process_FlipUVs PostProcessSteps = C.aiProcess_FlipUVs
Process_FlipWindingOrder PostProcessSteps = C.aiProcess_FlipWindingOrder
Process_SplitByBoneCount PostProcessSteps = C.aiProcess_SplitByBoneCount
Process_Debone PostProcessSteps = C.aiProcess_Debone
Process_ConvertToLeftHanded PostProcessSteps = C.aiProcess_ConvertToLeftHanded
ProcessPreset_TargetRealtime_Fast PostProcessSteps = C.aiProcessPreset_TargetRealtime_Fast
ProcessPreset_TargetRealtime_Quality PostProcessSteps = C.aiProcessPreset_TargetRealtime_Quality
ProcessPreset_TargetRealtime_MaxQuality PostProcessSteps = C.aiProcessPreset_TargetRealtime_MaxQuality
)
func (scene *Scene) ApplyPostProcessing(flags PostProcessSteps) *Scene {
pScene := (*C.struct_aiScene)(scene)
pFlags := C.uint(flags)
return (*Scene)(C.aiApplyPostProcessing(pScene, pFlags))
}
func GetPredefinedLogStream(streams DefaultLogStream, file string) LogStream {
pStreams := C.enum_aiDefaultLogStream(streams)
pFile := C.CString(file)
defer C.free(unsafe.Pointer(pFile))
ls := logStream(C.aiGetPredefinedLogStream(pStreams, pFile))
return &ls
}
func AttachLogStream(stream LogStream) {
if ls, ok := stream.(*logStream); ok {
// if the stream is a wrapped C-compatible aiLogStream unwrap it
C.aiAttachLogStream((*C.struct_aiLogStream)(ls))
} else {
// this i a log Stream implemented in go and needs to be wrapped to be C-compatible
ls := new(logStream)
ls.user = (*C.char)(unsafe.Pointer(&stream))
C.setLogStreamCallback((*C.struct_aiLogStream)(ls))
logStreams = append(logStreams, ls)
C.aiAttachLogStream((*C.struct_aiLogStream)(ls))
}
}
func EnableVerboseLogging(d bool) {
if d {
C.aiEnableVerboseLogging(C.AI_TRUE)
} else {
C.aiEnableVerboseLogging(C.AI_FALSE)
}
}
func DetachLogStream(stream LogStream) Return {
if ls, ok := stream.(*logStream); ok {
// if the stream is a wrapped C-compatible aiLogStream unwrap it
return Return(C.aiDetachLogStream((*C.struct_aiLogStream)(ls)))
} else {
for i, ls := range logStreams {
ls2 := *(*LogStream)(unsafe.Pointer(ls.user))
if ls2 == stream {
logStreams = append(logStreams[:i], logStreams[i+1:]...)
return Return(C.aiDetachLogStream((*C.struct_aiLogStream)(ls)))
}
}
return Return_Failure
}
}
func DetachAllLogStreams() {
C.aiDetachAllLogStreams()
logStreams = make([]*logStream, 0)
}
func (scene *Scene) ReleaseImport() {
C.aiReleaseImport((*C.struct_aiScene)(scene))
}
func GetErrorString() string {
return C.GoString(C.aiGetErrorString())
}
func IsExtensionSupported(extension string) bool {
szExtension := C.CString(extension)
defer C.free(unsafe.Pointer(szExtension))
switch C.aiIsExtensionSupported(szExtension) {
case C.AI_FALSE:
return false
default:
return true
}
}
func GetExtensionList() string {
var str C.struct_aiString
C.aiGetExtensionList(&str)
return C.GoString(&str.data[0])
}
func (scene *Scene) GetMemoryRequirements() MemoryInfo {
var meminfo C.struct_aiMemoryInfo
C.aiGetMemoryRequirements((*C.struct_aiScene)(scene), &meminfo)
return MemoryInfo(meminfo)
}
func CreatePropertyStore() *PropertyStore {
return (*PropertyStore)(C.aiCreatePropertyStore())
}
func (p *PropertyStore) Release() {
C.aiReleasePropertyStore((*C.struct_aiPropertyStore)(p))
}
func (p *PropertyStore) SetPropertyInteger(name string, value int) {
szName := C.CString(name)
defer C.free(unsafe.Pointer(szName))
C.aiSetImportPropertyInteger((*C.struct_aiPropertyStore)(p), szName, C.int(value))
}
func (p *PropertyStore) SetPropertyFloat(name string, value float32) {
szName := C.CString(name)
defer C.free(unsafe.Pointer(szName))
C.aiSetImportPropertyFloat((*C.struct_aiPropertyStore)(p), szName, C.ai_real(value))
}
func (p *PropertyStore) SetPropertyString(name string, value string) {
szName := C.CString(name)
cValue := C.CString(value)
defer C.free(unsafe.Pointer(szName))
defer C.free(unsafe.Pointer(cValue))
var st C.struct_aiString
C.strncpy(&st.data[0], cValue, C.MAXLEN)
C.aiSetImportPropertyString((*C.struct_aiPropertyStore)(p), szName, &st)
}
func (quat *Quaternion) FromMatrix(mat *Matrix3x3) {
C.aiCreateQuaternionFromMatrix((*C.struct_aiQuaternion)(quat), (*C.struct_aiMatrix3x3)(mat))
}
func (mat *Matrix4x4) Decompose(scaling *Vector3, rotation *Quaternion, position *Vector3) {
C.aiDecomposeMatrix(
(*C.struct_aiMatrix4x4)(mat),
(*C.struct_aiVector3D)(scaling),
(*C.struct_aiQuaternion)(rotation),
(*C.struct_aiVector3D)(position),
)
}
func (mat *Matrix4x4) Transpose() {
C.aiTransposeMatrix4((*C.struct_aiMatrix4x4)(mat))
}
func (mat *Matrix3x3) Transpose() {
C.aiTransposeMatrix3((*C.struct_aiMatrix3x3)(mat))
}
func (vec *Vector3) TransformByMatrix3(mat *Matrix3x3) {
C.aiTransformVecByMatrix3(
(*C.struct_aiVector3D)(vec),
(*C.struct_aiMatrix3x3)(mat),
)
}
func (vec *Vector3) TransformByMatrix4(mat *Matrix4x4) {
C.aiTransformVecByMatrix4(
(*C.struct_aiVector3D)(vec),
(*C.struct_aiMatrix4x4)(mat),
)
}
func (dst *Matrix4x4) Multiply(src *Matrix4x4) {
C.aiMultiplyMatrix4(
(*C.struct_aiMatrix4x4)(dst),
(*C.struct_aiMatrix4x4)(src),
)
}
func (dst *Matrix3x3) Multiply(src *Matrix3x3) {
C.aiMultiplyMatrix3(
(*C.struct_aiMatrix3x3)(dst),
(*C.struct_aiMatrix3x3)(src),
)
}
func (dst *Matrix3x3) Identity() {
C.aiIdentityMatrix3((*C.struct_aiMatrix3x3)(dst))
}
func (dst *Matrix4x4) Identity() {
C.aiIdentityMatrix4((*C.struct_aiMatrix4x4)(dst))
}