Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
5.3 kB
2
Indexable
Never
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
from PIL import Image
import pygame
from pygame.locals import *
import sys

mouse_pressed = False
start_x, start_y = 0, 0
rotation_x = 0
rotation_y = 0

# Положение фигур по оси Y
cone_y = 0.0
cube_y = 0.0
cylinder_y = 0.0

light_position = (-10.0, 10.0, 10.0, 1.0)  # Положение источника света (x, y, z, w)
light_diffuse = (1.0, 1.0, 1.0, 1.0)     # Цвет света (R, G, B, A)

texture_id = None

def load_texture(filename):
    img = Image.open(filename)
    img_data = img.tobytes()
    width, height = img.size

    global texture_id
    texture_id = glGenTextures(1)  # Генерируем идентификатор текстурного объекта
    glBindTexture(GL_TEXTURE_2D, texture_id)  # Привязываем текстурный объект
    glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
    glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
    glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
    glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, img_data)

def draw_cube():
    glPushMatrix()
    glTranslatef(0, cube_y, 0)

    glScalef(2.0, 2.0, 2.0)

    glEnable(GL_TEXTURE_2D)
    glBindTexture(GL_TEXTURE_2D, texture_id)  # Привязываем текстурный объект

    set_material()  # Установка свойств материала для куба

    glBegin(GL_QUADS)

    # Грань 1
    glTexCoord2f(0.0, 0.0)
    glVertex3f(-1, -1, -1)
    glTexCoord2f(1.0, 0.0)
    glVertex3f(1, -1, -1)
    glTexCoord2f(1.0, 1.0)
    glVertex3f(1, 1, -1)
    glTexCoord2f(0.0, 1.0)
    glVertex3f(-1, 1, -1)

    # Грань 2
    glTexCoord2f(0.0, 0.0)
    glVertex3f(1, -1, -1)
    glTexCoord2f(1.0, 0.0)
    glVertex3f(1, -1, 1)
    glTexCoord2f(1.0, 1.0)
    glVertex3f(1, 1, 1)
    glTexCoord2f(0.0, 1.0)
    glVertex3f(1, 1, -1)

    # Грань 3
    glTexCoord2f(0.0, 0.0)
    glVertex3f(1, -1, 1)
    glTexCoord2f(1.0, 0.0)
    glVertex3f(-1, -1, 1)
    glTexCoord2f(1.0, 1.0)
    glVertex3f(-1, 1, 1)
    glTexCoord2f(0.0, 1.0)
    glVertex3f(1, 1, 1)

    # Грань 4
    glTexCoord2f(0.0, 0.0)
    glVertex3f(-1, -1, 1)
    glTexCoord2f(1.0, 0.0)
    glVertex3f(-1, -1, -1)
    glTexCoord2f(1.0, 1.0)
    glVertex3f(-1, 1, -1)
    glTexCoord2f(0.0, 1.0)
    glVertex3f(-1, 1, 1)

    # Грань 5
    glTexCoord2f(0.0, 0.0)
    glVertex3f(-1, 1, -1)
    glTexCoord2f(1.0, 0.0)
    glVertex3f(1, 1, -1)
    glTexCoord2f(1.0, 1.0)
    glVertex3f(1, 1, 1)
    glTexCoord2f(0.0, 1.0)
    glVertex3f(-1, 1, 1)

    # Грань 6
    glTexCoord2f(0.0, 0.0)
    glVertex3f(-1, -1, 1)
    glTexCoord2f(1.0, 0.0)
    glVertex3f(1, -1, 1)
    glTexCoord2f(1.0, 1.0)
    glVertex3f(1, -1, -1)
    glTexCoord2f(0.0, 1.0)
    glVertex3f(-1, -1, -1)

    glEnd()

    glDisable(GL_TEXTURE_2D)

    qwglPopMatrix()

def set_material():
    mat_ambient = (0.2, 0.2, 0.2, 1.0)
    mat_diffuse = (0.8, 0.8, 0.8, 1.0)
    mat_specular = (0.0, 0.0, 0.0, 1.0)
    mat_shininess = 0.0  # Матовое покрытие

    glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient)
    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse)
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular)
    glMaterialf(GL_FRONT, GL_SHININESS, mat_shininess)

def disp():
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glViewport(0, 0, 800, 800)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluPerspective(45, 1, 0.1, 50.0)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        glTranslatef(0, 0, -10)

def main():
    global mouse_pressed, rotation_x, rotation_y
    pygame.init()
    display = (800, 800)
    pygame.display.set_mode(display, pygame.DOUBLEBUF | pygame.OPENGL)
    
    pygame.mouse.set_visible(True)
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            elif event.type == MOUSEBUTTONDOWN:
                if event.button == 1:
                    mouse_pressed = True
                    start_x, start_y = event.pos
            elif event.type == MOUSEBUTTONUP:
                if event.button == 1:
                    mouse_pressed = False
            elif event.type == MOUSEMOTION:
                if mouse_pressed:
                    delta_x = event.pos[0] - start_x
                    delta_y = event.pos[1] - start_y
                    rotation_x += delta_y * 0.5
                    rotation_y += delta_x * 0.5
                    start_x, start_y = event.pos
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glViewport(0, 0, 800, 800)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluPerspective(45, 1, 0.1, 50.0)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        glTranslatef(0, 0, -10)
        glRotatef(rotation_x, 1, 0, 0)
        glRotatef(rotation_y, 0, 1, 0)
        load_texture("/home/bersiv/Labs/CG/123.jpg")
        #disp()
        draw_cube()

        pygame.display.flip()
        pygame.time.wait(10)

main()