models_rotating_cube.c 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - rotating 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 Jopestpe (@jopestpe)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. //------------------------------------------------------------------------------------
  19. // Program main entry point
  20. //------------------------------------------------------------------------------------
  21. int main(void)
  22. {
  23. // Initialization
  24. //--------------------------------------------------------------------------------------
  25. const int screenWidth = 800;
  26. const int screenHeight = 450;
  27. InitWindow(screenWidth, screenHeight, "raylib [models] example - rotating cube");
  28. // Define the camera to look into our 3d world
  29. Camera camera = { 0 };
  30. camera.position = (Vector3){ 0.0f, 3.0f, 3.0f };
  31. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
  32. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
  33. camera.fovy = 45.0f;
  34. camera.projection = CAMERA_PERSPECTIVE;
  35. // Load image to create texture for the cube
  36. Model model = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f));
  37. Image img = LoadImage("resources/cubicmap_atlas.png");
  38. Image crop = ImageFromImage(img, (Rectangle){0, img.height/2.0f, img.width/2.0f, img.height/2.0f});
  39. Texture2D texture = LoadTextureFromImage(crop);
  40. UnloadImage(img);
  41. UnloadImage(crop);
  42. model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture;
  43. float rotation = 0.0f;
  44. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  45. //--------------------------------------------------------------------------------------
  46. // Main game loop
  47. while (!WindowShouldClose()) // Detect window close button or ESC key
  48. {
  49. // Update
  50. //----------------------------------------------------------------------------------
  51. rotation += 1.0f;
  52. //----------------------------------------------------------------------------------
  53. // Draw
  54. //----------------------------------------------------------------------------------
  55. BeginDrawing();
  56. ClearBackground(RAYWHITE);
  57. BeginMode3D(camera);
  58. // Draw model defining: position, size, rotation-axis, rotation (degrees), size, and tint-color
  59. DrawModelEx(model, (Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 0.5f, 1.0f, 0.0f },
  60. rotation, (Vector3){ 1.0f, 1.0f, 1.0f }, WHITE);
  61. DrawGrid(10, 1.0f);
  62. EndMode3D();
  63. DrawFPS(10, 10);
  64. EndDrawing();
  65. //----------------------------------------------------------------------------------
  66. }
  67. // De-Initialization
  68. //--------------------------------------------------------------------------------------
  69. UnloadTexture(texture); // Unload texture
  70. UnloadModel(model); // Unload model
  71. CloseWindow(); // Close window and OpenGL context
  72. //--------------------------------------------------------------------------------------
  73. return 0;
  74. }