Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from OpenGL.GL import *
- from OpenGL.GLUT import *
- from math import *
- import numpy as np
- f = list()
- v = list()
- tocke = list()
- width, height = 1400, 800
- scale = 100
- transition = 0
- P = []
- xg,yg,zg,xo,yo,zo = 0,0,0,0,0,0
- def matricaPogleda(xg,yg,zg,xo,yo,zo):
- T1 = np.matrix([[1,0,0,0],[0,1,0,0],[0,0,1,0],[-xo,-yo,-zo,1]])
- xg1 = xg - xo
- yg1 = yg - yo
- zg1 = zg - zo
- sina = yg1 / sqrt(pow(xg1,2) + pow(yg1,2))
- cosa = xg1 / sqrt(pow(xg1,2) + pow(yg1,2))
- T2 = np.matrix([[cosa,-sina,0,0],[sina,cosa,0,0],[0,0,1,0],[0,0,0,1]])
- xg2 = sqrt(pow(xg1,2) + pow(yg1,2))
- yg2 = 0
- zg2 = zg1
- sinb = xg2 / sqrt(pow(xg2,2) + pow(zg2,2))
- cosb = zg2 / sqrt(pow(xg2,2) + pow(zg2,2))
- T3 = np.matrix([[cosb,0,sinb,0],[0,1,0,0],[-sinb,0,cosb,0],[0,0,0,1]])
- T4 = np.matrix([[0,-1,0,0],[1,0,0,0],[0,0,1,0],[0,0,0,1]])
- T5 = np.matrix([[-1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]])
- T = T1 * T2 * T3 * T4 * T5
- return T
- def perspektivnaProjekcija(xg,yg,zg, xo,yo,zo):
- H = sqrt(pow(xo-xg,2) + pow(yo-yg,2) + pow(zo-zg,2))
- m = [[1,0,0,0],[0,1,0,0],[0,0,0,1/H],[0,0,0,0]]
- return np.array(m,dtype='float32')
- def poligon():
- global tocke
- glBegin(GL_LINES)
- for i in range(0,len(tocke)-1):
- glVertex3f(tocke[i][0], tocke[i][1], tocke[i][2])
- glVertex3f(tocke[i+1][0], tocke[i+1][1], tocke[i+1][2])
- glEnd()
- glutSwapBuffers()
- def brezier():
- global tocke,xg,yg,zg,xo,yo,zo,f
- glClear(GL_COLOR_BUFFER_BIT)
- konacnetocke = list()
- n = len(tocke) - 1
- t = 0
- n1 = factorial(n)
- while t <= 1:
- i = 0
- xk, yk , zk = 0,0,0
- for tocka in tocke:
- b = n1 / (factorial(i)*factorial(n-i)) * pow(t,i) * pow(1-t,n-i)
- x1 = b * tocka[0]
- y1 = b * tocka[1]
- z1 = b * tocka[2]
- xk += x1
- yk += y1
- zk += z1
- i += 1
- konacnetocke.append([xk,yk,zk])
- t += 0.01
- t1L = list()
- t2L = list()
- t3L = list()
- for tocka in konacnetocke:
- glClear(GL_COLOR_BUFFER_BIT)
- xo1, yo1, zo1 = tocka[0], tocka[1], tocka[2]
- P = perspektivnaProjekcija(xg,yg,zg,xo1,yo1,zo1)
- T = matricaPogleda (xg,yg,zg,xo1,yo1,zo1)
- for tocka in f:
- t1 = int(tocka[0])
- t2 = int(tocka[1])
- t3 = int(tocka[2])
- t1L = np.array([float(v[t1-1][0]), float(v[t1-1][1]), float(v[t1-1][2]), float(v[t1-1][3])])
- t2L = np.array([float(v[t2-1][0]), float(v[t2-1][1]), float(v[t2-1][2]), float(v[t3-1][3])])
- t3L = np.array([float(v[t3-1][0]), float(v[t3-1][1]), float(v[t3-1][2]), float(v[t3-1][3])])
- t32 = np.array(t3L) - np.array(t2L)
- t32 = t32[:-1]
- t12 = np.array(t1L) - np.array(t2L)
- t12 = t12[:-1]
- normalaPoligona = np.cross(t32, t12)
- sx = (np.array(t1L)[0] + np.array(t2L)[0] + np.array(t3L)[0]) / 3
- sy = (np.array(t1L)[1] + np.array(t2L)[1] + np.array(t3L)[1]) / 3
- sz = (np.array(t1L)[2] + np.array(t2L)[2] + np.array(t3L)[2]) / 3
- s = np.array([sx,sy,sz])
- t1L = np.array([float(v[t1-1][0]), float(v[t1-1][1]), float(v[t1-1][2]), float(v[t1-1][3])]).dot(T).dot(P)
- t2L = np.array([float(v[t2-1][0]), float(v[t2-1][1]), float(v[t2-1][2]), float(v[t3-1][3])]).dot(T).dot(P)
- t3L = np.array([float(v[t3-1][0]), float(v[t3-1][1]), float(v[t3-1][2]), float(v[t3-1][3])]).dot(T).dot(P)
- if (normalaPoligona.dot(s) > 0):
- glBegin(GL_LINE_LOOP)
- glVertex3f(np.array(t1L)[0][0] * scale + transition, np.array(t1L)[0][1] * scale + transition, np.array(t1L)[0][2] * scale + transition)
- glVertex3f(np.array(t2L)[0][0] * scale + transition, np.array(t2L)[0][1] * scale + transition, np.array(t2L)[0][2] * scale + transition)
- glVertex3f(np.array(t3L)[0][0] * scale + transition, np.array(t3L)[0][1] * scale + transition, np.array(t3L)[0][2] * scale + transition)
- glEnd()
- glutSwapBuffers()
- def draw_object():
- global f,v,scale,transition
- glClear(GL_COLOR_BUFFER_BIT)
- for tocka in f:
- t1 = int(tocka[0])
- t2 = int(tocka[1])
- t3 = int(tocka[2])
- glBegin(GL_LINE_LOOP)
- glVertex3f(float(v[t1-1][0]) * scale + transition, float(v[t1-1][1]) * scale + transition, float(v[t1-1][2]) * scale + transition)
- glVertex3f(float(v[t2-1][0]) * scale + transition, float(v[t2-1][1]) * scale + transition, float(v[t2-1][2]) * scale + transition)
- glVertex3f(float(v[t3-1][0]) * scale + transition, float(v[t3-1][1]) * scale + transition, float(v[t3-1][2]) * scale + transition)
- glEnd()
- glutSwapBuffers()
- def keyboard(key, mouseX, mouseY):
- global prikaz
- if key == 'q':
- exit()
- elif key == ' ':
- brezier()
- elif key == 'a':
- poligon()
- def refresh2d(width, height):
- glViewport(0, 0, width, height)
- glMatrixMode(GL_PROJECTION)
- glLoadIdentity()
- glOrtho(-width/2, width/2, -height/2, height/2, 300.0, -300)
- glMatrixMode (GL_MODELVIEW)
- glLoadIdentity()
- def main():
- lines = [line.rstrip('\n') for line in open('datoteka.txt')]
- for line in lines:
- (a, b, c) = (float(line.split(" ")[0]), float(line.split(" ")[1]), float(line.split(" ")[2]))
- tocke.append([a,b,c])
- lines1 = [line1.rstrip('\n') for line1 in open('teapot.txt')]
- (xg, yg, zg,hg) = (int(lines1[0].split(" ")[0]), int(lines1[0].split(" ")[1]), int(lines1[0].split(" ")[2]),1)
- (xo, yo, zo,ho) = (int(lines1[1].split(" ")[0]), int(lines1[1].split(" ")[1]), int(lines1[1].split(" ")[2]),1)
- for line in lines1[2:]:
- if line.startswith('v'):
- a,b,c = line.split(" ")[1:]
- a,b,c = (float(a), float(b), float(c))
- v.append([a,b,c,1])
- elif line.startswith('f'):
- f.append(line.split(" ")[1:])
- glutInit()
- glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE)
- glutInitWindowSize(width, height)
- glutInitWindowPosition(0, 0)
- glutCreateWindow("zadatak2")
- refresh2d(width, height)
- glutIdleFunc(brezier)
- glutKeyboardFunc(keyboard)
- glutMainLoop()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement