Advertisement
TNT_Guerrilla

Tesseract Screensaver 3 wide

Aug 22nd, 2023
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.90 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.  
  9. def tesseract_vertices():
  10.     return np.array([
  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.         [ 1,  1,  1,  1],
  27.     ])
  28.  
  29. def tesseract_edges():
  30.     return (
  31.         (0, 1), (0, 2), (0, 4), (0, 8),
  32.         (1, 3), (1, 5), (1, 9),
  33.         (2, 3), (2, 6), (2, 10),
  34.         (3, 7), (3, 11),
  35.         (4, 5), (4, 6), (4, 12),
  36.         (5, 7), (5, 13),
  37.         (6, 7), (6, 14),
  38.         (7, 15),
  39.         (8, 9), (8, 10), (8, 12),
  40.         (9, 11), (9, 13),
  41.         (10, 11), (10, 14),
  42.         (11, 15),
  43.         (12, 13), (12, 14),
  44.         (13, 15),
  45.         (14, 15)
  46.     )
  47.  
  48. def tesseract_faces():
  49.     return [
  50.         (0, 1, 5, 4),
  51.         (2, 3, 7, 6),
  52.         (0, 1, 3, 2),
  53.         (4, 5, 7, 6),
  54.         (0, 2, 6, 4),
  55.         (1, 3, 7, 5),
  56.         (8, 9, 13, 12),
  57.         (10, 11, 15, 14),
  58.         (8, 9, 11, 10),
  59.         (12, 13, 15, 14),
  60.         (8, 10, 14, 12),
  61.         (9, 11, 15, 13),
  62.         (0, 1, 9, 8),
  63.         (2, 3, 11, 10),
  64.         (0, 2, 10, 8),
  65.         (1, 3, 11, 9),
  66.         (4, 5, 13, 12),
  67.         (6, 7, 15, 14),
  68.         (4, 6, 14, 12),
  69.         (5, 7, 15, 13),
  70.     ]
  71.  
  72. def draw_tesseract(vertices, edges, faces, colors):
  73.     glBegin(GL_LINES)
  74.     for edge in edges:
  75.         for vertex in edge:
  76.             glColor3fv(colors[vertex])
  77.             glVertex3fv(vertices[vertex][:3])
  78.     glEnd()
  79.  
  80.     glBegin(GL_POLYGON)
  81.     for face in faces:
  82.         for vertex in face:
  83.             glColor4fv(np.append(colors[vertex], 0.5)) # Set opacity to 50%
  84.             glVertex3fv(vertices[vertex][:3])
  85.     glEnd()
  86.  
  87. def project_4d_to_3d(vertices, w):
  88.     projection_matrix = np.array([
  89.         [1, 0, 0, 0],
  90.         [0, 1, 0, 0],
  91.         [0, 0, 1,  0]
  92.     ])
  93.     return np.dot(vertices, projection_matrix.T)
  94.  
  95.  
  96. def rotation_matrix_4d(angle, axis1, axis2):
  97.     c = np.cos(angle)
  98.     s = np.sin(angle)
  99.     mat = np.identity(4)
  100.     mat[axis1, axis1] = c
  101.     mat[axis1, axis2] = -s
  102.     mat[axis2, axis1] = s
  103.     mat[axis2, axis2] = c
  104.     return mat
  105.  
  106. def main():
  107.     pygame.init()
  108.     display = (5760, 1080)
  109.     pygame.display.set_mode(display, DOUBLEBUF | OPENGL | NOFRAME)
  110.  
  111.     clock = pygame.time.Clock()
  112.     angle = 0
  113.  
  114.     while True:
  115.         for event in pygame.event.get():
  116.             if event.type == pygame.QUIT:
  117.                 pygame.quit()
  118.                 return
  119.             if event.type == pygame.KEYDOWN:
  120.                 pygame.quit()
  121.                 sys.exit()
  122.  
  123.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  124.  
  125.         section_width = display[0] // 3
  126.         section_height = display[1]
  127.  
  128.         colors = np.array([
  129.         [1, 0, 0],
  130.         [0, 1, 0],
  131.         [0, 0, 1],
  132.         [1, 1, 0],
  133.         [1, 0, 1],
  134.         [0, 1, 1],
  135.         [1, 1, 1],
  136.         [0.5, 0.5, 0.5],
  137.         [1, 0.5, 0],
  138.         [0.5, 1, 0],
  139.         [0, 0.5, 1],
  140.         [1, 1, 0.5],
  141.         [1, 0.5, 1],
  142.         [0.5, 1, 1],
  143.         [1, 0.5, 0.5],
  144.         [0.5, 1, 0.5]
  145.         ])
  146.  
  147.         for i in range(3):
  148.             # Set viewport to only draw on one-third of the screen
  149.             glViewport(i * section_width, 0, section_width, section_height)
  150.  
  151.             # Retrieve the current viewport settings
  152.             viewport = glGetIntegerv(GL_VIEWPORT)
  153.             print(f"Viewport for tesseract {i + 1}: {viewport}")
  154.  
  155.             # Camera settings
  156.             glMatrixMode(GL_PROJECTION)
  157.             glLoadIdentity()
  158.             gluPerspective(45, (section_width / section_height), 0.1, 50.0)
  159.             glMatrixMode(GL_MODELVIEW)
  160.             glLoadIdentity()
  161.             glTranslatef(0.0, 0.0, -5)
  162.             print()
  163.  
  164.             vertices_4d = tesseract_vertices()
  165.             edges = tesseract_edges()
  166.             faces = tesseract_faces()
  167.  
  168.             # ... the same rotation and drawing logic as before ...
  169.  
  170.             _rotate_xw = rotation_matrix_4d(angle, 0, 3)
  171.             _rotate_yw = rotation_matrix_4d(angle, 1, 3)
  172.             _rotate_zw = rotation_matrix_4d(angle, 2, 3)
  173.  
  174.             vertices_4d = vertices_4d.dot(_rotate_xw).dot(_rotate_yw).dot(_rotate_zw)
  175.             vertices_3d = project_4d_to_3d(vertices_4d, 2)
  176.  
  177.             glEnable(GL_BLEND)
  178.             glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
  179.             draw_tesseract(vertices_3d, edges, faces, colors)
  180.             glDisable(GL_BLEND)
  181.  
  182.         pygame.display.flip()
  183.         clock.tick(165)
  184.         angle += 0.05 * (60 / 165)
  185.  
  186. if __name__ == "__main__":
  187.     main()
  188.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement