Guest User

Untitled

a guest
Jan 19th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. import pygame
  2. from pygame.locals import *
  3. from OpenGL.GL import *
  4. from OpenGL.GLU import *
  5. import math
  6. import os
  7. import numpy as np
  8.  
  9. size = 30
  10. speed = 500
  11. amplitude_amplificator = 80
  12.  
  13.  
  14. color_table = ((1,0,0),
  15. (0,1,0),
  16. (0,0,1),
  17. (1,1,0),
  18. (1,0,1),
  19. (0,1,1),
  20. (1,0.5,0),
  21. (0.5,1,0),
  22. (0.5,1,0.5),
  23. (0,0.5,0)
  24. )
  25.  
  26.  
  27. locations = ((0,-975, 0),
  28. (0, 975, 0),
  29. (-1273,-975, 0),
  30. (-1273, 975, 0),
  31. (-2482, -975, 0),
  32. (-2482, 975, 0),
  33. (-3737, -975, 0),
  34. (-3737, 975, 0)
  35. )
  36.  
  37. lines = ((0,2),
  38. (2, 4),
  39. (4, 6),
  40. (1, 3),
  41. (3, 5),
  42. (5, 7),
  43. (0, 1),
  44. (2, 3),
  45. (4, 5),
  46. (6, 7),
  47.  
  48. )
  49.  
  50. amplitudes = ((3.38829249165602, 2.38305866657961, 2.52151563664636),
  51. (5.08487438107113, 2.36432294667884, 3.0843991148654),
  52. (3.44312569856563, 1.23112415468012, 1.29869765112226),
  53. (4.0421066637935, 1.40655294535107, 1.36083778879317),
  54. (3.78074337117764, 0.648255908566916, 0.752239154016233),
  55. (5.08887133464996, 0.607037324785205, 0.543523234321567),
  56. (4.49095206021647, 0.432732677308301, 2.18289872563964),
  57. (5.14707697114171, 0.335119576625248, 2.15666871777855)
  58. )
  59.  
  60. phases = ((-146.873017352057,0,-95.316526141321),
  61. (-149.008372080797, 5.24886681104675, 78.3075732082314),
  62. (-148.241584335287, 5.54327579087787, -118.279685417256),
  63. (-151.844141596427, 6.48705235395368, -113.246406750217),
  64. (-148.14233553496, 27.9523171503408, 65.8254568277543),
  65. (-157.058723259828, 38.8760924034639, 85.2339573112435),
  66. (-153.417593784393, -120.329988461629, 16.0421535833842),
  67. (-156.779107376825, 83.2350395893582, 10.7592173681729)
  68. )
  69.  
  70. # DRAW CUBE
  71. def Cube(po,si,co):
  72.  
  73. POS = (
  74. (po[0]+si, po[1]-si, po[2]-si),
  75. (po[0]+si, po[1]+si, po[2]-si),
  76. (po[0]-si, po[1]+si, po[2]-si),
  77. (po[0]-si, po[1]-si, po[2]-si),
  78. (po[0]+si, po[1]-si, po[2]+si),
  79. (po[0]+si, po[1]+si, po[2]+si),
  80. (po[0]-si, po[1]-si, po[2]+si),
  81. (po[0]-si, po[1]+si, po[2]+si)
  82. )
  83.  
  84. edges = (
  85. (0,1),
  86. (0,3),
  87. (0,4),
  88. (2,1),
  89. (2,3),
  90. (2,7),
  91. (6,3),
  92. (6,4),
  93. (6,7),
  94. (5,1),
  95. (5,4),
  96. (5,7)
  97. )
  98.  
  99. glBegin(GL_LINES)
  100. for edge in edges:
  101. for vertex in edge:
  102. glColor3f(co[0],co[1],co[2])
  103. glVertex3fv(POS[vertex])
  104. glEnd()
  105.  
  106. #DRAW ORIGINAL SHAPE IN LINES
  107. def Line_orig(po):
  108.  
  109. glBegin(GL_LINES)
  110. for edge in po:
  111. for vertex in edge:
  112. glVertex3fv(locations[vertex])
  113. glEnd()
  114.  
  115. #Hemisphere mapping
  116.  
  117. def map_hemisphere(x,y):
  118. z = math.sqrt(abs(1-math.pow(x,2)-math.pow(y,2)))
  119. return z
  120.  
  121. # Calculate angle of two spatial vectors
  122.  
  123. def angle_calculation(a,b):
  124.  
  125. r = math.degrees(math.acos((np.dot(a, b))/(np.linalg.norm(a)*np.linalg.norm(b))))
  126.  
  127. return r
  128.  
  129.  
  130. def main():
  131.  
  132. mouse_pressed = 0
  133. pygame.init()
  134. display = (1200,800)
  135. pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
  136.  
  137.  
  138. gluPerspective(45, (display[0]/display[1]), 0.1, 30000.0)
  139.  
  140. glTranslatef(0,0.0,-10000)
  141.  
  142. #glRotatef(90, 1, 0, 0)
  143.  
  144.  
  145. while True:
  146. for event in pygame.event.get():
  147. if event.type == pygame.QUIT:
  148. pygame.quit()
  149. quit()
  150.  
  151.  
  152. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
  153.  
  154. time = pygame.time.get_ticks()/1000
  155.  
  156. norm_mouse_pos = (2*pygame.mouse.get_pos()[0]/display[0]-1,2*pygame.mouse.get_pos()[1]/display[1]-1,map_hemisphere(2*pygame.mouse.get_pos()[0]/display[0]-1,2*pygame.mouse.get_pos()[1]/display[1]-1))
  157.  
  158.  
  159. if pygame.mouse.get_pressed()[0]==1:
  160.  
  161. if mouse_pressed == 0:
  162.  
  163. mouse_pressed = 1
  164.  
  165. clear = lambda: os.system('cls')
  166. clear()
  167.  
  168. p1 = (norm_mouse_pos[0],norm_mouse_pos[1],map_hemisphere(norm_mouse_pos[0],norm_mouse_pos[1]))
  169. print(p1)
  170.  
  171. else:
  172.  
  173. p2 = (norm_mouse_pos[0],norm_mouse_pos[1],map_hemisphere(norm_mouse_pos[0],norm_mouse_pos[1]))
  174. cist = np.cross(p1, p2)
  175. print(angle_calculation(p1,p2))
  176. glRotatef( angle_calculation(p1,p2) , -cist[0] , cist[1] , cist[2] )
  177.  
  178. else:
  179.  
  180. mouse_pressed = 0
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187. # Translation of the model via keyboard handling
  188.  
  189. keys=pygame.key.get_pressed()
  190.  
  191. if keys[K_w]:
  192. glTranslatef(0, 100, 0)
  193.  
  194. if keys[K_s]:
  195. glTranslatef(0, -100, 0)
  196.  
  197. if keys[K_a]:
  198. glTranslatef(-100, 0, 0)
  199.  
  200. if keys[K_d]:
  201. glTranslatef(100, 0, 0)
  202.  
  203. # Drawing the Cubes at Nodes Loactions
  204.  
  205. for item, el in enumerate(locations):
  206. Cube((el[0] + amplitudes[item][0]*math.sin(time + phases[item][0]*(3.1415927/180))*amplitude_amplificator,
  207. el[1] + amplitudes[item][1]*math.sin(time + phases[item][1]*(3.1415927/180))*amplitude_amplificator,
  208. el[2] + amplitudes[item][2]*math.sin(time + phases[item][2]*(3.1415927/180))*amplitude_amplificator
  209. ), size, color_table[item])
  210.  
  211. # Drawing the Original Shapes (Specified nodes in Lines Tuple)
  212.  
  213. Line_orig(lines)
  214.  
  215. # Drawing the Deformed Shape
  216.  
  217. glBegin(GL_LINES)
  218. for edge in lines:
  219. for vertex in edge:
  220. glVertex3fv((locations[vertex][0] + amplitudes[vertex][0]*math.sin(time + phases[vertex][0]*(3.1415927/180))*amplitude_amplificator,
  221. locations[vertex][1] + amplitudes[vertex][1]*math.sin(time + phases[vertex][1]*(3.1415927/180))*amplitude_amplificator ,
  222. locations[vertex][2] + amplitudes[vertex][2]*math.sin(time + phases[vertex][2]*(3.1415927/180))*amplitude_amplificator,
  223. ))
  224. glEnd()
  225.  
  226. # OpenGL Management
  227.  
  228.  
  229.  
  230. pygame.display.flip()
  231. pygame.time.wait(10)
  232.  
  233. main()
Add Comment
Please, Sign In to add comment