Advertisement
trishLEX

Lab6.physicsIsMade

May 5th, 2017
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.56 KB | None | 0 0
  1. import glfw
  2. from OpenGL.GL import *
  3. import math
  4.  
  5. #image = open('E:\Sorry\Documents\PycharmProjects\FirstOpenGL\Texture.bmp')
  6.  
  7. def background():
  8.     glClearColor(0.8, 0.8, 0.8, 1.0)
  9.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  10.     glEnable(GL_DEPTH_TEST)
  11.  
  12. global counter
  13. counter = 0
  14. def mouseCallback(window, button, action, mods):
  15.     global counter
  16.     size = 3
  17.     if button == glfw.MOUSE_BUTTON_1 and action and torus.isWire:
  18.         torus.isWire = False
  19.     elif button == glfw.MOUSE_BUTTON_1 and action and not torus.isWire:
  20.         torus.isWire = True
  21.     elif button == glfw.MOUSE_BUTTON_2 and action:
  22.         counter += 1
  23.         print(counter, size)
  24.         torus.currentGL_Pname = torus.GL_Pnames[counter % size]
  25.         print("NOW GLpname is", torus.currentGL_Pname)
  26.  
  27.  
  28. def scrollCallback(window, xoffset, yoffset):
  29.     if torus.currentGL_Pname == GL_LIGHT_MODEL_AMBIENT:
  30.         if yoffset > 0 and torus.GL_Pnames_dict.get(torus.currentGL_Pname)[0] < 1:
  31.             value = torus.GL_Pnames_dict.get(torus.currentGL_Pname)
  32.             for i in range(3):
  33.                 value[i] += 0.1
  34.             torus.GL_Pnames_dict.update({torus.currentGL_Pname : value})
  35.         elif yoffset < 0 and torus.GL_Pnames_dict.get(torus.currentGL_Pname)[0] > 0:
  36.             value = torus.GL_Pnames_dict.get(torus.currentGL_Pname)
  37.             for i in range(3):
  38.                 value[i] -= 0.1
  39.             torus.GL_Pnames_dict.update({torus.currentGL_Pname: value})
  40.     else:
  41.         if yoffset > 0:
  42.             value = torus.GL_Pnames_dict.get(torus.currentGL_Pname)
  43.             value = not value
  44.             torus.GL_Pnames_dict.update({torus.currentGL_Pname: value})
  45.         elif yoffset < 0:
  46.             value = torus.GL_Pnames_dict.get(torus.currentGL_Pname)
  47.             value = not value
  48.             torus.GL_Pnames_dict.update({torus.currentGL_Pname: value})
  49.     print(torus.GL_Pnames_dict)
  50.  
  51.  
  52.  
  53. def keyCallback(window, key, scancode, action, mods):
  54.     if key == glfw.KEY_W and action:
  55.         torus.center[1] += 0.1
  56.     elif key == glfw.KEY_A and action:
  57.         torus.center[0] -= 0.1
  58.     elif key == glfw.KEY_D and action:
  59.         torus.center[0] += 0.1
  60.     elif key == glfw.KEY_S and action:
  61.         torus.center[1] -= 0.1
  62.     elif key == glfw.KEY_UP and action:
  63.         torus.rotate_x += 10.0
  64.     elif key == glfw.KEY_DOWN and action:
  65.         torus.rotate_x -= 10.0
  66.     elif key == glfw.KEY_LEFT and action:
  67.         torus.rotate_y += 10.0
  68.     elif key == glfw.KEY_RIGHT and action:
  69.         torus.rotate_y -= 10.0
  70.     elif key == glfw.KEY_KP_SUBTRACT and action and torus.nsides > 4:
  71.         torus.nsides -= 1
  72.         torus.rings -= 1
  73.         torus.polygonMesh = []
  74.         torus.setMesh()
  75.     elif key == glfw.KEY_KP_ADD and action:
  76.         torus.nsides += 1
  77.         torus.rings += 1
  78.         torus.polygonMesh = []
  79.         torus.setMesh()
  80.     elif key == glfw.KEY_ENTER and action:
  81.         torus.center = h0
  82.         torus.drop = True
  83.  
  84. class Torus:
  85.     def __init__(self):
  86.         self.polygonMesh = []
  87.         self.normals = []
  88.         self.center = [0, 0, 0]
  89.         #self.size = 1
  90.         self.nsides = 4
  91.         self.rings = 4
  92.         self.r = 0.2
  93.         self.R = 0.4
  94.         self.rotate_x = 0
  95.         self.rotate_y = 0
  96.         self.isWire = False
  97.         self.setMesh()
  98.         self.GL_Pnames_dict = {GL_LIGHT_MODEL_AMBIENT : [0.2, 0.2, 0.2, 1.0], GL_LIGHT_MODEL_LOCAL_VIEWER : GL_FALSE, GL_LIGHT_MODEL_TWO_SIDE : GL_TRUE}
  99.         self.GL_Pnames = (GL_LIGHT_MODEL_AMBIENT, GL_LIGHT_MODEL_LOCAL_VIEWER, GL_LIGHT_MODEL_TWO_SIDE)
  100.         self.currentGL_Pname = GL_LIGHT_MODEL_AMBIENT
  101.         self.drop = False
  102.  
  103.     def setMesh(self):
  104.         for i in range(self.rings):
  105.             theta = i * 2.0 * math.pi / self.rings
  106.             theta1 = ((i + 1) % self.rings) * 2.0 * math.pi / self.rings
  107.             for j in range(self.nsides):
  108.                 phi = j * 2.0 * math.pi / self.nsides
  109.                 phi1 = ((j + 1) % self.nsides) * 2.0 * math.pi / self.nsides
  110.  
  111.                 p0 = [math.cos(theta) * (self.R + self.r * math.cos(phi)), - math.sin(theta) * (self.R + self.r * math.cos(phi)),
  112.                       self.r * math.sin(phi)]
  113.                 p1 = [math.cos(theta1) * (self.R + self.r * math.cos(phi)), - math.sin(theta1) * (self.R + self.r * math.cos(phi)),
  114.                       self.r * math.sin(phi)]
  115.                 p2 = [math.cos(theta1) * (self.R + self.r * math.cos(phi1)), - math.sin(theta1) * (self.R + self.r * math.cos(phi1)),
  116.                       self.r * math.sin(phi1)]
  117.                 p3 = [math.cos(theta) * (self.R + self.r * math.cos(phi1)), - math.sin(theta) * (self.R + self.r * math.cos(phi1)),
  118.                       self.r * math.sin(phi1)]
  119.  
  120.                 p = [p0, p1, p2, p3]
  121.  
  122.                 n0 = [math.cos(theta) * (math.cos(phi)), -math.sin(theta) * (math.cos(phi)), math.sin(phi)]
  123.                 n1 = [math.cos(theta1) * (math.cos(phi)), -math.sin(theta1) * (math.cos(phi)), math.sin(phi)]
  124.                 n2 = [math.cos(theta1) * (math.cos(phi1)), -math.sin(theta1) * (math.cos(phi1)), math.sin(phi1)]
  125.                 n3 = [math.cos(theta) * (math.cos(phi1)), -math.sin(theta) * (math.cos(phi1)), math.sin(phi1)]
  126.  
  127.                 n = [n0, n1, n2, n3]
  128.  
  129.                 self.polygonMesh.append(p)
  130.                 self.normals.append(n)
  131.  
  132.  
  133. def drawTorus():
  134.     glPointSize(1)
  135.     glLineWidth(2)
  136.  
  137.     if torus.isWire:
  138.         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
  139.     else:
  140.         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
  141.  
  142.     glMatrixMode(GL_PROJECTION)
  143.     glLoadIdentity()
  144.  
  145.     theta = math.asin(0.5 / math.sqrt(2))
  146.     phi = math.asin(0.5 / math.sqrt(2 - 0.25))
  147.  
  148.     a = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1]
  149.     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]
  150.     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]
  151.  
  152.     glMultMatrixf(a)
  153.     glMultMatrixf(b)
  154.     glMultMatrixf(c)
  155.  
  156.     glMatrixMode(GL_MODELVIEW)
  157.     glLoadIdentity()
  158.  
  159.     glRotatef(torus.rotate_x, 1, 0, 0)
  160.     glRotatef(torus.rotate_y, 0, 1, 0)
  161.  
  162.     glTranslatef(torus.center[0], torus.center[1], torus.center[2])
  163.  
  164.     count = torus.rings * torus.nsides
  165.     for i in range(count):
  166.         glEnable(GL_NORMALIZE)
  167.         glBegin(GL_POLYGON)
  168.  
  169.         glColor(1, 0.5, 0)
  170.  
  171.         glVertex3fv(torus.polygonMesh[i][3])
  172.         glVertex3fv(torus.polygonMesh[i][2])
  173.         glVertex3fv(torus.polygonMesh[i][1])
  174.         glVertex3fv(torus.polygonMesh[i][0])
  175.  
  176.         #glNormal3fv(torus.normals[i][3])
  177.         #glNormal3fv(torus.normals[i][2])
  178.         #glNormal3fv(torus.normals[i][1])
  179.         #glNormal3fv(torus.normals[i][0])
  180.  
  181.         glEnd()
  182.  
  183.  
  184. def makeLighting():
  185.     glEnable(GL_LIGHTING)
  186.     glEnable(GL_LIGHT0)
  187.  
  188.     glLightfv(GL_LIGHT0, GL_POSITION, (0, 0, 0, 0))
  189.     glLightfv(GL_LIGHT0, GL_AMBIENT | GL_DIFFUSE | GL_SPECULAR, (0, 0, 0, 1))
  190.  
  191.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, torus.GL_Pnames_dict.get(GL_LIGHT_MODEL_AMBIENT))
  192.     glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, torus.GL_Pnames_dict.get(GL_LIGHT_MODEL_LOCAL_VIEWER))
  193.     glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, torus.GL_Pnames_dict.get(GL_LIGHT_MODEL_TWO_SIDE))
  194.  
  195.     glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, [1, 0, 0, 1]) ###рассеянный свет
  196.     glMaterialfv(GL_FRONT, GL_SPECULAR, [0.1, 0.1, 0.1, 1]) ###отражаемый свет
  197.     glMaterialfv(GL_FRONT, GL_EMISSION, [0.1, 0, 0, 1]) ###излучаемый свет
  198.     glMaterialfv(GL_FRONT, GL_SHININESS, 128) #степень блеска
  199.  
  200.  
  201. def searchForLowestPoint():
  202.     min = 10
  203.     minP = False
  204.     for i in range(len(torus.polygonMesh)):
  205.         for j in range(4):
  206.             if torus.polygonMesh[i][j][2] < min:
  207.                 min = torus.polygonMesh[i][j][2]
  208.                 minP = torus.polygonMesh[i][j]
  209.     return minP
  210.  
  211.  
  212. def h(time):
  213.     global v0
  214.     global t
  215.     global h0
  216.     x = h0[0] + v0[0] * time
  217.     y = h0[1] + v0[1] * time - 9.8 * time * time / 2
  218.     z = h0[2] + v0[2] * time
  219.     if x + torus.R >= 1 or x - torus.R <= -1:
  220.         v0 = v(time)
  221.         v0 = [-v0[0], v0[1], v0[2]]
  222.         t = 0
  223.         if x + torus.R >= 1:
  224.             h0 = [0.6, torus.center[1], torus.center[2]]
  225.             return h0
  226.         elif x - torus.R <= -1:
  227.             h0 = [-0.6, torus.center[1], torus.center[2]]
  228.             return h0
  229.     elif y + torus.R >= 1 or y - torus.R <= -1:
  230.         v0 = v(time)
  231.         v0 = [v0[0], -v0[1], v0[2]]
  232.         t = 0
  233.         if y + torus.R >= 1:
  234.             h0 = [torus.center[0], 0.6, torus.center[2]]
  235.             return h0
  236.         elif y - torus.R <= -1:
  237.             h0 = [torus.center[0], -0.6, torus.center[2]]
  238.             return h0
  239.     elif z + torus.R >= 1 or z - torus.R <= -1:
  240.         v0 = v(time)
  241.         v0 = [v0[0], v0[1], -v0[2]]
  242.         t = 0
  243.         if z + torus.R >= 1:
  244.             h0 = [torus.center[0], torus.center[1], 0.6]
  245.             return h0
  246.         elif z - torus.R <= -1:
  247.             h0 = [torus.center[0], torus.center[1], -0.6]
  248.             return h0
  249.     else:
  250.         return [x, y, z]
  251.  
  252.  
  253. def v(time):
  254.     vector = [0, 0, 0]
  255.     vector[0] = v0[0]
  256.     vector[1] = v0[1] - 9.8 * time
  257.     vector[2] = v0[2]
  258.     return vector
  259.  
  260.  
  261. def drop(t):
  262.     if 0.6 in [abs(i) for i in h(t + 1 / 500)]:
  263.         print("impact, torus.center =", torus.center)
  264.         return torus.center
  265.     else:
  266.         point = h(t)
  267.         print("point", point)
  268.         return point
  269.  
  270.  
  271. global v0
  272. v0 = [0.2, 0.2, 0.2]
  273. global h0
  274. h0 = [0, 0.4, 0]
  275. global t
  276. t = 0
  277. def draw():
  278.     global t
  279.     drawTorus()
  280.     makeLighting()
  281.     if torus.drop:
  282.         t += 1 / 500
  283.         #print(t)
  284.         center = drop(t)
  285.         #print("center", center)
  286.         print("v0", v0)
  287.         torus.center = center
  288.  
  289. class Drawer:
  290.     window = False
  291.  
  292.     def __init__(self):
  293.         if not glfw.init():
  294.             return
  295.  
  296.         self.window = glfw.create_window(800, 800, "Lab3", None, None)
  297.         if not self.window:
  298.             glfw.terminate()
  299.             return
  300.  
  301.         glfw.make_context_current(self.window)
  302.  
  303.         glfw.set_mouse_button_callback(self.window, mouseCallback)
  304.         glfw.set_key_callback(self.window, keyCallback)
  305.         glfw.set_scroll_callback(self.window, scrollCallback)
  306.  
  307.     def startLoop(self):
  308.         while not glfw.window_should_close(self.window):
  309.             background()
  310.             draw()
  311.  
  312.             glfw.swap_buffers(self.window)
  313.             glfw.poll_events()
  314.  
  315.         glfw.terminate()
  316.  
  317. torus = Torus()
  318. drawer = Drawer()
  319. drawer.startLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement