2
0

models_obj_loading.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - Load and draw a 3d model (OBJ) (adapted for HTML5 platform)
  4. *
  5. * This example has been created using raylib 1.3 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2014 Ramon Santamaria (@raysan5)
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. #if defined(PLATFORM_WEB)
  13. #include <emscripten/emscripten.h>
  14. #endif
  15. //----------------------------------------------------------------------------------
  16. // Global Variables Definition
  17. //----------------------------------------------------------------------------------
  18. int screenWidth = 800;
  19. int screenHeight = 450;
  20. // Define the camera to look into our 3d world
  21. Camera camera = {{ 3.0f, 3.0f, 3.0f }, { 0.0f, 1.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };
  22. Model dwarf; // Declare OBJ model
  23. Texture2D texture; // Declare model texture
  24. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Define model position
  25. //----------------------------------------------------------------------------------
  26. // Module Functions Declaration
  27. //----------------------------------------------------------------------------------
  28. void UpdateDrawFrame(void); // Update and Draw one frame
  29. //----------------------------------------------------------------------------------
  30. // Main Enry Point
  31. //----------------------------------------------------------------------------------
  32. int main()
  33. {
  34. // Initialization
  35. //--------------------------------------------------------------------------------------
  36. InitWindow(screenWidth, screenHeight, "raylib [models] example - obj model loading");
  37. dwarf = LoadModel("resources/model/dwarf.obj"); // Load OBJ model
  38. texture = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model texture
  39. dwarf.material.texDiffuse = texture; // Set dwarf model diffuse texture
  40. #if defined(PLATFORM_WEB)
  41. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  42. #else
  43. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  44. //--------------------------------------------------------------------------------------
  45. // Main game loop
  46. while (!WindowShouldClose()) // Detect window close button or ESC key
  47. {
  48. UpdateDrawFrame();
  49. }
  50. #endif
  51. // De-Initialization
  52. //--------------------------------------------------------------------------------------
  53. UnloadTexture(texture); // Unload texture
  54. UnloadModel(dwarf); // Unload model
  55. CloseWindow(); // Close window and OpenGL context
  56. //--------------------------------------------------------------------------------------
  57. return 0;
  58. }
  59. //----------------------------------------------------------------------------------
  60. // Module Functions Definition
  61. //----------------------------------------------------------------------------------
  62. void UpdateDrawFrame(void)
  63. {
  64. // Update
  65. //----------------------------------------------------------------------------------
  66. //...
  67. //----------------------------------------------------------------------------------
  68. // Draw
  69. //----------------------------------------------------------------------------------
  70. BeginDrawing();
  71. ClearBackground(RAYWHITE);
  72. Begin3dMode(camera);
  73. DrawModel(dwarf, position, 2.0f, WHITE); // Draw 3d model with texture
  74. DrawGrid(10, 1.0f); // Draw a grid
  75. DrawGizmo(position); // Draw gizmo
  76. End3dMode();
  77. DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, GRAY);
  78. DrawFPS(10, 10);
  79. EndDrawing();
  80. //----------------------------------------------------------------------------------
  81. }