Void-voiD

Untitled

May 27th, 2020
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 18.32 KB | None | 0 0
  1. from math import sqrt, pi, cos, sin
  2. from mathutils import Matrix
  3. from pyglet import image
  4. from pyglet.gl import *
  5. from pyglet.window import key
  6. from pyglet.window import mouse
  7. from OpenGL.GLUT import *
  8. from numpy import array, cross
  9. import json
  10. import glfw
  11.  
  12.  
  13. window = pyglet.window.Window(1400, 1400, resizable = True, vsync = 0)
  14. window.set_minimum_size(144, 144)
  15. gl.glClearColor(0, 0.6, 0.8, 1)
  16. glutInit()
  17.  
  18. Width = 1000
  19. Height = 1000
  20. ratio = 1
  21. pos = array([0.0, 0.0, 0.0])
  22. rot = [0, 0, 0]
  23. isFramedMode = False
  24. Horizontal = 5
  25. Vertical = 7
  26. Points = [[array([0.0, 0.0, 0.0]) for i in range(Vertical)] for j in range(Horizontal)]
  27. heightOfParaboloid = 1
  28. P = 0.25
  29. Q = 0.5
  30. Changed = False
  31. border = array([[-0.4, -0.4, -0.4], [1.1, 1.1, 1.1]])
  32. figure = array([[-0.2, -sqrt(0.5) / 5.0, 0.4], [0.2, sqrt(0.5) / 5.0, 0.6]])
  33.  
  34. chess = image.load("wood5.bmp")
  35. texture = chess.get_texture()
  36.  
  37. ambient_color = (GLfloat * 4)()
  38. local_viewer = (GLfloat * 3)()
  39. two_side = (GLfloat * 1)()
  40. speed, vector, coordx, coordy, coordz, is_infinity_light, face, is_local, is_animated, is_lighted, is_textured, is_material = None, None, None, None, None, None, None, None, None, None, None, None
  41. ambient, diffuse, specular, shininess, emission = (GLfloat * 4)(), (GLfloat * 4)(), (GLfloat * 4)(), 0, (GLfloat * 4)()
  42. ids = glGenLists(7)
  43.  
  44.  
  45. def load():
  46.     with open('data.txt') as json_file:
  47.         global speed, vector, coordx, coordy, coordz, is_infinity_light, face, pname, ambient_color, local_viewer, two_side, parameters, is_local, is_animated, is_lighted, is_textured
  48.         global ambient, diffuse, specular, shininess, emission, is_material
  49.         result = json.load(json_file)
  50.         is_textured = result['is_textured']
  51.         is_lighted = result['is_lighted']
  52.         is_animated = result['is_animated']
  53.         speed = result['speed']
  54.         vector = array([speed, speed, speed])
  55.         coordx, coordy, coordz, is_infinity_light = result['light1'][0]['coordx'], result['light1'][0]['coordy'], \
  56.                                                     result['light1'][0]['coordz'], result['light1'][0][
  57.                                                         'is_infinity_light']
  58.         ambient_color[0], ambient_color[1], ambient_color[2], ambient_color[3] = result['light3'][0]["ambient_color"][
  59.                                                                                      0], \
  60.                                                                                  result['light3'][0]["ambient_color"][
  61.                                                                                      1], \
  62.                                                                                  result['light3'][0]["ambient_color"][
  63.                                                                                      2], \
  64.                                                                                  result['light3'][0]["ambient_color"][3]
  65.         is_local = result['light4'][0]["is_local"]
  66.         local_viewer[0], local_viewer[1], local_viewer[2] = result['light4'][0]["local_viewer"][0], \
  67.                                                             result['light4'][0]["local_viewer"][1], \
  68.                                                             result['light4'][0]["local_viewer"][2]
  69.         two_side[0] = result["light5"][0]["two_side"]
  70.         face = result["light6"][0]['face']
  71.         ambient[0], ambient[1], ambient[2], ambient[3] = result["ambient"][0], result["ambient"][1], result["ambient"][2], result["ambient"][3]
  72.         diffuse[0], diffuse[1], diffuse[2], diffuse[3] = result["diffuse"][0], result["diffuse"][1], result["diffuse"][2], result["diffuse"][3]
  73.         specular[0], specular[1], specular[2], specular[3] = result["specular"][0], result["specular"][1], result["specular"][2], result["specular"][3]
  74.         shininess = result["shininess"]
  75.         emission[0], emission[1], emission[2], emission[3] = result["emission"][0], result["emission"][1], result["emission"][2], result["emission"][3]
  76.         is_material = result["is_material"]
  77.  
  78.  
  79. def save():
  80.     global speed, coordx, coordy, coordz, is_infinity_light, face, pname, ambient_color, local_viewer, two_side, parameters, is_local, is_animated, is_lighted, is_textured
  81.     global ambient, diffuse, specular, shininess, emission, is_material
  82.     data = {}
  83.  
  84.     data["is_textured"] = is_textured
  85.     data['is_lighted'] = is_lighted
  86.     data['is_animated'] = is_animated
  87.     data['speed'] = speed
  88.     data['light1'] = []
  89.     data['light1'].append({
  90.         'coordx': coordx,
  91.         'coordy': coordy,
  92.         'coordz': coordz,
  93.         'is_infinity_light': is_infinity_light
  94.     })
  95.     data['light3'] = []
  96.     data['light3'].append({
  97.         'ambient_color': [ambient_color[0], ambient_color[1], ambient_color[2], ambient_color[3]]
  98.     })
  99.     data['light4'] = []
  100.     data['light4'].append({
  101.         'is_local': is_local,
  102.         'local_viewer': [local_viewer[0], local_viewer[1], local_viewer[2]]
  103.     })
  104.     data['light5'] = []
  105.     data['light5'].append({
  106.         'two_side': two_side[0]
  107.     })
  108.     data['light6'] = []
  109.     data['light6'].append({
  110.         'face': face,
  111.     })
  112.     data["ambient"] = [] * 4
  113.     data["ambient"][0], data["ambient"][1], data["ambient"][2], data["ambient"][3] = ambient[0], ambient[1], ambient[2], ambient[3]
  114.     data["diffuse"] = [] * 4
  115.     data["diffuse"][0], data["diffuse"][1], data["diffuse"][2], data["diffuse"][3] = diffuse[0], diffuse[1], diffuse[2], diffuse[3]
  116.     data["specular"] = [] * 4
  117.     data["specular"][0], data["specular"][1], data["specular"][2], data["specular"][3] = specular[0], specular[1], specular[2], specular[3]
  118.     data["shininess"] = shininess
  119.     data["emission"] = [] * 4
  120.     data["emission"][0], data["emission"][1], data["emission"][2], data["emission"][3] = emission[0], emission[1], emission[2], emission[3]
  121.     data["is_material"] = is_material
  122.     with open('data.txt', 'w') as outfile:
  123.         json.dump(data, outfile)
  124.  
  125.  
  126. load()
  127.  
  128.  
  129. def which(first, second, third, fourth):
  130.     global ids
  131.     glNewList(ids, GL_COMPILE)
  132.     glBegin(GL_LINE_LOOP)
  133.     glVertex3f(*first)
  134.     glVertex3f(*second)
  135.     glVertex3f(*third)
  136.     glVertex3f(*fourth)
  137.     glEnd()
  138.     glEndList()
  139.     ids += 1
  140.  
  141.  
  142. def baseCube():
  143.     lbf = [-0.5, -0.5, -0.5]
  144.     rbf = [0.5, -0.5, -0.5]
  145.     rtf = [0.5, 0.5, -0.5]
  146.     ltf = [-0.5, 0.5, -0.5]
  147.  
  148.     lbn = [-0.5, -0.5, 0.5]
  149.     rbn = [0.5, -0.5, 0.5]
  150.     rtn = [0.5, 0.5, 0.5]
  151.     ltn = [-0.5, 0.5, 0.5]
  152.  
  153.     # BOTTOM
  154.     which(lbn, rbn, rbf, lbf)
  155.  
  156.     # BACK
  157.     which(lbf, rbf, rtf, ltf)
  158.  
  159.     # LEFT
  160.     which(ltf, ltn, lbn, lbf)
  161.  
  162.     # RIGHT
  163.     which(rtn, rtf, rbf, rbn)
  164.  
  165.     # TOP
  166.     which(ltn, ltf, rtf, rtn)
  167.  
  168.     # FRONT
  169.     which(lbn, ltn, rtn, rbn)
  170.  
  171.  
  172. def sectorOfParaboloid(framed, i, j, top):
  173.     global heightOfParaboloid, Vertical, Horizontal, Points, ids
  174.  
  175.     glNewList(ids, GL_COMPILE)
  176.     if framed:
  177.         glBegin(GL_LINE_LOOP)
  178.     else:
  179.         glBegin(GL_POLYGON)
  180.  
  181.     a = None
  182.     b = None
  183.     c = None
  184.     d = None
  185.  
  186.     if not top:
  187.         a = Points[i][j]
  188.         if j + 1 == Vertical:
  189.             b = Points[i][0]
  190.         else:
  191.             b = Points[i][j + 1]
  192.         if i == 0:
  193.             c = array([0, 0, 0])
  194.             d = array([0, 0, 0])
  195.         else:
  196.             if j + 1 == Vertical:
  197.                 c = Points[i - 1][0]
  198.             else:
  199.                 c = Points[i - 1][j + 1]
  200.             d = Points[i - 1][j]
  201.     else:
  202.         a = Points[i][j]
  203.         a[1] = heightOfParaboloid
  204.         if i == 0:
  205.             b = array([0, 0, 0])
  206.             c = array([0, 0, 0])
  207.             b[1] = heightOfParaboloid
  208.             c[1] = heightOfParaboloid
  209.         else:
  210.             b = Points[i - 1][j]
  211.             b[1] = heightOfParaboloid
  212.             if j + 1 == Vertical:
  213.                 c = Points[i - 1][0]
  214.                 c[1] = heightOfParaboloid
  215.             else:
  216.                 c = Points[i - 1][j + 1]
  217.                 c[1] = heightOfParaboloid
  218.         if j + 1 == Vertical:
  219.             d = Points[i][0]
  220.             d[1] = heightOfParaboloid
  221.         else:
  222.             d = Points[i][j + 1]
  223.             d[1] = heightOfParaboloid
  224.  
  225.     normal = cross(c - a, d - b)
  226.     x = normal[0] ** 2 + normal[1] ** 2 + normal[2] ** 2
  227.     if x != 1:
  228.         length = sqrt(x) * 1.0
  229.         if length > 0:
  230.             normal[0] /= length
  231.             normal[1] /= length
  232.             normal[2] /= length
  233.     glNormal3f(*normal)
  234.  
  235.     glTexCoord2f(i * 1.0 / Horizontal, j * 1.0 / Vertical)
  236.     glVertex3f(*a)
  237.     if not top:
  238.         glTexCoord2f(i * 1.0 / Horizontal, (j + 1 % Vertical) * 1.0 / Vertical)
  239.         glVertex3f(*b)
  240.         if i == 0:
  241.             glTexCoord2f(0, 0)
  242.             glVertex3f(*c)
  243.         else:
  244.             glTexCoord2f((i - 1) * 1.0 / Horizontal, (j + 1 % Vertical) * 1.0 / Vertical)
  245.             glVertex3f(*c)
  246.             glTexCoord2f((i - 1) * 1.0 / Horizontal, j * 1.0 / Vertical)
  247.             glVertex3f(*d)
  248.     else:
  249.         if i == 0:
  250.             glTexCoord2f(0, 0)
  251.             glVertex3f(*b)
  252.         else:
  253.             glTexCoord2f((i - 1) * 1.0 / Horizontal, j * 1.0 / Vertical)
  254.             glVertex3f(*b)
  255.  
  256.             glTexCoord2f((i - 1) * 1.0 / Horizontal, (j + 1 % Vertical) * 1.0 / Vertical)
  257.             glVertex3f(*c)
  258.         glTexCoord2f(i * 1.0 / Horizontal, (j + 1 % Vertical) * 1.0 / Vertical)
  259.         glVertex3f(*d)
  260.  
  261.     glEnd()
  262.     glEndList()
  263.     ids += 1
  264.  
  265.  
  266. def EllepticalParaboloid(framed):
  267.     global Points, Horizontal, Vertical, heightOfParaboloid
  268.     for i in range(Horizontal):
  269.         for j in range(Vertical):
  270.             sectorOfParaboloid(framed, i, j, False)
  271.  
  272.     for i in range(Horizontal):
  273.         for j in range(Vertical):
  274.             sectorOfParaboloid(framed, i, j, True)
  275.  
  276.  
  277. def resetPoints(dt):
  278.     global Points, P, Q, Vertical, Horizontal, heightOfParaboloid, vector, pos, border, figure, is_animated
  279.     Points = [[array([0.0, 0.0, 0.0]) for i in range(Vertical)] for j in range(Horizontal)]
  280.     stepy = float(heightOfParaboloid) / Horizontal
  281.     anglexz = 2 * pi / Vertical
  282.     curangle = 0
  283.     h = 0.0
  284.  
  285.     for i in range(Horizontal):
  286.         h += stepy
  287.         for j in range(Vertical):
  288.             Points[i][j][0] = sqrt(2 * Q * h) * cos(curangle)
  289.             Points[i][j][1] = h
  290.             Points[i][j][2] = sqrt(2 * P * h) * sin(curangle)
  291.             curangle += anglexz
  292.  
  293.     if is_animated:
  294.         if figure[0][0] > border[0][0] and figure[0][1] > border[0][1] and figure[0][2] > border[0][2] and figure[1][0] < border[1][0] and figure[1][1] < border[1][1] and figure[1][2] < border[1][2]:
  295.             pos += vector
  296.             figure += vector
  297.         else:
  298.             # print("-------\n", figure, "\n---\n", border, "\n---\n", vector, "\n-------")
  299.             if figure[0][0] < border[0][0] or figure[1][0] > border[1][0]:
  300.                 vector[0] *= -1
  301.             if figure[0][1] < border[0][1] or figure[1][1] > border[1][1]:
  302.                 vector[1] *= -1
  303.             if figure[0][2] < border[0][2] or figure[1][2] > border[1][2]:
  304.                 vector[2] *= -1
  305.             pos += vector
  306.             figure += vector
  307.  
  308.  
  309. @window.event
  310. def on_draw():
  311.     global Width, Height
  312.     global ratio
  313.     global isFramedMode, Changed
  314.     global Horizontal, Vertical, heightOfParaboloid, P, Q
  315.     global texture, chess
  316.     global coordx, coordy, coordz, is_infinity_light, ambient_color, local_viewer, two_side, face, is_local, is_lighted, is_textured
  317.     global ambient, diffuse, specular, shininess, emission, is_material
  318.     global ids
  319.     window.clear()
  320.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  321.     glEnable(GL_DEPTH_TEST)
  322.  
  323.     if not is_textured:
  324.         glDisable(GL_TEXTURE_2D)
  325.  
  326.     if is_textured:
  327.         glEnable(texture.target)
  328.         glBindTexture(texture.target, texture.id)
  329.         glNewList(ids, GL_COMPILE)
  330.         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, chess.width, chess.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, chess.get_data())
  331.         glEnable(GL_TEXTURE_2D)
  332.         glEnable(GL_NORMALIZE)
  333.         glEndList()
  334.         ids += 1
  335.  
  336.     if not is_lighted:
  337.         glDisable(GL_LIGHTING)
  338.  
  339.     if is_lighted:
  340.         glNewList(ids, GL_COMPILE)
  341.         glEnable(GL_LIGHTING)
  342.         glEnable(GL_LIGHT0)
  343.         if not is_material:
  344.             glEnable(GL_COLOR_MATERIAL)
  345.         else:
  346.             glDisable(GL_COLOR_MATERIAL)
  347.         glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
  348.         glLightiv(GL_LIGHT0, GL_POSITION, (GLint * 4)(coordx, coordy, coordz, is_infinity_light))
  349.  
  350.         glLightfv(GL_LIGHT0, GL_AMBIENT, (GLfloat * 4)(0, 0, 0, 1))
  351.         glLightfv(GL_LIGHT0, GL_DIFFUSE, (GLfloat * 4)(0.2, 0.2, 0.2, 1))
  352.         glLightfv(GL_LIGHT0, GL_SPECULAR, (GLfloat * 4)(0.2, 0.2, 0.2, 1))
  353.  
  354.         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient_color)
  355.         if is_local:
  356.             glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_viewer)
  357.         glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, two_side)
  358.         glEndList()
  359.         ids += 1
  360.  
  361.         if is_material:
  362.             glNewList(ids, GL_COMPILE)
  363.             glMaterialfv(face, GL_AMBIENT, ambient)
  364.             glMaterialfv(face, GL_DIFFUSE, diffuse)
  365.             glMaterialfv(face, GL_SPECULAR, specular)
  366.             glMaterialfv(face, GL_SHININESS, (GLfloat * 1)(shininess))
  367.             glMaterialfv(face, GL_EMISSION, emission)
  368.             glEndList()
  369.             ids += 1
  370.         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
  371.  
  372.     # ------------------------------------------------------------
  373.  
  374.     cx = 3
  375.     cy = 3
  376.     cz = 3
  377.  
  378.     p = float(-1) / float(cx)
  379.     q = float(-1) / float(cy)
  380.     r = float(-1) / float(cz)
  381.  
  382.     matrixShift = Matrix([(1, 0, 0, 0),
  383.                           (0, 1, 0, 0),
  384.                           (0, 0, 1, 0),
  385.                           (0.175, 0.175, 0.175, 1)])
  386.     matrixRatio = Matrix([(1 / ratio, 0, 0, 0),
  387.                           (0, 1, 0, 0),
  388.                           (0, 0, 1, 0),
  389.                           (0, 0, 0, 1)])
  390.     matrixPerspective = Matrix([(1, 0, 0, p),
  391.                                 (0, 1, 0, q),
  392.                                 (0, 1, 0, r),
  393.                                 (0, 0, 0, 1)])
  394.     matrixProjection = Matrix([(1, 0, 0, 0),
  395.                                (0, 1, 0, 0),
  396.                                (0, 0, 0, 0),
  397.                                (0, 0, 0, 1)])
  398.     matrixFinal = matrixProjection @ matrixPerspective @ matrixShift @ matrixRatio
  399.     Matrix.transpose(matrixFinal)
  400.  
  401.     glMatrixMode(GL_PROJECTION)
  402.     value = (GLfloat * 16)()
  403.     for i in range(4):
  404.         for j in range(4):
  405.             value[i * 4 + j] = matrixFinal[i][j]
  406.     glLoadMatrixf(value)
  407.  
  408.     glMatrixMode(GL_MODELVIEW)
  409.  
  410.     glEnable(GL_CULL_FACE)
  411.     glFrontFace(GL_CW)
  412.  
  413.     glNewList(ids, GL_COMPILE)
  414.     glLoadIdentity()
  415.     glPushMatrix()
  416.     glScaled(1.5, 1.5, 1.5)
  417.     glTranslated(0.35, 0.35, 0.35)
  418.     glRotatef(30, 1, 0, 0)
  419.     glRotatef(-30, 0, 1, 0)
  420.     glRotatef(0, 0, 0, 1)
  421.     baseCube()
  422.     glPopMatrix()
  423.     glEndList()
  424.     ids += 1
  425.  
  426.     glMatrixMode(GL_MODELVIEW)
  427.     glEnable(GL_CULL_FACE)
  428.     glFrontFace(GL_CW)
  429.  
  430.     glNewList(ids, GL_COMPILE)
  431.     glLoadIdentity()
  432.     glPushMatrix()
  433.     glScaled(0.2, 0.2, 0.2)
  434.     glTranslated(*pos)
  435.     glRotatef(rot[0], 1, 0, 0)
  436.     glRotatef(rot[1], 0, 1, 0)
  437.     glRotatef(rot[2], 0, 0, 1)
  438.     EllepticalParaboloid(isFramedMode)
  439.     Changed = False
  440.     glPopMatrix()
  441.     glEndList()
  442.     ids += 1
  443.     glCallList(ids - 7)
  444.     glCallList(ids - 6)
  445.     glCallList(ids - 5)
  446.     glCallList(ids - 4)
  447.     glCallList(ids - 3)
  448.     glCallList(ids - 2)
  449.     glCallList(ids - 1)
  450.     glDeleteLists(ids, 7)
  451.  
  452.  
  453. @window.event
  454. def on_resize(width, height):
  455.     global ratio
  456.     glViewport(0, 0, width, height)
  457.     ratio = width / height
  458.     resetPoints(0)
  459.  
  460.  
  461. @window.event
  462. def on_mouse_press(x, y, button, modifiers):
  463.     global isFramedMode
  464.     if button == mouse.LEFT:
  465.         isFramedMode = not isFramedMode
  466.     resetPoints(0)
  467.  
  468.  
  469. @window.event
  470. def on_key_press(symbol, modifiers):
  471.     global pos, rot, Horizontal, Vertical, Changed
  472.     global speed, vector, is_infinity_light, two_side, face, is_local, is_animated, is_lighted, is_textured, is_material
  473.     if symbol == key.S:
  474.         pos[1] -= 0.05
  475.     elif symbol == key.W:
  476.         pos[1] += 0.05
  477.     elif symbol == key.D:
  478.         pos[0] += 0.05
  479.     elif symbol == key.A:
  480.         pos[0] -= 0.05
  481.     elif symbol == key.UP:
  482.         pos[2] += 0.05
  483.     elif symbol == key.DOWN:
  484.         pos[2] -= 0.05
  485.  
  486.     elif symbol == key.Z:
  487.         rot[0] -= 5
  488.     elif symbol == key.X:
  489.         rot[0] += 5
  490.     elif symbol == key.C:
  491.         rot[1] -= 5
  492.     elif symbol == key.V:
  493.         rot[1] += 5
  494.     elif symbol == key.B:
  495.         rot[2] -= 5
  496.     elif symbol == key.N:
  497.         rot[2] += 5
  498.  
  499.     elif symbol == key.O:
  500.         Horizontal -= 1
  501.     elif symbol == key.P:
  502.         Horizontal += 1
  503.     elif symbol == key.K:
  504.         Vertical -= 1
  505.     elif symbol == key.L:
  506.         Vertical += 1
  507.  
  508.     elif symbol == key.Q:
  509.         save()
  510.     elif symbol == key.E:
  511.         load()
  512.  
  513.     elif symbol == key.R:
  514.         is_material = not is_material
  515.     elif symbol == key.T:
  516.         is_infinity_light = not is_infinity_light
  517.     elif symbol == key.U:
  518.         if two_side[0] == 0:
  519.             two_side[0] = 1
  520.         elif two_side[0] == 1:
  521.             two_side[0] = 0
  522.     elif symbol == key.I:
  523.         if face == GL_FRONT_AND_BACK:
  524.             face = GL_FRONT
  525.         elif face == GL_FRONT:
  526.             face = GL_BACK
  527.         elif face == GL_BACK:
  528.             face = GL_FRONT_AND_BACK
  529.     elif symbol == key.H:
  530.         is_local = not is_local
  531.     elif symbol == key.G:
  532.         is_animated = not is_animated
  533.     elif symbol == key.F:
  534.         is_lighted = not is_lighted
  535.     elif symbol == key.M:
  536.         is_textured = not is_textured
  537.  
  538.     resetPoints(0)
  539.  
  540.  
  541. glfw.init()
  542. lastTime = glfw.get_time()
  543. nbFrames = 0
  544. summa = 0
  545. count = 0.0
  546.  
  547.  
  548. def calc_fps(dt):
  549.     global lastTime, nbFrames, summa, count
  550.     currentTime = glfw.get_time()
  551.     nbFrames += 1
  552.     if currentTime - lastTime >= 1.0:
  553.         summa += nbFrames
  554.         count += 1.0
  555.         print(nbFrames, summa / count, count)
  556.         nbFrames = 0
  557.         lastTime += 1.0
  558.  
  559.  
  560. resetPoints(0)
  561. if is_animated:
  562.     pyglet.clock.schedule_interval(resetPoints, 1.0 / 60)
  563. pyglet.clock.schedule(calc_fps)
  564. pyglet.app.run()
Add Comment
Please, Sign In to add comment