Advertisement
trishLEX

Lighting works

Jun 2nd, 2017
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.07 KB | None | 0 0
  1. import glfw
  2. from OpenGL.GL import *
  3. import OpenGL.GL.shaders
  4. import numpy
  5. import math
  6. import pyrr
  7. import PIL.Image
  8. import ctypes
  9. import json
  10.  
  11.  
  12. def background():
  13.     glClearColor(0.8, 0.8, 0.8, 1.0)
  14.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  15.     glEnable(GL_DEPTH_TEST)
  16.  
  17. def mouseCallback(window, button, action, mods):
  18.     size = 3
  19.     if button == glfw.MOUSE_BUTTON_1 and action and torus.isWire:
  20.         torus.isWire = False
  21.     elif button == glfw.MOUSE_BUTTON_1 and action and not torus.isWire:
  22.         torus.isWire = True
  23.     elif button == glfw.MOUSE_BUTTON_2 and action:
  24.         torus.counter += 1
  25.         print(torus.counter, size)
  26.         torus.currentGL_Pname = torus.GL_Pnames[torus.counter % size]
  27.         print("NOW GLpname is", torus.currentGL_Pname)
  28.  
  29. def keyCallback(window, key, scancode, action, mods):
  30.     if key == glfw.KEY_W and action:
  31.         torus.center[1] += 0.1
  32.     elif key == glfw.KEY_A and action:
  33.         torus.center[0] -= 0.1
  34.     elif key == glfw.KEY_D and action:
  35.         torus.center[0] += 0.1
  36.     elif key == glfw.KEY_S and action:
  37.         torus.center[1] -= 0.1
  38.     elif key == glfw.KEY_UP and action:
  39.         torus.rotate_x += 10.0
  40.     elif key == glfw.KEY_DOWN and action:
  41.         torus.rotate_x -= 10.0
  42.     elif key == glfw.KEY_LEFT and action:
  43.         torus.rotate_y += 10.0
  44.     elif key == glfw.KEY_RIGHT and action:
  45.         torus.rotate_y -= 10.0
  46.     elif key == glfw.KEY_KP_SUBTRACT and action and torus.nsides > 3:
  47.         torus.nsides -= 1
  48.         torus.rings -= 1
  49.         torus.polygonMesh = []
  50.         torus.vecOfNormals = []
  51.         torus.normals = []
  52.         torus.setMesh()
  53.     elif key == glfw.KEY_KP_ADD and action:
  54.         torus.nsides += 1
  55.         torus.rings += 1
  56.         torus.polygonMesh = []
  57.         torus.vecOfNormals = []
  58.         torus.normals = []
  59.         torus.setMesh()
  60.     elif key == glfw.KEY_DELETE and action:
  61.         torus.clear()
  62.     elif key == glfw.KEY_1 and action:
  63.         if torus.currentGL_Pname == GL_LIGHT_MODEL_AMBIENT:
  64.             if torus.GL_Pnames_dict.get(torus.currentGL_Pname)[0] > -3:
  65.                 value = torus.GL_Pnames_dict.get(torus.currentGL_Pname)
  66.                 for i in range(3):
  67.                     value[i] -= 0.1
  68.                 torus.GL_Pnames_dict.update({torus.currentGL_Pname: value})
  69.         else:
  70.             value = torus.GL_Pnames_dict.get(torus.currentGL_Pname)
  71.             value = not value
  72.             torus.GL_Pnames_dict.update({torus.currentGL_Pname: value})
  73.     elif key == glfw.KEY_2 and action:
  74.         if torus.currentGL_Pname == GL_LIGHT_MODEL_AMBIENT:
  75.             if torus.GL_Pnames_dict.get(torus.currentGL_Pname)[0] < 4:
  76.                 value = torus.GL_Pnames_dict.get(torus.currentGL_Pname)
  77.                 for i in range(3):
  78.                     value[i] += 0.1
  79.                 torus.GL_Pnames_dict.update({torus.currentGL_Pname: value})
  80.         else:
  81.             value = torus.GL_Pnames_dict.get(torus.currentGL_Pname)
  82.             value = not value
  83.             torus.GL_Pnames_dict.update({torus.currentGL_Pname: value})
  84.  
  85.  
  86. class Torus:
  87.     def __init__(self):
  88.         self.isTextured = False
  89.         self.polygonMesh = []
  90.         self.normals = []
  91.         self.vecOfNormals = []
  92.         self.center = [0, 0, 0]
  93.         self.nsides = 3
  94.         self.rings = 3
  95.         self.r = 0.2
  96.         self.R = 0.4
  97.         self.rotate_x = 0
  98.         self.rotate_y = 0
  99.         self.isWire = False
  100.         self.setMesh()
  101.         self.counter = 0
  102.         self.GL_Pnames_dict = {GL_LIGHT_MODEL_AMBIENT: [0.8, 0.8, 0.8, 1.0], GL_LIGHT_MODEL_LOCAL_VIEWER: GL_FALSE,
  103.                                GL_LIGHT_MODEL_TWO_SIDE: GL_FALSE}
  104.         self.GL_Pnames = (GL_LIGHT_MODEL_AMBIENT, GL_LIGHT_MODEL_LOCAL_VIEWER, GL_LIGHT_MODEL_TWO_SIDE)
  105.         self.currentGL_Pname = GL_LIGHT_MODEL_AMBIENT
  106.  
  107.  
  108.     def setMesh(self):
  109.         for i in range(self.rings):
  110.             theta = i * 2.0 * math.pi / self.rings
  111.             theta1 = ((i + 1) % self.rings) * 2.0 * math.pi / self.rings
  112.             for j in range(self.nsides):
  113.                 phi = j * 2.0 * math.pi / self.nsides
  114.                 phi1 = ((j + 1) % self.nsides) * 2.0 * math.pi / self.nsides
  115.  
  116.                 p0 = [math.cos(theta) * (self.R + self.r * math.cos(phi)),
  117.                       -math.sin(theta) * (self.R + self.r * math.cos(phi)),
  118.                       self.r * math.sin(phi)]
  119.                 p1 = [math.cos(theta1) * (self.R + self.r * math.cos(phi)),
  120.                       -math.sin(theta1) * (self.R + self.r * math.cos(phi)),
  121.                       self.r * math.sin(phi)]
  122.                 p2 = [math.cos(theta1) * (self.R + self.r * math.cos(phi1)),
  123.                       -math.sin(theta1) * (self.R + self.r * math.cos(phi1)),
  124.                       self.r * math.sin(phi1)]
  125.                 p3 = [math.cos(theta) * (self.R + self.r * math.cos(phi1)),
  126.                       -math.sin(theta) * (self.R + self.r * math.cos(phi1)),
  127.                       self.r * math.sin(phi1)]
  128.  
  129.                 p = [p0, p1, p2, p3]
  130.  
  131.                 n0 = [math.cos(theta) * (math.cos(phi)), -math.sin(theta) * (math.cos(phi)), math.sin(phi)]
  132.                 n1 = [math.cos(theta1) * (math.cos(phi)), -math.sin(theta1) * (math.cos(phi)), math.sin(phi)]
  133.                 n2 = [math.cos(theta1) * (math.cos(phi1)), -math.sin(theta1) * (math.cos(phi1)), math.sin(phi1)]
  134.                 n3 = [math.cos(theta) * (math.cos(phi1)), -math.sin(theta) * (math.cos(phi1)), math.sin(phi1)]
  135.  
  136.                 # vec1 = np.array(p1) - np.array(p0)
  137.                 # vec2 = np.array(p2) - np.array(p0)
  138.                 # n0 = np.cross(vec1, vec2)
  139.                 # n0 = -n0
  140.  
  141.                 n = [n0, n1, n2, n3]
  142.  
  143.                 self.polygonMesh.append(p)
  144.                 #self.normals.append(n0)
  145.                 self.normals.append(n)
  146.                 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]])
  147.  
  148.         print("len normals =", len(self.normals), self.normals)
  149.         print("len vecOfNormals =", len(self.vecOfNormals), self.vecOfNormals)
  150.  
  151.  
  152.     def clear(self):
  153.         self.polygonMesh = []
  154.         self.normals = []
  155.         self.vecOfNormals = []
  156.         self.center = [0, 0, 0]
  157.         self.nsides = 3
  158.         self.rings = 3
  159.         self.r = 0.2
  160.         self.R = 0.4
  161.         self.rotate_x = 0
  162.         self.rotate_y = 0
  163.         self.isWire = False
  164.         self.setMesh()
  165.  
  166. def makeLighting():
  167.     glEnable(GL_LIGHTING)
  168.     glEnable(GL_LIGHT0)
  169.     glLightfv(GL_LIGHT0, GL_POSITION, (0.0, 0.0, -1.0, 1))
  170.  
  171.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, torus.GL_Pnames_dict.get(GL_LIGHT_MODEL_AMBIENT))
  172.     glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, torus.GL_Pnames_dict.get(GL_LIGHT_MODEL_LOCAL_VIEWER))
  173.     glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, torus.GL_Pnames_dict.get(GL_LIGHT_MODEL_TWO_SIDE))
  174.  
  175. def drawTorus():
  176.     glPointSize(1)
  177.     glLineWidth(2)
  178.  
  179.     if torus.isWire:
  180.         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
  181.     else:
  182.         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
  183.  
  184.     glLoadIdentity()
  185.  
  186.     glRotatef(torus.rotate_x, 1, 0, 0)
  187.     glRotatef(torus.rotate_y, 0, 1, 0)
  188.  
  189.     glTranslatef(torus.center[0], torus.center[1], torus.center[2])
  190.  
  191.     count = torus.rings * torus.nsides
  192.     #count = 4
  193.  
  194.     #k = 1
  195.     # for i in range(count):
  196.     #     vertexes = []
  197.     #     for j in range(4):
  198.     #         for l in range(3):
  199.     #             vertexes.append(torus.polygonMesh[i][j][l])
  200.     #     indices = [l for l in range(12)]
  201.     #
  202.     #     normals = []
  203.     #     for j in range(4):
  204.     #         for l in range(3):
  205.     #             normals.append(torus.normals[i][l])
  206.     #     print(normals)
  207.     #     print(vertexes)
  208.     #     glEnableClientState(GL_NORMAL_ARRAY)
  209.     #     glNormalPointer(3, GL_FLOAT, 0, normals)
  210.     #
  211.     #     glEnableClientState(GL_VERTEX_ARRAY)
  212.     #     glVertexPointer(3, GL_FLOAT, 0, vertexes)
  213.     #     #glColor3f(1.0, 1.0, 1.0)
  214.     #     glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_INT, indices)
  215.  
  216.     for i in range(count):
  217.         glEnable(GL_NORMALIZE)
  218.         glBegin(GL_POLYGON)
  219.  
  220.         glNormal3fv(torus.normals[i][3])
  221.         glVertex3fv(torus.polygonMesh[i][3])
  222.  
  223.         glNormal3fv(torus.normals[i][2])
  224.         glVertex3fv(torus.polygonMesh[i][2])
  225.  
  226.         glNormal3fv(torus.normals[i][1])
  227.         glVertex3fv(torus.polygonMesh[i][1])
  228.  
  229.         glNormal3fv(torus.normals[i][0])
  230.         glVertex3fv(torus.polygonMesh[i][0])
  231.  
  232.         glEnd()
  233.  
  234.     # glBegin(GL_LINES)
  235.     #
  236.     # for i in range(count):
  237.     #     if torus.rings == 3:
  238.     #         if i % torus.rings == 0:
  239.     #             glColor3f(1.0, 0, 0)
  240.     #         elif i % torus.rings == 1:
  241.     #             glColor3f(0.0, 1, 0)
  242.     #         else:
  243.     #             glColor3f(0.0, 0, 1)
  244.     #     elif torus.rings == 4:
  245.     #         if i % torus.rings == 0:
  246.     #             glColor3f(1.0, 0, 0)
  247.     #         elif i % torus.rings == 1:
  248.     #             glColor3f(0.0, 1, 0)
  249.     #         elif i % torus.rings == 2:
  250.     #             glColor3f(0.0, 0, 1)
  251.     #         else:
  252.     #             glColor3f(0, 0, 0)
  253.     #     for j in range(4):
  254.     #         glVertex3fv(torus.vecOfNormals[i][j][0])
  255.     #         glVertex3fv(torus.vecOfNormals[i][j][1])
  256.     #
  257.     # glEnd()
  258.  
  259.  
  260. def draw():
  261.     drawTorus()
  262.     makeLighting()
  263.  
  264. class Drawer:
  265.     window = False
  266.  
  267.     def __init__(self):
  268.         if not glfw.init():
  269.             return
  270.  
  271.         self.window = glfw.create_window(800, 800, "Lab3", None, None)
  272.         if not self.window:
  273.             glfw.terminate()
  274.             return
  275.  
  276.         glfw.make_context_current(self.window)
  277.  
  278.         glfw.set_key_callback(self.window, keyCallback)
  279.         glfw.set_mouse_button_callback(self.window, mouseCallback)
  280.  
  281.     def startLoop(self):
  282.         while not glfw.window_should_close(self.window):
  283.             background()
  284.             draw()
  285.  
  286.             glfw.swap_buffers(self.window)
  287.             glfw.poll_events()
  288.  
  289.         glfw.terminate()
  290.  
  291.  
  292. drawer = Drawer()
  293. torus = Torus()
  294. drawer.startLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement