Advertisement
TNT_Guerrilla

Tesseract Screensaver

Aug 22nd, 2023
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.26 KB | Gaming | 0 0
  1. import pygame
  2. from pygame.locals import *
  3. from OpenGL.GL import *
  4. from OpenGL.GLU import *
  5. from OpenGL.GLUT import *
  6. import numpy as np
  7.  
  8. def tesseract_vertices():
  9.     return np.array([
  10.         [-1, -1, -1, -1],
  11.         [-1, -1, -1,  1],
  12.         [-1, -1,  1, -1],
  13.         [-1, -1,  1,  1],
  14.         [-1,  1, -1, -1],
  15.         [-1,  1, -1,  1],
  16.         [-1,  1,  1, -1],
  17.         [-1,  1,  1,  1],
  18.         [ 1, -1, -1, -1],
  19.         [ 1, -1, -1,  1],
  20.         [ 1, -1,  1, -1],
  21.         [ 1, -1,  1,  1],
  22.         [ 1,  1, -1, -1],
  23.         [ 1,  1, -1,  1],
  24.         [ 1,  1,  1, -1],
  25.         [ 1,  1,  1,  1],
  26.     ])
  27.  
  28. def tesseract_edges():
  29.     return (
  30.         (0, 1), (0, 2), (0, 4), (0, 8),
  31.         (1, 3), (1, 5), (1, 9),
  32.         (2, 3), (2, 6), (2, 10),
  33.         (3, 7), (3, 11),
  34.         (4, 5), (4, 6), (4, 12),
  35.         (5, 7), (5, 13),
  36.         (6, 7), (6, 14),
  37.         (7, 15),
  38.         (8, 9), (8, 10), (8, 12),
  39.         (9, 11), (9, 13),
  40.         (10, 11), (10, 14),
  41.         (11, 15),
  42.         (12, 13), (12, 14),
  43.         (13, 15),
  44.         (14, 15)
  45.     )
  46.  
  47. def tesseract_faces():
  48.     return [
  49.         (0, 1, 5, 4),
  50.         (2, 3, 7, 6),
  51.         (0, 1, 3, 2),
  52.         (4, 5, 7, 6),
  53.         (0, 2, 6, 4),
  54.         (1, 3, 7, 5),
  55.         (8, 9, 13, 12),
  56.         (10, 11, 15, 14),
  57.         (8, 9, 11, 10),
  58.         (12, 13, 15, 14),
  59.         (8, 10, 14, 12),
  60.         (9, 11, 15, 13),
  61.         (0, 1, 9, 8),
  62.         (2, 3, 11, 10),
  63.         (0, 2, 10, 8),
  64.         (1, 3, 11, 9),
  65.         (4, 5, 13, 12),
  66.         (6, 7, 15, 14),
  67.         (4, 6, 14, 12),
  68.         (5, 7, 15, 13),
  69.     ]
  70.  
  71. def draw_tesseract(vertices, edges, faces, colors):
  72.     glBegin(GL_LINES)
  73.     for edge in edges:
  74.         for vertex in edge:
  75.             glColor3fv(colors[vertex])
  76.             glVertex3fv(vertices[vertex][:3])
  77.     glEnd()
  78.  
  79.     glBegin(GL_POLYGON)
  80.     for face in faces:
  81.         for vertex in face:
  82.             glColor4fv(np.append(colors[vertex], 0.5)) # Set opacity to 50%
  83.             glVertex3fv(vertices[vertex][:3])
  84.     glEnd()
  85.  
  86. def project_4d_to_3d(vertices, w):
  87.     projection_matrix = np.array([
  88.         [1, 0, 0, 0],
  89.         [0, 1, 0, 0],
  90.         [0, 0, 1,  0]
  91.     ])
  92.     return np.dot(vertices, projection_matrix.T)
  93.  
  94.  
  95. def rotation_matrix_4d(angle, axis1, axis2):
  96.     c = np.cos(angle)
  97.     s = np.sin(angle)
  98.     mat = np.identity(4)
  99.     mat[axis1, axis1] = c
  100.     mat[axis1, axis2] = -s
  101.     mat[axis2, axis1] = s
  102.     mat[axis2, axis2] = c
  103.     return mat
  104.  
  105. def main():
  106.     pygame.init()
  107.     display = (1920, 1080)
  108.     pygame.display.set_mode(display, DOUBLEBUF | OPENGL | NOFRAME)
  109.  
  110.     gluPerspective(45, (display[0] / display[1]), 0.1, 50.0)
  111.     glTranslatef(0.0, 0.0, -5)
  112.  
  113.     clock = pygame.time.Clock()
  114.     angle = 0
  115.  
  116.     while True:
  117.         for event in pygame.event.get():
  118.             if event.type == pygame.QUIT:
  119.                 pygame.quit()
  120.                 return
  121.             if event.type == pygame.KEYDOWN:
  122.                 pygame.quit()
  123.                 sys.exit()
  124.  
  125.         glRotatef(1, 3, 1, 1)
  126.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  127.  
  128.         vertices_4d = tesseract_vertices()
  129.  
  130.         colors = np.array([
  131.             [1, 0, 0],
  132.             [0, 1, 0],
  133.             [0, 0, 1],
  134.             [1, 1, 0],
  135.             [1, 0, 1],
  136.             [0, 1, 1],
  137.             [1, 1, 1],
  138.             [0.5, 0.5, 0.5],
  139.             [1, 0.5, 0],
  140.             [0.5, 1, 0],
  141.             [0, 0.5, 1],
  142.             [1, 1, 0.5],
  143.             [1, 0.5, 1],
  144.             [0.5, 1, 1],
  145.             [1, 0.5, 0.5],
  146.             [0.5, 1, 0.5]
  147.         ])
  148.  
  149.         _rotate_xw = rotation_matrix_4d(angle, 0, 3)
  150.         _rotate_yw = rotation_matrix_4d(angle, 1, 3)
  151.         _rotate_zw = rotation_matrix_4d(angle, 2, 3)
  152.  
  153.         vertices_4d = vertices_4d.dot(_rotate_xw).dot(_rotate_yw).dot(_rotate_zw)
  154.  
  155.         vertices_3d = project_4d_to_3d(vertices_4d, 2)
  156.         edges = tesseract_edges()
  157.         faces = tesseract_faces()
  158.  
  159.         glEnable(GL_BLEND) # Enable blending
  160.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) # Enable transparency
  161.  
  162.         draw_tesseract(vertices_3d, edges, faces, colors)
  163.  
  164.         pygame.display.flip()
  165.         clock.tick(165)
  166.         angle += 0.01 * (60 / 165)
  167.  
  168. if __name__ == "__main__":
  169.     main()
  170.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement