Advertisement
Guest User

Untitled

a guest
May 21st, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.43 KB | None | 0 0
  1. import pyglet
  2. import pyglet.gl as pgl
  3. from pyglet.window import key
  4. import OpenGL.GL as ogl
  5. import math
  6.  
  7.  
  8.  
  9. WINDOW = 1000
  10. tr = 90
  11. INCREMENT = 5
  12. transparant = False
  13.  
  14. def draw_small():
  15.  
  16. pgl.glBegin(ogl.GL_QUADS)
  17. pgl.glColor3ub(255, 0, 0)
  18. pgl.glVertex3f(-150, -100, -50)
  19. pgl.glVertex3f(-150, -50, -50)
  20. pgl.glVertex3f(-100, -50, -50)
  21. pgl.glVertex3f(-100, -100, -50)
  22.  
  23. pgl.glColor3ub(0, 255, 0)
  24. pgl.glVertex3f(-150, -100, -50)
  25. pgl.glVertex3f(-150, -50, -50)
  26. pgl.glVertex3f(-150, -50, 0)
  27. pgl.glVertex3f(-150, -100, 0)
  28.  
  29. pgl.glColor3ub(255, 0, 255)
  30. pgl.glVertex3f(-150, -50, -50)
  31. pgl.glVertex3f(-150, -50, 0)
  32. pgl.glVertex3f(-100, -50, 0)
  33. pgl.glVertex3f(-100, -50, -50)
  34.  
  35. pgl.glColor3ub(255, 255, 0)
  36. pgl.glVertex3f(-100, -100, -50)
  37. pgl.glVertex3f(-100, -50, -50)
  38. pgl.glVertex3f(-100, -50, 0)
  39. pgl.glVertex3f(-100, -100, 0)
  40.  
  41. pgl.glColor3ub(0, 0, 255)
  42. pgl.glVertex3f(-150, -100, -50)
  43. pgl.glVertex3f(-100, -100, -50)
  44. pgl.glVertex3f(-100, -100, 0)
  45. pgl.glVertex3f(-150, -100, 0)
  46.  
  47. pgl.glColor3ub(0, 255, 255)
  48. pgl.glVertex3f(-150, -100, 0)
  49. pgl.glVertex3f(-100, -100, 0)
  50. pgl.glVertex3f(-100, -50, 0)
  51. pgl.glVertex3f(-150, -50, 0)
  52.  
  53. pgl.glEnd()
  54.  
  55.  
  56. def draw_big():
  57. angle = 0
  58. t = 0
  59. x = y = z = 0
  60. a = 50
  61. b = 100
  62.  
  63. if not transparant:
  64. pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_FILL)
  65. else:
  66. pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_LINE)
  67. pgl.glBegin(pgl.GL_TRIANGLE_STRIP)
  68. for i in range(0, tr + 1):
  69. pgl.glColor3ub(255, 0, 0)
  70. pgl.glVertex3f(x + a * math.cos(i * 2 * math.pi / tr), y + b * math.sin(i * 2 * math.pi / tr), z + 8)
  71. pgl.glVertex3f(x, y, z + 8)
  72. pgl.glEnd()
  73. for j in range(1, 9):
  74. pgl.glBegin(pgl.GL_TRIANGLE_STRIP)
  75. for i in range(0, tr + 1):
  76. pgl.glColor3ub(0, 255, 0)
  77. x1 = a * math.cos(i * 2 * math.pi / tr)
  78. y1 = b * math.sin(i * 2 * math.pi / tr)
  79. x2 = x1 * math.cos(t * math.pi / 360) + y1 * math.sin(t * math.pi / 360)
  80. y2 = - x1 * math.sin(t * math.pi / 360) + y1 * math.cos(t * math.pi / 360)
  81. pgl.glVertex3f(x2, y2, z + j*8)
  82. x1 = a * math.cos(i * 2 * math.pi / tr)
  83. y1 = b * math.sin(i * 2 * math.pi / tr)
  84. x2 = x1 * math.cos((t+16) * math.pi / 360) + y1 * math.sin((t+16) * math.pi / 360)
  85. y2 = - x1 * math.sin((t+16) * math.pi / 360) + y1 * math.cos((t+16) * math.pi / 360)
  86. pgl.glVertex3f(x2, y2, z + j * 8 + 8)
  87. t = t + 16
  88. pgl.glEnd()
  89. pgl.glBegin(pgl.GL_TRIANGLE_STRIP)
  90. pgl.glColor3ub(0, 0, 255)
  91. t = 128
  92. angle = angle + t
  93. for i in range(0, tr + 1):
  94. x1 = x + a * math.cos(i * 2 * math.pi / tr)
  95. y1 = y + b * math.sin(i * 2 * math.pi / tr)
  96. x2 = x1 * math.cos(t * math.pi / 360) + y1 * math.sin(t * math.pi / 360)
  97. y2 = - x1 * math.sin(t * math.pi / 360) + y1 * math.cos(t * math.pi / 360)
  98. pgl.glVertex3f(x2, y2, z + 72)
  99. pgl.glVertex3f(x, y, z + 72)
  100. angle = angle + i
  101. pgl.glEnd()
  102.  
  103.  
  104.  
  105.  
  106.  
  107. class Window(pyglet.window.Window):
  108.  
  109. xRotation = yRotation = zRotation = 30
  110. zoom = 1
  111. far = 100
  112. dist = 35
  113. x = y = z = 0
  114.  
  115. def __init__(self, width, height, title = '') :
  116. super(Window, self).__init__( 1300,1000, title)
  117. pgl.glClearColor(0, 0, 0, 1)
  118. pgl.glEnable(pgl.GL_DEPTH_TEST)
  119.  
  120.  
  121.  
  122. def on_draw(self) :
  123.  
  124. self.clear()
  125.  
  126. pgl.glClear(pgl.GL_COLOR_BUFFER_BIT | pgl.GL_DEPTH_BUFFER_BIT)
  127.  
  128. # объемный куб без проекции правый верхний угол
  129.  
  130. pgl.glLoadIdentity()
  131. pgl.glViewport(650, 500, 650, 500)
  132. pgl.glMatrixMode(ogl.GL_PROJECTION)
  133. pgl.glLoadIdentity()
  134.  
  135. pgl.gluPerspective(self.dist, 1.3, 1, 1000)
  136.  
  137. pgl.glMatrixMode(ogl.GL_MODELVIEW)
  138.  
  139. pgl.glTranslatef(0, 0, -400)
  140.  
  141.  
  142. pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_FILL)
  143. draw_small()
  144.  
  145. pgl.glPushMatrix()
  146. pgl.glTranslatef(self.x, self.y, self.z)
  147. pgl.glRotatef(self.xRotation, 1, 0, 0)
  148. pgl.glRotatef(self.yRotation, 0, 1, 0)
  149. pgl.glRotatef(self.zRotation, 0, 0, 1)
  150. pgl.glScalef(self.zoom, self.zoom, self.zoom)
  151.  
  152.  
  153.  
  154. draw_big()
  155.  
  156. pgl.glPopMatrix()
  157.  
  158. # фронтальная проекция правый нижний угол
  159.  
  160. pgl.glLoadIdentity()
  161. pgl.glViewport(650, 0, 650, 500)
  162. pgl.glMatrixMode(pgl.GL_PROJECTION)
  163. pgl.glLoadIdentity()
  164. pgl.glOrtho(-1300 / 8, 1300 / 8, -1000 / 8, 1000 / 8, 0, 600)
  165. pgl.glMatrixMode(pgl.GL_MODELVIEW)
  166. pgl.glLoadIdentity()
  167.  
  168. pgl.glTranslatef(0, 0, -100)
  169.  
  170. pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_FILL)
  171.  
  172. draw_small()
  173.  
  174. pgl.glPushMatrix()
  175. pgl.glTranslatef(self.x, self.y, self.z)
  176. pgl.glRotatef(self.xRotation, 1, 0, 0)
  177. pgl.glRotatef(self.yRotation, 0, 1, 0)
  178. pgl.glRotatef(self.zRotation, 0, 0, 1)
  179. pgl.glScalef(self.zoom, self.zoom, self.zoom)
  180.  
  181.  
  182. draw_big()
  183.  
  184. pgl.glPopMatrix()
  185.  
  186. # верхняя проекция левый верхний угол
  187.  
  188. pgl.glLoadIdentity()
  189. pgl.glViewport(0, 500, 650, 500)
  190. pgl.glMatrixMode(pgl.GL_PROJECTION)
  191. pgl.glLoadIdentity()
  192. pgl.glOrtho(-1300 / 8, 1300 / 8, -1000 / 8, 1000 / 8, 0, 600)
  193. pgl.glMatrixMode(pgl.GL_MODELVIEW)
  194. pgl.glLoadIdentity()
  195.  
  196. pgl.glTranslatef(0, 0, -100)
  197. pgl.glRotatef(90, 1, 0, 0)
  198.  
  199. pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_FILL)
  200.  
  201. draw_small()
  202.  
  203. pgl.glPushMatrix()
  204. pgl.glTranslatef(self.x, self.y, self.z)
  205. pgl.glRotatef(self.xRotation, 1, 0, 0)
  206. pgl.glRotatef(self.yRotation, 0, 1, 0)
  207. pgl.glRotatef(self.zRotation, 0, 0, 1)
  208. pgl.glScalef(self.zoom, self.zoom, self.zoom)
  209.  
  210.  
  211. draw_big()
  212.  
  213. pgl.glPopMatrix()
  214.  
  215. # боковая проекция левый нижний угол
  216.  
  217. pgl.glLoadIdentity()
  218. pgl.glViewport(0, 0, 650, 500)
  219. pgl.glMatrixMode(pgl.GL_PROJECTION)
  220. pgl.glLoadIdentity()
  221. pgl.glOrtho(-1300 / 8, 1300 / 8, -1000 / 8, 1000 / 8, 0, 600)
  222. pgl.glMatrixMode(pgl.GL_MODELVIEW)
  223. pgl.glLoadIdentity()
  224.  
  225. pgl.glTranslatef(0, 0, -100)
  226. pgl.glRotatef(-90, 0, 1, 0)
  227.  
  228. pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_FILL)
  229.  
  230. draw_small()
  231.  
  232. pgl.glPushMatrix()
  233. pgl.glTranslatef(self.x, self.y, self.z)
  234. pgl.glRotatef(self.xRotation, 1, 0, 0)
  235. pgl.glRotatef(self.yRotation, 0, 1, 0)
  236. pgl.glRotatef(self.zRotation, 0, 0, 1)
  237. pgl.glScalef(self.zoom, self.zoom, self.zoom)
  238.  
  239. draw_big()
  240.  
  241. pgl.glPopMatrix()
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252. def on_text_motion(self, motion):
  253. if motion == key.UP:
  254. self.xRotation -= INCREMENT
  255. elif motion == key.DOWN:
  256. self.xRotation += INCREMENT
  257. elif motion == key.LEFT:
  258. self.yRotation -= INCREMENT
  259. elif motion == key.RIGHT:
  260. self.yRotation += INCREMENT
  261.  
  262.  
  263. def on_key_press(self, symbol, modifiers):
  264. global transparant, dist,tr
  265. if symbol == key.T and not transparant:
  266. transparant = True
  267. elif symbol == key.T and transparant:
  268. transparant = False
  269. elif symbol == key.W:
  270. self.x +=2.5
  271. elif symbol == key.S:
  272. self.x -=2.5
  273. elif symbol == key.A:
  274. self.y -=2.5
  275. elif symbol == key.D:
  276. self.y +=2.5
  277. elif symbol == key.Q:
  278. self.z -=2.5
  279. elif symbol == key.E:
  280. self.z +=2.5
  281. elif symbol == key.Z:
  282. self.zRotation += INCREMENT
  283. elif symbol == key.X:
  284. self.zRotation -= INCREMENT
  285. elif symbol == key.ESCAPE:
  286. pyglet.app.exit()
  287. elif symbol == key.P:
  288. tr +=5
  289. elif symbol == key.O:
  290. if tr > 5:
  291. tr -=5
  292.  
  293.  
  294.  
  295. def on_mouse_scroll(self,x, y, scroll_x, scroll_y):
  296. if scroll_y < 0:
  297. self.zoom += 0.1
  298. elif scroll_y > 0 and self.zoom - 0.1 >0:
  299. self.zoom -= 0.1
  300.  
  301. Window(WINDOW, WINDOW, 'Cube')
  302. pyglet.app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement