core_quat_conversion.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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-2021 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. // Load a cylinder model for testing
  32. Model model = LoadModelFromMesh(GenMeshCylinder(0.2f, 1.0f, 32));
  33. // Generic quaternion for operations
  34. Quaternion q1 = { 0 };
  35. // Transform matrices required to draw 4 cylinders
  36. Matrix m1 = { 0 };
  37. Matrix m2 = { 0 };
  38. Matrix m3 = { 0 };
  39. Matrix m4 = { 0 };
  40. // Generic vectors for rotations
  41. Vector3 v1 = { 0 };
  42. Vector3 v2 = { 0 };
  43. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  44. //--------------------------------------------------------------------------------------
  45. // Main game loop
  46. while (!WindowShouldClose()) // Detect window close button or ESC key
  47. {
  48. // Update
  49. //--------------------------------------------------------------------------------------
  50. if (v2.x < 0) v2.x += PI*2;
  51. if (v2.y < 0) v2.y += PI*2;
  52. if (v2.z < 0) v2.z += PI*2;
  53. if (!IsKeyDown(KEY_SPACE))
  54. {
  55. v1.x += 0.01f;
  56. v1.y += 0.03f;
  57. v1.z += 0.05f;
  58. }
  59. if (v1.x > PI*2) v1.x -= PI*2;
  60. if (v1.y > PI*2) v1.y -= PI*2;
  61. if (v1.z > PI*2) v1.z -= PI*2;
  62. q1 = QuaternionFromEuler(v1.x, v1.y, v1.z);
  63. m1 = MatrixRotateZYX(v1);
  64. m2 = QuaternionToMatrix(q1);
  65. q1 = QuaternionFromMatrix(m1);
  66. m3 = QuaternionToMatrix(q1);
  67. v2 = QuaternionToEuler(q1); // Angles returned in radians
  68. m4 = MatrixRotateZYX(v2);
  69. //--------------------------------------------------------------------------------------
  70. // Draw
  71. //----------------------------------------------------------------------------------
  72. BeginDrawing();
  73. ClearBackground(RAYWHITE);
  74. BeginMode3D(camera);
  75. model.transform = m1;
  76. DrawModel(model, (Vector3){ -1, 0, 0 }, 1.0f, RED);
  77. model.transform = m2;
  78. DrawModel(model, (Vector3){ 1, 0, 0 }, 1.0f, RED);
  79. model.transform = m3;
  80. DrawModel(model, (Vector3){ 0, 0, 0 }, 1.0f, RED);
  81. model.transform = m4;
  82. DrawModel(model, (Vector3){ 0, 0, -1 }, 1.0f, RED);
  83. DrawGrid(10, 1.0f);
  84. EndMode3D();
  85. DrawText(TextFormat("%2.3f", v1.x), 20, 20, 20, (v1.x == v2.x)? GREEN: BLACK);
  86. DrawText(TextFormat("%2.3f", v1.y), 20, 40, 20, (v1.y == v2.y)? GREEN: BLACK);
  87. DrawText(TextFormat("%2.3f", v1.z), 20, 60, 20, (v1.z == v2.z)? GREEN: BLACK);
  88. DrawText(TextFormat("%2.3f", v2.x), 200, 20, 20, (v1.x == v2.x)? GREEN: BLACK);
  89. DrawText(TextFormat("%2.3f", v2.y), 200, 40, 20, (v1.y == v2.y)? GREEN: BLACK);
  90. DrawText(TextFormat("%2.3f", v2.z), 200, 60, 20, (v1.z == v2.z)? GREEN: BLACK);
  91. EndDrawing();
  92. //----------------------------------------------------------------------------------
  93. }
  94. // De-Initialization
  95. //--------------------------------------------------------------------------------------
  96. UnloadModel(model); // Unload model data (mesh and materials)
  97. CloseWindow(); // Close window and OpenGL context
  98. //--------------------------------------------------------------------------------------
  99. return 0;
  100. }