Untitled
unknown
plain_text
a month ago
2.7 kB
2
Indexable
Never
from OpenGL.GL import * from OpenGL.GLUT import * from OpenGL.GLU import import pygame from pygame.locals import * # Инициализация Pygame pygame.init() display = (800, 600) pygame.display.set_mode(display, DOUBLEBUF | OPENGL) # Настройка матрицы проекции и вида gluPerspective(45, (display[0] / display[1]), 0.1, 50.0) glTranslatef(0.0, 0.0, -5) # Загрузка текстуры texture_surface = pygame.image.load('cube_texture.jpg') texture_data = pygame.image.tostring(texture_surface, "RGB", 1) width, height = texture_surface.get_size() texture_id = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texture_id) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, texture_data) # Определение вершин и текстурных координат для куба vertices = ( (1, -1, -1), (1, 1, -1), (-1, 1, -1), (-1, -1, -1), (1, -1, 1), (1, 1, 1), (-1, -1, 1), (-1, 1, 1) ) edges = ( (0, 1), (1, 2), (2, 3), (3, 0), (4, 5), (5, 6), (6, 7), (7, 4), (0, 4), (1, 5), (2, 6), (3, 7) ) surfaces = ( (0, 1, 2, 3), (3, 2, 6, 7), (7, 6, 5, 4), (4, 5, 1, 0), (1, 5, 6, 2), (4, 0, 3, 7) ) tex_coords = ( (1, 0), (1, 1), (0, 1), (0, 0) ) # Функция для рисования текстурированного куба def draw_textured_cube(): glBegin(GL_QUADS) for surface in surfaces: for vertex in surface: glTexCoord2fv(tex_coords[vertex]) glVertex3fv(vertices[vertex]) glEnd() glBegin(GL_LINES) for edge in edges: for vertex in edge: glVertex3fv(vertices[vertex]) glEnd() # Основной цикл приложения while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() # Управление камерой с помощью мыши if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 4: glTranslatef(0, 0, 1.0) if event.button == 5: glTranslatef(0, 0, -1.0) # Очистка буфера glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Нарисовать текстурированный куб glBindTexture(GL_TEXTURE_2D, texture_id) draw_textured_cube() # Завершение рендеринга pygame.display.flip() pygame.time.wait(10)