Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.39 KB | None | 0 0
  1. #вариант 12
  2.  
  3. import glfw
  4. import math
  5. from OpenGL.GL import *
  6. from OpenGL.GLU import *
  7. from PIL import Image
  8. import time
  9.  
  10. t = 0.08
  11. rotate_y = 0
  12. rotate_x = 0
  13. rotate_z = 0
  14. scale = 0.2
  15. n = 10
  16. a = 30
  17. r = 0.7
  18. tx = t
  19. ty = t
  20. tz = -t
  21. animation = False
  22. textrs = False
  23. mode = False
  24. press1 = False
  25. press2 = False
  26. press3 = False
  27.  
  28. def init():
  29. global press1, press2, press3
  30. if press1:
  31. glLightfv(GL_LIGHT0, GL_AMBIENT, [0, 0, 1, 1])
  32. elif not press1:
  33. glLightfv(GL_LIGHT0, GL_AMBIENT, [0, 0, 0, 1])
  34. if press2:
  35. glLightfv(GL_LIGHT0, GL_DIFFUSE, [1, 0, 0, 1])
  36. elif not press2:
  37. glLightfv(GL_LIGHT0, GL_DIFFUSE, [1, 1, 1, 1])
  38. if press3:
  39. glLightfv(GL_LIGHT0, GL_SPECULAR, [0, 1, 0, 1])
  40. elif not press3:
  41. glLightfv(GL_LIGHT0, GL_SPECULAR, [1, 1, 1, 1])
  42.  
  43.  
  44. def add_h(i):
  45. x = r * math.cos(0 * math.pi / 2 + deltaAngle * i)
  46. y = r * math.sin(0 * math.pi / 2 + deltaAngle * i)
  47. Vertices.extend([x, y, deltaHeight * i])
  48. x = r * math.cos(1 * math.pi / 2 + deltaAngle * i)
  49. y = r * math.sin(1 * math.pi / 2 + deltaAngle * i)
  50. Vertices.extend([x, y, deltaHeight * i])
  51. x = r * math.cos(2 * math.pi / 2 + deltaAngle * i)
  52. y = r * math.sin(2 * math.pi / 2 + deltaAngle * i)
  53. Vertices.extend([x, y, deltaHeight * i])
  54. x = r * math.cos(3 * math.pi / 2 + deltaAngle * i)
  55. y = r * math.sin(3 * math.pi / 2 + deltaAngle * i)
  56. Vertices.extend([x, y, deltaHeight * i])
  57. Colors.extend([0, 0, 1])
  58. Colors.extend([0, 1, 0])
  59. Colors.extend([1, 0, 0])
  60. Colors.extend([0, 0, 1])
  61. texels.extend([1,0])
  62. texels.extend([0,0])
  63. texels.extend([0,1])
  64. texels.extend([1,1])
  65.  
  66. def add_st(a,b,i):
  67. x = r * math.cos(a * math.pi / 2 + deltaAngle * i)
  68. y = r * math.sin(a * math.pi / 2 + deltaAngle * i)
  69. Vertices.extend([x, y, deltaHeight * i])
  70. x = r * math.cos(b * math.pi / 2 + deltaAngle * i)
  71. y = r * math.sin(b * math.pi / 2 + deltaAngle * i)
  72. Vertices.extend([x, y, deltaHeight * i])
  73. x = r * math.cos(b * math.pi / 2 + deltaAngle * (i + 1))
  74. y = r * math.sin(b * math.pi / 2 + deltaAngle * (i + 1))
  75. Vertices.extend([x, y, deltaHeight * (i + 1)])
  76. x = r * math.cos(a * math.pi / 2 + deltaAngle * (i + 1))
  77. y = r * math.sin(a * math.pi / 2 + deltaAngle * (i + 1))
  78. Vertices.extend([x, y, deltaHeight * (i + 1)])
  79. Colors.extend([0, 0, 1])
  80. Colors.extend([0, 1, 0])
  81. Colors.extend([1, 0, 0])
  82. Colors.extend([0, 0, 1])
  83. texels.extend([0,0])
  84. texels.extend([0,1])
  85. texels.extend([1,1])
  86. texels.extend([1,0])
  87.  
  88. def texture():
  89. global tex
  90. image = Image.open('/home/kossem/brick.bmp')
  91. image = image.tobytes("raw","RGBX", 0, -1 )
  92. tex = glGenTextures(1)
  93. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR)
  94. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR)
  95. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
  96. gluBuild2DMipmaps(GL_TEXTURE_2D, 3, 256, 256, GL_RGBA, GL_UNSIGNED_BYTE, image)
  97.  
  98.  
  99. def normals(dot1, dot2, dot3):
  100. vect1 = [dot2[0]-dot1[0], dot2[1]-dot1[1], dot2[2]-dot1[2]]
  101. vect2 = [dot3[0]-dot1[0], dot3[1]-dot1[1], dot3[2]-dot1[2]]
  102. return [vect1[1]*vect2[2]-vect1[2]*vect2[1], vect1[2]*vect2[0]-vect1[0]*vect2[2], vect1[0]*vect2[1]-vect1[1]*vect2[0]]
  103.  
  104. def light(L):
  105. if L:
  106. glEnable(GL_LIGHTING)
  107. glEnable(GL_LIGHT0)
  108. glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, math.pi)
  109. glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 0)
  110. glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, [0, 0, -1])
  111.  
  112. def anim():
  113. global Vertices, tx, ty, tz
  114. i = 0
  115. for i in range (len(Vertices)):
  116. if i%3 == 0:
  117. Vertices[i] += tx
  118. elif i%3 == 1:
  119. Vertices[i] += ty
  120. elif i%3 == 2:
  121. Vertices[i] += tz
  122.  
  123. i = 0
  124. for i in range (len(Vertices)):
  125. if i%3 == 0 and Vertices[i] >= 3:
  126. tx = -t
  127. if i%3 == 0 and Vertices[i] <= -3:
  128. tx = t
  129. if i%3 == 1 and Vertices[i] >= 3:
  130. ty = -t
  131. if i%3 == 1 and Vertices[i] <= -3:
  132. ty = t
  133. if i%3 == 2 and Vertices[i] >= 3:
  134. tz = -t
  135. if i%3 == 2 and Vertices[i] <= -3:
  136. tz = t
  137.  
  138. def start_point():
  139. global Vertices, Colors, n, angle, a, deltaHeight, deltaAngle, Normals, texels, textrs
  140. Normals = list()
  141. Vertices = list()
  142. Colors = list()
  143. texels = list()
  144. if n <= 0:
  145. n = 10
  146. height = 2
  147. angle = (a * math.pi) / 180
  148. deltaHeight = height / n
  149. deltaAngle = angle / n
  150. add_h(0)
  151. for i in range(n):
  152. if i != n - 1:
  153. add_st(0, 1, i)
  154. add_st(1, 2, i)
  155. add_st(2, 3, i)
  156. add_st(3, 0, i)
  157. else:
  158. add_h(n-1)
  159. i = 0
  160. print(int(len(Vertices) / 12))
  161. while i < len(Vertices):
  162. if i == 0:
  163. Normals.extend([0, 0, -1]*4)
  164. i += 12
  165. elif i == len(Vertices)-12:
  166. Normals.extend([0, 0, 1]*4)
  167. i += 12
  168. else:
  169. Normals.extend(normals([Vertices[i], Vertices[i+1], Vertices[i+2]], [Vertices[i+3], Vertices[i+4], Vertices[i+5]], [Vertices[i+6], Vertices[i+7], Vertices[i+8]])*4)
  170. i += 12
  171. i = 0
  172.  
  173.  
  174. def key_callback(window, key, scancode, action, mods):
  175. global rotate_y, rotate_x, scale, n, mode, a, animation, textrs, press1, press2, press3, rotate_z
  176. if action == glfw.PRESS and key == glfw.KEY_ESCAPE:
  177. glfw.set_window_should_close(window, 1)
  178. return
  179. elif action == glfw.PRESS and key == glfw.KEY_Y and animation == False:
  180. animation = True
  181. elif action == glfw.PRESS and key == glfw.KEY_Y and animation == True:
  182. animation = False
  183. elif key == glfw.KEY_RIGHT:
  184. rotate_y += 5
  185. elif key == glfw.KEY_LEFT:
  186. rotate_y -= 5
  187. elif key == glfw.KEY_O:
  188. rotate_z += 5
  189. elif key == glfw.KEY_P:
  190. rotate_z -= 5
  191. if key == glfw.KEY_UP:
  192. rotate_x += 5
  193. elif key == glfw.KEY_DOWN:
  194. rotate_x -= 5
  195. elif action == glfw.PRESS and key == glfw.KEY_A:
  196. scale += 0.1
  197. elif action == glfw.PRESS and key == glfw.KEY_Z and scale > 0.2:
  198. scale -= 0.1
  199. elif action == glfw.PRESS and key == glfw.KEY_T and textrs == False :
  200. print("TEXTURE MOD ON")
  201. glEnable(GL_TEXTURE_2D)
  202. textrs = True
  203. set_up()
  204. elif action == glfw.PRESS and key == glfw.KEY_T and textrs == True:
  205. print("TEXTURE MOD OFF")
  206. glDisable(GL_TEXTURE_2D)
  207. textrs = False
  208. set_up()
  209. elif action == glfw.PRESS and key == glfw.KEY_M and mode == True:
  210. mode = False
  211. set_up()
  212. elif action == glfw.PRESS and key == glfw.KEY_M and mode == False:
  213. mode = True
  214. set_up()
  215. elif action == glfw.PRESS and key == glfw.KEY_S:
  216. n += 10
  217. start_point()
  218. set_up()
  219. elif action == glfw.PRESS and key == glfw.KEY_X:
  220. n -= 10
  221. start_point()
  222. set_up()
  223. elif action == glfw.PRESS and key == glfw.KEY_D:
  224. a += 15
  225. start_point()
  226. set_up()
  227. elif action == glfw.PRESS and key == glfw.KEY_C:
  228. a -= 15
  229. start_point()
  230. set_up()
  231. if action == glfw.PRESS and key == glfw.KEY_9 and not press1:
  232. press1 = True
  233. glLightfv(GL_LIGHT0, GL_AMBIENT, [0, 0, 1, 1])
  234. elif action == glfw.PRESS and key == glfw.KEY_9 and press1:
  235. press1 = False
  236. glLightfv(GL_LIGHT0, GL_AMBIENT, [0, 0, 0, 1])
  237. if action == glfw.PRESS and key == glfw.KEY_8 and not press2:
  238. glLightfv(GL_LIGHT0, GL_DIFFUSE, [1,0,0,1])
  239. press2 = True
  240. elif action == glfw.PRESS and key == glfw.KEY_8 and press2:
  241. glLightfv(GL_LIGHT0, GL_DIFFUSE, [1,1,1,1])
  242. press2 = False
  243. if action == glfw.PRESS and key == glfw.KEY_7 and not press3:
  244. glLightfv(GL_LIGHT0, GL_SPECULAR, [0,1,0,1])
  245. press3 = True
  246. elif action == glfw.PRESS and key == glfw.KEY_7 and press3:
  247. glLightfv(GL_LIGHT0, GL_SPECULAR, [1,1,1,1])
  248. press3 = False
  249. if action == glfw.PRESS and key == glfw.KEY_5:
  250. save_f()
  251. if action == glfw.PRESS and key == glfw.KEY_6:
  252. load_f()
  253. init()
  254. if action == glfw.PRESS and key == glfw.KEY_L:
  255. print("LIGHT MOD ON")
  256. light(True)
  257. set_up()
  258.  
  259. def save_f():
  260. global animation, t, rotate_x, textrs, rotate_y, n, scale, a, mode, press1, press2, press3
  261. f = open('text.txt', 'w')
  262. if animation: f.write("True"+'\t')
  263. else: f.write("False"+'\t')
  264. if textrs: f.write("True"+'\t')
  265. else: f.write("False"+'\t')
  266. f.write(str(t)+'\t')
  267. f.write(str(rotate_x)+'\t')
  268. f.write(str(rotate_y)+'\t')
  269. f.write(str(n)+'\t')
  270. f.write(str(scale)+'\t')
  271. f.write(str(a)+'\t')
  272. if press1: f.write("True"+'\t')
  273. else: f.write("False"+'\t')
  274. if press2: f.write("True"+'\t')
  275. else: f.write("False"+'\t')
  276. if press3: f.write("True"+'\t')
  277. else: f.write("False"+'\t')
  278. f.write(str(mode)+'\t')
  279. f.close()
  280.  
  281.  
  282. def load_f():
  283. global animation, t, rotate_x, textrs, rotate_y, n, scale, a, mode, press1, press2, press3
  284. with open('text.txt','r') as f:
  285. for line in f:
  286. words = line.split()
  287. if words[0] == 'False': animation = False
  288. else: animation = True
  289. if words[1] == 'False': textrs = False
  290. else: textrs = True
  291. t = float(words[2])
  292. rotate_x = int(words[3])
  293. rotate_y = int(words[4])
  294. n = int(words[5])
  295. scale = float(words[6])
  296. a = int(words[7])
  297. if words[8] == 'False': press1 = False
  298. else: press1 = True
  299. if words[9] == 'False': press2 = False
  300. else: press2 = True
  301. if words[10] == 'False': press3 = False
  302. else: press3 = True
  303. if words[11] == 'False': mode = False
  304. else: mode = True
  305.  
  306.  
  307. def size_callback(window, width, height):
  308. if width < height:
  309. glViewport(0, 0, width, width)
  310. else:
  311. glViewport(0, 0, height, height)
  312.  
  313.  
  314. def prism():
  315. global Vertices, Colors, Normals, texels, animation, textrs, mode
  316. if animation:
  317. anim()
  318. glEnableClientState(GL_VERTEX_ARRAY)
  319. glEnableClientState(GL_COLOR_ARRAY)
  320. glEnableClientState(GL_NORMAL_ARRAY)
  321. glEnableClientState(GL_TEXTURE_COORD_ARRAY)
  322. if textrs:
  323. glEnable(GL_TEXTURE_2D)
  324. else:
  325. glDisable(GL_TEXTURE_2D)
  326. glColorPointer(3, GL_FLOAT, 0, Colors)
  327. glVertexPointer(3, GL_FLOAT, 0, Vertices)
  328. glNormalPointer(GL_FLOAT, 0, Normals)
  329. glTexCoordPointer(2, GL_FLOAT, 0, texels)
  330. if mode:
  331. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
  332. else:
  333. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
  334. glDrawArrays(GL_QUADS, 0, int(len(Vertices) / 3 ))
  335. glDisableClientState(GL_VERTEX_ARRAY)
  336. glDisableClientState(GL_COLOR_ARRAY)
  337. glDisableClientState(GL_NORMAL_ARRAY)
  338. glDisableClientState(GL_TEXTURE_COORD_ARRAY)
  339.  
  340.  
  341.  
  342. def Ort_Matrix(md):
  343. glMatrixMode(GL_PROJECTION)
  344. glLoadIdentity()
  345. if md == "z":
  346. glMultMatrixf([1, 0, 0, 0,
  347. 0, 1, 0, 0,
  348. 0, 0, -1, 0,
  349. 0, 0, 0, 1])
  350. elif md == "x":
  351. glMultMatrixf([0, 0, -1, 0,
  352. 0, 1, 0, 0,
  353. -1, 0, 0, 0,
  354. 0, 0, 0, 1])
  355. elif md == "y":
  356. glMultMatrixf([1, 0, 0, 0,
  357. 0, 0, -1, 0,
  358. 0, -1, 0, 0,
  359. 0, 0, 0, 1])
  360. glMatrixMode(GL_MODELVIEW)
  361.  
  362. def draw_pr(md):
  363. Ort_Matrix(md)
  364. glLoadIdentity()
  365. glPushMatrix()
  366. glRotatef(rotate_x, 1.0, 0.0, 0.0)
  367. glRotatef(rotate_y, 0.0, 1.0, 0.0)
  368. glRotatef(rotate_z, 0.0, 0.0, 1.0)
  369. glScale(scale, scale, scale)
  370. glCallList(display)
  371. glPopMatrix()
  372.  
  373.  
  374. def draw(window):
  375. global rotate_y, rotate_x, scale, mode, position
  376. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  377. glEnable(GL_DEPTH_TEST)
  378. glDepthFunc(GL_LESS)
  379. width, height = glfw.get_window_size(window)
  380. #glViewport(int(width/2),int(height/2),int(width/2),int(height/2))
  381. draw_pr("x")
  382. #glViewport(0,0,int(width/2),int(height/2))
  383. #draw_pr("y")
  384. #glViewport(int(width/2),0,int(width/2),int(height/2))
  385. #draw_pr("z")
  386.  
  387. def set_up():
  388. global display
  389. display = glGenLists(1)
  390. glNewList(display,GL_COMPILE)
  391. prism()
  392. glEndList()
  393.  
  394. def main():
  395. if not glfw.init():
  396. return
  397.  
  398. global window
  399. window = glfw.create_window(800, 800, "Lab 3", None, None)
  400. if not window:
  401. glfw.terminate()
  402. return
  403. forCheck = 0
  404. sum_time = 0
  405. start_point()
  406. glfw.make_context_current(window)
  407. glfw.set_key_callback(window, key_callback)
  408. glfw.set_framebuffer_size_callback(window, size_callback)
  409. texture()
  410. set_up()
  411. while not glfw.window_should_close(window):
  412. start = time.clock()
  413. draw(window)
  414. glfw.swap_buffers(window)
  415. glfw.poll_events()
  416. time_from_start = time.clock()-start
  417. sum_time += time_from_start
  418. if forCheck == 50:
  419. print("time = ", round(sum_time/50.0, 5))
  420. forCheck = 0
  421. sum_time = 0
  422. forCheck += 1
  423. glfw.destroy_window(window)
  424. glfw.terminate()
  425.  
  426.  
  427. if __name__ == '__main__':
  428. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement