import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
from PIL import Image
import os
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]
]
faces = [
(0,1,2,3),
(4,5,6,7),
(0,1,5,4),
(2,3,7,6),
(0,3,7,4),
(1,2,6,5)
]
uvs = [
[(0,0),(1,0),(1,1),(0,1)],
[(0,0),(1,0),(1,1),(0,1)],
[(0,0),(1,0),(1,1),(0,1)],
[(0,0),(1,0),(1,1),(0,1)],
[(0,0),(1,0),(1,1),(0,1)],
[(0,0),(1,0),(1,1),(0,1)]
]
def load_texture(path):
if not os.path.exists(path):
raise FileNotFoundError(f"Texture file '{path}' not found!")
img = Image.open(path).transpose(Image.FLIP_TOP_BOTTOM)
img_data = img.convert("RGBA").tobytes()
tex_id = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, tex_id)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.width, img.height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, img_data)
return tex_id
def Cube(tex_id):
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, tex_id)
glBegin(GL_QUADS)
for face_index, face in enumerate(faces):
for i, vertex in enumerate(face):
glTexCoord2fv(uvs[face_index][i])
glVertex3fv(vertices[vertex])
glEnd()
glDisable(GL_TEXTURE_2D)
def draw_axes():
glLineWidth(3)
glBegin(GL_LINES)
glColor3f(1,0,0)
glVertex3f(0,0,0)
glVertex3f(3,0,0)
Y axis in green
glColor3f(0,1,0)
glVertex3f(0,0,0)
glVertex3f(0,3,0)
Z axis in blue
glColor3f(0,0,1)
glVertex3f(0,0,0)
glVertex3f(0,0,3)
glEnd()
def main():
pygame.init()
display = (800,600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
glEnable(GL_DEPTH_TEST)
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(0.0, 0.0, -10)
cube_pos = [0,0,0]
dragging_cube = False
dragging_camera = False
last_mouse = (0,0)
Load JPEG texture
texture = load_texture("download.jpg") # This is where you would put your image texture path i think
clock = pygame.time.Clock()
running = True
while running:
dt = clock.tick()/1000
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
LMB drag for cube
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
dragging_cube = True
last_mouse = event.pos
elif event.button == 3: # RMB drag for camera
dragging_camera = True
last_mouse = event.pos
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
dragging_cube = False
elif event.button == 3:
dragging_camera = False
elif event.type == pygame.MOUSEMOTION:
dx = event.pos[0] - last_mouse[0]
dy = event.pos[1] - last_mouse[1]
if dragging_cube:
cube_pos[0] += dx * 0.01
cube_pos[1] -= dy * 0.01
elif dragging_camera:
Move camera by translating cube_pos opposite to mouse movement
cube_pos[0] -= dx * 0.01
cube_pos[1] += dy * 0.01
last_mouse = event.pos
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glPushMatrix()
glTranslatef(cube_pos[0], cube_pos[1], cube_pos[2])
Cube(texture)
draw_axes()
glPopMatrix()
pygame.display.flip()
pygame.quit()
if _name_ == "__main__":
main()
Информация по комментариям в разработке