Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import glfw
- from OpenGL.GL import *
- import math
- import numpy as np
- def background():
- glClearColor(0.8, 0.8, 0.8, 1.0)
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
- glEnable(GL_DEPTH_TEST)
- def mouseCallback(window, button, action, mods):
- if button == glfw.MOUSE_BUTTON_1 and action and torus.isWire:
- torus.isWire = False
- elif button == glfw.MOUSE_BUTTON_1 and action and not torus.isWire:
- torus.isWire = True
- def keyCallback(window, key, scancode, action, mods):
- if key == glfw.KEY_W and action:
- torus.center[1] += 0.1
- elif key == glfw.KEY_A and action:
- torus.center[0] -= 0.1
- elif key == glfw.KEY_D and action:
- torus.center[0] += 0.1
- elif key == glfw.KEY_S and action:
- torus.center[1] -= 0.1
- elif key == glfw.KEY_UP and action:
- torus.rotate_x += 10.0
- elif key == glfw.KEY_DOWN and action:
- torus.rotate_x -= 10.0
- elif key == glfw.KEY_LEFT and action:
- torus.rotate_y += 10.0
- elif key == glfw.KEY_RIGHT and action:
- torus.rotate_y -= 10.0
- elif key == glfw.KEY_KP_SUBTRACT and action and torus.nsides > 3:
- torus.nsides -= 1
- torus.rings -= 1
- torus.polygonMesh = []
- torus.vecOfNormals = []
- torus.normals = []
- torus.setMesh()
- elif key == glfw.KEY_KP_ADD and action:
- torus.nsides += 1
- torus.rings += 1
- torus.polygonMesh = []
- torus.vecOfNormals = []
- torus.normals = []
- torus.setMesh()
- elif key == glfw.KEY_DELETE and action:
- torus.clear()
- class Torus:
- def __init__(self):
- self.isTextured = False
- self.polygonMesh = []
- self.normals = []
- self.vecOfNormals = []
- self.center = [0, 0, 0]
- self.nsides = 3
- self.rings = 3
- self.r = 0.2
- self.R = 0.4
- self.rotate_x = 0
- self.rotate_y = 0
- self.isWire = False
- self.setMesh()
- def setMesh(self):
- for i in range(self.rings):
- theta = i * 2.0 * math.pi / self.rings
- theta1 = ((i + 1) % self.rings) * 2.0 * math.pi / self.rings
- for j in range(self.nsides):
- phi = j * 2.0 * math.pi / self.nsides
- phi1 = ((j + 1) % self.nsides) * 2.0 * math.pi / self.nsides
- p0 = [math.cos(theta) * (self.R + self.r * math.cos(phi)),
- -math.sin(theta) * (self.R + self.r * math.cos(phi)),
- self.r * math.sin(phi)]
- p1 = [math.cos(theta1) * (self.R + self.r * math.cos(phi)),
- -math.sin(theta1) * (self.R + self.r * math.cos(phi)),
- self.r * math.sin(phi)]
- p2 = [math.cos(theta1) * (self.R + self.r * math.cos(phi1)),
- -math.sin(theta1) * (self.R + self.r * math.cos(phi1)),
- self.r * math.sin(phi1)]
- p3 = [math.cos(theta) * (self.R + self.r * math.cos(phi1)),
- -math.sin(theta) * (self.R + self.r * math.cos(phi1)),
- self.r * math.sin(phi1)]
- p = [p0, p1, p2, p3]
- n0 = [math.cos(theta) * (math.cos(phi)), -math.sin(theta) * (math.cos(phi)), math.sin(phi)]
- n1 = [math.cos(theta1) * (math.cos(phi)), -math.sin(theta1) * (math.cos(phi)), math.sin(phi)]
- n2 = [math.cos(theta1) * (math.cos(phi1)), -math.sin(theta1) * (math.cos(phi1)), math.sin(phi1)]
- n3 = [math.cos(theta) * (math.cos(phi1)), -math.sin(theta) * (math.cos(phi1)), math.sin(phi1)]
- # vec1 = np.array(p1) - np.array(p0)
- # vec2 = np.array(p2) - np.array(p0)
- # n0 = np.cross(vec1, vec2)
- # n0 = -n0
- n = [n0, n1, n2, n3]
- self.polygonMesh.append(p)
- #self.normals.append(n0)
- self.normals.append(n)
- 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]])
- print("len normals =", len(self.normals), self.normals)
- print("len vecOfNormals =", len(self.vecOfNormals), self.vecOfNormals)
- def clear(self):
- self.polygonMesh = []
- self.normals = []
- self.vecOfNormals = []
- self.center = [0, 0, 0]
- self.nsides = 3
- self.rings = 3
- self.r = 0.2
- self.R = 0.4
- self.rotate_x = 0
- self.rotate_y = 0
- self.isWire = False
- self.setMesh()
- def makeLighting():
- glEnable(GL_LIGHTING)
- glEnable(GL_LIGHT0)
- glLightfv(GL_LIGHT0, GL_POSITION, (0.0, 0.0, -1.0, 1))
- glLightModelfv(GL_LIGHT_MODEL_AMBIENT, [0.9, 0.9, 0.9])
- def drawTorus():
- glPointSize(1)
- glLineWidth(2)
- if torus.isWire:
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
- else:
- glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
- glLoadIdentity()
- glRotatef(torus.rotate_x, 1, 0, 0)
- glRotatef(torus.rotate_y, 0, 1, 0)
- glTranslatef(torus.center[0], torus.center[1], torus.center[2])
- count = torus.rings * torus.nsides
- #count = 4
- #k = 1
- # for i in range(count):
- # vertexes = []
- # for j in range(4):
- # for l in range(3):
- # vertexes.append(torus.polygonMesh[i][j][l])
- # indices = [l for l in range(12)]
- #
- # normals = []
- # for j in range(4):
- # for l in range(3):
- # normals.append(torus.normals[i][l])
- # print(normals)
- # print(vertexes)
- # glEnableClientState(GL_NORMAL_ARRAY)
- # glNormalPointer(3, GL_FLOAT, 0, normals)
- #
- # glEnableClientState(GL_VERTEX_ARRAY)
- # glVertexPointer(3, GL_FLOAT, 0, vertexes)
- # #glColor3f(1.0, 1.0, 1.0)
- # glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_INT, indices)
- for i in range(count):
- glEnable(GL_NORMALIZE)
- glBegin(GL_POLYGON)
- glNormal3f(torus.normals[i][3][0], torus.normals[i][3][1], torus.normals[i][3][2])
- glVertex3f(torus.polygonMesh[i][3][0], torus.polygonMesh[i][3][1], torus.polygonMesh[i][3][2])
- glNormal3f(torus.normals[i][2][0], torus.normals[i][2][1], torus.normals[i][2][2])
- glVertex3f(torus.polygonMesh[i][2][0], torus.polygonMesh[i][2][1], torus.polygonMesh[i][2][2])
- glNormal3f(torus.normals[i][1][0], torus.normals[i][1][1], torus.normals[i][1][2])
- glVertex3f(torus.polygonMesh[i][1][0], torus.polygonMesh[i][1][1], torus.polygonMesh[i][1][2])
- glNormal3f(torus.normals[i][0][0], torus.normals[i][0][1], torus.normals[i][0][2])
- glVertex3f(torus.polygonMesh[i][0][0], torus.polygonMesh[i][0][1], torus.polygonMesh[i][0][2])
- glEnd()
- # glBegin(GL_LINES)
- #
- # for i in range(count):
- # if torus.rings == 3:
- # if i % torus.rings == 0:
- # glColor3f(1.0, 0, 0)
- # elif i % torus.rings == 1:
- # glColor3f(0.0, 1, 0)
- # else:
- # glColor3f(0.0, 0, 1)
- # elif torus.rings == 4:
- # if i % torus.rings == 0:
- # glColor3f(1.0, 0, 0)
- # elif i % torus.rings == 1:
- # glColor3f(0.0, 1, 0)
- # elif i % torus.rings == 2:
- # glColor3f(0.0, 0, 1)
- # else:
- # glColor3f(0, 0, 0)
- # for j in range(4):
- # glVertex3fv(torus.vecOfNormals[i][j][0])
- # glVertex3fv(torus.vecOfNormals[i][j][1])
- #
- # glEnd()
- def draw():
- drawTorus()
- makeLighting()
- class Drawer:
- window = False
- def __init__(self):
- if not glfw.init():
- return
- self.window = glfw.create_window(800, 800, "Lab3", None, None)
- if not self.window:
- glfw.terminate()
- return
- glfw.make_context_current(self.window)
- glfw.set_key_callback(self.window, keyCallback)
- glfw.set_mouse_button_callback(self.window, mouseCallback)
- def startLoop(self):
- while not glfw.window_should_close(self.window):
- background()
- draw()
- glfw.swap_buffers(self.window)
- glfw.poll_events()
- glfw.terminate()
- drawer = Drawer()
- torus = Torus()
- drawer.startLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement