Advertisement
Guest User

Untitled

a guest
May 4th, 2019
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.43 KB | None | 0 0
  1. import pyglet
  2. import pyglet.gl as pgl
  3. from pyglet.window import key
  4. import math
  5.  
  6. win = pyglet.window.Window(1300, 1000, resizable=True)
  7.  
  8. WINDOW = 1000
  9. tr = 90
  10. INCREMENT = 5
  11. transparant = False
  12. xRotation = -70
  13. zRotation = -30
  14. yRotation = 180
  15. zoom = 1
  16. far = 100
  17. dist = 35
  18. x = -180
  19. y = 70
  20. z = 0
  21. light = False
  22.  
  23. param = 0
  24. t_prev = -5
  25. start_loop = False
  26. run_loop = False
  27.  
  28.  
  29.  
  30. def run_loop(dt):
  31. on_draw()
  32.  
  33.  
  34. def draw_figure():
  35. angle = 0
  36. t = 0
  37. x = y = z = 0
  38. a = 50
  39. b = 100
  40.  
  41. if not transparant:
  42. pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_FILL)
  43. else:
  44. pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_LINE)
  45. pgl.glBegin(pgl.GL_TRIANGLE_STRIP)
  46. for i in range(0, tr + 1):
  47. pgl.glColor3ub(0, 0, 0)
  48. if i >= 1:
  49. t1 = a1
  50. t2 = a2
  51. t3 = a3
  52. a1 = x + a * math.cos(i * 2 * math.pi / tr)
  53. a2 = y + b * math.sin(i * 2 * math.pi / tr)
  54. a3 = z + 8
  55. pgl.glVertex3f(a1, a2, a3)
  56. if i >= 1:
  57. pgl.glNormal3f((b2 - t2) * (a3 - b3) - (b3 - t3) * (a2 - b2), (b3 - t3) * (a1 - b1) - (b1 - t1) * (a3 - b3),
  58. (b1 - t1) * (a2 - b2) - (b2 - t2) * (a1 - b1))
  59. b1 = x
  60. b2 = y
  61. b3 = z + 8
  62. pgl.glVertex3f(b1, b2, b3)
  63. pgl.glEnd()
  64. for j in range(1, 27):
  65. pgl.glBegin(pgl.GL_TRIANGLE_STRIP)
  66. pgl.glColor3ub(0, 0, 0)
  67. x1 = a * math.cos(0)
  68. y1 = b * math.sin(0)
  69. a1 = x1 * math.cos(t * math.pi / 360) + y1 * math.sin(t * math.pi / 360)
  70. a2 = - x1 * math.sin(t * math.pi / 360) + y1 * math.cos(t * math.pi / 360)
  71. a3 = z + j * 8
  72. pgl.glVertex3f(a1, a2, a3)
  73. x1 = a * math.cos(0)
  74. y1 = b * math.sin(0)
  75. b1 = x1 * math.cos((t + 16) * math.pi / 360) + y1 * math.sin((t + 16) * math.pi / 360)
  76. b2 = - x1 * math.sin((t + 16) * math.pi / 360) + y1 * math.cos((t + 16) * math.pi / 360)
  77. b3 = z + j * 8 + 8
  78. pgl.glVertex3f(b1, b2, b3)
  79. for i in range(1, tr + 1):
  80. pgl.glColor3ub(0, 0, 0)
  81. x1 = a * math.cos(i * 2 * math.pi / tr)
  82. y1 = b * math.sin(i * 2 * math.pi / tr)
  83. t1 = a1
  84. t2 = a2
  85. t3 = a3
  86. a1 = x1 * math.cos(t * math.pi / 360) + y1 * math.sin(t * math.pi / 360)
  87. a2 = - x1 * math.sin(t * math.pi / 360) + y1 * math.cos(t * math.pi / 360)
  88. a3 = z + j * 8
  89. pgl.glNormal3f((b2 - t2) * (a3 - b3) - (b3 - t3) * (a2 - b2), (b3 - t3) * (a1 - b1) - (b1 - t1) * (a3 - b3),
  90. (b1 - t1) * (a2 - b2) - (b2 - t2) * (a1 - b1))
  91. pgl.glVertex3f(a1, a2, a3)
  92. x1 = a * math.cos(i * 2 * math.pi / tr)
  93. y1 = b * math.sin(i * 2 * math.pi / tr)
  94. b1 = x1 * math.cos((t + 16) * math.pi / 360) + y1 * math.sin((t + 16) * math.pi / 360)
  95. b2 = - x1 * math.sin((t + 16) * math.pi / 360) + y1 * math.cos((t + 16) * math.pi / 360)
  96. b3 = z + j * 8 + 8
  97. pgl.glVertex3f(b1, b2, b3)
  98. t = t + 16
  99. pgl.glEnd()
  100. pgl.glBegin(pgl.GL_TRIANGLE_STRIP)
  101. t = 416
  102. z = z + 144
  103. angle = angle + t
  104. for i in range(0, tr + 1):
  105. pgl.glColor3ub(0, 0, 0)
  106. x1 = x + a * math.cos(i * 2 * math.pi / tr)
  107. y1 = y + b * math.sin(i * 2 * math.pi / tr)
  108. x2 = x1 * math.cos(t * math.pi / 360) + y1 * math.sin(t * math.pi / 360)
  109. y2 = - x1 * math.sin(t * math.pi / 360) + y1 * math.cos(t * math.pi / 360)
  110. if i >= 1:
  111. t1 = a1
  112. t2 = a2
  113. t3 = a3
  114. a1 = x2
  115. a2 = y2
  116. a3 = z + 72
  117. pgl.glVertex3f(a1, a2, a3)
  118. if i >= 1:
  119. pgl.glNormal3f((b2 - t2) * (a3 - b3) - (b3 - t3) * (a2 - b2), (b3 - t3) * (a1 - b1) - (b1 - t1) * (a3 - b3),
  120. (b1 - t1) * (a2 - b2) - (b2 - t2) * (a1 - b1))
  121. b1 = x
  122. b2 = y
  123. b3 = z + 72
  124. pgl.glVertex3f(b1, b2, b3)
  125. angle = angle + i
  126. pgl.glEnd()
  127.  
  128.  
  129. @win.event
  130. def on_draw():
  131. global x, y, z, start_loop, run_loop, param, t_prev, zRotation
  132.  
  133.  
  134.  
  135. pgl.glClearColor(0.3, 0.3, 0.3, 0.0)
  136. pgl.glEnable(pgl.GL_DEPTH_TEST)
  137. if light:
  138. pgl.glEnable(pgl.GL_LIGHTING)
  139. pgl.glLightModelf(pgl.GL_LIGHT_MODEL_TWO_SIDE, pgl.GL_TRUE)
  140. pgl.glEnable(pgl.GL_NORMALIZE)
  141. pgl.glClear(pgl.GL_COLOR_BUFFER_BIT | pgl.GL_DEPTH_BUFFER_BIT)
  142.  
  143. pgl.glLoadIdentity()
  144. pgl.glViewport(0, 0, 1300, 1000)
  145. pgl.glMatrixMode(pgl.GL_PROJECTION)
  146. pgl.glLoadIdentity()
  147. pgl.glOrtho(-1300 / 4, 1300 / 4, -1000 / 4, 1000 / 4, -400, 600)
  148. pgl.glMatrixMode(pgl.GL_MODELVIEW)
  149. pgl.glLoadIdentity()
  150.  
  151. if light:
  152. material_diffuse = [1.0, 1.0, 1.0, 1.0]
  153. pgl.glMaterialfv(pgl.GL_FRONT_AND_BACK, pgl.GL_DIFFUSE,
  154. (pgl.GLfloat * len(material_diffuse))(*material_diffuse))
  155.  
  156. light5_diffuse = [1.0, 0.0, 0.0];
  157.  
  158. light5_position = [0.5, 0.5, 1.0, 0];
  159.  
  160. pgl.glEnable(pgl.GL_LIGHT5);
  161.  
  162. pgl.glLightfv(pgl.GL_LIGHT5, pgl.GL_DIFFUSE, (pgl.GLfloat * len(light5_diffuse))(*light5_diffuse));
  163.  
  164. pgl.glLightfv(pgl.GL_LIGHT5, pgl.GL_POSITION, (pgl.GLfloat * len(light5_position))(*light5_position));
  165.  
  166. pgl.glLightf(pgl.GL_LIGHT5, pgl.GL_CONSTANT_ATTENUATION, 0.0);
  167.  
  168. pgl.glLightf(pgl.GL_LIGHT5, pgl.GL_LINEAR_ATTENUATION, 0.4);
  169.  
  170. pgl.glLightf(pgl.GL_LIGHT5, pgl.GL_QUADRATIC_ATTENUATION, 0.8);
  171.  
  172. light6_diffuse = [0.0, 1.0, 0.0];
  173.  
  174. light6_position = [0.5 * math.cos(2 * math.pi / 3), 0.5 * math.sin(2 * math.pi / 3), 1.0, 0];
  175.  
  176. pgl.glEnable(pgl.GL_LIGHT6);
  177.  
  178. pgl.glLightfv(pgl.GL_LIGHT6, pgl.GL_DIFFUSE, (pgl.GLfloat * len(light6_diffuse))(*light6_diffuse));
  179.  
  180. pgl.glLightfv(pgl.GL_LIGHT6, pgl.GL_POSITION, (pgl.GLfloat * len(light6_position))(*light6_position));
  181.  
  182. pgl.glLightf(pgl.GL_LIGHT6, pgl.GL_CONSTANT_ATTENUATION, 0.0);
  183.  
  184. pgl.glLightf(pgl.GL_LIGHT6, pgl.GL_LINEAR_ATTENUATION, 0.4);
  185.  
  186. pgl.glLightf(pgl.GL_LIGHT6, pgl.GL_QUADRATIC_ATTENUATION, 0.8);
  187.  
  188. light7_diffuse = [0.0, 0.0, 1.0];
  189.  
  190. light7_position = [0.5 * math.cos(4 * math.pi / 3), 0.5 * math.sin(4 * math.pi / 3), 1.0, 0];
  191.  
  192. pgl.glEnable(pgl.GL_LIGHT7);
  193.  
  194. pgl.glLightfv(pgl.GL_LIGHT7, pgl.GL_DIFFUSE, (pgl.GLfloat * len(light7_diffuse))(*light7_diffuse));
  195.  
  196. pgl.glLightfv(pgl.GL_LIGHT7, pgl.GL_POSITION, (pgl.GLfloat * len(light7_position))(*light7_position));
  197.  
  198. pgl.glLightf(pgl.GL_LIGHT7, pgl.GL_CONSTANT_ATTENUATION, 0.0);
  199.  
  200. pgl.glLightf(pgl.GL_LIGHT7, pgl.GL_LINEAR_ATTENUATION, 0.4);
  201.  
  202. pgl.glLightf(pgl.GL_LIGHT7, pgl.GL_QUADRATIC_ATTENUATION, 0.8);
  203.  
  204. pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_FILL)
  205. pgl.glPushMatrix()
  206. pgl.glTranslatef(x, y, z)
  207. pgl.glRotatef(xRotation, 1, 0, 0)
  208. pgl.glRotatef(yRotation, 0, 1, 0)
  209. pgl.glRotatef(zRotation, 0, 0, 1)
  210. pgl.glScalef(zoom, zoom, zoom)
  211.  
  212. draw_figure()
  213.  
  214. if start_loop:
  215. start_loop = False
  216. run_loop = True
  217. x = ((1 - (param / 100)) ** 3) * (-180) + 3 * (param / 100) * ((1 - (param / 100)) ** 2) * (100) + 3 * (
  218. (param / 100) ** 2) * (
  219. 1 - (param / 100)) * (0) + ((param / 100) ** 3) * (200)
  220. y = ((1 - (param / 100)) ** 3) * 70 + 3 * (param / 100) * ((1 - (param / 100)) ** 2) * (140) + 3 * (
  221. (param / 100) ** 2) * (
  222. 1 - (param / 100)) * (140) + ((param / 100) ** 3) * (
  223. 70)
  224. z = ((1 - (param / 100)) ** 3) * 0 + 3 * (param / 100) * ((1 - (param / 100)) ** 2) * (100) + 3 * (
  225. (param / 100) ** 2) * (
  226. 1 - (param / 100)) * (100) + ((param / 100) ** 3) * (
  227. 0)
  228.  
  229. elif run_loop:
  230. if param < 100 and param > t_prev:
  231. zRotation += INCREMENT
  232. param = param + 1
  233. t_prev = t_prev + 1
  234. if (param == 100):
  235. t_prev = 105
  236. x = ((1 - (param / 100)) ** 3) * (-180) + 3 * (param / 100) * ((1 - (param / 100)) ** 2) * (100) + 3 * (
  237. (param / 100) ** 2) * (1 - (param / 100)) * (0) + ((param / 100) ** 3) * (200)
  238. y = ((1 - (param / 100)) ** 3) * 70 + 3 * (param / 100) * ((1 - (param / 100)) ** 2) * (140) + 3 * (
  239. (param / 100) ** 2) * (1 - (param / 100)) * (140) + ((param / 100) ** 3) * (
  240. 70)
  241. z = ((1 - (param / 100)) ** 3) * 0 + 3 * (param / 100) * ((1 - (param / 100)) ** 2) * (100) + 3 * (
  242. (param / 100) ** 2) * (1 - (param / 100)) * (100) + ((param / 100) ** 3) * (
  243. 0)
  244. print(param)
  245. print("плюс")
  246. if param > 0 and param < t_prev:
  247. zRotation -= INCREMENT
  248. param = param - 1
  249. t_prev = t_prev - 1
  250. if (param == 0):
  251. t_prev = -5
  252. x = ((1 - (param / 100)) ** 3) * (-180) + 3 * (param / 100) * ((1 - (param / 100)) ** 2) * (100) + 3 * (
  253. (param / 100) ** 2) * (1 - (param / 100)) * (0) + ((param / 100) ** 3) * (200)
  254. y = ((1 - (param / 100)) ** 3) * 70 + 3 * (param / 100) * ((1 - (param / 100)) ** 2) * (140) + 3 * (
  255. (param / 100) ** 2) * (1 - (param / 100)) * (140) + ((param / 100) ** 3) * (
  256. 70)
  257. z = ((1 - (param / 100)) ** 3) * 0 + 3 * (param / 100) * ((1 - (param / 100)) ** 2) * (100) + 3 * (
  258. (param / 100) ** 2) * (1 - (param / 100)) * (100) + ((param / 100) ** 3) * (
  259. 0)
  260. print(param)
  261. print("минус")
  262.  
  263. pgl.glPopMatrix()
  264.  
  265.  
  266. @win.event
  267. def on_text_motion(motion):
  268. global xRotation, yRotation, INCREMENT
  269. if motion == key.UP:
  270. xRotation -= INCREMENT
  271. elif motion == key.DOWN:
  272. xRotation += INCREMENT
  273. elif motion == key.LEFT:
  274. yRotation -= INCREMENT
  275. elif motion == key.RIGHT:
  276. yRotation += INCREMENT
  277.  
  278.  
  279. @win.event
  280. def on_key_press(symbol, modifiers):
  281. global transparant, dist, tr, x, y, z, INCREMENT, zRotation, light, t, start_loop, run_loop
  282. if symbol == key.T and not transparant:
  283. transparant = True
  284. elif symbol == key.T and transparant:
  285. transparant = False
  286. elif symbol == key.W:
  287. x += 2.5
  288. elif symbol == key.S:
  289. x -= 2.5
  290. elif symbol == key.A:
  291. y -= 2.5
  292. elif symbol == key.D:
  293. y += 2.5
  294. elif symbol == key.Q:
  295. z -= 2.5
  296. elif symbol == key.E:
  297. z += 2.5
  298. elif symbol == key.L:
  299. light = not light
  300. elif symbol == key.Z:
  301. zRotation += INCREMENT
  302. elif symbol == key.X:
  303. zRotation -= INCREMENT
  304. elif symbol == key.ESCAPE:
  305. pyglet.app.exit()
  306. elif symbol == key.P:
  307. tr += 5
  308. elif symbol == key.O:
  309. if tr > 5:
  310. tr -= 5
  311. elif symbol == key.SPACE:
  312. if not run_loop:
  313. start_loop = True
  314. else:
  315. run_loop = False
  316.  
  317.  
  318. @win.event
  319. def on_mouse_scroll(x, y, scroll_x, scroll_y):
  320. global zoom
  321. if scroll_y < 0:
  322. zoom += 0.1
  323. elif scroll_y > 0 and zoom - 0.1 > 0:
  324. zoom -= 0.1
  325.  
  326.  
  327. @win.event
  328. def on_mouse_press(x, y, button, modifiers):
  329. if button == pyglet.window.mouse.LEFT:
  330. print(x, y)
  331.  
  332.  
  333. pyglet.clock.schedule_interval(run_loop, 0.1)
  334. pyglet.app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement