From f98e6e2e572be73ee3dda2872e2c8d9c8cbf0831 Mon Sep 17 00:00:00 2001 From: Milan Nikolic Date: Sun, 16 Jan 2022 11:13:25 +0100 Subject: [PATCH] Add animation functions, issue #156 --- raylib/raylib.go | 18 ++++++++++++++++-- raylib/rmodels.go | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/raylib/raylib.go b/raylib/raylib.go index 62f8692..e3da088 100644 --- a/raylib/raylib.go +++ b/raylib/raylib.go @@ -842,6 +842,7 @@ type MaterialMap struct { Value float32 } +// Model, meshes, materials and animation data type Model struct { // Local transform matrix Transform Matrix @@ -860,13 +861,13 @@ func newModelFromPointer(ptr unsafe.Pointer) Model { return *(*Model)(ptr) } -// BoneInfo type. +// BoneInfo type type BoneInfo struct { Name [32]int8 Parent int32 } -// Transform type. +// Transform type type Transform struct { Translation Vector3 Rotation Vector4 @@ -891,6 +892,19 @@ func newRayFromPointer(ptr unsafe.Pointer) Ray { 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 type RayCollision struct { Hit bool diff --git a/raylib/rmodels.go b/raylib/rmodels.go index 7b667b6..1e7165d 100644 --- a/raylib/rmodels.go +++ b/raylib/rmodels.go @@ -31,6 +31,11 @@ func (r *Ray) cptr() *C.Ray { 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 func DrawLine3D(startPos Vector3, endPos Vector3, col color.RGBA) { cstartPos := startPos.cptr() @@ -342,12 +347,12 @@ func GenMeshCubicmap(cubicmap Image, size Vector3) Mesh { } // LoadMaterials - Load material data (.MTL) -func LoadMaterials(fileName string) Material { +func LoadMaterials(fileName string) []Material { cfileName := C.CString(fileName) defer C.free(unsafe.Pointer(cfileName)) ccount := C.int(0) ret := C.LoadMaterials(cfileName, &ccount) - v := newMaterialFromPointer(unsafe.Pointer(&ret)) + v := (*[1 << 24]Material)(unsafe.Pointer(ret))[:int(ccount)] return v } @@ -380,6 +385,44 @@ func SetModelMeshMaterial(model *Model, meshId int32, materialId int32) { 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) func DrawModel(model Model, position Vector3, scale float32, tint color.RGBA) { cmodel := model.cptr()