Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.64 KB | None | 0 0
  1. import pygame
  2. from pygame.locals import *
  3.  
  4. from OpenGL.GL import *
  5. from OpenGL.GLU import *
  6.  
  7. vertices = (
  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.     (-1, 1, 1)
  16.     )
  17. edges = (
  18.     (0,1),
  19.     (0,3),
  20.     (0,4),
  21.     (2,1),
  22.     (2,3),
  23.     (2,7),
  24.     (6,3),
  25.     (6,4),
  26.     (6,7),
  27.     (5,1),
  28.     (5,4),
  29.     (5,7),
  30.     )
  31. surfaces = (
  32.     (0,1,2,3),
  33.     (3,2,7,6),
  34.     (6,7,5,4),
  35.     (4,5,1,0),
  36.     (1,5,7,2),
  37.     (4,0,3,6),
  38.     )
  39. #поверхности 0 1 2 3 4 5
  40. normals = [
  41.     ( 0,  0, -1),  
  42.     (-1,  0,  0),  
  43.     ( 0,  1,  1),  
  44.     ( 1,  0,  0),  
  45.     ( 0,  1,  0),  
  46.     ( 0, -1,  0)  
  47. ]  
  48. colors = (
  49.     (1,0,0),
  50.     (0,1,0),
  51.     (0,0,1),
  52.     (0,1,0),
  53.     (1,1,1),
  54.     (0,1,1),
  55.     (1,0,0),
  56.     (0,1,0),
  57.     (0,0,1),
  58.     (1,0,0),
  59.     (1,1,1),
  60.     (1,0,1),
  61.     )
  62.  
  63. def Cube():
  64.     glBegin(GL_QUADS)
  65.     for i_surface, surface in enumerate(surfaces):
  66.         x = 0
  67.  
  68.         glNormal3fv(normals[i_surface]) #установить вектор нормали вершины поверхности
  69.  
  70.         for vertex in surface:
  71.             x+=2
  72.             glColor3fv(colors[x])
  73.             glVertex3fv(vertices[vertex])
  74.     glEnd()
  75.  
  76.     glColor3fv(colors[2])
  77.     glBegin(GL_LINES)
  78.     for edge in edges:
  79.         for vertex in edge:
  80.             glVertex3fv(vertices[vertex])
  81.     glEnd()    
  82.  
  83. def main():
  84.     pygame.init()
  85.     display = (600,500)
  86.     pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
  87.     glMatrixMode(GL_PROJECTION)
  88.     gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
  89.    
  90.     glMatrixMode(GL_MODELVIEW)
  91.     glTranslatef(0,0.0, -10)
  92.     glLight(GL_LIGHT0, GL_POSITION,  (0, 0, 1, 0)) # directional light from the front
  93.     #glLight(GL_LIGHT0, GL_POSITION,  (5, 5, 5, 1)) # point light from the left, top, front
  94.     glLightfv(GL_LIGHT0, GL_AMBIENT, (0, 0, 0, 1))
  95.     #glLightfv(GL_LIGHT0, GL_SPECULAR, (1, 1, 1, 1))
  96.     glLightfv(GL_LIGHT0, GL_DIFFUSE, (1, 1, 1, 1))
  97.     glEnable(GL_DEPTH_TEST)
  98.  
  99.     while True:
  100.         for event in pygame.event.get():
  101.             if event.type == pygame.QUIT:
  102.                 pygame.quit()
  103.                 quit()
  104.  
  105.         glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
  106.        
  107.         glEnable(GL_LIGHTING)
  108.         glEnable(GL_LIGHT0)
  109.         glEnable(GL_COLOR_MATERIAL)
  110.         glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
  111.  
  112.         glRotatef(1, 3, 1, 2)
  113.         Cube()
  114.            
  115.         glDisable(GL_LIGHT0)
  116.         glDisable(GL_LIGHTING)
  117.         glDisable(GL_COLOR_MATERIAL)
  118.        
  119.         pygame.display.flip()
  120. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement