Advertisement
Saito100

Amnesia Tesseract Creator

Jan 8th, 2024 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.71 KB | Source Code | 0 0
  1. //16x4 array for tesseract vertices.
  2. float[][] vert4;
  3. //16x3 array for projected vertices.
  4. float[][] vert3;
  5. //Index array for edges.
  6. int[] edges(64);
  7. //Rotation multiplier.
  8. float rotMul = 0.4f;
  9. //4D influence multiplier.
  10. float wScale = 2.0f;
  11.  
  12. //Create tesseract geometry.
  13. void TesseractInit()
  14. {
  15.     //Initialize vertices.
  16.     int vertIdx = 0;
  17.     for (int x = -1; x <= 1; x += 2) {
  18.         for (int y = -1; y <= 1; y += 2) {
  19.             for (int z = -1; z <= 1; z += 2) {
  20.                 for (int w = -1; w <= 1; w += 2) {
  21.                     vert4[vertIdx][0] = x;
  22.                     vert4[vertIdx][1] = y;
  23.                     vert4[vertIdx][2] = z;
  24.                     vert4[vertIdx][3] = w;
  25.                     vertIdx++;
  26.                 }
  27.             }
  28.         }
  29.     }
  30.    
  31.     //Initialize edges.
  32.     int edgeIdx = 0;
  33.     for (int i = 0; i < 16; i++) {
  34.         for (int j = i + 1; j < 16; j++) {
  35.             if (i != j) {
  36.                 int diffCount = 0;
  37.                 for (int k = 0; k < 4; k++) {
  38.                     if (vert4[i][k] != vert4[j][k]) {
  39.                         diffCount++;
  40.                     }
  41.                 }
  42.                 if (diffCount == 1) {
  43.                     edges[edgeIdx] = i;
  44.                     edgeIdx++;
  45.                     edges[edgeIdx] = j;
  46.                     edgeIdx++;
  47.                 }
  48.             }
  49.         }
  50.     }
  51.  
  52.     GetDupe();
  53. }
  54.  
  55. //Track usage of vertex duplicates.
  56. bool[][] vertDupe;
  57.  
  58. //Create final edge indices.
  59. int[] finalIdx1(64);
  60. int[] finalIdx2(64);
  61.  
  62. void GetDupe()
  63. {
  64.     //Initialize tracking arrays.
  65.     vertDupe.resize(16);
  66.     for (int i = 0; i < 16; i++) {
  67.         vertDupe[i].resize(4);
  68.         for (int j = 0; j < 4; j++) {
  69.             vertDupe[i][j] = false;
  70.         }
  71.     }
  72.  
  73.     //Define edges.
  74.     for (int i = 0; i < 32; i++) {
  75.         int vertIdx1 = edges[i * 2];
  76.         int vertIdx2 = edges[i * 2 + 1];
  77.  
  78.         //Find the first unused duplicate for each vertex.
  79.         int dupeIdx1 = TrackDupe(vertIdx1);
  80.         int dupeIdx2 = TrackDupe(vertIdx2);
  81.  
  82.         finalIdx1[i] = vertIdx1 * 4 + dupeIdx1;
  83.         finalIdx2[i + 1] = vertIdx2 * 4 + dupeIdx2;
  84.     }
  85.  
  86.     VertRotate(rotMul);
  87. }
  88.  
  89. //Find the first unused duplicate for a vertex.
  90. int TrackDupe(int vertIdx) {
  91.     for (int i = 0; i < 4; i++) {
  92.         if (!vertDupe[vertIdx][i]) {
  93.             vertDupe[vertIdx][i] = true; //Mark as used.
  94.             return i;
  95.         }
  96.     }
  97.     return 0;
  98. }
  99.  
  100. //Rotation planes.
  101. int rotPlane1 = 2;
  102. int rotPlane2 = 3;
  103.  
  104. void VertRotate(float rotMul)
  105. {
  106.     float cosAngle = MathCos(rotMul);
  107.     float sinAngle = MathSin(rotMul);
  108.    
  109.     for (int i = 0; i < 16; i++) {
  110.         ApplyRotationInPlane(rotPlane1, i, cosAngle, sinAngle);
  111.         ApplyRotationInPlane(rotPlane2, i, cosAngle, sinAngle);
  112.     }
  113.  
  114.     VertProject(wScale);
  115. }
  116.  
  117. void ApplyRotationInPlane(int rotPlane, int vertIdx, float cosAngle, float sinAngle)
  118. {
  119.     float x = vert4[vertIdx][0];
  120.     float y = vert4[vertIdx][1];
  121.     float z = vert4[vertIdx][2];
  122.     float w = vert4[vertIdx][3];
  123.    
  124.     //Apply rotation based on the plane.
  125.     switch (rotPlane) {
  126.         case 0: //XY plane.
  127.             vert4[vertIdx][0] = cosAngle * x - sinAngle * y;
  128.             vert4[vertIdx][1] = sinAngle * x + cosAngle * y;
  129.             break;
  130.         case 1: //XZ Plane
  131.             vert4[vertIdx][0] = cosAngle * x - sinAngle * z;
  132.             vert4[vertIdx][2] = sinAngle * x + cosAngle * z;
  133.             break;
  134.         case 2: //XW Plane
  135.             vert4[vertIdx][0] = cosAngle * x - sinAngle * w;
  136.             vert4[vertIdx][3] = sinAngle * x + cosAngle * w;
  137.             break;
  138.         case 3: //YZ Plane
  139.             vert4[vertIdx][1] = cosAngle * y - sinAngle * z;
  140.             vert4[vertIdx][2] = sinAngle * y + cosAngle * z;
  141.             break;
  142.         case 4: //YW Plane
  143.             vert4[vertIdx][1] = cosAngle * y - sinAngle * w;
  144.             vert4[vertIdx][3] = sinAngle * y + cosAngle * w;
  145.             break;
  146.         case 5: //ZW Plane
  147.             vert4[vertIdx][2] = cosAngle * z - sinAngle * w;
  148.             vert4[vertIdx][3] = sinAngle * z + cosAngle * w;
  149.             break;
  150.     }
  151. }
  152.  
  153. //Project the 4D vertices to 3D space.
  154. void VertProject(float wScale)
  155. {
  156.     for (int i = 0; i < 16; i++) {
  157.         //Perspective factor based on the 'w' component.
  158.         float wMul = 1.0f / (wScale - vert4[i][3]);
  159.  
  160.         //Apply the perspective transformation.
  161.         vert3[i][0] = vert4[i][0] * wMul;
  162.         vert3[i][1] = vert4[i][1] * wMul;
  163.         vert3[i][2] = vert4[i][2] * wMul;
  164.     }
  165.    
  166.     TesseractRender();
  167. }
  168.  
  169. //Render the tesseract.
  170. void TesseractRender()
  171. {
  172.     //Place vertices.
  173.     for (int i = 0; i < 64; i++) {
  174.         DetachFromStickyArea("vertex_" + i);
  175.         int origIdx = i / 4; //Each original vertex has 4 duplicates.
  176.         SetEntityPos("vertex_" + i, vert3[origIdx][0], vert3[origIdx][1], vert3[origIdx][2]);
  177.     }
  178.    
  179.     //Place edges.
  180.     for (int i = 0; i < 32; i++) {
  181.         AttachBodyToStickyArea("vertex_" + finalIdx1[i], "edge_" + i + "_Body_1");
  182.         AttachBodyToStickyArea("vertex_" + finalIdx2[i + 1], "edge_" + i + "_Body_2");
  183.     }
  184.  
  185.     AddTimer("MAIN", 1.0f, "MainLoop");
  186. }
  187.  
  188. void MainLoop(string &in asTimer)
  189. {
  190.     VertRotate(rotMul);
  191. }
  192.  
  193. void OnStart()
  194. {
  195.     //Resize 4D vertex array.
  196.     vert4.resize(16);
  197.     for (int i = 0; i < 16; i++) {
  198.         vert4[i].resize(4);
  199.     }
  200.  
  201.     //Resize 3D vertex array.
  202.     vert3.resize(16);
  203.     for (int i = 0; i < 16; i++) {
  204.         vert3[i].resize(3);
  205.     }
  206.    
  207.     //Initiate tesseract.
  208.     TesseractInit();
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement