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.

130 lines
4.7 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - quat conversions
  4. *
  5. * Generally you should really stick to eulers OR quats...
  6. * This tests that various conversions are equivalent.
  7. *
  8. * This example has been created using raylib 3.5 (www.raylib.com)
  9. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  10. *
  11. * Example contributed by Chris Camacho (@chriscamacho) and reviewed by Ramon Santamaria (@raysan5)
  12. *
  13. * Copyright (c) 2020 Chris Camacho (@chriscamacho) and Ramon Santamaria (@raysan5)
  14. *
  15. ********************************************************************************************/
  16. #include "raylib.h"
  17. #include "raymath.h"
  18. int main(void)
  19. {
  20. // Initialization
  21. //--------------------------------------------------------------------------------------
  22. const int screenWidth = 800;
  23. const int screenHeight = 450;
  24. InitWindow(screenWidth, screenHeight, "raylib [core] example - quat conversions");
  25. Camera3D camera = { 0 };
  26. camera.position = (Vector3){ 0.0f, 10.0f, 10.0f }; // Camera position
  27. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  28. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  29. camera.fovy = 45.0f; // Camera field-of-view Y
  30. camera.projection = CAMERA_PERSPECTIVE; // Camera mode type
  31. Mesh mesh = GenMeshCylinder(0.2f, 1.0f, 32);
  32. Model model = LoadModelFromMesh(mesh);
  33. // Some required variables
  34. Quaternion q1 = { 0 };
  35. Matrix m1 = { 0 }, m2 = { 0 }, m3 = { 0 }, m4 = { 0 };
  36. Vector3 v1 = { 0 }, v2 = { 0 };
  37. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  38. //--------------------------------------------------------------------------------------
  39. // Main game loop
  40. while (!WindowShouldClose()) // Detect window close button or ESC key
  41. {
  42. // Update
  43. //--------------------------------------------------------------------------------------
  44. if (!IsKeyDown(KEY_SPACE))
  45. {
  46. v1.x += 0.01f;
  47. v1.y += 0.03f;
  48. v1.z += 0.05f;
  49. }
  50. if (v1.x > PI*2) v1.x -= PI*2;
  51. if (v1.y > PI*2) v1.y -= PI*2;
  52. if (v1.z > PI*2) v1.z -= PI*2;
  53. q1 = QuaternionFromEuler(v1.x, v1.y, v1.z);
  54. m1 = MatrixRotateZYX(v1);
  55. m2 = QuaternionToMatrix(q1);
  56. q1 = QuaternionFromMatrix(m1);
  57. m3 = QuaternionToMatrix(q1);
  58. v2 = QuaternionToEuler(q1);
  59. v2.x *= DEG2RAD;
  60. v2.y *= DEG2RAD;
  61. v2.z *= DEG2RAD;
  62. m4 = MatrixRotateZYX(v2);
  63. //--------------------------------------------------------------------------------------
  64. // Draw
  65. //----------------------------------------------------------------------------------
  66. BeginDrawing();
  67. ClearBackground(RAYWHITE);
  68. BeginMode3D(camera);
  69. model.transform = m1;
  70. DrawModel(model, (Vector3){ -1, 0, 0 }, 1.0f, RED);
  71. model.transform = m2;
  72. DrawModel(model, (Vector3){ 1, 0, 0 }, 1.0f, RED);
  73. model.transform = m3;
  74. DrawModel(model, (Vector3){ 0, 0, 0 }, 1.0f, RED);
  75. model.transform = m4;
  76. DrawModel(model, (Vector3){ 0, 0, -1 }, 1.0f, RED);
  77. DrawGrid(10, 1.0f);
  78. EndMode3D();
  79. if (v2.x < 0) v2.x += PI*2;
  80. if (v2.y < 0) v2.y += PI*2;
  81. if (v2.z < 0) v2.z += PI*2;
  82. Color cx,cy,cz;
  83. cx = cy = cz = BLACK;
  84. if (v1.x == v2.x) cx = GREEN;
  85. if (v1.y == v2.y) cy = GREEN;
  86. if (v1.z == v2.z) cz = GREEN;
  87. DrawText(TextFormat("%2.3f", v1.x), 20, 20, 20, cx);
  88. DrawText(TextFormat("%2.3f", v1.y), 20, 40, 20, cy);
  89. DrawText(TextFormat("%2.3f", v1.z), 20, 60, 20, cz);
  90. DrawText(TextFormat("%2.3f", v2.x), 200, 20, 20, cx);
  91. DrawText(TextFormat("%2.3f", v2.y), 200, 40, 20, cy);
  92. DrawText(TextFormat("%2.3f", v2.z), 200, 60, 20, cz);
  93. EndDrawing();
  94. //----------------------------------------------------------------------------------
  95. }
  96. // De-Initialization
  97. //--------------------------------------------------------------------------------------
  98. UnloadModel(model); // Unload model data (mesh and materials)
  99. CloseWindow(); // Close window and OpenGL context
  100. //--------------------------------------------------------------------------------------
  101. return 0;
  102. }