raylib/ports/models/models_loading_iqm

Licence This example has its own licence, rather than Odin's: raylib/ports/LICENSE
These are ported Raylib examples, and as such, they are subject to Raylib's license. Copyright (c) 2013-2024 Ramon Santamaria (@raysan5)
The rest of the licence
This software is provided "as-is", without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution.

Code

models_loading_iqm.odin ¶
110 linesSource

1/*******************************************************************************************
2*
3*   raylib [models] example - loading iqm
4*
5*   Example complexity rating: [★★☆☆] 2/4
6*
7*   Example originally created with raylib 2.5, last time updated with raylib 3.5
8*
9*   Example contributed by Culacant (@culacant) and reviewed by Ramon Santamaria (@raysan5)
10*
11*   Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
12*   BSD-like license that allows static linking with closed source software
13*
14*   Copyright (c) 2019-2025 Culacant (@culacant) and Ramon Santamaria (@raysan5)
15*
16********************************************************************************************
17*
18*   NOTE: To export a model from blender, make sure it is not posed, the vertices need to be
19*         in the same position as they would be in edit mode and the scale of your models is
20*         set to 0. Scaling can be done from the export menu
21*
22********************************************************************************************/
23
24package raylib_examples
25
26import rl "vendor:raylib"
27
28//------------------------------------------------------------------------------------
29// Program main entry point
30//------------------------------------------------------------------------------------
31main :: proc() {
32    // Initialization
33    //--------------------------------------------------------------------------------------
34	SCREEN_WIDTH :: 800
35	SCREEN_HEIGHT :: 450
36
37	rl.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [models] example - loading iqm")
38
39    // Define the camera to look into our 3d world
40	camera := rl.Camera { 
41    	position = { 10.0, 10.0, 10.0 }, // Camera position
42    	target = { 0.0, 0.0, 0.0 },      // Camera looking at point
43    	up = { 0.0, 1.0, 0.0 },          // Camera up vector (rotation towards target)
44		fovy = 45.0,                     // Camera field-of-view Y
45    	projection = .PERSPECTIVE,       // Camera mode type
46	}
47
48	model := rl.LoadModel("resources/models/iqm/guy.iqm")            // Load the animated model mesh and basic data
49	texture := rl.LoadTexture("resources/models/iqm/guytex.png")     // Load model texture and set material
50	rl.SetMaterialTexture(&model.materials[0], .ALBEDO, texture)     // Set model material map texture
51
52	position := rl.Vector3{ 0.0, 0.0, 0.0 }            // Set model position
53
54    // Load animation data
55	animsCount: i32
56	anims := rl.LoadModelAnimations("resources/models/iqm/guyanim.iqm", &animsCount)
57	animFrameCounter: f32
58
59	rl.DisableCursor()                    // Catch cursor
60	rl.SetTargetFPS(60)                   // Set our game to run at 60 frames-per-second
61    //--------------------------------------------------------------------------------------
62
63    // Main game loop
64	for !rl.WindowShouldClose() {        // Detect window close button or ESC key
65        // Update
66        //----------------------------------------------------------------------------------
67		rl.UpdateCamera(&camera, .ORBITAL)
68
69		animFrameCounter += 1
70		rl.UpdateModelAnimation(model, anims[0], animFrameCounter)
71		if animFrameCounter >= f32(anims[0].keyframeCount) {
72			animFrameCounter = 0
73		}
74
75        //----------------------------------------------------------------------------------
76
77        // Draw
78        //----------------------------------------------------------------------------------
79		rl.BeginDrawing()
80
81			rl.ClearBackground(rl.RAYWHITE)
82
83			rl.BeginMode3D(camera)
84
85				rl.DrawModelEx(model, position, { 1.0, 0.0, 0.0 }, -90.0, { 1.0, 1.0, 1.0 }, rl.WHITE)
86
87				for i := 0; i < int(model.skeleton.boneCount); i += 1 {
88					// rl.DrawCube(anims[0].keyframePoses[i32(animFrameCounter)][i].translation, 0.2, 0.2, 0.2, rl.RED)
89                }
90
91				rl.DrawGrid(10, 1.0)         // Draw a grid
92
93			rl.EndMode3D()
94
95			rl.DrawText("PRESS SPACE to PLAY MODEL ANIMATION", 10, 10, 20, rl.MAROON)
96			rl.DrawText("(c) Guy IQM 3D model by @culacant", SCREEN_WIDTH - 200, SCREEN_HEIGHT - 20, 10, rl.GRAY)
97
98		rl.EndDrawing()
99        //----------------------------------------------------------------------------------
100    }
101
102    // De-Initialization
103    //--------------------------------------------------------------------------------------
104	rl.UnloadTexture(texture)                     // Unload texture
105	rl.UnloadModelAnimations(anims, animsCount)   // Unload model animations data
106	rl.UnloadModel(model)                         // Unload model
107
108	rl.CloseWindow()                  // Close window and OpenGL context
109    //--------------------------------------------------------------------------------------
110}

Assets 3

Declarations Used 28