models_yaw_pitch_roll.c 8.1 KB

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