Advertisement
trishLEX

Untitled

May 4th, 2017
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.21 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 = 4
  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_SHININESS:
  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] *= 1.5
  34.             torus.GL_Pnames_dict.update({torus.currentGL_Pname : value})
  35.         elif yoffset < 0:
  36.             value = torus.GL_Pnames_dict.get(torus.currentGL_Pname)
  37.             for i in range(3):
  38.                 value[i] /= 1.5
  39.             torus.GL_Pnames_dict.update({torus.currentGL_Pname: value})
  40.     else:
  41.         if yoffset > 0 and torus.GL_Pnames_dict.get(torus.currentGL_Pname) < 128:
  42.             value = torus.GL_Pnames_dict.get(torus.currentGL_Pname)
  43.             value += 16
  44.             torus.GL_Pnames_dict.update({torus.currentGL_Pname: value})
  45.         elif yoffset < 0 and torus.GL_Pnames_dict.get(torus.currentGL_Pname) > 0:
  46.             value = torus.GL_Pnames_dict.get(torus.currentGL_Pname)
  47.             value -= 16
  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 = [0, 0.5, 0]
  82.         torus.drop = True
  83.  
  84.  
  85. class Torus:
  86.     def __init__(self):
  87.         self.polygonMesh = []
  88.         self.normals = []
  89.         self.center = [0, 0, 0]
  90.         #self.size = 1
  91.         self.nsides = 4
  92.         self.rings = 4
  93.         self.r = 0.2
  94.         self.R = 0.4
  95.         self.rotate_x = 0
  96.         self.rotate_y = 0
  97.         self.isWire = False
  98.         self.setMesh()
  99.         self.GL_Pnames_dict = {GL_AMBIENT_AND_DIFFUSE : [1, 0, 0, 1], GL_SPECULAR : [0.1, 0.1, 0.1, 1], GL_EMISSION : [0.1, 0, 0, 1], GL_SHININESS : 128}
  100.         self.GL_Pnames = (GL_AMBIENT_AND_DIFFUSE, GL_SPECULAR, GL_EMISSION, GL_SHININESS)
  101.         self.currentGL_Pname = GL_AMBIENT_AND_DIFFUSE
  102.         self.drop = False
  103.  
  104.     def setMesh(self):
  105.         for i in range(self.rings):
  106.             theta = i * 2.0 * math.pi / self.rings
  107.             theta1 = ((i + 1) % self.rings) * 2.0 * math.pi / self.rings
  108.             for j in range(self.nsides):
  109.                 phi = j * 2.0 * math.pi / self.nsides
  110.                 phi1 = ((j + 1) % self.nsides) * 2.0 * math.pi / self.nsides
  111.  
  112.                 p0 = [math.cos(theta) * (self.R + self.r * math.cos(phi)), - math.sin(theta) * (self.R + self.r * math.cos(phi)),
  113.                       self.r * math.sin(phi)]
  114.                 p1 = [math.cos(theta1) * (self.R + self.r * math.cos(phi)), - math.sin(theta1) * (self.R + self.r * math.cos(phi)),
  115.                       self.r * math.sin(phi)]
  116.                 p2 = [math.cos(theta1) * (self.R + self.r * math.cos(phi1)), - math.sin(theta1) * (self.R + self.r * math.cos(phi1)),
  117.                       self.r * math.sin(phi1)]
  118.                 p3 = [math.cos(theta) * (self.R + self.r * math.cos(phi1)), - math.sin(theta) * (self.R + self.r * math.cos(phi1)),
  119.                       self.r * math.sin(phi1)]
  120.  
  121.                 p = [p0, p1, p2, p3]
  122.  
  123.                 n0 = [math.cos(theta) * (math.cos(phi)), -math.sin(theta) * (math.cos(phi)), math.sin(phi)]
  124.                 n1 = [math.cos(theta1) * (math.cos(phi)), -math.sin(theta1) * (math.cos(phi)), math.sin(phi)]
  125.                 n2 = [math.cos(theta1) * (math.cos(phi1)), -math.sin(theta1) * (math.cos(phi1)), math.sin(phi1)]
  126.                 n3 = [math.cos(theta) * (math.cos(phi1)), -math.sin(theta) * (math.cos(phi1)), math.sin(phi1)]
  127.  
  128.                 n = [n0, n1, n2, n3]
  129.  
  130.                 self.polygonMesh.append(p)
  131.                 self.normals.append(n)
  132.  
  133.  
  134. def drawTorus():
  135.     glPointSize(1)
  136.     glLineWidth(2)
  137.  
  138.     if torus.isWire:
  139.         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
  140.     else:
  141.         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
  142.  
  143.     glMatrixMode(GL_PROJECTION)
  144.     glLoadIdentity()
  145.  
  146.     theta = math.asin(0.5 / math.sqrt(2))
  147.     phi = math.asin(0.5 / math.sqrt(2 - 0.25))
  148.  
  149.     a = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1]
  150.     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]
  151.     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]
  152.  
  153.     glMultMatrixf(a)
  154.     glMultMatrixf(b)
  155.     glMultMatrixf(c)
  156.  
  157.     glMatrixMode(GL_MODELVIEW)
  158.     glLoadIdentity()
  159.  
  160.     glRotatef(torus.rotate_x, 1, 0, 0)
  161.     glRotatef(torus.rotate_y, 0, 1, 0)
  162.  
  163.     glTranslatef(torus.center[0], torus.center[1], torus.center[2])
  164.  
  165.     count = torus.rings * torus.nsides
  166.     for i in range(count):
  167.         glEnable(GL_NORMALIZE)
  168.         glBegin(GL_POLYGON)
  169.  
  170.         glColor(1, 0.5, 0)
  171.  
  172.         glVertex3fv(torus.polygonMesh[i][3])
  173.         glVertex3fv(torus.polygonMesh[i][2])
  174.         glVertex3fv(torus.polygonMesh[i][1])
  175.         glVertex3fv(torus.polygonMesh[i][0])
  176.  
  177.         #glNormal3fv(torus.normals[i][3])
  178.         #glNormal3fv(torus.normals[i][2])
  179.         #glNormal3fv(torus.normals[i][1])
  180.         #glNormal3fv(torus.normals[i][0])
  181.  
  182.         glEnd()
  183.  
  184.  
  185. def makeLighting():
  186.     glEnable(GL_LIGHTING)
  187.     glEnable(GL_LIGHT0)
  188.  
  189.     glLightfv(GL_LIGHT0, GL_POSITION, (0, 0, 0, 0))
  190.     glLightfv(GL_LIGHT0, GL_AMBIENT | GL_DIFFUSE | GL_SPECULAR, (0, 0, 0, 1))
  191.  
  192.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, (0.5, 0.5, 0.5, 1))
  193.  
  194.     glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, torus.GL_Pnames_dict.get(GL_AMBIENT_AND_DIFFUSE)) ###рассеянный свет
  195.     glMaterialfv(GL_FRONT, GL_SPECULAR, torus.GL_Pnames_dict.get(GL_SPECULAR)) ###отражаемый свет
  196.     glMaterialfv(GL_FRONT, GL_EMISSION, torus.GL_Pnames_dict.get(GL_EMISSION)) ###излучаемый свет
  197.     glMaterialfv(GL_FRONT, GL_SHININESS, torus.GL_Pnames_dict.get(GL_SHININESS)) #степень блеска
  198.  
  199.  
  200. def searchForLowestPoint():
  201.     min = 10
  202.     minP = False
  203.     for i in range(len(torus.polygonMesh)):
  204.         for j in range(4):
  205.             if torus.polygonMesh[i][j][2] < min:
  206.                 min = torus.polygonMesh[i][j][2]
  207.                 minP = torus.polygonMesh[i][j]
  208.     return minP
  209.  
  210.  
  211. def impact():
  212.     global v0
  213.     if torus.center[0] + torus.R >= 1 or torus.center[0] - torus.R <= -1:
  214.         v0 = [-v0[0], v0[1], v0[2]]
  215.         return v0
  216.     elif torus.center[1] + torus.R >= 1 or torus.center[1] - torus.R <= -1:
  217.         v0 = [v0[0], -v0[1], v0[2]]
  218.         return v0
  219.     elif torus.center[2] + torus.R >= 1 or torus.center[2] - torus.R <= -1:
  220.         v0 = [v0[0], v0[1], -v0[2]]
  221.         return v0
  222.     else:
  223.         return v0
  224.  
  225.  
  226. def drop(t):
  227.     v0 = impact()
  228.     x = h0[0] + v0[0]*t
  229.     y = h0[1] + v0[1]*t - 9.8 * t * t / 2
  230.     z = h0[2] + v0[2]*t
  231.     return [x, y, z]
  232.  
  233.  
  234. def h(t):
  235.     global v0
  236.     x = h0[0] + v0[0] * t
  237.     y = h0[1] + v0[1] * t - 9.8 * t * t / 2
  238.     z = h0[2] + v0[2] * t
  239.     if x + torus.R >= 1 or x - torus.R <= -1:
  240.         v0 = [-v0[0], v0[1], v0[2]]
  241.         return torus.center
  242.     elif y + torus.R >= 1 or y - torus.R <= -1:
  243.         v0 = [v0[0], -v0[1], v0[2]]
  244.         return torus.center
  245.     elif z + torus.R >= 1 or z - torus.R <= -1:
  246.         v0 = [v0[0], v0[1], -v0[2]]
  247.         return torus.center
  248.     else:
  249.         return [x, y, z]
  250.  
  251.  
  252. def v(t):
  253.     v = [0, 0, 0]
  254.     v[0] = v0[0]
  255.     v[1] = v0[1] - 9.8 * t
  256.     v[2] = v0[2]
  257.     return v
  258.  
  259.  
  260. def drop1(t):
  261.     if torus.center == h(t + 1/60):
  262.         return torus.center
  263.     else:
  264.         point = h(t)
  265.         return point
  266.  
  267.  
  268. global v0
  269. v0 = [0, -1, 0]
  270. global h0
  271. h0 = [0, 0.5, 0]
  272. global t
  273. t = 0
  274. def draw():
  275.     global t
  276.     drawTorus()
  277.     makeLighting()
  278.     if torus.drop:
  279.         t += 1 / 60
  280.         #print(t)
  281.         center = drop1(t)
  282.         print("center", center)
  283.         print("v0", v0)
  284.         torus.center = center
  285.  
  286. class Drawer:
  287.     window = False
  288.  
  289.     def __init__(self):
  290.         if not glfw.init():
  291.             return
  292.  
  293.         self.window = glfw.create_window(800, 800, "Lab3", None, None)
  294.         if not self.window:
  295.             glfw.terminate()
  296.             return
  297.  
  298.         glfw.make_context_current(self.window)
  299.  
  300.         glfw.set_mouse_button_callback(self.window, mouseCallback)
  301.         glfw.set_key_callback(self.window, keyCallback)
  302.         glfw.set_scroll_callback(self.window, scrollCallback)
  303.  
  304.     def startLoop(self):
  305.         while not glfw.window_should_close(self.window):
  306.             background()
  307.             draw()
  308.  
  309.             glfw.swap_buffers(self.window)
  310.             glfw.poll_events()
  311.  
  312.         glfw.terminate()
  313.  
  314. torus = Torus()
  315. drawer = Drawer()
  316. drawer.startLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement