You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 rivejä
8.4 KiB

7 vuotta sitten
5 vuotta sitten
7 vuotta sitten
5 vuotta sitten
5 vuotta sitten
7 vuotta sitten
  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 done with multiplication to combine rotations
  79. Matrix transform = MatrixIdentity();
  80. transform = MatrixMultiply(transform, MatrixRotateZ(DEG2RAD*roll));
  81. transform = MatrixMultiply(transform, MatrixRotateX(DEG2RAD*pitch));
  82. transform = MatrixMultiply(transform, MatrixRotateY(DEG2RAD*yaw));
  83. model.transform = transform;
  84. */
  85. // matrix created from multiple axes at once
  86. model.transform = MatrixRotateXYZ((Vector3){DEG2RAD*pitch,DEG2RAD*yaw,DEG2RAD*roll});
  87. //----------------------------------------------------------------------------------
  88. // Draw
  89. //----------------------------------------------------------------------------------
  90. BeginDrawing();
  91. ClearBackground(RAYWHITE);
  92. // Draw framebuffer texture (Ahrs Display)
  93. int centerX = framebuffer.texture.width/2;
  94. int centerY = framebuffer.texture.height/2;
  95. float scaleFactor = 0.5f;
  96. BeginTextureMode(framebuffer);
  97. BeginBlendMode(BLEND_ALPHA);
  98. DrawTexturePro(texBackground, (Rectangle){ 0, 0, texBackground.width, texBackground.height },
  99. (Rectangle){ centerX, centerY, texBackground.width*scaleFactor, texBackground.height*scaleFactor},
  100. (Vector2){ texBackground.width/2*scaleFactor, texBackground.height/2*scaleFactor + pitchOffset*scaleFactor }, roll, WHITE);
  101. DrawTexturePro(texPitch, (Rectangle){ 0, 0, texPitch.width, texPitch.height },
  102. (Rectangle){ centerX, centerY, texPitch.width*scaleFactor, texPitch.height*scaleFactor },
  103. (Vector2){ texPitch.width/2*scaleFactor, texPitch.height/2*scaleFactor + pitchOffset*scaleFactor }, roll, WHITE);
  104. DrawTexturePro(texPlane, (Rectangle){ 0, 0, texPlane.width, texPlane.height },
  105. (Rectangle){ centerX, centerY, texPlane.width*scaleFactor, texPlane.height*scaleFactor },
  106. (Vector2){ texPlane.width/2*scaleFactor, texPlane.height/2*scaleFactor }, 0, WHITE);
  107. EndBlendMode();
  108. EndTextureMode();
  109. // Draw 3D model (recomended to draw 3D always before 2D)
  110. BeginMode3D(camera);
  111. DrawModel(model, (Vector3){ 0, 6.0f, 0 }, 1.0f, WHITE); // Draw 3d model with texture
  112. DrawGrid(10, 10.0f);
  113. EndMode3D();
  114. // Draw 2D GUI stuff
  115. DrawAngleGauge(texAngleGauge, 80, 70, roll, "roll", RED);
  116. DrawAngleGauge(texAngleGauge, 190, 70, pitch, "pitch", GREEN);
  117. DrawAngleGauge(texAngleGauge, 300, 70, yaw, "yaw", SKYBLUE);
  118. DrawRectangle(30, 360, 260, 70, Fade(SKYBLUE, 0.5f));
  119. DrawRectangleLines(30, 360, 260, 70, Fade(DARKBLUE, 0.5f));
  120. DrawText("Pitch controlled with: KEY_UP / KEY_DOWN", 40, 370, 10, DARKGRAY);
  121. DrawText("Roll controlled with: KEY_LEFT / KEY_RIGHT", 40, 390, 10, DARKGRAY);
  122. DrawText("Yaw controlled with: KEY_A / KEY_S", 40, 410, 10, DARKGRAY);
  123. // Draw framebuffer texture
  124. DrawTextureRec(framebuffer.texture, (Rectangle){ 0, 0, framebuffer.texture.width, -framebuffer.texture.height },
  125. (Vector2){ screenWidth - framebuffer.texture.width - 20, 20 }, Fade(WHITE, 0.8f));
  126. DrawRectangleLines(screenWidth - framebuffer.texture.width - 20, 20, framebuffer.texture.width, framebuffer.texture.height, DARKGRAY);
  127. EndDrawing();
  128. //----------------------------------------------------------------------------------
  129. }
  130. // De-Initialization
  131. //--------------------------------------------------------------------------------------
  132. // Unload all loaded data
  133. UnloadTexture(model.materials[0].maps[MAP_DIFFUSE].texture);
  134. UnloadModel(model);
  135. UnloadRenderTexture(framebuffer);
  136. UnloadTexture(texAngleGauge);
  137. UnloadTexture(texBackground);
  138. UnloadTexture(texPitch);
  139. UnloadTexture(texPlane);
  140. CloseWindow(); // Close window and OpenGL context
  141. //--------------------------------------------------------------------------------------
  142. return 0;
  143. }
  144. // Draw angle gauge controls
  145. void DrawAngleGauge(Texture2D angleGauge, int x, int y, float angle, char title[], Color color)
  146. {
  147. Rectangle srcRec = { 0, 0, angleGauge.width, angleGauge.height };
  148. Rectangle dstRec = { x, y, angleGauge.width, angleGauge.height };
  149. Vector2 origin = { angleGauge.width/2, angleGauge.height/2};
  150. int textSize = 20;
  151. DrawTexturePro(angleGauge, srcRec, dstRec, origin, angle, color);
  152. DrawText(FormatText("%5.1f", angle), x - MeasureText(FormatText("%5.1f", angle), textSize) / 2, y + 10, textSize, DARKGRAY);
  153. DrawText(title, x - MeasureText(title, textSize) / 2, y + 60, textSize, DARKGRAY);
  154. }