ex07c_3d_models.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. SetModelTexture(&cat, texture); // Link texture to model
  26. //--------------------------------------------------------------------------------------
  27. // Main game loop
  28. while (!WindowShouldClose()) // Detect window close button or ESC key
  29. {
  30. // Update
  31. //----------------------------------------------------------------------------------
  32. if (IsKeyDown(KEY_LEFT)) position.x -= 0.2;
  33. if (IsKeyDown(KEY_RIGHT)) position.x += 0.2;
  34. if (IsKeyDown(KEY_UP)) position.z -= 0.2;
  35. if (IsKeyDown(KEY_DOWN)) position.z += 0.2;
  36. //----------------------------------------------------------------------------------
  37. // Draw
  38. //----------------------------------------------------------------------------------
  39. BeginDrawing();
  40. ClearBackground(RAYWHITE);
  41. Begin3dMode(camera);
  42. DrawModel(cat, position, 0.1f, WHITE); // Draw 3d model with texture
  43. DrawGrid(10.0, 1.0); // Draw a grid
  44. DrawGizmo(position);
  45. End3dMode();
  46. DrawFPS(10, 10);
  47. EndDrawing();
  48. //----------------------------------------------------------------------------------
  49. }
  50. // De-Initialization
  51. //--------------------------------------------------------------------------------------
  52. UnloadTexture(texture); // Unload texture
  53. UnloadModel(cat); // Unload model
  54. CloseWindow(); // Close window and OpenGL context
  55. //--------------------------------------------------------------------------------------
  56. return 0;
  57. }