models_yaw_pitch_roll.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 based on Berni work on Raspberry Pi:
  9. * http://forum.raylib.com/index.php?p=/discussion/124/line-versus-triangle-drawing-order
  10. *
  11. * Copyright (c) 2017 Ramon Santamaria (@raysan5)
  12. *
  13. ********************************************************************************************/
  14. #include "raylib.h"
  15. #include "raymath.h"
  16. // Draw angle gauge controls
  17. void DrawAngleGauge(Texture2D angleGauge, int x, int y, float angle, char title[], Color color);
  18. //----------------------------------------------------------------------------------
  19. // Main entry point
  20. //----------------------------------------------------------------------------------
  21. int main()
  22. {
  23. // Initialization
  24. //--------------------------------------------------------------------------------------
  25. const int screenWidth = 800;
  26. const int screenHeight = 450;
  27. InitWindow(screenWidth, screenHeight, "raylib [models] example - plane rotations (yaw, pitch, roll)");
  28. Texture2D texAngleGauge = LoadTexture("resources/angle_gauge.png");
  29. Texture2D texBackground = LoadTexture("resources/background.png");
  30. Texture2D texPitch = LoadTexture("resources/pitch.png");
  31. Texture2D texPlane = LoadTexture("resources/plane.png");
  32. RenderTexture2D framebuffer = LoadRenderTexture(192, 192);
  33. // Model loading
  34. Model model = LoadModel("resources/plane.obj"); // Load OBJ model
  35. model.material.maps[MAP_DIFFUSE].texture = LoadTexture("resources/plane_diffuse.png"); // Set map diffuse texture
  36. GenTextureMipmaps(&model.material.maps[MAP_DIFFUSE].texture);
  37. Camera camera = { 0 };
  38. camera.position = (Vector3){ 0.0f, 60.0f, -120.0f };// Camera position perspective
  39. camera.target = (Vector3){ 0.0f, 12.0f, 0.0f }; // Camera looking at point
  40. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  41. camera.fovy = 30.0f; // Camera field-of-view Y
  42. float pitch = 0.0f;
  43. float roll = 0.0f;
  44. float yaw = 0.0f;
  45. SetTargetFPS(60);
  46. //--------------------------------------------------------------------------------------
  47. while (!WindowShouldClose()) // Detect window close button or ESC key
  48. {
  49. // Update
  50. //----------------------------------------------------------------------------------
  51. // Plane roll (x-axis) controls
  52. if (IsKeyDown(KEY_LEFT)) roll += 1.0f;
  53. else if (IsKeyDown(KEY_RIGHT)) roll -= 1.0f;
  54. else
  55. {
  56. if (roll > 0.0f) roll -= 0.5f;
  57. else if (roll < 0.0f) roll += 0.5f;
  58. }
  59. // Plane yaw (y-axis) controls
  60. if (IsKeyDown(KEY_S)) yaw += 1.0f;
  61. else if (IsKeyDown(KEY_A)) yaw -= 1.0f;
  62. else
  63. {
  64. if (yaw > 0.0f) yaw -= 0.5f;
  65. else if (yaw < 0.0f) yaw += 0.5f;
  66. }
  67. // Plane pitch (z-axis) controls
  68. if (IsKeyDown(KEY_DOWN)) pitch += 0.6f;
  69. else if (IsKeyDown(KEY_UP)) pitch -= 0.6f;
  70. else
  71. {
  72. if (pitch > 0.3f) pitch -= 0.3f;
  73. else if (pitch < -0.3f) pitch += 0.3f;
  74. }
  75. // Wraps the phase of an angle to fit between -180 and +180 degrees
  76. int pitchOffset = pitch;
  77. while (pitchOffset > 180) pitchOffset -= 360;
  78. while (pitchOffset < -180) pitchOffset += 360;
  79. pitchOffset *= 10;
  80. Matrix transform = MatrixIdentity();
  81. transform = MatrixMultiply(transform, MatrixRotateZ(DEG2RAD*roll));
  82. transform = MatrixMultiply(transform, MatrixRotateX(DEG2RAD*pitch));
  83. transform = MatrixMultiply(transform, MatrixRotateY(DEG2RAD*yaw));
  84. model.transform = transform;
  85. //----------------------------------------------------------------------------------
  86. // Draw
  87. //----------------------------------------------------------------------------------
  88. BeginDrawing();
  89. ClearBackground(RAYWHITE);
  90. // Draw framebuffer texture (Ahrs Display)
  91. int centerX = framebuffer.texture.width/2;
  92. int centerY = framebuffer.texture.height/2;
  93. float scaleFactor = 0.5f;
  94. BeginTextureMode(framebuffer);
  95. BeginBlendMode(BLEND_ALPHA);
  96. DrawTexturePro(texBackground, (Rectangle){ 0, 0, texBackground.width, texBackground.height },
  97. (Rectangle){ centerX, centerY, texBackground.width*scaleFactor, texBackground.height*scaleFactor},
  98. (Vector2){ texBackground.width/2*scaleFactor, texBackground.height/2*scaleFactor + pitchOffset*scaleFactor }, roll, WHITE);
  99. DrawTexturePro(texPitch, (Rectangle){ 0, 0, texPitch.width, texPitch.height },
  100. (Rectangle){ centerX, centerY, texPitch.width*scaleFactor, texPitch.height*scaleFactor },
  101. (Vector2){ texPitch.width/2*scaleFactor, texPitch.height/2*scaleFactor + pitchOffset*scaleFactor }, roll, WHITE);
  102. DrawTexturePro(texPlane, (Rectangle){ 0, 0, texPlane.width, texPlane.height },
  103. (Rectangle){ centerX, centerY, texPlane.width*scaleFactor, texPlane.height*scaleFactor },
  104. (Vector2){ texPlane.width/2*scaleFactor, texPlane.height/2*scaleFactor }, 0, WHITE);
  105. EndBlendMode();
  106. EndTextureMode();
  107. // Draw 3D model (recomended to draw 3D always before 2D)
  108. Begin3dMode(camera);
  109. DrawModel(model, (Vector3){ 0, 6.0f, 0 }, 1.0f, WHITE); // Draw 3d model with texture
  110. DrawGrid(10, 10.0f);
  111. End3dMode();
  112. // Draw 2D GUI stuff
  113. DrawAngleGauge(texAngleGauge, 80, 70, roll, "roll", RED);
  114. DrawAngleGauge(texAngleGauge, 190, 70, pitch, "pitch", GREEN);
  115. DrawAngleGauge(texAngleGauge, 300, 70, yaw, "yaw", SKYBLUE);
  116. DrawRectangle(30, 360, 260, 70, Fade(SKYBLUE, 0.5f));
  117. DrawRectangleLines(30, 360, 260, 70, Fade(DARKBLUE, 0.5f));
  118. DrawText("Pitch controlled with: KEY_UP / KEY_DOWN", 40, 370, 10, DARKGRAY);
  119. DrawText("Roll controlled with: KEY_LEFT / KEY_RIGHT", 40, 390, 10, DARKGRAY);
  120. DrawText("Yaw controlled with: KEY_A / KEY_S", 40, 410, 10, DARKGRAY);
  121. // Draw framebuffer texture
  122. DrawTextureRec(framebuffer.texture, (Rectangle){ 0, 0, framebuffer.texture.width, -framebuffer.texture.height },
  123. (Vector2){ screenWidth - framebuffer.texture.width - 20, 20 }, Fade(WHITE, 0.8f));
  124. DrawRectangleLines(screenWidth - framebuffer.texture.width - 20, 20, framebuffer.texture.width, framebuffer.texture.height, DARKGRAY);
  125. EndDrawing();
  126. //----------------------------------------------------------------------------------
  127. }
  128. // De-Initialization
  129. //--------------------------------------------------------------------------------------
  130. // Unload all loaded data
  131. UnloadModel(model);
  132. UnloadRenderTexture(framebuffer);
  133. UnloadTexture(texAngleGauge);
  134. UnloadTexture(texBackground);
  135. UnloadTexture(texPitch);
  136. UnloadTexture(texPlane);
  137. CloseWindow(); // Close window and OpenGL context
  138. //--------------------------------------------------------------------------------------
  139. return 0;
  140. }
  141. // Draw angle gauge controls
  142. void DrawAngleGauge(Texture2D angleGauge, int x, int y, float angle, char title[], Color color)
  143. {
  144. Rectangle srcRec = { 0, 0, angleGauge.width, angleGauge.height };
  145. Rectangle dstRec = { x, y, angleGauge.width, angleGauge.height };
  146. Vector2 origin = { angleGauge.width/2, angleGauge.height/2};
  147. int textSize = 20;
  148. DrawTexturePro(angleGauge, srcRec, dstRec, origin, angle, color);
  149. DrawText(FormatText("%5.1f°", angle), x - MeasureText(FormatText("%5.1f°", angle), textSize) / 2, y + 10, textSize, DARKGRAY);
  150. DrawText(title, x - MeasureText(title, textSize) / 2, y + 60, textSize, DARKGRAY);
  151. }