Advertisement
RockField64

draw_cube matrix model

Nov 25th, 2022 (edited)
929
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.80 KB | Help | 0 0
  1. import pygame
  2. from OpenGL.GL import *
  3. from OpenGL.GLU import *
  4. from pygame.locals import *
  5.  
  6. vertices = (
  7.     (1, 1, 1),
  8.     (1, -1, 1),
  9.     (-1, -1, 1),
  10.     (-1, 1, 1),
  11.     (1, 1, -1),
  12.     (1, -1, -1),
  13.     (-1, -1, -1),
  14.     (-1, 1, -1)
  15.     )
  16.  
  17. edges = (
  18.     (0, 1),
  19.     (2, 3),
  20.     (4, 5),
  21.     (6, 7),
  22.     (0, 1),
  23.     (5, 4),
  24.     (3, 2),
  25.     (6, 7),
  26.     (0, 3),
  27.     (7, 4),
  28.     (1, 2),
  29.     (6, 5)
  30.     )
  31.  
  32.  
  33. def draw_object():
  34.     glPushMatrix()
  35.     glColor3f(0, 1, 0)
  36.     glBegin(GL_QUADS)
  37.     x = 0
  38.     for edge in edges:
  39.         x = x + 1
  40.         for vertex in edge:
  41.             glVertex3iv(vertices[vertex])
  42.     glEnd()
  43.     glPopMatrix()
  44.  
  45.  
  46.  
  47.  
  48. def main():
  49.     pygame.init()
  50.     display = (800,600)
  51.     pygame.display.set_caption("05 Lab 1")
  52.     pygame.display.set_mode(display, DOUBLEBUF|OPENGL|RESIZABLE)
  53.  
  54.     glEnable(GL_DEPTH_TEST)
  55.     gluPerspective(40, (display[0]/display[1]), 1, 150)
  56.     glTranslatef(0.0, 0.0, -10)
  57.     glRotatef(0, 1, 0, 0)
  58.  
  59.  
  60.     while True:
  61.         for event in pygame.event.get():
  62.             if event.type == pygame.QUIT:
  63.                 pygame.quit()
  64.                 quit()
  65.  
  66.         if event.type==pygame.KEYDOWN:
  67.             if event.key==pygame.K_UP:
  68.                 glTranslatef(0, 0, 0.5)
  69.             if event.key==pygame.K_DOWN:
  70.                 glTranslatef(0,0, -0.5)
  71.  
  72.         keys=pygame.key.get_pressed()
  73.         if keys[K_w]:
  74.             glTranslatef(0, 0.5, 0)
  75.         if keys[K_s]:
  76.             glTranslatef(0, -0.5, 0)
  77.         if keys[K_a]:
  78.             glTranslatef(-0.5, 0, 0)
  79.         if keys[K_d]:
  80.             glTranslatef(0.5, 0, 0)
  81.  
  82.         glRotatef(1, 1, 2, 1)
  83.         glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
  84.         draw_object()
  85.         pygame.display.flip()
  86.         pygame.time.wait(10)
  87.  
  88.  
  89. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement