Void-voiD

Untitled

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