models_yaw_pitch_roll.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. camera.type = CAMERA_PERSPECTIVE; // Camera type
  43. float pitch = 0.0f;
  44. float roll = 0.0f;
  45. float yaw = 0.0f;
  46. SetTargetFPS(60);
  47. //--------------------------------------------------------------------------------------
  48. while (!WindowShouldClose()) // Detect window close button or ESC key
  49. {
  50. // Update
  51. //----------------------------------------------------------------------------------
  52. // Plane roll (x-axis) controls
  53. if (IsKeyDown(KEY_LEFT)) roll += 1.0f;
  54. else if (IsKeyDown(KEY_RIGHT)) roll -= 1.0f;
  55. else
  56. {
  57. if (roll > 0.0f) roll -= 0.5f;
  58. else if (roll < 0.0f) roll += 0.5f;
  59. }
  60. // Plane yaw (y-axis) controls
  61. if (IsKeyDown(KEY_S)) yaw += 1.0f;
  62. else if (IsKeyDown(KEY_A)) yaw -= 1.0f;
  63. else
  64. {
  65. if (yaw > 0.0f) yaw -= 0.5f;
  66. else if (yaw < 0.0f) yaw += 0.5f;
  67. }
  68. // Plane pitch (z-axis) controls
  69. if (IsKeyDown(KEY_DOWN)) pitch += 0.6f;
  70. else if (IsKeyDown(KEY_UP)) pitch -= 0.6f;
  71. else
  72. {
  73. if (pitch > 0.3f) pitch -= 0.3f;
  74. else if (pitch < -0.3f) pitch += 0.3f;
  75. }
  76. // Wraps the phase of an angle to fit between -180 and +180 degrees
  77. int pitchOffset = pitch;
  78. while (pitchOffset > 180) pitchOffset -= 360;
  79. while (pitchOffset < -180) pitchOffset += 360;
  80. pitchOffset *= 10;
  81. Matrix transform = MatrixIdentity();
  82. transform = MatrixMultiply(transform, MatrixRotateZ(DEG2RAD*roll));
  83. transform = MatrixMultiply(transform, MatrixRotateX(DEG2RAD*pitch));
  84. transform = MatrixMultiply(transform, MatrixRotateY(DEG2RAD*yaw));
  85. model.transform = transform;
  86. //----------------------------------------------------------------------------------
  87. // Draw
  88. //----------------------------------------------------------------------------------
  89. BeginDrawing();
  90. ClearBackground(RAYWHITE);
  91. // Draw framebuffer texture (Ahrs Display)
  92. int centerX = framebuffer.texture.width/2;
  93. int centerY = framebuffer.texture.height/2;
  94. float scaleFactor = 0.5f;
  95. BeginTextureMode(framebuffer);
  96. BeginBlendMode(BLEND_ALPHA);
  97. DrawTexturePro(texBackground, (Rectangle){ 0, 0, texBackground.width, texBackground.height },
  98. (Rectangle){ centerX, centerY, texBackground.width*scaleFactor, texBackground.height*scaleFactor},
  99. (Vector2){ texBackground.width/2*scaleFactor, texBackground.height/2*scaleFactor + pitchOffset*scaleFactor }, roll, WHITE);
  100. DrawTexturePro(texPitch, (Rectangle){ 0, 0, texPitch.width, texPitch.height },
  101. (Rectangle){ centerX, centerY, texPitch.width*scaleFactor, texPitch.height*scaleFactor },
  102. (Vector2){ texPitch.width/2*scaleFactor, texPitch.height/2*scaleFactor + pitchOffset*scaleFactor }, roll, WHITE);
  103. DrawTexturePro(texPlane, (Rectangle){ 0, 0, texPlane.width, texPlane.height },
  104. (Rectangle){ centerX, centerY, texPlane.width*scaleFactor, texPlane.height*scaleFactor },
  105. (Vector2){ texPlane.width/2*scaleFactor, texPlane.height/2*scaleFactor }, 0, WHITE);
  106. EndBlendMode();
  107. EndTextureMode();
  108. // Draw 3D model (recomended to draw 3D always before 2D)
  109. BeginMode3D(camera);
  110. DrawModel(model, (Vector3){ 0, 6.0f, 0 }, 1.0f, WHITE); // Draw 3d model with texture
  111. DrawGrid(10, 10.0f);
  112. EndMode3D();
  113. // Draw 2D GUI stuff
  114. DrawAngleGauge(texAngleGauge, 80, 70, roll, "roll", RED);
  115. DrawAngleGauge(texAngleGauge, 190, 70, pitch, "pitch", GREEN);
  116. DrawAngleGauge(texAngleGauge, 300, 70, yaw, "yaw", SKYBLUE);
  117. DrawRectangle(30, 360, 260, 70, Fade(SKYBLUE, 0.5f));
  118. DrawRectangleLines(30, 360, 260, 70, Fade(DARKBLUE, 0.5f));
  119. DrawText("Pitch controlled with: KEY_UP / KEY_DOWN", 40, 370, 10, DARKGRAY);
  120. DrawText("Roll controlled with: KEY_LEFT / KEY_RIGHT", 40, 390, 10, DARKGRAY);
  121. DrawText("Yaw controlled with: KEY_A / KEY_S", 40, 410, 10, DARKGRAY);
  122. // Draw framebuffer texture
  123. DrawTextureRec(framebuffer.texture, (Rectangle){ 0, 0, framebuffer.texture.width, -framebuffer.texture.height },
  124. (Vector2){ screenWidth - framebuffer.texture.width - 20, 20 }, Fade(WHITE, 0.8f));
  125. DrawRectangleLines(screenWidth - framebuffer.texture.width - 20, 20, framebuffer.texture.width, framebuffer.texture.height, DARKGRAY);
  126. EndDrawing();
  127. //----------------------------------------------------------------------------------
  128. }
  129. // De-Initialization
  130. //--------------------------------------------------------------------------------------
  131. // Unload all loaded data
  132. UnloadModel(model);
  133. UnloadRenderTexture(framebuffer);
  134. UnloadTexture(texAngleGauge);
  135. UnloadTexture(texBackground);
  136. UnloadTexture(texPitch);
  137. UnloadTexture(texPlane);
  138. CloseWindow(); // Close window and OpenGL context
  139. //--------------------------------------------------------------------------------------
  140. return 0;
  141. }
  142. // Draw angle gauge controls
  143. void DrawAngleGauge(Texture2D angleGauge, int x, int y, float angle, char title[], Color color)
  144. {
  145. Rectangle srcRec = { 0, 0, angleGauge.width, angleGauge.height };
  146. Rectangle dstRec = { x, y, angleGauge.width, angleGauge.height };
  147. Vector2 origin = { angleGauge.width/2, angleGauge.height/2};
  148. int textSize = 20;
  149. DrawTexturePro(angleGauge, srcRec, dstRec, origin, angle, color);
  150. DrawText(FormatText("%5.1f", angle), x - MeasureText(FormatText("%5.1f", angle), textSize) / 2, y + 10, textSize, DARKGRAY);
  151. DrawText(title, x - MeasureText(title, textSize) / 2, y + 60, textSize, DARKGRAY);
  152. }