Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

123 rader
5.0 KiB

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