Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.49 KB | None | 0 0
  1. import pyglet
  2. import pyglet.gl as pgl
  3. from pyglet.window import key
  4. import math
  5. import json
  6. import time
  7.  
  8.  
  9. win = pyglet.window.Window(1300, 1000, resizable=True)
  10.  
  11. global_time = time.time()
  12. frame_counter = 0
  13.  
  14. WINDOW = 1000
  15. tr = 96
  16. tr_temp = 90
  17. INCREMENT = 5
  18. transparant = False
  19. xRotation = -70
  20. zRotation = -30
  21. yRotation = 180
  22. zoom = 1
  23. x = -180
  24. y = 70
  25. z = 0
  26. light = False
  27.  
  28. param = 0
  29. t_prev = -5
  30. start_loop = False
  31. run_l = False
  32. Texture = False
  33. texture_obj = None
  34. lamp1 = False
  35. lamp2 = False
  36. lamp3 = False
  37.  
  38. data = {}
  39.  
  40. with open('PycharmProjects/untitled/data.json') as fp:
  41. data = json.load(fp)
  42. transparant = data['transparant']
  43. xRotation = data['xRotation']
  44. yRotation = data['yRotation']
  45. zRotation = data['zRotation']
  46. zoom = data['zoom']
  47. x = data['x']
  48. y = data['y']
  49. z = data['z']
  50. light = data['light']
  51. param = data['param']
  52. t_prev = data['t_prev']
  53. start_loop = data['start_loop']
  54. run_l = data['run_l']
  55. Texture = data['Texture']
  56. lamp1 = data['lamp1']
  57. lamp2 = data['lamp2']
  58. lamp3 = data['lamp3']
  59. tr = data['tr']
  60.  
  61.  
  62. def writeToJSONFile(path, filename, data):
  63. filePathName = path + '.json'
  64. with open(filePathName, 'w') as fp:
  65. json.dump(data,fp)
  66.  
  67.  
  68. def run_loop(dt):
  69. on_draw()
  70.  
  71.  
  72. def draw_figure():
  73. global texture_obj
  74. angle = 0
  75. t = 0
  76. x = y = z = 0
  77. a = 50
  78. b = 100
  79. array_size = 0
  80. array = []
  81.  
  82.  
  83. if Texture:
  84. if texture_obj == None:
  85. texture_obj = pyglet.image.load("PycharmProjects/untitled/test1_texture.bmp").get_texture()
  86. pgl.glBindTexture(pgl.GL_TEXTURE_2D, texture_obj.id)
  87. pgl.glEnable(pgl.GL_TEXTURE_2D)
  88. # pgl.glTexParameterf(pgl.GL_TEXTURE_2D, pgl.GL_TEXTURE_WRAP_T, pgl.GL_REPEAT)
  89. # pgl.glTexParameterf(pgl.GL_TEXTURE_2D, pgl.GL_TEXTURE_MAG_FILTER, pgl.GL_NEAREST)
  90. # #
  91. #
  92. # pgl.glTexEnvi(pgl.GL_TEXTURE_ENV, pgl.GL_TEXTURE_ENV_MODE, pgl.GL_REPLACE)
  93.  
  94. else:
  95. pgl.glDisable(pgl.GL_TEXTURE_2D)
  96.  
  97. if not transparant:
  98. pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_FILL)
  99. else:
  100. pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_LINE)
  101. pgl.glBegin(pgl.GL_TRIANGLE_STRIP)
  102. for i in range(0, tr + 1):
  103. pgl.glColor3ub(0, 0, 0)
  104. if i >= 1:
  105. t1 = a1
  106. t2 = a2
  107. t3 = a3
  108. u = math.cos(i * 2 * math.pi / tr)
  109. v = math.sin(i * 2 * math.pi / tr)
  110. a1 = x + a * u
  111. a2 = y + b * v
  112. a3 = z + 8
  113. if Texture:
  114. pgl.glTexCoord2f(u * 0.5 + 0.5, v * 0.5 + 0.5)
  115. pgl.glVertex3f(a1, a2, a3)
  116. if i >= 1:
  117. pgl.glNormal3f((b2 - t2) * (a3 - b3) - (b3 - t3) * (a2 - b2), (b3 - t3) * (a1 - b1) - (b1 - t1) * (a3 - b3),
  118. (b1 - t1) * (a2 - b2) - (b2 - t2) * (a1 - b1))
  119. b1 = x
  120. b2 = y
  121. b3 = z + 8
  122. if Texture:
  123. pgl.glTexCoord2f(0.5, 0.5)
  124. pgl.glVertex3f(b1, b2, b3)
  125. pgl.glEnd()
  126. for j in range(1, 27):
  127. pgl.glBegin(pgl.GL_TRIANGLE_STRIP)
  128. pgl.glColor3ub(0, 0, 0)
  129. x1 = a * math.cos(0)
  130. y1 = b * math.sin(0)
  131. a1 = x1 * math.cos(t * math.pi / 360) + y1 * math.sin(t * math.pi / 360)
  132. a2 = - x1 * math.sin(t * math.pi / 360) + y1 * math.cos(t * math.pi / 360)
  133. a3 = z + j * 8
  134. if Texture:
  135. pgl.glTexCoord2f(0.0, 0.0)
  136. a01 = a1
  137. a02 = a2
  138. a03 = a3
  139. print(a1,a2,a3)
  140. array.extend([a1,a2,a3])
  141. array_size += 3
  142. # pgl.glVertex3f(a1, a2, a3)
  143. b1 = x1 * math.cos((t + 16) * math.pi / 360) + y1 * math.sin((t + 16) * math.pi / 360)
  144. b2 = - x1 * math.sin((t + 16) * math.pi / 360) + y1 * math.cos((t + 16) * math.pi / 360)
  145. b3 = z + j * 8 + 8
  146. u = math.cos((t + 16) * math.pi / 360) + math.sin((t + 16) * math.pi / 360)
  147. v = (j - 1) / 26
  148. if Texture:
  149. pgl.glTexCoord2f(0.0, 0.0)
  150. array.extend([b1,b2,b3])
  151. print(b1,b2,b3)
  152. array_size += 3
  153. # pgl.glVertex3f(b1, b2, b3)
  154. b01 = b1
  155. b02 = b2
  156. b03 = b3
  157. for i in range(1, tr):
  158. pgl.glColor3ub(0, 0, 0)
  159. x1 = a * math.cos(i * 2 * math.pi / tr)
  160. y1 = b * math.sin(i * 2 * math.pi / tr)
  161. t1 = a1
  162. t2 = a2
  163. t3 = a3
  164. a1 = x1 * math.cos(t * math.pi / 360) + y1 * math.sin(t * math.pi / 360)
  165. a2 = - x1 * math.sin(t * math.pi / 360) + y1 * math.cos(t * math.pi / 360)
  166. a3 = z + j * 8
  167. pgl.glNormal3f((b2 - t2) * (a3 - b3) - (b3 - t3) * (a2 - b2), (b3 - t3) * (a1 - b1) - (b1 - t1) * (a3 - b3),
  168. (b1 - t1) * (a2 - b2) - (b2 - t2) * (a1 - b1))
  169. v = math.cos(t * math.pi / 360) + math.sin(t * math.pi / 360)
  170. if Texture:
  171. pgl.glTexCoord2f((i - 1) / tr, (26 - j) / 26)
  172. array.extend([a1,a2,a3])
  173. print(a1,a2,a3)
  174. array_size += 3
  175. # pgl.glVertex3f(a1, a2, a3)
  176. x1 = a * math.cos(i * 2 * math.pi / tr)
  177. y1 = b * math.sin(i * 2 * math.pi / tr)
  178. b1 = x1 * math.cos((t + 16) * math.pi / 360) + y1 * math.sin((t + 16) * math.pi / 360)
  179. b2 = - x1 * math.sin((t + 16) * math.pi / 360) + y1 * math.cos((t + 16) * math.pi / 360)
  180. b3 = z + j * 8 + 8
  181. v = math.cos((t + 16) * math.pi / 360) + math.sin((t + 16) * math.pi / 360)
  182. if Texture:
  183. pgl.glTexCoord2f((i - 1) / tr, (25 - j) / 26)
  184. array.extend([b1,b2,b3])
  185. print(b1,b2,b3)
  186. array_size += 3
  187. # pgl.glVertex3f(b1, b2, b3)
  188. print(a01,a02,a03)
  189. array.extend([a01,a02,a03])
  190. array_size += 3
  191. # pgl.glVertex3f(a01, a02, a03)
  192. print(b01,b02,b03)
  193. array.extend([b01,b02,b03])
  194. array_size += 3
  195. # pgl.glVertex3f(b01, b02, b03)
  196. t = t + 16
  197. pgl.glEnd()
  198. pgl.glBegin(pgl.GL_TRIANGLE_STRIP)
  199. t = 416
  200. z = z + 144
  201. angle = angle + t
  202. for i in range(0, tr + 1):
  203. pgl.glColor3ub(0, 0, 0)
  204. u = math.cos(i * 2 * math.pi / tr)
  205. v = math.sin(i * 2 * math.pi / tr)
  206. x1 = x + a * u
  207. y1 = y + b * v
  208. x2 = x1 * math.cos(t * math.pi / 360) + y1 * math.sin(t * math.pi / 360)
  209. y2 = - x1 * math.sin(t * math.pi / 360) + y1 * math.cos(t * math.pi / 360)
  210. if i >= 1:
  211. t1 = a1
  212. t2 = a2
  213. t3 = a3
  214. a1 = x2
  215. a2 = y2
  216. a3 = z + 72
  217. if Texture:
  218. pgl.glTexCoord2f(u * 0.5 + 0.5, v * 0.5 + 0.5)
  219. pgl.glVertex3f(a1, a2, a3)
  220. if i >= 1:
  221. pgl.glNormal3f((b2 - t2) * (a3 - b3) - (b3 - t3) * (a2 - b2), (b3 - t3) * (a1 - b1) - (b1 - t1) * (a3 - b3),
  222. (b1 - t1) * (a2 - b2) - (b2 - t2) * (a1 - b1))
  223. b1 = x
  224. b2 = y
  225. b3 = z + 72
  226. if Texture:
  227. pgl.glTexCoord2f(0.5, 0.5)
  228. pgl.glVertex3f(b1, b2, b3)
  229. angle = angle + i
  230. pgl.glEnd()
  231. pgl.glEnableClientState(pgl.GL_VERTEX_ARRAY)
  232. data = (pgl.GLfloat * len(array))(*array)
  233. pgl.glVertexPointer(3, pgl.GL_FLOAT, 0, data)
  234. pgl.glDrawArrays(pgl.GL_TRIANGLE_STRIP, 0, array_size)
  235. pgl.glDisableClientState(pgl.GL_VERTEX_ARRAY)
  236.  
  237.  
  238.  
  239. @win.event
  240. def on_draw():
  241. global x, y, z, start_loop, run_l, param, t_prev, zRotation,frame_counter
  242. start_time = time.time()
  243.  
  244. pgl.glClearColor(0.0, 0.0, 0.0, 0.0)
  245. pgl.glEnable(pgl.GL_DEPTH_TEST)
  246. if light:
  247. pgl.glEnable(pgl.GL_LIGHTING)
  248. pgl.glLightModelf(pgl.GL_LIGHT_MODEL_TWO_SIDE, pgl.GL_FALSE)
  249. pgl.glEnable(pgl.GL_NORMALIZE)
  250. else:
  251. pgl.glDisable(pgl.GL_LIGHTING)
  252. pgl.glDisable(pgl.GL_NORMALIZE)
  253. pgl.glClear(pgl.GL_COLOR_BUFFER_BIT | pgl.GL_DEPTH_BUFFER_BIT)
  254.  
  255. pgl.glLoadIdentity()
  256. pgl.glViewport(0, 0, 1300, 1000)
  257. pgl.glMatrixMode(pgl.GL_PROJECTION)
  258. pgl.glLoadIdentity()
  259. pgl.glOrtho(-1300 / 4, 1300 / 4, -1000 / 4, 1000 / 4, -400, 600)
  260. pgl.glMatrixMode(pgl.GL_MODELVIEW)
  261. pgl.glLoadIdentity()
  262.  
  263. if light:
  264. material_diffuse = [1.0, 1.0, 1.0, 1.0]
  265. pgl.glMaterialfv(pgl.GL_FRONT_AND_BACK, pgl.GL_DIFFUSE,
  266. (pgl.GLfloat * len(material_diffuse))(*material_diffuse))
  267.  
  268. if lamp1:
  269. light5_diffuse = [1.0, 0.0, 0.0];
  270.  
  271. light5_position = [0, 0, -1.0, 0];
  272.  
  273. pgl.glEnable(pgl.GL_LIGHT5);
  274.  
  275. pgl.glLightfv(pgl.GL_LIGHT5, pgl.GL_DIFFUSE, (pgl.GLfloat * len(light5_diffuse))(*light5_diffuse));
  276.  
  277. pgl.glLightfv(pgl.GL_LIGHT5, pgl.GL_POSITION, (pgl.GLfloat * len(light5_position))(*light5_position));
  278.  
  279. pgl.glLightf(pgl.GL_LIGHT5, pgl.GL_CONSTANT_ATTENUATION, 0.0);
  280.  
  281. pgl.glLightf(pgl.GL_LIGHT5, pgl.GL_LINEAR_ATTENUATION, 0.4);
  282.  
  283. pgl.glLightf(pgl.GL_LIGHT5, pgl.GL_QUADRATIC_ATTENUATION, 0.8);
  284. else:
  285. pgl.glDisable(pgl.GL_LIGHT5);
  286.  
  287.  
  288. if lamp2:
  289. light6_diffuse = [0.0, 1.0, 0.0];
  290.  
  291. light6_position = [1, 0.5, -1.0, 0];
  292.  
  293. pgl.glEnable(pgl.GL_LIGHT6);
  294.  
  295. pgl.glLightfv(pgl.GL_LIGHT6, pgl.GL_DIFFUSE, (pgl.GLfloat * len(light6_diffuse))(*light6_diffuse));
  296.  
  297. pgl.glLightfv(pgl.GL_LIGHT6, pgl.GL_POSITION, (pgl.GLfloat * len(light6_position))(*light6_position));
  298.  
  299. pgl.glLightf(pgl.GL_LIGHT6, pgl.GL_CONSTANT_ATTENUATION, 0.0);
  300.  
  301. pgl.glLightf(pgl.GL_LIGHT6, pgl.GL_LINEAR_ATTENUATION, 0.4);
  302.  
  303. pgl.glLightf(pgl.GL_LIGHT6, pgl.GL_QUADRATIC_ATTENUATION, 0.8);
  304. else:
  305. pgl.glDisable(pgl.GL_LIGHT6);
  306.  
  307.  
  308. if lamp3:
  309. light7_diffuse = [0.0, 0.0, 1.0];
  310.  
  311. light7_position = [0.5 * math.cos(4 * math.pi / 3), 0.5 * math.sin(4 * math.pi / 3), 1.0, 0];
  312.  
  313. pgl.glEnable(pgl.GL_LIGHT7);
  314.  
  315. pgl.glLightfv(pgl.GL_LIGHT7, pgl.GL_DIFFUSE, (pgl.GLfloat * len(light7_diffuse))(*light7_diffuse));
  316.  
  317. pgl.glLightfv(pgl.GL_LIGHT7, pgl.GL_POSITION, (pgl.GLfloat * len(light7_position))(*light7_position));
  318.  
  319. pgl.glLightf(pgl.GL_LIGHT7, pgl.GL_CONSTANT_ATTENUATION, 0.0);
  320.  
  321. pgl.glLightf(pgl.GL_LIGHT7, pgl.GL_LINEAR_ATTENUATION, 0.4);
  322.  
  323. pgl.glLightf(pgl.GL_LIGHT7, pgl.GL_QUADRATIC_ATTENUATION, 0.8);
  324. else:
  325. pgl.glDisable(pgl.GL_LIGHT7);
  326.  
  327. pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_FILL)
  328. pgl.glPushMatrix()
  329. pgl.glTranslatef(x, y, z)
  330. pgl.glRotatef(xRotation, 1, 0, 0)
  331. pgl.glRotatef(yRotation, 0, 1, 0)
  332. pgl.glRotatef(zRotation, 0, 0, 1)
  333. pgl.glScalef(zoom, zoom, zoom)
  334.  
  335. draw_figure()
  336.  
  337. if start_loop:
  338. start_loop = False
  339. run_l = True
  340. x = ((1 - (param / 100)) ** 3) * (-180) + 3 * (param / 100) * ((1 - (param / 100)) ** 2) * (100) + 3 * (
  341. (param / 100) ** 2) * (
  342. 1 - (param / 100)) * (0) + ((param / 100) ** 3) * (200)
  343. y = ((1 - (param / 100)) ** 3) * 70 + 3 * (param / 100) * ((1 - (param / 100)) ** 2) * (140) + 3 * (
  344. (param / 100) ** 2) * (
  345. 1 - (param / 100)) * (140) + ((param / 100) ** 3) * (
  346. 70)
  347. z = ((1 - (param / 100)) ** 3) * 0 + 3 * (param / 100) * ((1 - (param / 100)) ** 2) * (100) + 3 * (
  348. (param / 100) ** 2) * (
  349. 1 - (param / 100)) * (100) + ((param / 100) ** 3) * (
  350. 0)
  351.  
  352. elif run_l:
  353. if param < 100 and param > t_prev:
  354. zRotation += INCREMENT
  355. param = param + 1
  356. t_prev = t_prev + 1
  357. if (param == 100):
  358. t_prev = 105
  359. x = ((1 - (param / 100)) ** 3) * (-180) + 3 * (param / 100) * ((1 - (param / 100)) ** 2) * (100) + 3 * (
  360. (param / 100) ** 2) * (1 - (param / 100)) * (0) + ((param / 100) ** 3) * (200)
  361. y = ((1 - (param / 100)) ** 3) * 70 + 3 * (param / 100) * ((1 - (param / 100)) ** 2) * (140) + 3 * (
  362. (param / 100) ** 2) * (1 - (param / 100)) * (140) + ((param / 100) ** 3) * (
  363. 70)
  364. z = ((1 - (param / 100)) ** 3) * 0 + 3 * (param / 100) * ((1 - (param / 100)) ** 2) * (100) + 3 * (
  365. (param / 100) ** 2) * (1 - (param / 100)) * (100) + ((param / 100) ** 3) * (
  366. 0)
  367. if param > 0 and param < t_prev:
  368. zRotation -= INCREMENT
  369. param = param - 1
  370. t_prev = t_prev - 1
  371. if (param == 0):
  372. t_prev = -5
  373. x = ((1 - (param / 100)) ** 3) * (-180) + 3 * (param / 100) * ((1 - (param / 100)) ** 2) * (100) + 3 * (
  374. (param / 100) ** 2) * (1 - (param / 100)) * (0) + ((param / 100) ** 3) * (200)
  375. y = ((1 - (param / 100)) ** 3) * 70 + 3 * (param / 100) * ((1 - (param / 100)) ** 2) * (140) + 3 * (
  376. (param / 100) ** 2) * (1 - (param / 100)) * (140) + ((param / 100) ** 3) * (
  377. 70)
  378. z = ((1 - (param / 100)) ** 3) * 0 + 3 * (param / 100) * ((1 - (param / 100)) ** 2) * (100) + 3 * (
  379. (param / 100) ** 2) * (1 - (param / 100)) * (100) + ((param / 100) ** 3) * (
  380. 0)
  381.  
  382. frame_counter +=1
  383. # print("FPS: ", 1.0 / (time.time() - start_time))
  384. pgl.glPopMatrix()
  385.  
  386.  
  387. @win.event
  388. def on_text_motion(motion):
  389. global xRotation, yRotation, INCREMENT
  390. if motion == key.UP:
  391. xRotation -= INCREMENT
  392. elif motion == key.DOWN:
  393. xRotation += INCREMENT
  394. elif motion == key.LEFT:
  395. yRotation -= INCREMENT
  396. elif motion == key.RIGHT:
  397. yRotation += INCREMENT
  398.  
  399.  
  400. @win.event
  401. def on_key_press(symbol, modifiers):
  402. global transparant, data, tr, x, y, z, INCREMENT, zRotation, light, t, start_loop, run_l, Texture, tr_temp
  403. global xRotation, yRotation, zRotation,zoom, t_prev, texture_obj, lamp1, lamp2, lamp3, global_time, frame_counter
  404. if symbol == key.T and not transparant:
  405. transparant = True
  406. elif symbol == key.T and transparant:
  407. transparant = False
  408. elif symbol == key.W:
  409. x += 2.5
  410. elif symbol == key.S:
  411. x -= 2.5
  412. elif symbol == key.A:
  413. y -= 2.5
  414. elif symbol == key.D:
  415. y += 2.5
  416. elif symbol == key.Q:
  417. z -= 2.5
  418. elif symbol == key.E:
  419. z += 2.5
  420. elif symbol == key.L:
  421. light = not light
  422. lamp1 = True
  423. lamp2 = True
  424. lamp3 = True
  425. elif symbol == key.Z:
  426. zRotation += INCREMENT
  427. elif symbol == key.X:
  428. zRotation -= INCREMENT
  429. elif symbol == key.ESCAPE:
  430. path = 'PycharmProjects/untitled/data'
  431. fileName = 'data'
  432. data['transparant'] = transparant
  433. data['xRotation'] = xRotation
  434. data['yRotation'] = yRotation
  435. data['zRotation'] = zRotation
  436. data['zoom'] = zoom
  437. data['x'] = x
  438. data['y'] = y
  439. data['z'] = z
  440. data['light'] = light
  441. data['param'] = param
  442. data['t_prev'] = t_prev
  443. data['start_loop'] = start_loop
  444. data['run_l'] = run_l
  445. data['Texture'] = Texture
  446. data['lamp1'] = lamp1
  447. data['lamp2'] = lamp2
  448. data['lamp3'] = lamp3
  449. data['tr'] = tr
  450. data['tr_temp'] = tr_temp
  451. writeToJSONFile(path, fileName, data)
  452. pyglet.app.exit()
  453. elif symbol == key.P:
  454. tr += 5
  455. elif symbol == key.O:
  456. if tr > 6:
  457. tr -= 5
  458. elif symbol == key.SPACE:
  459. if not run_l:
  460. tr_temp = tr
  461. tr = 6
  462. start_loop = True
  463. else:
  464. run_l = False
  465. tr = tr_temp
  466. elif symbol == key.M:
  467. print("pressed")
  468. Texture = not Texture
  469. elif symbol == key._1:
  470. lamp1 = not lamp1
  471. elif symbol == key._2:
  472. lamp2 = not lamp2
  473. elif symbol == key._3:
  474. lamp3 = not lamp3
  475. elif symbol == key.F:
  476. print("FPS: ", frame_counter / (time.time() - global_time))
  477. frame_counter = 0
  478. global_time = time.time()
  479.  
  480.  
  481.  
  482. @win.event
  483. def on_mouse_scroll(x, y, scroll_x, scroll_y):
  484. global zoom
  485. if scroll_y < 0:
  486. zoom += 0.1
  487. elif scroll_y > 0 and zoom - 0.1 > 0:
  488. zoom -= 0.1
  489.  
  490.  
  491. @win.event
  492. def on_mouse_press(x, y, button, modifiers):
  493. if button == pyglet.window.mouse.LEFT:
  494. print(x, y)
  495.  
  496.  
  497. pyglet.clock.schedule_interval(run_loop, 0.1)
  498. pyglet.app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement