Add animation functions, issue #156

This commit is contained in:
Milan Nikolic 2022-01-16 11:13:25 +01:00
parent 91296a7d50
commit f98e6e2e57
No known key found for this signature in database
GPG key ID: 9229D0EAA3AA4E75
2 changed files with 61 additions and 4 deletions

View file

@ -842,6 +842,7 @@ type MaterialMap struct {
Value float32 Value float32
} }
// Model, meshes, materials and animation data
type Model struct { type Model struct {
// Local transform matrix // Local transform matrix
Transform Matrix Transform Matrix
@ -860,13 +861,13 @@ func newModelFromPointer(ptr unsafe.Pointer) Model {
return *(*Model)(ptr) return *(*Model)(ptr)
} }
// BoneInfo type. // BoneInfo type
type BoneInfo struct { type BoneInfo struct {
Name [32]int8 Name [32]int8
Parent int32 Parent int32
} }
// Transform type. // Transform type
type Transform struct { type Transform struct {
Translation Vector3 Translation Vector3
Rotation Vector4 Rotation Vector4
@ -891,6 +892,19 @@ func newRayFromPointer(ptr unsafe.Pointer) Ray {
return *(*Ray)(ptr) return *(*Ray)(ptr)
} }
// ModelAnimation type
type ModelAnimation struct {
BoneCount int32
FrameCount int32
Bones *BoneInfo
FramePoses **Transform
}
// newModelAnimationFromPointer - Returns new ModelAnimation from pointer
func newModelAnimationFromPointer(ptr unsafe.Pointer) ModelAnimation {
return *(*ModelAnimation)(ptr)
}
// RayCollision type - ray hit information // RayCollision type - ray hit information
type RayCollision struct { type RayCollision struct {
Hit bool Hit bool

View file

@ -31,6 +31,11 @@ func (r *Ray) cptr() *C.Ray {
return (*C.Ray)(unsafe.Pointer(r)) return (*C.Ray)(unsafe.Pointer(r))
} }
// cptr returns C pointer
func (r *ModelAnimation) cptr() *C.ModelAnimation {
return (*C.ModelAnimation)(unsafe.Pointer(r))
}
// DrawLine3D - Draw a line in 3D world space // DrawLine3D - Draw a line in 3D world space
func DrawLine3D(startPos Vector3, endPos Vector3, col color.RGBA) { func DrawLine3D(startPos Vector3, endPos Vector3, col color.RGBA) {
cstartPos := startPos.cptr() cstartPos := startPos.cptr()
@ -342,12 +347,12 @@ func GenMeshCubicmap(cubicmap Image, size Vector3) Mesh {
} }
// LoadMaterials - Load material data (.MTL) // LoadMaterials - Load material data (.MTL)
func LoadMaterials(fileName string) Material { func LoadMaterials(fileName string) []Material {
cfileName := C.CString(fileName) cfileName := C.CString(fileName)
defer C.free(unsafe.Pointer(cfileName)) defer C.free(unsafe.Pointer(cfileName))
ccount := C.int(0) ccount := C.int(0)
ret := C.LoadMaterials(cfileName, &ccount) ret := C.LoadMaterials(cfileName, &ccount)
v := newMaterialFromPointer(unsafe.Pointer(&ret)) v := (*[1 << 24]Material)(unsafe.Pointer(ret))[:int(ccount)]
return v return v
} }
@ -380,6 +385,44 @@ func SetModelMeshMaterial(model *Model, meshId int32, materialId int32) {
C.SetModelMeshMaterial(cmodel, cmeshId, cmaterialId) C.SetModelMeshMaterial(cmodel, cmeshId, cmaterialId)
} }
// LoadModelAnimations - Load model animations from file
func LoadModelAnimations(fileName string) []ModelAnimation {
cfileName := C.CString(fileName)
defer C.free(unsafe.Pointer(cfileName))
ccount := C.uint(0)
ret := C.LoadModelAnimations(cfileName, &ccount)
v := (*[1 << 24]ModelAnimation)(unsafe.Pointer(ret))[:int(ccount)]
return v
}
// UpdateModelAnimation - Update model animation pose
func UpdateModelAnimation(model Model, anim ModelAnimation, frame int32) {
cmodel := model.cptr()
canim := anim.cptr()
cframe := (C.int)(frame)
C.UpdateModelAnimation(*cmodel, *canim, cframe)
}
// UnloadModelAnimation - Unload animation data
func UnloadModelAnimation(anim ModelAnimation) {
canim := anim.cptr()
C.UnloadModelAnimation(*canim)
}
// UnloadModelAnimations - Unload animation array data
func UnloadModelAnimations(animations []ModelAnimation) {
C.UnloadModelAnimations((*C.ModelAnimation)(unsafe.Pointer(&animations[0])), (C.uint)(len(animations)))
}
// IsModelAnimationValid - Check model animation skeleton match
func IsModelAnimationValid(model Model, anim ModelAnimation) bool {
cmodel := model.cptr()
canim := anim.cptr()
ret := C.IsModelAnimationValid(*cmodel, *canim)
v := bool(ret)
return v
}
// DrawModel - Draw a model (with texture if set) // DrawModel - Draw a model (with texture if set)
func DrawModel(model Model, position Vector3, scale float32, tint color.RGBA) { func DrawModel(model Model, position Vector3, scale float32, tint color.RGBA) {
cmodel := model.cptr() cmodel := model.cptr()