ex07c_3d_models.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*******************************************************************************************
  2. *
  3. * raylib example 07c - Load and draw a 3d model (OBJ)
  4. *
  5. * This example has been created using raylib 1.0 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2013 Ramon Santamaria (Ray San - [email protected])
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. int main()
  13. {
  14. // Initialization
  15. //--------------------------------------------------------------------------------------
  16. int screenWidth = 800;
  17. int screenHeight = 450;
  18. Vector3 position = { 0.0, 0.0, 0.0 };
  19. // Define the camera to look into our 3d world
  20. Camera camera = {{ 10.0, 8.0, 10.0 }, { 0.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }};
  21. InitWindow(screenWidth, screenHeight, "raylib example 07c - 3d models");
  22. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  23. Texture2D texture = LoadTexture("resources/catwhite.png");
  24. Model cat = LoadModel("resources/cat.obj");
  25. //--------------------------------------------------------------------------------------
  26. // Main game loop
  27. while (!WindowShouldClose()) // Detect window close button or ESC key
  28. {
  29. // Update
  30. //----------------------------------------------------------------------------------
  31. if (IsKeyPressed(KEY_LEFT)) position.x -= 0.2;
  32. if (IsKeyPressed(KEY_RIGHT)) position.x += 0.2;
  33. if (IsKeyPressed(KEY_UP)) position.z -= 0.2;
  34. if (IsKeyPressed(KEY_DOWN)) position.z += 0.2;
  35. //----------------------------------------------------------------------------------
  36. // Draw
  37. //----------------------------------------------------------------------------------
  38. BeginDrawing();
  39. ClearBackground(RAYWHITE);
  40. Begin3dMode(camera);
  41. DrawModelEx(cat, texture, position, 0.1f, WHITE); // Draw 3d model with texture
  42. DrawGrid(10.0, 1.0); // Draw a grid
  43. DrawGizmo(position, false);
  44. End3dMode();
  45. DrawFPS(10, 10);
  46. EndDrawing();
  47. //----------------------------------------------------------------------------------
  48. }
  49. // De-Initialization
  50. //--------------------------------------------------------------------------------------
  51. UnloadTexture(texture); // Unload texture
  52. UnloadModel(cat); // Unload model
  53. CloseWindow(); // Close window and OpenGL context
  54. //--------------------------------------------------------------------------------------
  55. return 0;
  56. }