SHARE
TWEET

light.check

trishLEX May 31st, 2017 119 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import glfw
  2. from OpenGL.GL import *
  3. import math
  4. import numpy as np
  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. def mouseCallback(window, button, action, mods):
  12.     if button == glfw.MOUSE_BUTTON_1 and action and torus.isWire:
  13.         torus.isWire = False
  14.     elif button == glfw.MOUSE_BUTTON_1 and action and not torus.isWire:
  15.         torus.isWire = True
  16.  
  17. def keyCallback(window, key, scancode, action, mods):
  18.     if key == glfw.KEY_W and action:
  19.         torus.center[1] += 0.1
  20.     elif key == glfw.KEY_A and action:
  21.         torus.center[0] -= 0.1
  22.     elif key == glfw.KEY_D and action:
  23.         torus.center[0] += 0.1
  24.     elif key == glfw.KEY_S and action:
  25.         torus.center[1] -= 0.1
  26.     elif key == glfw.KEY_UP and action:
  27.         torus.rotate_x += 10.0
  28.     elif key == glfw.KEY_DOWN and action:
  29.         torus.rotate_x -= 10.0
  30.     elif key == glfw.KEY_LEFT and action:
  31.         torus.rotate_y += 10.0
  32.     elif key == glfw.KEY_RIGHT and action:
  33.         torus.rotate_y -= 10.0
  34.     elif key == glfw.KEY_KP_SUBTRACT and action and torus.nsides > 3:
  35.         torus.nsides -= 1
  36.         torus.rings -= 1
  37.         torus.polygonMesh = []
  38.         torus.vecOfNormals = []
  39.         torus.normals = []
  40.         torus.setMesh()
  41.     elif key == glfw.KEY_KP_ADD and action:
  42.         torus.nsides += 1
  43.         torus.rings += 1
  44.         torus.polygonMesh = []
  45.         torus.vecOfNormals = []
  46.         torus.normals = []
  47.         torus.setMesh()
  48.     elif key == glfw.KEY_DELETE and action:
  49.         torus.clear()
  50.  
  51.  
  52. class Torus:
  53.     def __init__(self):
  54.         self.isTextured = False
  55.         self.polygonMesh = []
  56.         self.normals = []
  57.         self.vecOfNormals = []
  58.         self.center = [0, 0, 0]
  59.         self.nsides = 3
  60.         self.rings = 3
  61.         self.r = 0.2
  62.         self.R = 0.4
  63.         self.rotate_x = 0
  64.         self.rotate_y = 0
  65.         self.isWire = False
  66.         self.setMesh()
  67.  
  68.  
  69.     def setMesh(self):
  70.         for i in range(self.rings):
  71.             theta = i * 2.0 * math.pi / self.rings
  72.             theta1 = ((i + 1) % self.rings) * 2.0 * math.pi / self.rings
  73.             for j in range(self.nsides):
  74.                 phi = j * 2.0 * math.pi / self.nsides
  75.                 phi1 = ((j + 1) % self.nsides) * 2.0 * math.pi / self.nsides
  76.  
  77.                 p0 = [math.cos(theta) * (self.R + self.r * math.cos(phi)),
  78.                       -math.sin(theta) * (self.R + self.r * math.cos(phi)),
  79.                       self.r * math.sin(phi)]
  80.                 p1 = [math.cos(theta1) * (self.R + self.r * math.cos(phi)),
  81.                       -math.sin(theta1) * (self.R + self.r * math.cos(phi)),
  82.                       self.r * math.sin(phi)]
  83.                 p2 = [math.cos(theta1) * (self.R + self.r * math.cos(phi1)),
  84.                       -math.sin(theta1) * (self.R + self.r * math.cos(phi1)),
  85.                       self.r * math.sin(phi1)]
  86.                 p3 = [math.cos(theta) * (self.R + self.r * math.cos(phi1)),
  87.                       -math.sin(theta) * (self.R + self.r * math.cos(phi1)),
  88.                       self.r * math.sin(phi1)]
  89.  
  90.                 p = [p0, p1, p2, p3]
  91.  
  92.                 n0 = [math.cos(theta) * (math.cos(phi)), -math.sin(theta) * (math.cos(phi)), math.sin(phi)]
  93.                 n1 = [math.cos(theta1) * (math.cos(phi)), -math.sin(theta1) * (math.cos(phi)), math.sin(phi)]
  94.                 n2 = [math.cos(theta1) * (math.cos(phi1)), -math.sin(theta1) * (math.cos(phi1)), math.sin(phi1)]
  95.                 n3 = [math.cos(theta) * (math.cos(phi1)), -math.sin(theta) * (math.cos(phi1)), math.sin(phi1)]
  96.  
  97.                 # vec1 = np.array(p1) - np.array(p0)
  98.                 # vec2 = np.array(p2) - np.array(p0)
  99.                 # n0 = np.cross(vec1, vec2)
  100.                 # n0 = -n0
  101.  
  102.                 n = [n0, n1, n2, n3]
  103.  
  104.                 self.polygonMesh.append(p)
  105.                 #self.normals.append(n0)
  106.                 self.normals.append(n)
  107.                 self.vecOfNormals.append([[np.array(p0), np.array(p0) + n0], [np.array(p1), np.array(p1) + n0], [np.array(p2), np.array(p2) + n0], [np.array(p3), np.array(p3) + n0]])
  108.  
  109.         print("len normals =", len(self.normals), self.normals)
  110.         print("len vecOfNormals =", len(self.vecOfNormals), self.vecOfNormals)
  111.  
  112.  
  113.     def clear(self):
  114.         self.polygonMesh = []
  115.         self.normals = []
  116.         self.vecOfNormals = []
  117.         self.center = [0, 0, 0]
  118.         self.nsides = 3
  119.         self.rings = 3
  120.         self.r = 0.2
  121.         self.R = 0.4
  122.         self.rotate_x = 0
  123.         self.rotate_y = 0
  124.         self.isWire = False
  125.         self.setMesh()
  126.  
  127. def makeLighting():
  128.     glEnable(GL_LIGHTING)
  129.     glEnable(GL_LIGHT0)
  130.     glLightfv(GL_LIGHT0, GL_POSITION, (0.0, 0.0, -1.0, 1))
  131.  
  132.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, [0.9, 0.9, 0.9])
  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.     glLoadIdentity()
  144.  
  145.     glRotatef(torus.rotate_x, 1, 0, 0)
  146.     glRotatef(torus.rotate_y, 0, 1, 0)
  147.  
  148.     glTranslatef(torus.center[0], torus.center[1], torus.center[2])
  149.  
  150.     count = torus.rings * torus.nsides
  151.     #count = 4
  152.  
  153.     #k = 1
  154.     # for i in range(count):
  155.     #     vertexes = []
  156.     #     for j in range(4):
  157.     #         for l in range(3):
  158.     #             vertexes.append(torus.polygonMesh[i][j][l])
  159.     #     indices = [l for l in range(12)]
  160.     #
  161.     #     normals = []
  162.     #     for j in range(4):
  163.     #         for l in range(3):
  164.     #             normals.append(torus.normals[i][l])
  165.     #     print(normals)
  166.     #     print(vertexes)
  167.     #     glEnableClientState(GL_NORMAL_ARRAY)
  168.     #     glNormalPointer(3, GL_FLOAT, 0, normals)
  169.     #
  170.     #     glEnableClientState(GL_VERTEX_ARRAY)
  171.     #     glVertexPointer(3, GL_FLOAT, 0, vertexes)
  172.     #     #glColor3f(1.0, 1.0, 1.0)
  173.     #     glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_INT, indices)
  174.  
  175.     for i in range(count):
  176.         glEnable(GL_NORMALIZE)
  177.         glBegin(GL_POLYGON)
  178.  
  179.         glNormal3f(torus.normals[i][3][0], torus.normals[i][3][1], torus.normals[i][3][2])
  180.         glVertex3f(torus.polygonMesh[i][3][0], torus.polygonMesh[i][3][1], torus.polygonMesh[i][3][2])
  181.  
  182.         glNormal3f(torus.normals[i][2][0], torus.normals[i][2][1], torus.normals[i][2][2])
  183.         glVertex3f(torus.polygonMesh[i][2][0], torus.polygonMesh[i][2][1], torus.polygonMesh[i][2][2])
  184.  
  185.         glNormal3f(torus.normals[i][1][0], torus.normals[i][1][1], torus.normals[i][1][2])
  186.         glVertex3f(torus.polygonMesh[i][1][0], torus.polygonMesh[i][1][1], torus.polygonMesh[i][1][2])
  187.  
  188.         glNormal3f(torus.normals[i][0][0], torus.normals[i][0][1], torus.normals[i][0][2])
  189.         glVertex3f(torus.polygonMesh[i][0][0], torus.polygonMesh[i][0][1], torus.polygonMesh[i][0][2])
  190.  
  191.         glEnd()
  192.  
  193.     # glBegin(GL_LINES)
  194.     #
  195.     # for i in range(count):
  196.     #     if torus.rings == 3:
  197.     #         if i % torus.rings == 0:
  198.     #             glColor3f(1.0, 0, 0)
  199.     #         elif i % torus.rings == 1:
  200.     #             glColor3f(0.0, 1, 0)
  201.     #         else:
  202.     #             glColor3f(0.0, 0, 1)
  203.     #     elif torus.rings == 4:
  204.     #         if i % torus.rings == 0:
  205.     #             glColor3f(1.0, 0, 0)
  206.     #         elif i % torus.rings == 1:
  207.     #             glColor3f(0.0, 1, 0)
  208.     #         elif i % torus.rings == 2:
  209.     #             glColor3f(0.0, 0, 1)
  210.     #         else:
  211.     #             glColor3f(0, 0, 0)
  212.     #     for j in range(4):
  213.     #         glVertex3fv(torus.vecOfNormals[i][j][0])
  214.     #         glVertex3fv(torus.vecOfNormals[i][j][1])
  215.     #
  216.     # glEnd()
  217.  
  218.  
  219. def draw():
  220.     drawTorus()
  221.     makeLighting()
  222.  
  223. class Drawer:
  224.     window = False
  225.  
  226.     def __init__(self):
  227.         if not glfw.init():
  228.             return
  229.  
  230.         self.window = glfw.create_window(800, 800, "Lab3", None, None)
  231.         if not self.window:
  232.             glfw.terminate()
  233.             return
  234.  
  235.         glfw.make_context_current(self.window)
  236.  
  237.         glfw.set_key_callback(self.window, keyCallback)
  238.         glfw.set_mouse_button_callback(self.window, mouseCallback)
  239.  
  240.     def startLoop(self):
  241.         while not glfw.window_should_close(self.window):
  242.             background()
  243.             draw()
  244.  
  245.             glfw.swap_buffers(self.window)
  246.             glfw.poll_events()
  247.  
  248.         glfw.terminate()
  249.  
  250.  
  251. drawer = Drawer()
  252. torus = Torus()
  253. drawer.startLoop()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top