models_yaw_pitch_roll.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - yaw pitch roll
  4. *
  5. * Example complexity rating: [★★☆☆] 2/4
  6. *
  7. * Example originally created with raylib 1.8, last time updated with raylib 4.0
  8. *
  9. * Example contributed by Berni (@Berni8k) and reviewed by Ramon Santamaria (@raysan5)
  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) 2017-2025 Berni (@Berni8k) and Ramon Santamaria (@raysan5)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #include "raymath.h" // Required for: MatrixRotateXYZ()
  19. //------------------------------------------------------------------------------------
  20. // Program main entry point
  21. //------------------------------------------------------------------------------------
  22. int main(void)
  23. {
  24. // Initialization
  25. //--------------------------------------------------------------------------------------
  26. const int screenWidth = 800;
  27. const int screenHeight = 450;
  28. //SetConfigFlags(FLAG_MSAA_4X_HINT | FLAG_WINDOW_HIGHDPI);
  29. InitWindow(screenWidth, screenHeight, "raylib [models] example - yaw pitch roll");
  30. Camera camera = { 0 };
  31. camera.position = (Vector3){ 0.0f, 50.0f, -120.0f };// Camera position perspective
  32. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  33. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  34. camera.fovy = 30.0f; // Camera field-of-view Y
  35. camera.projection = CAMERA_PERSPECTIVE; // Camera type
  36. Model model = LoadModel("resources/models/obj/plane.obj"); // Load model
  37. Texture2D texture = LoadTexture("resources/models/obj/plane_diffuse.png"); // Load model texture
  38. model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set map diffuse texture
  39. float pitch = 0.0f;
  40. float roll = 0.0f;
  41. float yaw = 0.0f;
  42. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  43. //--------------------------------------------------------------------------------------
  44. // Main game loop
  45. while (!WindowShouldClose()) // Detect window close button or ESC key
  46. {
  47. // Update
  48. //----------------------------------------------------------------------------------
  49. // Plane pitch (x-axis) controls
  50. if (IsKeyDown(KEY_DOWN)) pitch += 0.6f;
  51. else if (IsKeyDown(KEY_UP)) pitch -= 0.6f;
  52. else
  53. {
  54. if (pitch > 0.3f) pitch -= 0.3f;
  55. else if (pitch < -0.3f) pitch += 0.3f;
  56. }
  57. // Plane yaw (y-axis) controls
  58. if (IsKeyDown(KEY_S)) yaw -= 1.0f;
  59. else if (IsKeyDown(KEY_A)) yaw += 1.0f;
  60. else
  61. {
  62. if (yaw > 0.0f) yaw -= 0.5f;
  63. else if (yaw < 0.0f) yaw += 0.5f;
  64. }
  65. // Plane roll (z-axis) controls
  66. if (IsKeyDown(KEY_LEFT)) roll -= 1.0f;
  67. else if (IsKeyDown(KEY_RIGHT)) roll += 1.0f;
  68. else
  69. {
  70. if (roll > 0.0f) roll -= 0.5f;
  71. else if (roll < 0.0f) roll += 0.5f;
  72. }
  73. // Tranformation matrix for rotations
  74. model.transform = MatrixRotateXYZ((Vector3){ DEG2RAD*pitch, DEG2RAD*yaw, DEG2RAD*roll });
  75. //----------------------------------------------------------------------------------
  76. // Draw
  77. //----------------------------------------------------------------------------------
  78. BeginDrawing();
  79. ClearBackground(RAYWHITE);
  80. // Draw 3D model (recomended to draw 3D always before 2D)
  81. BeginMode3D(camera);
  82. DrawModel(model, (Vector3){ 0.0f, -8.0f, 0.0f }, 1.0f, WHITE); // Draw 3d model with texture
  83. DrawGrid(10, 10.0f);
  84. EndMode3D();
  85. // Draw controls info
  86. DrawRectangle(30, 370, 260, 70, Fade(GREEN, 0.5f));
  87. DrawRectangleLines(30, 370, 260, 70, Fade(DARKGREEN, 0.5f));
  88. DrawText("Pitch controlled with: KEY_UP / KEY_DOWN", 40, 380, 10, DARKGRAY);
  89. DrawText("Roll controlled with: KEY_LEFT / KEY_RIGHT", 40, 400, 10, DARKGRAY);
  90. DrawText("Yaw controlled with: KEY_A / KEY_S", 40, 420, 10, DARKGRAY);
  91. DrawText("(c) WWI Plane Model created by GiaHanLam", screenWidth - 240, screenHeight - 20, 10, DARKGRAY);
  92. EndDrawing();
  93. //----------------------------------------------------------------------------------
  94. }
  95. // De-Initialization
  96. //--------------------------------------------------------------------------------------
  97. UnloadModel(model); // Unload model data
  98. UnloadTexture(texture); // Unload texture data
  99. CloseWindow(); // Close window and OpenGL context
  100. //--------------------------------------------------------------------------------------
  101. return 0;
  102. }