2
0

models_yaw_pitch_roll.c 4.4 KB

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