Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import glfw
- from OpenGL.GL import *
- import math
- image = open('E:\Sorry\Documents\PycharmProjects\FirstOpenGL\Texture.bmp')
- 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 counter
- counter = 0
- def mouseCallback(window, button, action, mods):
- global counter
- size = 4
- 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
- elif button == glfw.MOUSE_BUTTON_2 and action:
- counter += 1
- print(counter, size)
- torus.currentGL_Pname = torus.GL_Pnames[counter % size]
- print("NOW GLpname is", torus.currentGL_Pname)
- def scrollCallback(window, xoffset, yoffset):
- if torus.currentGL_Pname != GL_SHININESS:
- if yoffset > 0 and torus.GL_Pnames_dict.get(torus.currentGL_Pname)[0] < 1:
- value = torus.GL_Pnames_dict.get(torus.currentGL_Pname)
- for i in range(3):
- value[i] *= 1.5
- torus.GL_Pnames_dict.update({torus.currentGL_Pname : value})
- elif yoffset < 0:
- value = torus.GL_Pnames_dict.get(torus.currentGL_Pname)
- for i in range(3):
- value[i] /= 1.5
- torus.GL_Pnames_dict.update({torus.currentGL_Pname: value})
- else:
- if yoffset > 0 and torus.GL_Pnames_dict.get(torus.currentGL_Pname) < 128:
- value = torus.GL_Pnames_dict.get(torus.currentGL_Pname)
- value += 16
- torus.GL_Pnames_dict.update({torus.currentGL_Pname: value})
- elif yoffset < 0 and torus.GL_Pnames_dict.get(torus.currentGL_Pname) > 0:
- value = torus.GL_Pnames_dict.get(torus.currentGL_Pname)
- value -= 16
- torus.GL_Pnames_dict.update({torus.currentGL_Pname: value})
- print(torus.GL_Pnames_dict)
- 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 > 4:
- torus.nsides -= 1
- torus.rings -= 1
- torus.polygonMesh = []
- torus.setMesh()
- elif key == glfw.KEY_KP_ADD and action:
- torus.nsides += 1
- torus.rings += 1
- torus.polygonMesh = []
- torus.setMesh()
- elif key == glfw.KEY_ENTER and action:
- torus.center = [0, 0.5, 0]
- torus.drop = True
- class Torus:
- def __init__(self):
- self.polygonMesh = []
- self.normals = []
- self.center = [0, 0, 0]
- #self.size = 1
- self.nsides = 4
- self.rings = 4
- self.r = 0.2
- self.R = 0.4
- self.rotate_x = 0
- self.rotate_y = 0
- self.isWire = False
- self.setMesh()
- 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}
- self.GL_Pnames = (GL_AMBIENT_AND_DIFFUSE, GL_SPECULAR, GL_EMISSION, GL_SHININESS)
- self.currentGL_Pname = GL_AMBIENT_AND_DIFFUSE
- self.drop = False
- 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)]
- n = [n0, n1, n2, n3]
- self.polygonMesh.append(p)
- self.normals.append(n)
- def drawTorus():
- glPointSize(1)
- glLineWidth(2)
- if torus.isWire:
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
- else:
- glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
- glMatrixMode(GL_PROJECTION)
- glLoadIdentity()
- 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()
- 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
- for i in range(count):
- glEnable(GL_NORMALIZE)
- glBegin(GL_POLYGON)
- glColor(1, 0.5, 0)
- glVertex3fv(torus.polygonMesh[i][3])
- glVertex3fv(torus.polygonMesh[i][2])
- glVertex3fv(torus.polygonMesh[i][1])
- glVertex3fv(torus.polygonMesh[i][0])
- #glNormal3fv(torus.normals[i][3])
- #glNormal3fv(torus.normals[i][2])
- #glNormal3fv(torus.normals[i][1])
- #glNormal3fv(torus.normals[i][0])
- glEnd()
- def makeLighting():
- glEnable(GL_LIGHTING)
- glEnable(GL_LIGHT0)
- glLightfv(GL_LIGHT0, GL_POSITION, (0, 0, 0, 0))
- glLightfv(GL_LIGHT0, GL_AMBIENT | GL_DIFFUSE | GL_SPECULAR, (0, 0, 0, 1))
- glLightModelfv(GL_LIGHT_MODEL_AMBIENT, (0.5, 0.5, 0.5, 1))
- glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, torus.GL_Pnames_dict.get(GL_AMBIENT_AND_DIFFUSE)) ###рассеянный свет
- glMaterialfv(GL_FRONT, GL_SPECULAR, torus.GL_Pnames_dict.get(GL_SPECULAR)) ###отражаемый свет
- glMaterialfv(GL_FRONT, GL_EMISSION, torus.GL_Pnames_dict.get(GL_EMISSION)) ###излучаемый свет
- glMaterialfv(GL_FRONT, GL_SHININESS, torus.GL_Pnames_dict.get(GL_SHININESS)) #степень блеска
- def searchForLowestPoint():
- min = 10
- minP = False
- for i in range(len(torus.polygonMesh)):
- for j in range(4):
- if torus.polygonMesh[i][j][2] < min:
- min = torus.polygonMesh[i][j][2]
- minP = torus.polygonMesh[i][j]
- return minP
- def impact():
- global v0
- if torus.center[0] + torus.R >= 1 or torus.center[0] - torus.R <= -1:
- v0 = [-v0[0], v0[1], v0[2]]
- return v0
- elif torus.center[1] + torus.R >= 1 or torus.center[1] - torus.R <= -1:
- v0 = [v0[0], -v0[1], v0[2]]
- return v0
- elif torus.center[2] + torus.R >= 1 or torus.center[2] - torus.R <= -1:
- v0 = [v0[0], v0[1], -v0[2]]
- return v0
- else:
- return v0
- def drop(t):
- v0 = impact()
- x = h0[0] + v0[0]*t
- y = h0[1] + v0[1]*t - 9.8 * t * t / 2
- z = h0[2] + v0[2]*t
- return [x, y, z]
- def h(t):
- global v0
- x = h0[0] + v0[0] * t
- y = h0[1] + v0[1] * t - 9.8 * t * t / 2
- z = h0[2] + v0[2] * t
- if x + torus.R >= 1 or x - torus.R <= -1:
- v0 = [-v0[0], v0[1], v0[2]]
- return torus.center
- elif y + torus.R >= 1 or y - torus.R <= -1:
- v0 = [v0[0], -v0[1], v0[2]]
- return torus.center
- elif z + torus.R >= 1 or z - torus.R <= -1:
- v0 = [v0[0], v0[1], -v0[2]]
- return torus.center
- else:
- return [x, y, z]
- def v(t):
- v = [0, 0, 0]
- v[0] = v0[0]
- v[1] = v0[1] - 9.8 * t
- v[2] = v0[2]
- return v
- def drop1(t):
- if torus.center == h(t + 1/60):
- return torus.center
- else:
- point = h(t)
- return point
- global v0
- v0 = [0, -1, 0]
- global h0
- h0 = [0, 0.5, 0]
- global t
- t = 0
- def draw():
- global t
- drawTorus()
- makeLighting()
- if torus.drop:
- t += 1 / 60
- #print(t)
- center = drop1(t)
- print("center", center)
- print("v0", v0)
- torus.center = center
- 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_mouse_button_callback(self.window, mouseCallback)
- glfw.set_key_callback(self.window, keyCallback)
- glfw.set_scroll_callback(self.window, scrollCallback)
- def startLoop(self):
- while not glfw.window_should_close(self.window):
- background()
- draw()
- glfw.swap_buffers(self.window)
- glfw.poll_events()
- glfw.terminate()
- torus = Torus()
- drawer = Drawer()
- drawer.startLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement