diff --git a/examples/models/animation/main.go b/examples/models/animation/main.go index 851d38f1..b8d3c40d 100644 --- a/examples/models/animation/main.go +++ b/examples/models/animation/main.go @@ -21,8 +21,6 @@ package main import ( - "unsafe" - rl "github.com/gen2brain/raylib-go/raylib" ) @@ -76,9 +74,8 @@ func main() { rl.DrawModelEx(model, position, rl.NewVector3(1, 0, 0), -90, rl.NewVector3(1, 1, 1), rl.White) // Draw translation cubes for i := int32(0); i < model.BoneCount; i++ { - framePose := unsafe.Slice(anims[0].FramePoses, anims[0].FrameCount) - trans := unsafe.Slice(framePose[animFrameCount], model.BoneCount) - rl.DrawCube(trans[i].Translation, 0.2, 0.2, 0.2, rl.Red) + pose := anims[0].GetFramePose(animFrameCount, int(i)) + rl.DrawCube(pose.Translation, 0.2, 0.2, 0.2, rl.Red) } rl.DrawGrid(10, 1) diff --git a/examples/models/gltf_loading/main.go b/examples/models/gltf_loading/main.go index 26f31200..a8298787 100644 --- a/examples/models/gltf_loading/main.go +++ b/examples/models/gltf_loading/main.go @@ -62,7 +62,7 @@ func main() { rl.EndMode3D() - rl.DrawText("current animation number: "+fmt.Sprint(animIndex), 10, 10, 10, rl.Black) + rl.DrawText(fmt.Sprintf("current animation: %s [%d]", animPlaying.GetName(), animIndex), 10, 10, 10, rl.Black) rl.DrawText("UP/DOWN ARROW KEYS CHANGE ANIMATION", 10, 30, 10, rl.Black) rl.EndDrawing() diff --git a/examples/models/m3d_loading/main.go b/examples/models/m3d_loading/main.go index 2d8ef560..192775f5 100644 --- a/examples/models/m3d_loading/main.go +++ b/examples/models/m3d_loading/main.go @@ -19,9 +19,7 @@ package main import ( - "unsafe" - - "github.com/gen2brain/raylib-go/raylib" + rl "github.com/gen2brain/raylib-go/raylib" ) const ( @@ -116,9 +114,8 @@ func main() { if drawSkeleton { modelBones := model.GetBones() modelPoses := model.GetBindPose() - animBones := unsafe.Slice(anims[animID].Bones, anims[animID].BoneCount) - animPoses := unsafe.Slice(anims[animID].FramePoses, anims[animID].FrameCount) - transforms := unsafe.Slice(animPoses[animFrameCounter], anims[animID].BoneCount) + anim := anims[animID] + animBones := anim.GetBones() for bone := 0; bone < int(model.BoneCount)-1; bone++ { if !animPlaying || animsCount == 0 { // Display the bind-pose skeleton @@ -128,9 +125,11 @@ func main() { } } else { // // Display the frame-pose skeleton - rl.DrawCube(transforms[bone].Translation, 0.05, 0.05, 0.05, rl.Red) + pos := anim.GetFramePose(animFrameCounter, bone).Translation + rl.DrawCube(pos, 0.05, 0.05, 0.05, rl.Red) if animBones[bone].Parent >= 0 { - rl.DrawLine3D(transforms[bone].Translation, transforms[animBones[bone].Parent].Translation, rl.Red) + endPos := anim.GetFramePose(animFrameCounter, int(animBones[bone].Parent)).Translation + rl.DrawLine3D(pos, endPos, rl.Red) } } } diff --git a/raylib/raylib.go b/raylib/raylib.go index 95c6911d..0cd31264 100644 --- a/raylib/raylib.go +++ b/raylib/raylib.go @@ -929,7 +929,29 @@ type ModelAnimation struct { FrameCount int32 Bones *BoneInfo FramePoses **Transform - Name [32]int8 + Name [32]uint8 +} + +// GetBones returns the bones information (skeleton) of a ModelAnimation as go slice +func (m ModelAnimation) GetBones() []BoneInfo { + return unsafe.Slice(m.Bones, m.BoneCount) +} + +// GetFramePose returns the Transform for a specific bone at a specific frame +func (m ModelAnimation) GetFramePose(frame, bone int) Transform { + framePoses := unsafe.Slice(m.FramePoses, m.FrameCount) + return unsafe.Slice(framePoses[frame], m.BoneCount)[bone] +} + +// GetName returns the ModelAnimation's name as go string +func (m ModelAnimation) GetName() string { + var end int + for end = range m.Name { + if m.Name[end] == 0 { + break + } + } + return string(m.Name[:end]) } // RayCollision type - ray hit information