0% found this document useful (0 votes)
2 views

TP04 (1).py

This document contains a Python script that uses Pygame and OpenGL to create a 3D diamond shape. It allows for user interaction to rotate and scale the diamond using keyboard inputs. The script sets up the OpenGL environment, defines the diamond's vertices, faces, and colors, and continuously renders the diamond in a loop.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

TP04 (1).py

This document contains a Python script that uses Pygame and OpenGL to create a 3D diamond shape. It allows for user interaction to rotate and scale the diamond using keyboard inputs. The script sets up the OpenGL environment, defines the diamond's vertices, faces, and colors, and continuously renders the diamond in a loop.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

import pygame

from pygame.locals import *


from OpenGL.GL import *
from OpenGL.GLU import *

vertices = [
(0.0, 1.0, 0.0),
(-0.5, 0.0, 0.5),
(0.5, 0.0, 0.5),
(0.5, 0.0, -0.5),
(-0.5, 0.0, -0.5),
(0.0, -1.0, 0.0)
]

faces = [
(0, 1, 2),
(0, 2, 3),
(0, 3, 4),
(0, 4, 1),
(5, 1, 2),
(5, 2, 3),
(5, 3, 4),
(5, 4, 1)
]

colors = [
(1.0, 0.0, 0.0), # Red
(1.0, 1.0, 0.0), # Yellow
(0.0, 1.0, 0.0), # Green
(0.0, 1.0, 1.0), # Cyan
(0.0, 0.0, 1.0), # Blue
(1.0, 0.0, 1.0), # Magenta ]

def draw_diamond():
glBegin(GL_TRIANGLES)
for i, face in enumerate(faces):
glColor3fv(colors[i % len(colors)])
for vertex in face:
glVertex3fv(vertices[vertex])
glEnd()

def main():
pygame.init()
display = (800, 600)
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
pygame.display.set_caption("3D Diamond with Transformations")

gluPerspective(45, (display[0] / display[1]), 0.1, 50.0)


glTranslatef(0.0, 0.0, -5)

rotate_x, rotate_y, rotate_z = 0, 0, 0


scale = 1.0

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
rotate_x += 5
if event.key == pygame.K_DOWN:
rotate_x -= 5
if event.key == pygame.K_LEFT:
rotate_y -= 5
if event.key == pygame.K_RIGHT:
rotate_y += 5
if event.key == pygame.K_a:
rotate_z -= 5
if event.key == pygame.K_d:
rotate_z += 5
if event.key == pygame.K_w:
scale += 0.1
if event.key == pygame.K_s:
scale -= 0.1
if event.key == pygame.K_r:

rotate_x, rotate_y, rotate_z = 0, 0, 0


scale = 1.0

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glPushMatrix()

glScalef(scale, scale, scale)


glRotatef(rotate_x, 1, 0, 0)
glRotatef(rotate_y, 0, 1, 0)
glRotatef(rotate_z, 0, 0, 1)

draw_diamond()
glPopMatrix()

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

main()

You might also like