TNT_Guerrilla

Tesseract Screensaver Dynamic

Aug 22nd, 2023
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.51 KB | Gaming | 0 0
  1. import os
  2. from enum import IntEnum
  3.  
  4. import numpy as np
  5. import pygame
  6. from OpenGL.GL import *
  7. from OpenGL.GLU import *
  8. from pygame.locals import *
  9.  
  10.  
  11. os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (-1920, 0)
  12.  
  13.  
  14. class Axis(IntEnum):
  15.     X = 0
  16.     Y = 1
  17.     Z = 2
  18.     W = 3
  19.  
  20.  
  21. class Tesseract:
  22.     def __init__(self, vertices: np.array, edges: list, faces: list, colors: np.array):
  23.         self.vertices = vertices
  24.         self.edges = edges
  25.         self.faces = faces
  26.         self.colors = colors
  27.         self.angle = 0
  28.  
  29.         self.vertices_4d = vertices
  30.         self.vertices_3d = None
  31.  
  32.     def _4d_rotation(self, axis: int, other_axis: int) -> np.array:
  33.         c = np.cos(self.angle)
  34.         s = np.sin(self.angle)
  35.         matrix = np.identity(4)
  36.         matrix[axis, axis] = c
  37.         matrix[axis, other_axis] = -s
  38.         matrix[other_axis, axis] = s
  39.         matrix[other_axis, other_axis] = c
  40.         return matrix
  41.  
  42.     def rotate(self) -> None:
  43.         xw = self._4d_rotation(Axis.X, Axis.W)
  44.         yw = self._4d_rotation(Axis.Y, Axis.W)
  45.         zw = self._4d_rotation(Axis.Z, Axis.W)
  46.         rotation_matrices = xw @ yw @ zw
  47.  
  48.         self.vertices_4d = self.vertices @ rotation_matrices
  49.  
  50.     def project_to_3d(self, _w) -> None:
  51.         projection_matrix = np.array(
  52.                 [
  53.                         [1, 0, 0, 0],
  54.                         [0, 1, 0, 0],
  55.                         [0, 0, 1, 0]
  56.                 ]
  57.         )
  58.         self.vertices_3d = np.dot(self.vertices_4d, projection_matrix.T)
  59.  
  60.     def update(self) -> None:
  61.         self.rotate()
  62.         self.project_to_3d(2)
  63.  
  64.         self.angle += 0.01 * (60 / 165)
  65.  
  66.     def _draw_edges(self) -> None:
  67.         glBegin(GL_LINES)
  68.         for edge in self.edges:
  69.             for vertex in edge:
  70.                 glColor3fv(self.colors[vertex])
  71.                 glVertex3fv(self.vertices_3d[vertex][:3])
  72.         glEnd()
  73.  
  74.     def _draw_faces(self, opacity: float = 0.5) -> None:
  75.         glBegin(GL_POLYGON)
  76.         for face in self.faces:
  77.             for vertex in face:
  78.                 glColor4fv(np.append(self.colors[vertex], opacity))
  79.                 glVertex3fv(self.vertices_3d[vertex][:3])
  80.         glEnd()
  81.  
  82.     def draw(self) -> None:
  83.         self._draw_edges()
  84.         self._draw_faces()
  85.  
  86.  
  87. class Monitors:
  88.     def __init__(self, sizes: list[(int, int)]):
  89.         self.displays = [pygame.Rect((0, 0), (width, height)) for width, height in sizes]
  90.         self.count = len(self.displays)
  91.  
  92.     @property
  93.     def max_width(self) -> int:
  94.         return sum([display.width for display in self.displays])
  95.  
  96.     @property
  97.     def max_height(self) -> int:
  98.         return max([display.height for display in self.displays])
  99.  
  100.     @property
  101.     def max_size(self) -> tuple[int, int]:
  102.         return self.max_width, self.max_height
  103.  
  104.     def total_width_to(self, index) -> int:
  105.         return sum([display.width for display in self.displays[:index]])
  106.  
  107.  
  108. tesseract_data = {
  109.         "beautiful": {
  110.                 "vertices": np.array(
  111.                         [
  112.                                 [-1, -1, -1, -1],
  113.                                 [-1, -1, -1, 1],
  114.                                 [-1, -1, 1, -1],
  115.                                 [-1, -1, 1, 1],
  116.                                 [-1, 1, -1, -1],
  117.                                 [-1, 1, -1, 1],
  118.                                 [-1, 1, 1, -1],
  119.                                 [-1, 1, 1, 1],
  120.                                 [1, -1, -1, -1],
  121.                                 [1, -1, -1, 1],
  122.                                 [1, -1, 1, -1],
  123.                                 [1, -1, 1, 1],
  124.                                 [1, 1, -1, -1],
  125.                                 [1, 1, -1, 1],
  126.                                 [1, 1, 1, -1],
  127.                                 [1, 1, 1, 1],
  128.                         ]
  129.                 ),
  130.                 "edges": [
  131.                         (0, 1), (0, 2), (0, 4), (0, 8),
  132.                         (1, 3), (1, 5), (1, 9),
  133.                         (2, 3), (2, 6), (2, 10),
  134.                         (3, 7), (3, 11),
  135.                         (4, 5), (4, 6), (4, 12),
  136.                         (5, 7), (5, 13),
  137.                         (6, 7), (6, 14),
  138.                         (7, 15),
  139.                         (8, 9), (8, 10), (8, 12),
  140.                         (9, 11), (9, 13),
  141.                         (10, 11), (10, 14),
  142.                         (11, 15),
  143.                         (12, 13), (12, 14),
  144.                         (13, 15),
  145.                         (14, 15)
  146.                 ],
  147.                 "faces": [
  148.                         (0, 1, 5, 4),
  149.                         (2, 3, 7, 6),
  150.                         (0, 1, 3, 2),
  151.                         (4, 5, 7, 6),
  152.                         (0, 2, 6, 4),
  153.                         (1, 3, 7, 5),
  154.                         (8, 9, 13, 12),
  155.                         (10, 11, 15, 14),
  156.                         (8, 9, 11, 10),
  157.                         (12, 13, 15, 14),
  158.                         (8, 10, 14, 12),
  159.                         (9, 11, 15, 13),
  160.                         (0, 1, 9, 8),
  161.                         (2, 3, 11, 10),
  162.                         (0, 2, 10, 8),
  163.                         (1, 3, 11, 9),
  164.                         (4, 5, 13, 12),
  165.                         (6, 7, 15, 14),
  166.                         (4, 6, 14, 12),
  167.                         (5, 7, 15, 13),
  168.                 ],
  169.                 "colors": np.array(
  170.                         [
  171.                                 [1, 0, 0],
  172.                                 [0, 1, 0],
  173.                                 [0, 0, 1],
  174.                                 [1, 1, 0],
  175.                                 [1, 0, 1],
  176.                                 [0, 1, 1],
  177.                                 [1, 1, 1],
  178.                                 [0.5, 0.5, 0.5],
  179.                                 [1, 0.5, 0],
  180.                                 [0.5, 1, 0],
  181.                                 [0, 0.5, 1],
  182.                                 [1, 1, 0.5],
  183.                                 [1, 0.5, 1],
  184.                                 [0.5, 1, 1],
  185.                                 [1, 0.5, 0.5],
  186.                                 [0.5, 1, 0.5]
  187.                         ]
  188.                 )
  189.         }
  190. }
  191.  
  192.  
  193. def main():
  194.     pygame.init()
  195.  
  196.     monitors = Monitors(pygame.display.get_desktop_sizes())
  197.     pygame.display.set_mode(monitors.max_size, DOUBLEBUF | OPENGL | NOFRAME)
  198.     clock = pygame.time.Clock()
  199.     running = True
  200.  
  201.     tesseract = Tesseract(**tesseract_data["beautiful"])
  202.  
  203.     while running:
  204.         for event in pygame.event.get():
  205.             if event.type == pygame.QUIT or event.type == pygame.KEYDOWN:
  206.                 running = False
  207.  
  208.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  209.  
  210.         for index, display in enumerate(monitors.displays):
  211.             glViewport(monitors.total_width_to(index), 0, display.width, display.height)
  212.             glGetIntegerv(GL_VIEWPORT)
  213.  
  214.             glMatrixMode(GL_PROJECTION)
  215.             glLoadIdentity()
  216.             gluPerspective(55, (display.width / display.height), 0.1, 50.0)
  217.             glMatrixMode(GL_MODELVIEW)
  218.             glLoadIdentity()
  219.             glTranslatef(0.0, 0.3, -5)
  220.  
  221.             glRotatef(1, 3, 1, 1)
  222.  
  223.             glEnable(GL_BLEND)
  224.             glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
  225.  
  226.             tesseract.update()
  227.             tesseract.draw()
  228.  
  229.             glDisable(GL_BLEND)
  230.  
  231.         pygame.display.flip()
  232.         clock.tick(165)
  233.  
  234.     pygame.quit()
  235.  
  236.  
  237. if __name__ == "__main__":
  238.     main()
  239.  
Advertisement
Add Comment
Please, Sign In to add comment