|
1 | 1 | /******************************************************************************************* |
2 | 2 | * |
3 | | -* raylib [models] example - Load models M3D |
| 3 | +* raylib [models] example - loading m3d |
| 4 | +* |
| 5 | +* Example complexity rating: [★★☆☆] 2/4 |
4 | 6 | * |
5 | 7 | * Example originally created with raylib 4.5, last time updated with raylib 4.5 |
6 | 8 | * |
|
13 | 15 | * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, |
14 | 16 | * BSD-like license that allows static linking with closed source software |
15 | 17 | * |
16 | | -* Copyright (c) 2022-2024 bzt (@bztsrc) |
| 18 | +* Copyright (c) 2022-2025 bzt (@bztsrc) |
17 | 19 | * |
18 | 20 | ********************************************************************************************/ |
| 21 | + |
19 | 22 | package main |
20 | 23 |
|
21 | 24 | import ( |
22 | | - rl "github.com/gen2brain/raylib-go/raylib" |
23 | | -) |
| 25 | + "fmt" |
24 | 26 |
|
25 | | -const ( |
26 | | - screenWidth = 800 |
27 | | - screenHeight = 450 |
| 27 | + rl "github.com/gen2brain/raylib-go/raylib" |
28 | 28 | ) |
29 | 29 |
|
| 30 | +// ------------------------------------------------------------------------------------ |
| 31 | +// Program main entry point |
| 32 | +// ------------------------------------------------------------------------------------ |
30 | 33 | func main() { |
31 | 34 | // Initialization |
32 | | - rl.InitWindow(screenWidth, screenHeight, "raylib [models] example - M3D model loading") |
| 35 | + //-------------------------------------------------------------------------------------- |
| 36 | + const screenWidth, screenHeight = 800, 450 |
33 | 37 |
|
34 | | - // Define the camera to look into our 3d world |
35 | | - camera := rl.Camera{ |
36 | | - Position: rl.NewVector3(1.5, 1.5, 1.5), |
37 | | - Target: rl.NewVector3(0.0, 0.4, 0.0), |
38 | | - Up: rl.NewVector3(0.0, 1.0, 0.0), |
39 | | - Fovy: 45.0, |
40 | | - Projection: rl.CameraPerspective, |
41 | | - } |
42 | | - |
43 | | - position := rl.NewVector3(0.0, 0.0, 0.0) |
| 38 | + rl.InitWindow(screenWidth, screenHeight, "raylib [models] example - loading m3d") |
| 39 | + defer rl.CloseWindow() // Close window and OpenGL context |
44 | 40 |
|
45 | | - modelFileName := "cesium_man.m3d" |
46 | | - drawMesh := true |
47 | | - drawSkeleton := true |
48 | | - animPlaying := false // Store anim state, what to draw |
| 41 | + // Define the camera to look into our 3d world |
| 42 | + var camera rl.Camera |
| 43 | + camera.Position = rl.Vector3{X: 1.5, Y: 1.5, Z: 1.5} // Camera position |
| 44 | + camera.Target.Y = 0.4 // Camera looking at point |
| 45 | + camera.Up.Y = 1 // Camera up vector (rotation towards target) |
| 46 | + camera.Fovy = 45 // Camera field-of-view Y |
| 47 | + camera.Projection = rl.CameraPerspective // Camera projection type |
49 | 48 |
|
50 | 49 | // Load model |
51 | | - model := rl.LoadModel(modelFileName) |
| 50 | + model := rl.LoadModel("cesium_man.m3d") // Load the animated model mesh and basic data |
| 51 | + defer rl.UnloadModel(model) // Unload model |
| 52 | + var position rl.Vector3 // Set model position |
52 | 53 |
|
53 | | - // Load animations |
| 54 | + anims := rl.LoadModelAnimations("cesium_man.m3d") // Load animation data |
| 55 | + defer rl.UnloadModelAnimations(anims) // Unload model animations data |
| 56 | + animCount := uint32(len(anims)) |
54 | 57 |
|
55 | | - animFrameCounter := 0 |
56 | | - animID := 0 |
57 | | - anims := rl.LoadModelAnimations(modelFileName) |
58 | | - animsCount := int32(len(anims)) |
| 58 | + // Animation playing variables |
| 59 | + var animIndex uint32 // Current animation playing |
| 60 | + var animCurrentFrame float32 // Current animation frame (supporting interpolated frames) |
59 | 61 |
|
60 | | - rl.DisableCursor() |
61 | | - rl.SetTargetFPS(60) |
| 62 | + rl.SetTargetFPS(60) // Set our game to run at 60 frames-per-second |
| 63 | + //-------------------------------------------------------------------------------------- |
62 | 64 |
|
63 | 65 | // Main game loop |
64 | | - for !rl.WindowShouldClose() { |
| 66 | + for !rl.WindowShouldClose() { // Detect window close button or ESC key |
65 | 67 | // Update |
66 | | - rl.UpdateCamera(&camera, rl.CameraFirstPerson) |
67 | | - |
68 | | - if animsCount > 0 { |
69 | | - // Play animation when space bar is held down (or step one frame with N) |
70 | | - if rl.IsKeyDown(rl.KeySpace) || rl.IsKeyPressed(rl.KeyN) { |
71 | | - animFrameCounter++ |
72 | | - if animFrameCounter >= int(anims[animID].FrameCount) { |
73 | | - animFrameCounter = 0 |
74 | | - } |
75 | | - rl.UpdateModelAnimation(model, anims[animID], int32(animFrameCounter)) |
76 | | - animPlaying = true |
77 | | - } |
78 | | - |
79 | | - // Select animation by pressing C |
80 | | - if rl.IsKeyPressed(rl.KeyC) { |
81 | | - animFrameCounter = 0 |
82 | | - animID++ |
83 | | - if animID >= int(animsCount) { |
84 | | - animID = 0 |
85 | | - } |
86 | | - rl.UpdateModelAnimation(model, anims[animID], 0) |
87 | | - animPlaying = true |
88 | | - } |
| 68 | + //---------------------------------------------------------------------------------- |
| 69 | + rl.UpdateCamera(&camera, rl.CameraOrbital) |
| 70 | + |
| 71 | + // Select current animation |
| 72 | + if rl.IsKeyPressed(rl.KeyRight) { |
| 73 | + animIndex = (animIndex + 1) % animCount |
| 74 | + } else if rl.IsKeyPressed(rl.KeyLeft) { |
| 75 | + animIndex = (animIndex + animCount - 1) % animCount |
89 | 76 | } |
90 | 77 |
|
91 | | - // Toggle skeleton drawing |
92 | | - if rl.IsKeyPressed(rl.KeyB) { |
93 | | - drawSkeleton = !drawSkeleton |
94 | | - } |
95 | | - |
96 | | - // Toggle mesh drawing |
97 | | - if rl.IsKeyPressed(rl.KeyM) { |
98 | | - drawMesh = !drawMesh |
| 78 | + // Update model animation |
| 79 | + animCurrentFrame += 1 |
| 80 | + if animCurrentFrame >= float32(anims[animIndex].KeyframeCount) { |
| 81 | + animCurrentFrame = 0 |
99 | 82 | } |
| 83 | + rl.UpdateModelAnimation(model, anims[animIndex], animCurrentFrame) |
| 84 | + //---------------------------------------------------------------------------------- |
100 | 85 |
|
101 | 86 | // Draw |
| 87 | + //---------------------------------------------------------------------------------- |
102 | 88 | rl.BeginDrawing() |
103 | 89 |
|
104 | 90 | rl.ClearBackground(rl.RayWhite) |
105 | 91 |
|
106 | 92 | rl.BeginMode3D(camera) |
107 | 93 |
|
108 | 94 | // Draw 3d model with texture |
109 | | - if drawMesh { |
110 | | - rl.DrawModel(model, position, 1.0, rl.White) |
111 | | - } |
112 | | - |
113 | | - // Draw the animated skeleton |
114 | | - if drawSkeleton { |
115 | | - modelBones := model.GetBones() |
116 | | - modelPoses := model.GetBindPose() |
117 | | - anim := anims[animID] |
118 | | - animBones := anim.GetBones() |
119 | | - for bone := 0; bone < int(model.BoneCount)-1; bone++ { |
120 | | - if !animPlaying || animsCount == 0 { |
121 | | - // Display the bind-pose skeleton |
122 | | - rl.DrawCube(modelPoses[bone].Translation, 0.04, 0.04, 0.04, rl.Red) |
123 | | - if modelBones[bone].Parent >= 0 { |
124 | | - rl.DrawLine3D(modelPoses[bone].Translation, modelPoses[modelBones[bone].Parent].Translation, rl.Red) |
125 | | - } |
126 | | - } else { |
127 | | - // // Display the frame-pose skeleton |
128 | | - pos := anim.GetFramePose(animFrameCounter, bone).Translation |
129 | | - rl.DrawCube(pos, 0.05, 0.05, 0.05, rl.Red) |
130 | | - if animBones[bone].Parent >= 0 { |
131 | | - endPos := anim.GetFramePose(animFrameCounter, int(animBones[bone].Parent)).Translation |
132 | | - rl.DrawLine3D(pos, endPos, rl.Red) |
133 | | - } |
| 95 | + if !rl.IsKeyDown(rl.KeySpace) { |
| 96 | + rl.DrawModel(model, position, 1, rl.White) |
| 97 | + } else { |
| 98 | + // Draw the animated skeleton |
| 99 | + |
| 100 | + // Loop to (boneCount - 1) because the last one is a special "no bone" bone, |
| 101 | + // needed to workaround buggy models without a -1, a cube is always drawn at the origin |
| 102 | + for i := 0; i < int(model.Skeleton.BoneCount)-1; i++ { |
| 103 | + framePose := anims[animIndex].GetFramePose(int(animCurrentFrame), i).Translation |
| 104 | + // Display the frame-pose skeleton |
| 105 | + rl.DrawCube(framePose, 0.05, 0.05, 0.05, rl.Red) |
| 106 | + if model.Skeleton.GetBones()[i].Parent >= 0 { |
| 107 | + rl.DrawLine3D(framePose, anims[animIndex].GetFramePose(int(animCurrentFrame), int(model.Skeleton.GetBones()[i].Parent)).Translation, rl.Red) |
134 | 108 | } |
135 | 109 | } |
136 | 110 | } |
137 | 111 |
|
138 | | - rl.DrawGrid(10, 1.0) |
| 112 | + rl.DrawGrid(10, 1) |
139 | 113 |
|
140 | 114 | rl.EndMode3D() |
141 | 115 |
|
142 | | - rl.DrawText("PRESS SPACE to PLAY MODEL ANIMATION", 10, screenHeight-80, 10, rl.Maroon) |
143 | | - rl.DrawText("PRESS N to STEP ONE ANIMATION FRAME", 10, screenHeight-60, 10, rl.DarkGray) |
144 | | - rl.DrawText("PRESS C to CYCLE THROUGH ANIMATIONS", 10, screenHeight-40, 10, rl.DarkGray) |
145 | | - rl.DrawText("PRESS M to toggle MESH, B to toggle SKELETON DRAWING", 10, screenHeight-20, 10, rl.DarkGray) |
146 | | - rl.DrawText("(c) CesiumMan model by KhronosGroup", screenWidth-210, screenHeight-20, 10, rl.Gray) |
| 116 | + rl.DrawText(fmt.Sprintf("Current animation: %s", anims[animIndex].GetName()), 10, 10, 20, rl.LightGray) |
| 117 | + rl.DrawText("Press SPACE to draw skeleton", 10, 40, 20, rl.Maroon) |
| 118 | + rl.DrawText("(c) CesiumMan model by KhronosGroup", int32(rl.GetScreenWidth()-210), int32(rl.GetScreenHeight()-20), 10, rl.Gray) |
147 | 119 |
|
148 | 120 | rl.EndDrawing() |
| 121 | + //---------------------------------------------------------------------------------- |
149 | 122 | } |
150 | | - |
151 | | - // De-Initialization |
152 | | - rl.UnloadModelAnimations(anims) |
153 | | - rl.UnloadModel(model) |
154 | | - rl.CloseWindow() |
155 | 123 | } |
0 commit comments