Advertisement
Guest User

irg zadatak 2

a guest
May 23rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.33 KB | None | 0 0
  1. from OpenGL.GL import *
  2. from OpenGL.GLUT import *
  3. from math import *
  4. import numpy as np
  5.  
  6. f = list()
  7. v = list()
  8. tocke = list()
  9. width, height = 1400, 800
  10. scale = 100
  11. transition = 0
  12. P = []
  13. xg,yg,zg,xo,yo,zo = 0,0,0,0,0,0
  14.  
  15. def matricaPogleda(xg,yg,zg,xo,yo,zo):
  16.  
  17.     T1 = np.matrix([[1,0,0,0],[0,1,0,0],[0,0,1,0],[-xo,-yo,-zo,1]])
  18.  
  19.     xg1 = xg - xo
  20.     yg1 = yg - yo
  21.     zg1 = zg - zo
  22.  
  23.     sina = yg1 / sqrt(pow(xg1,2) + pow(yg1,2))
  24.     cosa = xg1 / sqrt(pow(xg1,2) + pow(yg1,2))
  25.  
  26.     T2 = np.matrix([[cosa,-sina,0,0],[sina,cosa,0,0],[0,0,1,0],[0,0,0,1]])
  27.  
  28.  
  29.     xg2 = sqrt(pow(xg1,2) + pow(yg1,2))
  30.     yg2 = 0
  31.     zg2 = zg1
  32.  
  33.     sinb =  xg2 / sqrt(pow(xg2,2) + pow(zg2,2))
  34.     cosb =  zg2 / sqrt(pow(xg2,2) + pow(zg2,2))
  35.  
  36.     T3 = np.matrix([[cosb,0,sinb,0],[0,1,0,0],[-sinb,0,cosb,0],[0,0,0,1]])
  37.  
  38.     T4 = np.matrix([[0,-1,0,0],[1,0,0,0],[0,0,1,0],[0,0,0,1]])
  39.  
  40.     T5 = np.matrix([[-1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]])
  41.     T = T1 * T2 * T3 * T4 * T5
  42.     return T
  43.  
  44.  
  45. def perspektivnaProjekcija(xg,yg,zg, xo,yo,zo):
  46.     H = sqrt(pow(xo-xg,2) + pow(yo-yg,2) + pow(zo-zg,2))
  47.     m = [[1,0,0,0],[0,1,0,0],[0,0,0,1/H],[0,0,0,0]]
  48.     return np.array(m,dtype='float32')
  49.  
  50. def poligon():
  51.     global tocke
  52.  
  53.     glBegin(GL_LINES)
  54.     for i in range(0,len(tocke)-1):
  55.             glVertex3f(tocke[i][0], tocke[i][1], tocke[i][2])
  56.             glVertex3f(tocke[i+1][0], tocke[i+1][1], tocke[i+1][2])
  57.     glEnd()
  58.     glutSwapBuffers()
  59.  
  60. def brezier():
  61.     global tocke,xg,yg,zg,xo,yo,zo,f
  62.     glClear(GL_COLOR_BUFFER_BIT)
  63.     konacnetocke = list()
  64.     n = len(tocke) - 1
  65.     t = 0
  66.     n1 = factorial(n)
  67.     while t <= 1:
  68.         i = 0
  69.         xk, yk , zk = 0,0,0
  70.         for tocka in tocke:
  71.             b = n1 / (factorial(i)*factorial(n-i))  * pow(t,i) * pow(1-t,n-i)
  72.             x1 = b * tocka[0]
  73.             y1 = b * tocka[1]
  74.             z1 = b * tocka[2]
  75.             xk += x1
  76.             yk += y1
  77.             zk += z1
  78.             i += 1
  79.         konacnetocke.append([xk,yk,zk])
  80.         t += 0.01
  81.  
  82.     t1L = list()
  83.     t2L = list()
  84.     t3L = list()
  85.     for tocka in konacnetocke:
  86.         glClear(GL_COLOR_BUFFER_BIT)
  87.         xo1, yo1, zo1 = tocka[0], tocka[1], tocka[2]
  88.         P = perspektivnaProjekcija(xg,yg,zg,xo1,yo1,zo1)
  89.         T = matricaPogleda (xg,yg,zg,xo1,yo1,zo1)
  90.  
  91.         for tocka in f:
  92.             t1 = int(tocka[0])
  93.             t2 = int(tocka[1])
  94.             t3 = int(tocka[2])
  95.             t1L = np.array([float(v[t1-1][0]), float(v[t1-1][1]), float(v[t1-1][2]), float(v[t1-1][3])])
  96.             t2L = np.array([float(v[t2-1][0]), float(v[t2-1][1]), float(v[t2-1][2]), float(v[t3-1][3])])
  97.             t3L = np.array([float(v[t3-1][0]), float(v[t3-1][1]), float(v[t3-1][2]), float(v[t3-1][3])])
  98.  
  99.             t32 = np.array(t3L) - np.array(t2L)
  100.             t32 = t32[:-1]
  101.             t12 = np.array(t1L) - np.array(t2L)
  102.             t12 = t12[:-1]
  103.  
  104.             normalaPoligona = np.cross(t32, t12)
  105.             sx = (np.array(t1L)[0] + np.array(t2L)[0] + np.array(t3L)[0]) / 3
  106.             sy = (np.array(t1L)[1] + np.array(t2L)[1] + np.array(t3L)[1]) / 3
  107.             sz = (np.array(t1L)[2] + np.array(t2L)[2] + np.array(t3L)[2]) / 3
  108.             s = np.array([sx,sy,sz])
  109.  
  110.             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)
  111.             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)
  112.             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)
  113.  
  114.             if (normalaPoligona.dot(s) > 0):
  115.  
  116.                 glBegin(GL_LINE_LOOP)
  117.                 glVertex3f(np.array(t1L)[0][0] * scale + transition, np.array(t1L)[0][1] * scale + transition, np.array(t1L)[0][2] * scale + transition)
  118.                 glVertex3f(np.array(t2L)[0][0] * scale + transition, np.array(t2L)[0][1] * scale + transition, np.array(t2L)[0][2] * scale + transition)
  119.                 glVertex3f(np.array(t3L)[0][0] * scale + transition, np.array(t3L)[0][1] * scale + transition, np.array(t3L)[0][2] * scale + transition)
  120.                 glEnd()
  121.         glutSwapBuffers()
  122.  
  123. def draw_object():
  124.     global f,v,scale,transition
  125.     glClear(GL_COLOR_BUFFER_BIT)
  126.     for tocka in f:
  127.         t1 = int(tocka[0])
  128.         t2 = int(tocka[1])
  129.         t3 = int(tocka[2])
  130.         glBegin(GL_LINE_LOOP)
  131.         glVertex3f(float(v[t1-1][0]) * scale + transition, float(v[t1-1][1]) * scale + transition, float(v[t1-1][2]) * scale + transition)
  132.         glVertex3f(float(v[t2-1][0]) * scale + transition, float(v[t2-1][1]) * scale + transition, float(v[t2-1][2]) * scale + transition)
  133.         glVertex3f(float(v[t3-1][0]) * scale + transition, float(v[t3-1][1]) * scale + transition, float(v[t3-1][2]) * scale + transition)
  134.         glEnd()
  135.     glutSwapBuffers()
  136.  
  137. def keyboard(key, mouseX, mouseY):
  138.     global prikaz
  139.     if key == 'q':
  140.         exit()
  141.     elif key == ' ':
  142.          brezier()
  143.     elif key == 'a':
  144.         poligon()
  145.  
  146.  
  147. def refresh2d(width, height):
  148.     glViewport(0, 0, width, height)
  149.     glMatrixMode(GL_PROJECTION)
  150.     glLoadIdentity()
  151.     glOrtho(-width/2, width/2, -height/2, height/2, 300.0, -300)
  152.     glMatrixMode (GL_MODELVIEW)
  153.     glLoadIdentity()
  154.  
  155. def main():
  156.     lines = [line.rstrip('\n') for line in open('datoteka.txt')]
  157.     for line in lines:
  158.         (a, b, c) = (float(line.split(" ")[0]), float(line.split(" ")[1]), float(line.split(" ")[2]))
  159.         tocke.append([a,b,c])
  160.  
  161.     lines1 = [line1.rstrip('\n') for line1 in open('teapot.txt')]
  162.     (xg, yg, zg,hg) = (int(lines1[0].split(" ")[0]), int(lines1[0].split(" ")[1]), int(lines1[0].split(" ")[2]),1)
  163.     (xo, yo, zo,ho) = (int(lines1[1].split(" ")[0]), int(lines1[1].split(" ")[1]), int(lines1[1].split(" ")[2]),1)
  164.     for line in lines1[2:]:
  165.         if line.startswith('v'):
  166.             a,b,c = line.split(" ")[1:]
  167.             a,b,c = (float(a), float(b), float(c))
  168.             v.append([a,b,c,1])
  169.         elif line.startswith('f'):
  170.             f.append(line.split(" ")[1:])
  171.  
  172.     glutInit()
  173.     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE)
  174.     glutInitWindowSize(width, height)
  175.     glutInitWindowPosition(0, 0)
  176.     glutCreateWindow("zadatak2")
  177.     refresh2d(width, height)
  178.     glutIdleFunc(brezier)
  179.  
  180.  
  181.     glutKeyboardFunc(keyboard)
  182.     glutMainLoop()
  183.  
  184. if __name__ == '__main__':
  185.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement