|
| 1 | +/******************************************************************************************* |
| 2 | +* |
| 3 | +* raylib [models] example - geometry textures cube |
| 4 | +* |
| 5 | +* Example complexity rating: [★☆☆☆] 1/4 |
| 6 | +* |
| 7 | +* Example originally created with raylib 5.6-dev, last time updated with raylib 5.6-dev |
| 8 | +* |
| 9 | +* Example contributed by Jopestpe (@jopestpe) |
| 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) 2025-2025 Jopestpe (@jopestpe) |
| 15 | +* |
| 16 | +********************************************************************************************/ |
| 17 | + |
| 18 | +#include "raylib.h" |
| 19 | + |
| 20 | +//------------------------------------------------------------------------------------ |
| 21 | +// Program main entry point |
| 22 | +//------------------------------------------------------------------------------------ |
| 23 | +int main(void) |
| 24 | +{ |
| 25 | + // Initialization |
| 26 | + //-------------------------------------------------------------------------------------- |
| 27 | + const int screenWidth = 800; |
| 28 | + const int screenHeight = 450; |
| 29 | + |
| 30 | + InitWindow(screenWidth, screenHeight, "raylib [models] example - geometry textures cube"); |
| 31 | + |
| 32 | + // Define the camera to look into our 3d world |
| 33 | + Camera camera = { 0 }; |
| 34 | + camera.position = (Vector3){ 0.0f, 0.0f, 4.0f }; |
| 35 | + camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; |
| 36 | + camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; |
| 37 | + camera.fovy = 45.0f; |
| 38 | + camera.projection = CAMERA_PERSPECTIVE; |
| 39 | + |
| 40 | + // Load image to create texture for the cube |
| 41 | + Model model = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f)); |
| 42 | + Image img = LoadImage("resources/cubicmap_atlas.png"); |
| 43 | + Image crop = ImageFromImage(img, (Rectangle){0, img.height/2, img.width/2, img.height/2}); |
| 44 | + Texture2D texture = LoadTextureFromImage(crop); |
| 45 | + UnloadImage(img); |
| 46 | + UnloadImage(crop); |
| 47 | + |
| 48 | + model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; |
| 49 | + |
| 50 | + float rotation = 0.0f; |
| 51 | + |
| 52 | + SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
| 53 | + //-------------------------------------------------------------------------------------- |
| 54 | + |
| 55 | + // Main game loop |
| 56 | + while (!WindowShouldClose()) // Detect window close button or ESC key |
| 57 | + { |
| 58 | + // Update |
| 59 | + //---------------------------------------------------------------------------------- |
| 60 | + rotation += 1.0f; |
| 61 | + |
| 62 | + // Draw |
| 63 | + //---------------------------------------------------------------------------------- |
| 64 | + BeginDrawing(); |
| 65 | + |
| 66 | + ClearBackground(RAYWHITE); |
| 67 | + |
| 68 | + BeginMode3D(camera); |
| 69 | + |
| 70 | + DrawModelEx(model, (Vector3){0,0,0}, (Vector3){0.5f,1,0}, rotation, (Vector3){1,1,1}, WHITE); |
| 71 | + |
| 72 | + EndMode3D(); |
| 73 | + |
| 74 | + DrawFPS(10, 10); |
| 75 | + |
| 76 | + EndDrawing(); |
| 77 | + //---------------------------------------------------------------------------------- |
| 78 | + } |
| 79 | + |
| 80 | + // De-Initialization |
| 81 | + //-------------------------------------------------------------------------------------- |
| 82 | + UnloadTexture(texture); // Unload texture |
| 83 | + UnloadModel(model); // Unload model |
| 84 | + CloseWindow(); // Close window and OpenGL context |
| 85 | + //-------------------------------------------------------------------------------------- |
| 86 | + |
| 87 | + return 0; |
| 88 | +} |
0 commit comments