Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import glfw
- from OpenGL.GL import *
- import math
- def background():
- glClearColor(0.8, 0.8, 0.8, 1.0)
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
- glEnable(GL_DEPTH_TEST)
- global polygonMesh
- polygonMesh = []
- def setMesh(r, R, nsides, rings): # r - inner radius, R - outer radius
- for i in range(rings):
- theta = float(i * 2.0 * math.pi / rings)
- theta1 = float((i + 1) * 2.0 * math.pi / rings)
- for j in range(nsides):
- phi = float(j * 2.0 * math.pi / nsides)
- phi1 = float((j + 1) * 2.0 * math.pi / nsides)
- p0 = [math.cos(theta) * (R + r * math.cos(phi)), - math.sin(theta) * (R + r * math.cos(phi)),
- r * math.sin(phi)]
- p1 = [math.cos(theta1) * (R + r * math.cos(phi)), - math.sin(theta1) * (R + r * math.cos(phi)),
- r * math.sin(phi)]
- p2 = [math.cos(theta1) * (R + r * math.cos(phi1)), - math.sin(theta1) * (R + r * math.cos(phi1)),
- r * math.sin(phi1)]
- p3 = [math.cos(theta) * (R + r * math.cos(phi1)), - math.sin(theta) * (R + r * math.cos(phi1)),
- r * math.sin(phi1)]
- p = [p0, p1, p2, p3]
- polygonMesh.append(p)
- global viewportTorus
- global viewportCube
- def sizeCallback(window, width, height):
- viewportCube[2] = int(width / 3)
- viewportCube[3] = int(height / 3)
- viewportTorus[0] = viewportCube[2]
- viewportTorus[1] = viewportCube[3]
- viewportTorus[2] = width - viewportCube[2]
- viewportTorus[3] = height - viewportCube[3]
- def torus():
- glPointSize(1)
- glLineWidth(1)
- glViewport(viewportTorus[0], viewportTorus[1], viewportTorus[2], viewportTorus[3])
- glMatrixMode(GL_MODELVIEW)
- glLoadIdentity()
- global rotate_x
- global rotate_y
- glRotatef(rotate_x, 1, 0, 0)
- glRotatef(rotate_y, 0, 1, 0)
- glTranslatef(centr[0], centr[1], centr[2])
- global size
- glScalef(size, size, size)
- for i in polygonMesh:
- glBegin(GL_POLYGON)
- glColor(1, 0.5, 0)
- glVertex3fv(i[0])
- glVertex3fv(i[1])
- glVertex3fv(i[2])
- glVertex3fv(i[3])
- glEnd()
- def cube():
- glPointSize(1)
- glLineWidth(1)
- glViewport(viewportCube[0], viewportCube[1], viewportCube[2], viewportCube[3])
- glBegin(GL_QUADS)
- glColor(1, 1, 1)
- glVertex3f(- 0.5, - 0.5, - 0.5)
- glVertex3f(0.5, - 0.5, - 0.5)
- glVertex3f(0.5, 0.5, - 0.5)
- glVertex3f(- 0.5, 0.5, - 0.5)
- glColor(1, 0, 0)
- glVertex3f(- 0.5, - 0.5, 0.5)
- glVertex3f(0.5, - 0.5, 0.5)
- glVertex3f(0.5, 0.5, 0.5)
- glVertex3f(- 0.5, 0.5, 0.5)
- glColor(0, 1, 0)
- glVertex3f(0.5, - 0.5, - 0.5)
- glVertex3f(0.5, - 0.5, 0.5)
- glVertex3f(0.5, 0.5, 0.5)
- glVertex3f(0.5, 0.5, - 0.5)
- glColor(0, 0, 1)
- glVertex3f(- 0.5, - 0.5, - 0.5)
- glVertex3f(- 0.5, - 0.5, 0.5)
- glVertex3f(- 0.5, 0.5, 0.5)
- glVertex3f(- 0.5, 0.5, - 0.5)
- glColor(1, 1, 0)
- glVertex3f(- 0.5, 0.5, - 0.5)
- glVertex3f(0.5, 0.5, - 0.5)
- glVertex3f(0.5, 0.5, 0.5)
- glVertex3f(- 0.5, 0.5, 0.5)
- glColor(1, 0, 1)
- glVertex3f(- 0.5, - 0.5, - 0.5)
- glVertex3f(0.5, - 0.5, - 0.5)
- glVertex3f(0.5, - 0.5, 0.5)
- glVertex3f(- 0.5, - 0.5, 0.5)
- glEnd()
- global solid
- solid = True
- def mouseCallback(window, button, action, mods):
- global solid
- if button == glfw.MOUSE_BUTTON_1 and action and not (solid):
- solid = True
- elif button == glfw.MOUSE_BUTTON_1 and action and solid:
- solid = False
- global size
- size = 1
- def scrollCallback(window, xoffset, yoffset):
- global size
- if yoffset > 0:
- size *= 1.1
- if yoffset < 0:
- size *= 0.9
- global centr
- centr = [0, 0, 0]
- global rotate_x
- rotate_x = 0
- global rotate_y
- rotate_y = 0
- global nsides
- nsides = 4
- def keyCallback(window, key, scancode, action, mods):
- global rotate_x
- global rotate_y
- global nsides
- global polygonMesh
- if key == glfw.KEY_W and action:
- centr[1] += 0.1
- elif key == glfw.KEY_A and action:
- centr[0] -= 0.1
- elif key == glfw.KEY_D and action:
- centr[0] += 0.1
- elif key == glfw.KEY_S and action:
- centr[1] -= 0.1
- elif key == glfw.KEY_UP and action:
- rotate_x += 10.0
- elif key == glfw.KEY_DOWN and action:
- rotate_x -= 10.0
- elif key == glfw.KEY_LEFT and action:
- rotate_y -= 10.0
- elif key == glfw.KEY_RIGHT and action:
- rotate_y += 10.0
- elif key == glfw.KEY_KP_SUBTRACT and action and nsides > 4:
- nsides -= 1
- polygonMesh = []
- setMesh(0.2, 0.4, nsides, nsides)
- elif key == glfw.KEY_KP_ADD and action:
- nsides += 1
- polygonMesh = []
- setMesh(0.2, 0.4, nsides, nsides)
- def draw():
- global solid
- global nsides
- if solid:
- glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
- else:
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
- glMatrixMode(GL_PROJECTION)
- glLoadIdentity()
- ###фронтальная диметрия
- # alpha = math.pi / 4
- # l = 0.5
- # a = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1]
- # b = [1, 0, 0, 0, 0, 1, 0, 0, -l*math.cos(alpha), -l*math.sin(alpha), 1, 0, 0, 0, 0, 1]
- # glMultMatrixf(a)
- # glMultMatrixf(b)
- theta = math.asin(0.5 / math.sqrt(2))
- phi = math.asin(0.5 / math.sqrt(2 - 0.25))
- a = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1]
- 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]
- 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]
- glMultMatrixf(a)
- glMultMatrixf(b)
- glMultMatrixf(c)
- glMatrixMode(GL_MODELVIEW)
- glLoadIdentity()
- cube()
- torus()
- class Drawer:
- window = False
- def __init__(self):
- if not glfw.init():
- return
- self.window = glfw.create_window(900, 900, "Lab3", None, None)
- if not self.window:
- glfw.terminate()
- return
- glfw.make_context_current(self.window)
- global viewportCube
- global viewportTorus
- size = glfw.get_window_size(self.window)
- viewportCube = [0, 0, int(size[0] / 3), int(size[1] / 3)]
- viewportTorus = [viewportCube[2], viewportCube[3], size[0] - viewportCube[2], size[1] - viewportCube[3]]
- glfw.set_mouse_button_callback(self.window, mouseCallback)
- glfw.set_key_callback(self.window, keyCallback)
- glfw.set_scroll_callback(self.window, scrollCallback)
- glfw.set_window_size_callback(self.window, sizeCallback)
- setMesh(0.2, 0.5, 4, 4)
- 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()
- drawer.startLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement