Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //16x4 array for tesseract vertices.
- float[][] vert4;
- //16x3 array for projected vertices.
- float[][] vert3;
- //Index array for edges.
- int[] edges(64);
- //Rotation multiplier.
- float rotMul = 0.4f;
- //4D influence multiplier.
- float wScale = 2.0f;
- //Create tesseract geometry.
- void TesseractInit()
- {
- //Initialize vertices.
- int vertIdx = 0;
- for (int x = -1; x <= 1; x += 2) {
- for (int y = -1; y <= 1; y += 2) {
- for (int z = -1; z <= 1; z += 2) {
- for (int w = -1; w <= 1; w += 2) {
- vert4[vertIdx][0] = x;
- vert4[vertIdx][1] = y;
- vert4[vertIdx][2] = z;
- vert4[vertIdx][3] = w;
- vertIdx++;
- }
- }
- }
- }
- //Initialize edges.
- int edgeIdx = 0;
- for (int i = 0; i < 16; i++) {
- for (int j = i + 1; j < 16; j++) {
- if (i != j) {
- int diffCount = 0;
- for (int k = 0; k < 4; k++) {
- if (vert4[i][k] != vert4[j][k]) {
- diffCount++;
- }
- }
- if (diffCount == 1) {
- edges[edgeIdx] = i;
- edgeIdx++;
- edges[edgeIdx] = j;
- edgeIdx++;
- }
- }
- }
- }
- GetDupe();
- }
- //Track usage of vertex duplicates.
- bool[][] vertDupe;
- //Create final edge indices.
- int[] finalIdx1(64);
- int[] finalIdx2(64);
- void GetDupe()
- {
- //Initialize tracking arrays.
- vertDupe.resize(16);
- for (int i = 0; i < 16; i++) {
- vertDupe[i].resize(4);
- for (int j = 0; j < 4; j++) {
- vertDupe[i][j] = false;
- }
- }
- //Define edges.
- for (int i = 0; i < 32; i++) {
- int vertIdx1 = edges[i * 2];
- int vertIdx2 = edges[i * 2 + 1];
- //Find the first unused duplicate for each vertex.
- int dupeIdx1 = TrackDupe(vertIdx1);
- int dupeIdx2 = TrackDupe(vertIdx2);
- finalIdx1[i] = vertIdx1 * 4 + dupeIdx1;
- finalIdx2[i + 1] = vertIdx2 * 4 + dupeIdx2;
- }
- VertRotate(rotMul);
- }
- //Find the first unused duplicate for a vertex.
- int TrackDupe(int vertIdx) {
- for (int i = 0; i < 4; i++) {
- if (!vertDupe[vertIdx][i]) {
- vertDupe[vertIdx][i] = true; //Mark as used.
- return i;
- }
- }
- return 0;
- }
- //Rotation planes.
- int rotPlane1 = 2;
- int rotPlane2 = 3;
- void VertRotate(float rotMul)
- {
- float cosAngle = MathCos(rotMul);
- float sinAngle = MathSin(rotMul);
- for (int i = 0; i < 16; i++) {
- ApplyRotationInPlane(rotPlane1, i, cosAngle, sinAngle);
- ApplyRotationInPlane(rotPlane2, i, cosAngle, sinAngle);
- }
- VertProject(wScale);
- }
- void ApplyRotationInPlane(int rotPlane, int vertIdx, float cosAngle, float sinAngle)
- {
- float x = vert4[vertIdx][0];
- float y = vert4[vertIdx][1];
- float z = vert4[vertIdx][2];
- float w = vert4[vertIdx][3];
- //Apply rotation based on the plane.
- switch (rotPlane) {
- case 0: //XY plane.
- vert4[vertIdx][0] = cosAngle * x - sinAngle * y;
- vert4[vertIdx][1] = sinAngle * x + cosAngle * y;
- break;
- case 1: //XZ Plane
- vert4[vertIdx][0] = cosAngle * x - sinAngle * z;
- vert4[vertIdx][2] = sinAngle * x + cosAngle * z;
- break;
- case 2: //XW Plane
- vert4[vertIdx][0] = cosAngle * x - sinAngle * w;
- vert4[vertIdx][3] = sinAngle * x + cosAngle * w;
- break;
- case 3: //YZ Plane
- vert4[vertIdx][1] = cosAngle * y - sinAngle * z;
- vert4[vertIdx][2] = sinAngle * y + cosAngle * z;
- break;
- case 4: //YW Plane
- vert4[vertIdx][1] = cosAngle * y - sinAngle * w;
- vert4[vertIdx][3] = sinAngle * y + cosAngle * w;
- break;
- case 5: //ZW Plane
- vert4[vertIdx][2] = cosAngle * z - sinAngle * w;
- vert4[vertIdx][3] = sinAngle * z + cosAngle * w;
- break;
- }
- }
- //Project the 4D vertices to 3D space.
- void VertProject(float wScale)
- {
- for (int i = 0; i < 16; i++) {
- //Perspective factor based on the 'w' component.
- float wMul = 1.0f / (wScale - vert4[i][3]);
- //Apply the perspective transformation.
- vert3[i][0] = vert4[i][0] * wMul;
- vert3[i][1] = vert4[i][1] * wMul;
- vert3[i][2] = vert4[i][2] * wMul;
- }
- TesseractRender();
- }
- //Render the tesseract.
- void TesseractRender()
- {
- //Place vertices.
- for (int i = 0; i < 64; i++) {
- DetachFromStickyArea("vertex_" + i);
- int origIdx = i / 4; //Each original vertex has 4 duplicates.
- SetEntityPos("vertex_" + i, vert3[origIdx][0], vert3[origIdx][1], vert3[origIdx][2]);
- }
- //Place edges.
- for (int i = 0; i < 32; i++) {
- AttachBodyToStickyArea("vertex_" + finalIdx1[i], "edge_" + i + "_Body_1");
- AttachBodyToStickyArea("vertex_" + finalIdx2[i + 1], "edge_" + i + "_Body_2");
- }
- AddTimer("MAIN", 1.0f, "MainLoop");
- }
- void MainLoop(string &in asTimer)
- {
- VertRotate(rotMul);
- }
- void OnStart()
- {
- //Resize 4D vertex array.
- vert4.resize(16);
- for (int i = 0; i < 16; i++) {
- vert4[i].resize(4);
- }
- //Resize 3D vertex array.
- vert3.resize(16);
- for (int i = 0; i < 16; i++) {
- vert3[i].resize(3);
- }
- //Initiate tesseract.
- TesseractInit();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement