0% found this document useful (0 votes)
202 views2 pages

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
202 views2 pages

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

import pygame

from [Link] import *


from [Link] import *
from [Link] 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():
[Link]()
display = (800, 600)
[Link].set_mode(display, DOUBLEBUF | OPENGL)
[Link].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 [Link]():
if [Link] == [Link]:
[Link]()
quit()
if [Link] == [Link]:
if [Link] == pygame.K_UP:
rotate_x += 5
if [Link] == pygame.K_DOWN:
rotate_x -= 5
if [Link] == pygame.K_LEFT:
rotate_y -= 5
if [Link] == pygame.K_RIGHT:
rotate_y += 5
if [Link] == pygame.K_a:
rotate_z -= 5
if [Link] == pygame.K_d:
rotate_z += 5
if [Link] == pygame.K_w:
scale += 0.1
if [Link] == pygame.K_s:
scale -= 0.1
if [Link] == 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()

[Link]()
[Link](15)

main()

You might also like