models_yaw_pitch_roll.c 4.6 KB

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