Advertisement
trishLEX

LAB3.V2

Mar 26th, 2017
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.00 KB | None | 0 0
  1. import glfw
  2. from OpenGL.GL import *
  3. import math
  4.  
  5.  
  6. def background():
  7.     glClearColor(0.8, 0.8, 0.8, 1.0)
  8.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  9.     glEnable(GL_DEPTH_TEST)
  10.  
  11.  
  12. global polygonMesh
  13. polygonMesh = []
  14.  
  15.  
  16. def setMesh(r, R, nsides, rings):  # r - inner radius, R - outer radius
  17.     for i in range(rings):
  18.         theta = float(i * 2.0 * math.pi / rings)
  19.         theta1 = float((i + 1) * 2.0 * math.pi / rings)
  20.         for j in range(nsides):
  21.             phi = float(j * 2.0 * math.pi / nsides)
  22.             phi1 = float((j + 1) * 2.0 * math.pi / nsides)
  23.  
  24.             p0 = [math.cos(theta) * (R + r * math.cos(phi)), - math.sin(theta) * (R + r * math.cos(phi)),
  25.                   r * math.sin(phi)]
  26.             p1 = [math.cos(theta1) * (R + r * math.cos(phi)), - math.sin(theta1) * (R + r * math.cos(phi)),
  27.                   r * math.sin(phi)]
  28.             p2 = [math.cos(theta1) * (R + r * math.cos(phi1)), - math.sin(theta1) * (R + r * math.cos(phi1)),
  29.                   r * math.sin(phi1)]
  30.             p3 = [math.cos(theta) * (R + r * math.cos(phi1)), - math.sin(theta) * (R + r * math.cos(phi1)),
  31.                   r * math.sin(phi1)]
  32.  
  33.             p = [p0, p1, p2, p3]
  34.  
  35.             polygonMesh.append(p)
  36.  
  37.  
  38. global viewportTorus
  39. global viewportCube
  40.  
  41.  
  42. def sizeCallback(window, width, height):
  43.     viewportCube[2] = int(width / 3)
  44.     viewportCube[3] = int(height / 3)
  45.     viewportTorus[0] = viewportCube[2]
  46.     viewportTorus[1] = viewportCube[3]
  47.     viewportTorus[2] = width - viewportCube[2]
  48.     viewportTorus[3] = height - viewportCube[3]
  49.  
  50.  
  51. def torus():
  52.     glPointSize(1)
  53.     glLineWidth(1)
  54.  
  55.     glViewport(viewportTorus[0], viewportTorus[1], viewportTorus[2], viewportTorus[3])
  56.  
  57.     glMatrixMode(GL_MODELVIEW)
  58.     glLoadIdentity()
  59.  
  60.     global rotate_x
  61.     global rotate_y
  62.     glRotatef(rotate_x, 1, 0, 0)
  63.     glRotatef(rotate_y, 0, 1, 0)
  64.  
  65.     glTranslatef(centr[0], centr[1], centr[2])
  66.  
  67.     global size
  68.     glScalef(size, size, size)
  69.  
  70.     for i in polygonMesh:
  71.         glBegin(GL_POLYGON)
  72.  
  73.         glColor(1, 0.5, 0)
  74.  
  75.         glVertex3fv(i[0])
  76.         glVertex3fv(i[1])
  77.         glVertex3fv(i[2])
  78.         glVertex3fv(i[3])
  79.  
  80.         glEnd()
  81.  
  82.  
  83. def cube():
  84.     glPointSize(1)
  85.     glLineWidth(1)
  86.  
  87.     glViewport(viewportCube[0], viewportCube[1], viewportCube[2], viewportCube[3])
  88.  
  89.     glBegin(GL_QUADS)
  90.  
  91.     glColor(1, 1, 1)
  92.     glVertex3f(- 0.5, - 0.5, - 0.5)
  93.     glVertex3f(0.5, - 0.5, - 0.5)
  94.     glVertex3f(0.5, 0.5, - 0.5)
  95.     glVertex3f(- 0.5, 0.5, - 0.5)
  96.  
  97.     glColor(1, 0, 0)
  98.     glVertex3f(- 0.5, - 0.5, 0.5)
  99.     glVertex3f(0.5, - 0.5, 0.5)
  100.     glVertex3f(0.5, 0.5, 0.5)
  101.     glVertex3f(- 0.5, 0.5, 0.5)
  102.  
  103.     glColor(0, 1, 0)
  104.     glVertex3f(0.5, - 0.5, - 0.5)
  105.     glVertex3f(0.5, - 0.5, 0.5)
  106.     glVertex3f(0.5, 0.5, 0.5)
  107.     glVertex3f(0.5, 0.5, - 0.5)
  108.  
  109.     glColor(0, 0, 1)
  110.     glVertex3f(- 0.5, - 0.5, - 0.5)
  111.     glVertex3f(- 0.5, - 0.5, 0.5)
  112.     glVertex3f(- 0.5, 0.5, 0.5)
  113.     glVertex3f(- 0.5, 0.5, - 0.5)
  114.  
  115.     glColor(1, 1, 0)
  116.     glVertex3f(- 0.5, 0.5, - 0.5)
  117.     glVertex3f(0.5, 0.5, - 0.5)
  118.     glVertex3f(0.5, 0.5, 0.5)
  119.     glVertex3f(- 0.5, 0.5, 0.5)
  120.  
  121.     glColor(1, 0, 1)
  122.     glVertex3f(- 0.5, - 0.5, - 0.5)
  123.     glVertex3f(0.5, - 0.5, - 0.5)
  124.     glVertex3f(0.5, - 0.5, 0.5)
  125.     glVertex3f(- 0.5, - 0.5, 0.5)
  126.  
  127.     glEnd()
  128.  
  129.  
  130. global solid
  131. solid = True
  132.  
  133.  
  134. def mouseCallback(window, button, action, mods):
  135.     global solid
  136.     if button == glfw.MOUSE_BUTTON_1 and action and not (solid):
  137.         solid = True
  138.     elif button == glfw.MOUSE_BUTTON_1 and action and solid:
  139.         solid = False
  140.  
  141.  
  142. global size
  143. size = 1
  144.  
  145.  
  146. def scrollCallback(window, xoffset, yoffset):
  147.     global size
  148.     if yoffset > 0:
  149.         size *= 1.1
  150.     if yoffset < 0:
  151.         size *= 0.9
  152.  
  153.  
  154. global centr
  155. centr = [0, 0, 0]
  156. global rotate_x
  157. rotate_x = 0
  158. global rotate_y
  159. rotate_y = 0
  160. global nsides
  161. nsides = 4
  162.  
  163.  
  164. def keyCallback(window, key, scancode, action, mods):
  165.     global rotate_x
  166.     global rotate_y
  167.     global nsides
  168.     global polygonMesh
  169.     if key == glfw.KEY_W and action:
  170.         centr[1] += 0.1
  171.     elif key == glfw.KEY_A and action:
  172.         centr[0] -= 0.1
  173.     elif key == glfw.KEY_D and action:
  174.         centr[0] += 0.1
  175.     elif key == glfw.KEY_S and action:
  176.         centr[1] -= 0.1
  177.     elif key == glfw.KEY_UP and action:
  178.         rotate_x += 10.0
  179.     elif key == glfw.KEY_DOWN and action:
  180.         rotate_x -= 10.0
  181.     elif key == glfw.KEY_LEFT and action:
  182.         rotate_y -= 10.0
  183.     elif key == glfw.KEY_RIGHT and action:
  184.         rotate_y += 10.0
  185.     elif key == glfw.KEY_KP_SUBTRACT and action and nsides > 4:
  186.         nsides -= 1
  187.         polygonMesh = []
  188.         setMesh(0.2, 0.4, nsides, nsides)
  189.     elif key == glfw.KEY_KP_ADD and action:
  190.         nsides += 1
  191.         polygonMesh = []
  192.         setMesh(0.2, 0.4, nsides, nsides)
  193.  
  194.  
  195. def draw():
  196.     global solid
  197.     global nsides
  198.     if solid:
  199.         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
  200.     else:
  201.         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
  202.     glMatrixMode(GL_PROJECTION)
  203.     glLoadIdentity()
  204.     ###фронтальная диметрия
  205.     # alpha = math.pi / 4
  206.     # l = 0.5
  207.     # a = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1]
  208.     # b = [1, 0, 0, 0, 0, 1, 0, 0, -l*math.cos(alpha), -l*math.sin(alpha), 1, 0, 0, 0, 0, 1]
  209.     # glMultMatrixf(a)
  210.     # glMultMatrixf(b)
  211.     theta = math.asin(0.5 / math.sqrt(2))
  212.     phi = math.asin(0.5 / math.sqrt(2 - 0.25))
  213.     a = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1]
  214.     b = [1, 0, 0, 0, 0, math.cos(theta), math.sin(theta), 0, 0, -math.sin(theta), math.cos(theta), 0, 0, 0, 0, 1]
  215.     c = [math.cos(phi), 0, -math.sin(phi), 0, 0, 1, 0, 0, math.sin(phi), 0, math.cos(phi), 0, 0, 0, 0, 1]
  216.     glMultMatrixf(a)
  217.     glMultMatrixf(b)
  218.     glMultMatrixf(c)
  219.     glMatrixMode(GL_MODELVIEW)
  220.     glLoadIdentity()
  221.     cube()
  222.     torus()
  223.  
  224.  
  225. class Drawer:
  226.     window = False
  227.  
  228.     def __init__(self):
  229.         if not glfw.init():
  230.             return
  231.  
  232.         self.window = glfw.create_window(900, 900, "Lab3", None, None)
  233.         if not self.window:
  234.             glfw.terminate()
  235.             return
  236.  
  237.         glfw.make_context_current(self.window)
  238.  
  239.         global viewportCube
  240.         global viewportTorus
  241.         size = glfw.get_window_size(self.window)
  242.         viewportCube = [0, 0, int(size[0] / 3), int(size[1] / 3)]
  243.         viewportTorus = [viewportCube[2], viewportCube[3], size[0] - viewportCube[2], size[1] - viewportCube[3]]
  244.  
  245.         glfw.set_mouse_button_callback(self.window, mouseCallback)
  246.         glfw.set_key_callback(self.window, keyCallback)
  247.         glfw.set_scroll_callback(self.window, scrollCallback)
  248.         glfw.set_window_size_callback(self.window, sizeCallback)
  249.  
  250.         setMesh(0.2, 0.5, 4, 4)
  251.  
  252.     def startLoop(self):
  253.         while not glfw.window_should_close(self.window):
  254.             background()
  255.             draw()
  256.  
  257.             glfw.swap_buffers(self.window)
  258.             glfw.poll_events()
  259.  
  260.         glfw.terminate()
  261.  
  262.  
  263. drawer = Drawer()
  264. drawer.startLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement