models_tesseract_view.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - tesseract view
  4. *
  5. * NOTE: This example only works on platforms that support drag & drop (Windows, Linux, OSX, Html5?)
  6. *
  7. * Example complexity rating: [★★☆☆] 2/4
  8. *
  9. * Example originally created with raylib 5.6-dev, last time updated with raylib 5.6-dev
  10. *
  11. * Example contributed by Timothy van der Valk (@arceryz) and reviewed by Ramon Santamaria (@raysan5)
  12. *
  13. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  14. * BSD-like license that allows static linking with closed source software
  15. *
  16. * Copyright (c) 2024-2025 Timothy van der Valk (@arceryz) and Ramon Santamaria (@raysan5)
  17. *
  18. ********************************************************************************************/
  19. #include "raylib.h"
  20. #include "raymath.h"
  21. //------------------------------------------------------------------------------------
  22. // Program main entry point
  23. //------------------------------------------------------------------------------------
  24. int main(void)
  25. {
  26. // Initialization
  27. //--------------------------------------------------------------------------------------
  28. const int screenWidth = 800;
  29. const int screenHeight = 450;
  30. InitWindow(screenWidth, screenHeight, "raylib [models] example - tesseract view");
  31. // Define the camera to look into our 3d world
  32. Camera camera = { 0 };
  33. camera.position = (Vector3){ 4.0f, 4.0f, 4.0f }; // Camera position
  34. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  35. camera.up = (Vector3){ 0.0f, 0.0f, 1.0f }; // Camera up vector (rotation towards target)
  36. camera.fovy = 50.0f; // Camera field-of-view Y
  37. camera.projection = CAMERA_PERSPECTIVE; // Camera mode type
  38. // Find the coordinates by setting XYZW to +-1
  39. Vector4 tesseract[16] = {
  40. { 1, 1, 1, 1 }, { 1, 1, 1, -1 },
  41. { 1, 1, -1, 1 }, { 1, 1, -1, -1 },
  42. { 1, -1, 1, 1 }, { 1, -1, 1, -1 },
  43. { 1, -1, -1, 1 }, { 1, -1, -1, -1 },
  44. { -1, 1, 1, 1 }, { -1, 1, 1, -1 },
  45. { -1, 1, -1, 1 }, { -1, 1, -1, -1 },
  46. { -1, -1, 1, 1 }, { -1, -1, 1, -1 },
  47. { -1, -1, -1, 1 }, { -1, -1, -1, -1 },
  48. };
  49. float rotation = 0.0f;
  50. Vector3 transformed[16] = { 0 };
  51. float wValues[16] = { 0 };
  52. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  53. //--------------------------------------------------------------------------------------
  54. // Main game loop
  55. while (!WindowShouldClose()) // Detect window close button or ESC key
  56. {
  57. // Update
  58. //----------------------------------------------------------------------------------
  59. rotation = DEG2RAD*45.0f*(float)GetTime();
  60. for (int i = 0; i < 16; i++)
  61. {
  62. Vector4 p = tesseract[i];
  63. // Rotate the XW part of the vector
  64. Vector2 rotXW = Vector2Rotate((Vector2){ p.x, p.w }, rotation);
  65. p.x = rotXW.x;
  66. p.w = rotXW.y;
  67. // Projection from XYZW to XYZ from perspective point (0, 0, 0, 3)
  68. // NOTE: Trace a ray from (0, 0, 0, 3) > p and continue until W = 0
  69. float c = 3.0f/(3.0f - p.w);
  70. p.x = c*p.x;
  71. p.y = c*p.y;
  72. p.z = c*p.z;
  73. // Split XYZ coordinate and W values later for drawing
  74. transformed[i] = (Vector3){ p.x, p.y, p.z };
  75. wValues[i] = p.w;
  76. }
  77. //----------------------------------------------------------------------------------
  78. // Draw
  79. //----------------------------------------------------------------------------------
  80. BeginDrawing();
  81. ClearBackground(RAYWHITE);
  82. BeginMode3D(camera);
  83. for (int i = 0; i < 16; i++)
  84. {
  85. // Draw spheres to indicate the W value
  86. DrawSphere(transformed[i], fabsf(wValues[i]*0.1f), RED);
  87. for (int j = 0; j < 16; j++)
  88. {
  89. // Two lines are connected if they differ by 1 coordinate
  90. // This way we dont have to keep an edge list
  91. Vector4 v1 = tesseract[i];
  92. Vector4 v2 = tesseract[j];
  93. int diff = (int)(v1.x == v2.x) + (int)(v1.y == v2.y) + (int)(v1.z == v2.z) + (int)(v1.w == v2.w);
  94. // Draw only differing by 1 coordinate and the lower index only (duplicate lines)
  95. if (diff == 3 && i < j) DrawLine3D(transformed[i], transformed[j], MAROON);
  96. }
  97. }
  98. EndMode3D();
  99. EndDrawing();
  100. //----------------------------------------------------------------------------------
  101. }
  102. // De-Initialization
  103. //--------------------------------------------------------------------------------------
  104. CloseWindow(); // Close window and OpenGL context
  105. //--------------------------------------------------------------------------------------
  106. return 0;
  107. }