TP04 (1).py
TP04 (1).py
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")
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:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glPushMatrix()
draw_diamond()
glPopMatrix()
pygame.display.flip()
pygame.time.wait(15)
main()