Back to posts Edit this post
Copy content

19 Mar 14:35

import pygame from Mesh3D import * # Importujemy naszą klasę Mesh3D z pliku Mesh3D.py from pygame.locals import * from OpenGL.GL import * from OpenGL.GLU import * from Object import * from Transform import * pygame.init() # Inicjujemy moduły Pygame screen_width = 500 screen_height = 500 # Tworzymy okno 500×500 z trybem OpenGL i podwójnym buforowaniem screen = pygame.display.set_mode((screen_width, screen_height), DOUBLEBUF|OPENGL) pygame.display.set_caption('OpenGL in Python') done = False white = pygame.Color(255, 255, 255) glLightfv(GL_LIGHT0, GL_AMBIENT, (1, 0, 1, 1)) # np. fioletowe ambient glLightfv(GL_LIGHT0, GL_DIFFUSE, (1, 1, 0, 1)) # np. żółte diffuse glLightfv(GL_LIGHT0, GL_SPECULAR, (0, 1, 0, 1)) # np. zielone specular # Tworzymy obiekt typu Object i dodajemy komponent siatki sześcianu glEnable(GL_DEPTH_TEST) # włącz test głębokości (z-buffer) cube = Object("Cube") # utworzenie obiektu o nazwie "Cube" cube.add_component(Transform((0, 0, -1))) # dodanie komponentu Transform z pozycją (0,0,-1) cube.add_component(Cube(GL_POLYGON, filename = "/home/lab/Downloads/dirt_block.png")) # dodanie komponentu mesh (sześcian z teksturą) cube2 = Object("Cube2") cube2.add_component(Transform((1, 0, 1))) cube2.add_component(Cube(GL_POLYGON, filename = "/home/lab/Downloads/diax.png")) glEnable(GL_LIGHTING) # włączenie obsługi oświetlenia gluPerspective(60, screen_width / screen_height, 0.1, 100.0) #glOrtho(-1, 1, 1, -1, 0.1, 50.0) glTranslatef(0.0, 0.0, -3.5) glMatrixMode(GL_PROJECTION) glMatrixMode(GL_MODELVIEW) glEnable(GL_LIGHT0) glMaterialfv(GL_FRONT, GL_DIFFUSE, (0, 1, 1, 0.5)) lum = 1 clock = pygame.time.Clock() fps = 100 # Pętla główna aplikacji while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True # Wyjście z pętli, gdy zamykamy okno # Czyścimy bufor koloru i bufor głębi glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) cube2.update() # glRotatef(6, 1, 1, 1) cube.update() # Wywołujemy metodę update() obiektu, która rysuje wszystkie komponenty glScale(2, 2, 2) glScale(1/2, 1/2, 1/2) # pygame.time.wait(100) glMaterialfv(GL_FRONT, GL_DIFFUSE, (1, 0, 0, lum)) lum += 0.2 lum %= 1 pygame.display.flip() # Prezentujemy nowo narysowaną klatkę print(f"tick={clock.tick(fps)}, fps={clock.get_fps()}") clock.tick(fps) pygame.quit() # Sprzątanie i zakończenie programu

No files