Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #вариант 12
- import glfw
- import math
- from OpenGL.GL import *
- from OpenGL.GLU import *
- from PIL import Image
- import time
- t = 0.08
- rotate_y = 0
- rotate_x = 0
- rotate_z = 0
- scale = 0.2
- n = 10
- a = 30
- r = 0.7
- tx = t
- ty = t
- tz = -t
- animation = False
- textrs = False
- mode = False
- press1 = False
- press2 = False
- press3 = False
- def init():
- global press1, press2, press3
- if press1:
- glLightfv(GL_LIGHT0, GL_AMBIENT, [0, 0, 1, 1])
- elif not press1:
- glLightfv(GL_LIGHT0, GL_AMBIENT, [0, 0, 0, 1])
- if press2:
- glLightfv(GL_LIGHT0, GL_DIFFUSE, [1, 0, 0, 1])
- elif not press2:
- glLightfv(GL_LIGHT0, GL_DIFFUSE, [1, 1, 1, 1])
- if press3:
- glLightfv(GL_LIGHT0, GL_SPECULAR, [0, 1, 0, 1])
- elif not press3:
- glLightfv(GL_LIGHT0, GL_SPECULAR, [1, 1, 1, 1])
- def add_h(i):
- x = r * math.cos(0 * math.pi / 2 + deltaAngle * i)
- y = r * math.sin(0 * math.pi / 2 + deltaAngle * i)
- Vertices.extend([x, y, deltaHeight * i])
- x = r * math.cos(1 * math.pi / 2 + deltaAngle * i)
- y = r * math.sin(1 * math.pi / 2 + deltaAngle * i)
- Vertices.extend([x, y, deltaHeight * i])
- x = r * math.cos(2 * math.pi / 2 + deltaAngle * i)
- y = r * math.sin(2 * math.pi / 2 + deltaAngle * i)
- Vertices.extend([x, y, deltaHeight * i])
- x = r * math.cos(3 * math.pi / 2 + deltaAngle * i)
- y = r * math.sin(3 * math.pi / 2 + deltaAngle * i)
- Vertices.extend([x, y, deltaHeight * i])
- Colors.extend([0, 0, 1])
- Colors.extend([0, 1, 0])
- Colors.extend([1, 0, 0])
- Colors.extend([0, 0, 1])
- texels.extend([1,0])
- texels.extend([0,0])
- texels.extend([0,1])
- texels.extend([1,1])
- def add_st(a,b,i):
- x = r * math.cos(a * math.pi / 2 + deltaAngle * i)
- y = r * math.sin(a * math.pi / 2 + deltaAngle * i)
- Vertices.extend([x, y, deltaHeight * i])
- x = r * math.cos(b * math.pi / 2 + deltaAngle * i)
- y = r * math.sin(b * math.pi / 2 + deltaAngle * i)
- Vertices.extend([x, y, deltaHeight * i])
- x = r * math.cos(b * math.pi / 2 + deltaAngle * (i + 1))
- y = r * math.sin(b * math.pi / 2 + deltaAngle * (i + 1))
- Vertices.extend([x, y, deltaHeight * (i + 1)])
- x = r * math.cos(a * math.pi / 2 + deltaAngle * (i + 1))
- y = r * math.sin(a * math.pi / 2 + deltaAngle * (i + 1))
- Vertices.extend([x, y, deltaHeight * (i + 1)])
- Colors.extend([0, 0, 1])
- Colors.extend([0, 1, 0])
- Colors.extend([1, 0, 0])
- Colors.extend([0, 0, 1])
- texels.extend([0,0])
- texels.extend([0,1])
- texels.extend([1,1])
- texels.extend([1,0])
- def texture():
- global tex
- image = Image.open('/home/kossem/brick.bmp')
- image = image.tobytes("raw","RGBX", 0, -1 )
- tex = glGenTextures(1)
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR)
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR)
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
- gluBuild2DMipmaps(GL_TEXTURE_2D, 3, 256, 256, GL_RGBA, GL_UNSIGNED_BYTE, image)
- def normals(dot1, dot2, dot3):
- vect1 = [dot2[0]-dot1[0], dot2[1]-dot1[1], dot2[2]-dot1[2]]
- vect2 = [dot3[0]-dot1[0], dot3[1]-dot1[1], dot3[2]-dot1[2]]
- return [vect1[1]*vect2[2]-vect1[2]*vect2[1], vect1[2]*vect2[0]-vect1[0]*vect2[2], vect1[0]*vect2[1]-vect1[1]*vect2[0]]
- def light(L):
- if L:
- glEnable(GL_LIGHTING)
- glEnable(GL_LIGHT0)
- glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, math.pi)
- glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 0)
- glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, [0, 0, -1])
- def anim():
- global Vertices, tx, ty, tz
- i = 0
- for i in range (len(Vertices)):
- if i%3 == 0:
- Vertices[i] += tx
- elif i%3 == 1:
- Vertices[i] += ty
- elif i%3 == 2:
- Vertices[i] += tz
- i = 0
- for i in range (len(Vertices)):
- if i%3 == 0 and Vertices[i] >= 3:
- tx = -t
- if i%3 == 0 and Vertices[i] <= -3:
- tx = t
- if i%3 == 1 and Vertices[i] >= 3:
- ty = -t
- if i%3 == 1 and Vertices[i] <= -3:
- ty = t
- if i%3 == 2 and Vertices[i] >= 3:
- tz = -t
- if i%3 == 2 and Vertices[i] <= -3:
- tz = t
- def start_point():
- global Vertices, Colors, n, angle, a, deltaHeight, deltaAngle, Normals, texels, textrs
- Normals = list()
- Vertices = list()
- Colors = list()
- texels = list()
- if n <= 0:
- n = 10
- height = 2
- angle = (a * math.pi) / 180
- deltaHeight = height / n
- deltaAngle = angle / n
- add_h(0)
- for i in range(n):
- if i != n - 1:
- add_st(0, 1, i)
- add_st(1, 2, i)
- add_st(2, 3, i)
- add_st(3, 0, i)
- else:
- add_h(n-1)
- i = 0
- print(int(len(Vertices) / 12))
- while i < len(Vertices):
- if i == 0:
- Normals.extend([0, 0, -1]*4)
- i += 12
- elif i == len(Vertices)-12:
- Normals.extend([0, 0, 1]*4)
- i += 12
- else:
- Normals.extend(normals([Vertices[i], Vertices[i+1], Vertices[i+2]], [Vertices[i+3], Vertices[i+4], Vertices[i+5]], [Vertices[i+6], Vertices[i+7], Vertices[i+8]])*4)
- i += 12
- i = 0
- def key_callback(window, key, scancode, action, mods):
- global rotate_y, rotate_x, scale, n, mode, a, animation, textrs, press1, press2, press3, rotate_z
- if action == glfw.PRESS and key == glfw.KEY_ESCAPE:
- glfw.set_window_should_close(window, 1)
- return
- elif action == glfw.PRESS and key == glfw.KEY_Y and animation == False:
- animation = True
- elif action == glfw.PRESS and key == glfw.KEY_Y and animation == True:
- animation = False
- elif key == glfw.KEY_RIGHT:
- rotate_y += 5
- elif key == glfw.KEY_LEFT:
- rotate_y -= 5
- elif key == glfw.KEY_O:
- rotate_z += 5
- elif key == glfw.KEY_P:
- rotate_z -= 5
- if key == glfw.KEY_UP:
- rotate_x += 5
- elif key == glfw.KEY_DOWN:
- rotate_x -= 5
- elif action == glfw.PRESS and key == glfw.KEY_A:
- scale += 0.1
- elif action == glfw.PRESS and key == glfw.KEY_Z and scale > 0.2:
- scale -= 0.1
- elif action == glfw.PRESS and key == glfw.KEY_T and textrs == False :
- print("TEXTURE MOD ON")
- glEnable(GL_TEXTURE_2D)
- textrs = True
- set_up()
- elif action == glfw.PRESS and key == glfw.KEY_T and textrs == True:
- print("TEXTURE MOD OFF")
- glDisable(GL_TEXTURE_2D)
- textrs = False
- set_up()
- elif action == glfw.PRESS and key == glfw.KEY_M and mode == True:
- mode = False
- set_up()
- elif action == glfw.PRESS and key == glfw.KEY_M and mode == False:
- mode = True
- set_up()
- elif action == glfw.PRESS and key == glfw.KEY_S:
- n += 10
- start_point()
- set_up()
- elif action == glfw.PRESS and key == glfw.KEY_X:
- n -= 10
- start_point()
- set_up()
- elif action == glfw.PRESS and key == glfw.KEY_D:
- a += 15
- start_point()
- set_up()
- elif action == glfw.PRESS and key == glfw.KEY_C:
- a -= 15
- start_point()
- set_up()
- if action == glfw.PRESS and key == glfw.KEY_9 and not press1:
- press1 = True
- glLightfv(GL_LIGHT0, GL_AMBIENT, [0, 0, 1, 1])
- elif action == glfw.PRESS and key == glfw.KEY_9 and press1:
- press1 = False
- glLightfv(GL_LIGHT0, GL_AMBIENT, [0, 0, 0, 1])
- if action == glfw.PRESS and key == glfw.KEY_8 and not press2:
- glLightfv(GL_LIGHT0, GL_DIFFUSE, [1,0,0,1])
- press2 = True
- elif action == glfw.PRESS and key == glfw.KEY_8 and press2:
- glLightfv(GL_LIGHT0, GL_DIFFUSE, [1,1,1,1])
- press2 = False
- if action == glfw.PRESS and key == glfw.KEY_7 and not press3:
- glLightfv(GL_LIGHT0, GL_SPECULAR, [0,1,0,1])
- press3 = True
- elif action == glfw.PRESS and key == glfw.KEY_7 and press3:
- glLightfv(GL_LIGHT0, GL_SPECULAR, [1,1,1,1])
- press3 = False
- if action == glfw.PRESS and key == glfw.KEY_5:
- save_f()
- if action == glfw.PRESS and key == glfw.KEY_6:
- load_f()
- init()
- if action == glfw.PRESS and key == glfw.KEY_L:
- print("LIGHT MOD ON")
- light(True)
- set_up()
- def save_f():
- global animation, t, rotate_x, textrs, rotate_y, n, scale, a, mode, press1, press2, press3
- f = open('text.txt', 'w')
- if animation: f.write("True"+'\t')
- else: f.write("False"+'\t')
- if textrs: f.write("True"+'\t')
- else: f.write("False"+'\t')
- f.write(str(t)+'\t')
- f.write(str(rotate_x)+'\t')
- f.write(str(rotate_y)+'\t')
- f.write(str(n)+'\t')
- f.write(str(scale)+'\t')
- f.write(str(a)+'\t')
- if press1: f.write("True"+'\t')
- else: f.write("False"+'\t')
- if press2: f.write("True"+'\t')
- else: f.write("False"+'\t')
- if press3: f.write("True"+'\t')
- else: f.write("False"+'\t')
- f.write(str(mode)+'\t')
- f.close()
- def load_f():
- global animation, t, rotate_x, textrs, rotate_y, n, scale, a, mode, press1, press2, press3
- with open('text.txt','r') as f:
- for line in f:
- words = line.split()
- if words[0] == 'False': animation = False
- else: animation = True
- if words[1] == 'False': textrs = False
- else: textrs = True
- t = float(words[2])
- rotate_x = int(words[3])
- rotate_y = int(words[4])
- n = int(words[5])
- scale = float(words[6])
- a = int(words[7])
- if words[8] == 'False': press1 = False
- else: press1 = True
- if words[9] == 'False': press2 = False
- else: press2 = True
- if words[10] == 'False': press3 = False
- else: press3 = True
- if words[11] == 'False': mode = False
- else: mode = True
- def size_callback(window, width, height):
- if width < height:
- glViewport(0, 0, width, width)
- else:
- glViewport(0, 0, height, height)
- def prism():
- global Vertices, Colors, Normals, texels, animation, textrs, mode
- if animation:
- anim()
- glEnableClientState(GL_VERTEX_ARRAY)
- glEnableClientState(GL_COLOR_ARRAY)
- glEnableClientState(GL_NORMAL_ARRAY)
- glEnableClientState(GL_TEXTURE_COORD_ARRAY)
- if textrs:
- glEnable(GL_TEXTURE_2D)
- else:
- glDisable(GL_TEXTURE_2D)
- glColorPointer(3, GL_FLOAT, 0, Colors)
- glVertexPointer(3, GL_FLOAT, 0, Vertices)
- glNormalPointer(GL_FLOAT, 0, Normals)
- glTexCoordPointer(2, GL_FLOAT, 0, texels)
- if mode:
- glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
- else:
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
- glDrawArrays(GL_QUADS, 0, int(len(Vertices) / 3 ))
- glDisableClientState(GL_VERTEX_ARRAY)
- glDisableClientState(GL_COLOR_ARRAY)
- glDisableClientState(GL_NORMAL_ARRAY)
- glDisableClientState(GL_TEXTURE_COORD_ARRAY)
- def Ort_Matrix(md):
- glMatrixMode(GL_PROJECTION)
- glLoadIdentity()
- if md == "z":
- glMultMatrixf([1, 0, 0, 0,
- 0, 1, 0, 0,
- 0, 0, -1, 0,
- 0, 0, 0, 1])
- elif md == "x":
- glMultMatrixf([0, 0, -1, 0,
- 0, 1, 0, 0,
- -1, 0, 0, 0,
- 0, 0, 0, 1])
- elif md == "y":
- glMultMatrixf([1, 0, 0, 0,
- 0, 0, -1, 0,
- 0, -1, 0, 0,
- 0, 0, 0, 1])
- glMatrixMode(GL_MODELVIEW)
- def draw_pr(md):
- Ort_Matrix(md)
- glLoadIdentity()
- glPushMatrix()
- glRotatef(rotate_x, 1.0, 0.0, 0.0)
- glRotatef(rotate_y, 0.0, 1.0, 0.0)
- glRotatef(rotate_z, 0.0, 0.0, 1.0)
- glScale(scale, scale, scale)
- glCallList(display)
- glPopMatrix()
- def draw(window):
- global rotate_y, rotate_x, scale, mode, position
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
- glEnable(GL_DEPTH_TEST)
- glDepthFunc(GL_LESS)
- width, height = glfw.get_window_size(window)
- #glViewport(int(width/2),int(height/2),int(width/2),int(height/2))
- draw_pr("x")
- #glViewport(0,0,int(width/2),int(height/2))
- #draw_pr("y")
- #glViewport(int(width/2),0,int(width/2),int(height/2))
- #draw_pr("z")
- def set_up():
- global display
- display = glGenLists(1)
- glNewList(display,GL_COMPILE)
- prism()
- glEndList()
- def main():
- if not glfw.init():
- return
- global window
- window = glfw.create_window(800, 800, "Lab 3", None, None)
- if not window:
- glfw.terminate()
- return
- forCheck = 0
- sum_time = 0
- start_point()
- glfw.make_context_current(window)
- glfw.set_key_callback(window, key_callback)
- glfw.set_framebuffer_size_callback(window, size_callback)
- texture()
- set_up()
- while not glfw.window_should_close(window):
- start = time.clock()
- draw(window)
- glfw.swap_buffers(window)
- glfw.poll_events()
- time_from_start = time.clock()-start
- sum_time += time_from_start
- if forCheck == 50:
- print("time = ", round(sum_time/50.0, 5))
- forCheck = 0
- sum_time = 0
- forCheck += 1
- glfw.destroy_window(window)
- glfw.terminate()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement