models_gltf_model.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - Load 3d gltf model
  4. *
  5. * This example has been created using raylib 3.5 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Example contributed by Hristo Stamenov (@object71) and reviewed by Ramon Santamaria (@raysan5)
  9. *
  10. * Copyright (c) 2021 Hristo Stamenov (@object71) and Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************
  13. *
  14. * To export a model from blender, make sure it is not posed, the vertices need to be in the
  15. * same position as they would be in edit mode.
  16. * and that the scale of your models is set to 0. Scaling can be done from the export menu.
  17. *
  18. ********************************************************************************************/
  19. #include "raylib.h"
  20. #include <stdlib.h>
  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 - model animation");
  28. // Define the camera to look into our 3d world
  29. Camera camera = { 0 };
  30. camera.position = (Vector3){ 10.0f, 10.0f, 10.0f }; // Camera position
  31. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  32. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  33. camera.fovy = 45.0f; // Camera field-of-view Y
  34. camera.projection = CAMERA_PERSPECTIVE; // Camera mode type
  35. Model model[7];
  36. model[0] = LoadModel("resources/gltf/raylib_32x32.glb");
  37. model[1] = LoadModel("resources/gltf/rigged_figure.glb");
  38. model[2] = LoadModel("resources/gltf/Avocado.glb");
  39. model[3] = LoadModel("resources/gltf/GearboxAssy.glb");
  40. model[4] = LoadModel("resources/gltf/BoxAnimated.glb");
  41. model[5] = LoadModel("resources/gltf/AnimatedTriangle.gltf");
  42. model[6] = LoadModel("resources/gltf/AnimatedMorphCube.glb");
  43. int currentModel = 0;
  44. int modelCount = 7;
  45. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
  46. SetCameraMode(camera, CAMERA_FREE); // Set free camera mode
  47. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  48. //--------------------------------------------------------------------------------------
  49. // Main game loop
  50. while (!WindowShouldClose()) // Detect window close button or ESC key
  51. {
  52. // Update
  53. //----------------------------------------------------------------------------------
  54. UpdateCamera(&camera);
  55. if (IsKeyReleased(KEY_RIGHT))
  56. {
  57. currentModel++;
  58. if (currentModel == modelCount) currentModel = 0;
  59. }
  60. if (IsKeyReleased(KEY_LEFT))
  61. {
  62. currentModel--;
  63. if (currentModel < 0) currentModel = modelCount - 1;
  64. }
  65. // Draw
  66. //----------------------------------------------------------------------------------
  67. BeginDrawing();
  68. ClearBackground(SKYBLUE);
  69. BeginMode3D(camera);
  70. DrawModelEx(model[currentModel], position, (Vector3){ 0.0f, 1.0f, 0.0f }, 180.0f, (Vector3){ 2.0f, 2.0f, 2.0f }, WHITE);
  71. DrawGrid(10, 1.0f); // Draw a grid
  72. EndMode3D();
  73. EndDrawing();
  74. //----------------------------------------------------------------------------------
  75. }
  76. // De-Initialization
  77. //--------------------------------------------------------------------------------------
  78. for(int i = 0; i < modelCount; i++) UnloadModel(model[i]); // Unload models
  79. CloseWindow(); // Close window and OpenGL context
  80. //--------------------------------------------------------------------------------------
  81. return 0;
  82. }