mechanizmos58

vpython_flight_simulator_skybox_cloud.py

Aug 9th, 2021 (edited)
53
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # 10_08_21 can read png
  2.  
  3. from vpython import *
  4. import sys
  5.  
  6. SAFE_DIV = 1e-50
  7.  
  8. '''
  9. tested on: Linux Mint 20.2, Geany 1.36, python 3.8.10,
  10. Web Epiphany, i5-7260U CPU @ 2.20GHz × 2, Iris Plus Graphics 640
  11. vpython version: ['7.6.1', 'jupyter']
  12. '''
  13.  
  14. def main():
  15.  
  16.     print(version)
  17.  
  18.     g.rho = 0.0
  19.     g.theta = 0.0
  20.     g.phi = 0.0
  21.  
  22.     # doesn't work - don't know why - must have Web as default
  23.     # set_browser(type='/usr/bin/epiphany-browser')
  24.  
  25.     scene.width, scene.height = 1390, 1015
  26.     scene.align = 'left'
  27.  
  28.     scene.resizable = False # doesn't work inside canvas(...)
  29.     scene.autoscale = False
  30.     scene.autozoom = False
  31.     scene.userspin = False
  32.     scene.userzoom = False
  33.     scene.userpan = False
  34.  
  35.     # ground
  36.     box(pos=vec(0, 0, 0), size=vec(30,1e-2,30), color=vec(1, 1, 1),
  37.             texture=textures.stucco, shininess=0.0)
  38.  
  39.     # central building
  40.     cent = box(pos=vec(0, 1, 0), size=vec(1,2,1), color=vec(0.5, 0, 0.5),
  41.             texture=textures.metal)
  42.  
  43.     # metal tower 1
  44.     box(pos=vec(3, 3, 0), size=vec(0.3,6,0.3), color=vec(0, 0, 1),
  45.             texture=textures.metal)
  46.  
  47.     # metal tower 2
  48.     box(pos=vec(-3, 3, 0), size=vec(0.3,6,0.3), color=vec(0.1, 0, 0.5),
  49.             texture=textures.metal)
  50.  
  51.     # runway1
  52.     box(pos=vec(1.2, 1.5, 0), size=vec(0.5, 0.05, 10), color=vec(.3,.1,.1),
  53.             texture=textures.gravel, shininess=0.0)
  54.  
  55.     # supports
  56.     cylinder(pos=vec(1.2, 0, 4), size=vec(1.5, 0.1, 0.1), axis=vec(0, 1, 0),
  57.             color=vec(.3,.3,.3))
  58.     cylinder(pos=vec(1.2, 0, -4), size=vec(1.5, 0.1, 0.1), axis=vec(0, 1, 0),
  59.             color=vec(.3,.3,.3))
  60.  
  61.     # runway2
  62.     box(pos=vec(-1.7, 0.001, 0), size=vec(0.5, 0.01, 10), color=vec(.1,.1,.1),
  63.             texture=textures.gravel, shininess=0.0)
  64.  
  65.     # gangway
  66.     cylinder(pos=vec(-3,1.4,0), size=vec(6,0,0), radius=0.05, color=vec(.3,.3,.3))
  67.  
  68.     # hoop
  69.     clr=vec(0,0,0.5)
  70.     curve(vec(-0.5,3,0), vec(0.5,3,0), vec(0.5,4,0),
  71.             vec(-0.5,4,0), vec(-0.5,3,0), radius=0.015, color=clr, shininess=0)
  72.     curve(vec(0,3,0), vec(0,2,0), radius=0.015, color=clr, shininess=0)
  73.  
  74.  
  75.     if 0: # CLOUD LAYER
  76.         '''
  77.        GIMP:
  78.        File -> New: 1024x1024
  79.        Filters -> Render -> Noise -> Difference Clouds
  80.        Tools -> Paint Tools -> Dodge/Burn: Burn around edge to fade to black
  81.        Export As clouds_1024x1024.jpg '''
  82.  
  83.         box(pos=vec(0, 8, 0), size=vec(60,1e-2,60), color=vec(1, 0.7, 0.7),
  84.                 texture='clouds_1024x1024.jpg', shininess=0.0)
  85.  
  86.         box(pos=vec(0, 7.7, 0), size=vec(60,1e-2,60), color=vec(1, 0.7, 0.7),
  87.                 texture={'file': 'clouds_1024x1024.jpg', 'turn': 90},
  88.                 shininess=0.0, opacity=0.5)
  89.  
  90.     if 0: # SPACE BACKGROUND
  91.  
  92.         #  ref. youtube - Let's Code Physics - Space Background
  93.         sphere(pos=vec(0,0,0), texture='space_1nVWbbd.jpg', radius=35, shininess=0)
  94.  
  95.  
  96.     if 0: # SKYBOX
  97.  
  98.         '''
  99.            +---+
  100.            | 5 |
  101.        +---+---+---+---+
  102.        | 1 | 2 | 3 | 4 |
  103.        +---+---+---+---+
  104.            | 6 |
  105.            +---+
  106.  
  107.        front=2, back=4, right=3, left=1, top=5, bottom=6  '''
  108.         # images are 500x500
  109.         # 30 - (1/500)*30 = 29.94
  110.         scene.lights = []
  111.         thick = 0.01
  112.         box(pos=vec(0, 0, -29.94), size=vec(60, 60, thick), texture='sb2.jpg')
  113.         box(pos=vec(0, 0, 29.94),  size=vec(60, 60, thick), texture='sb4.jpg')
  114.         box(pos=vec(29.94, 0, 0),  size=vec(thick, 60, 60), texture='sb3.jpg')
  115.         box(pos=vec(-29.94, 0, 0), size=vec(thick, 60, 60), texture='sb1.jpg')
  116.         box(pos=vec(0, 29.94, 0),  size=vec(60, thick, 60), texture='sb5.jpg')
  117.         box(pos=vec(0, -29.94, 0), size=vec(60, thick, 60), texture='sb6.jpg')
  118.  
  119.     scene.camera.pos = vec(0, 0.5, 0.0)
  120.  
  121.     scene.range = 10
  122.  
  123.     scene.right = vpcross(scene.forward, scene.up)
  124.  
  125. #--------------------------------------------------------------------------
  126.  
  127.     g.scene2 = canvas(title='', width=510, height=1015,
  128.                     center=vec(0,0,0), background=vec(0,0,0), align='right')
  129.  
  130.     g.scene2.lights = []
  131.     g.scene2.resizable = False # doesn't work inside canvas(...)
  132.     g.scene2.autoscale = False
  133.     g.scene2.autozoom = False
  134.     g.scene2.userspin = False
  135.     g.scene2.userzoom = False
  136.     g.scene2.userpan = False
  137.  
  138.     g.scene2.range = 10
  139.  
  140.     gray = color.gray(0.5)
  141.     cylinder(canvas=g.scene2, axis=vec(1,0,0), pos=vec(-2,0,0),
  142.              size=vec(4, 0.1, 0.1), color=gray)
  143.     cylinder(canvas=g.scene2, axis=vec(0,1,0), pos=vec(0,-2,0),
  144.              size=vec(4, 0.1, 0.1), color=gray)
  145.  
  146.     l1 = label(text = 'click on center of cross hairs to start',
  147.          color=gray, pos = vec(0, 18, 0), height=18, box=False, line=False)
  148.  
  149.     l2 = label(text = 're-click anywhere to reset reference position',
  150.         color=gray, pos = vec(0, 17, 0), height=18, box=False, line=False)
  151.  
  152.     l3 = label(text = 'move pointer away from reference to pitch and yaw',
  153.         color=gray, pos = vec(0, 16, 0), height=18, box=False, line=False)
  154.  
  155.     l4 = label(text = 'up arrow to increase speed    down arrow to decrease speed',
  156.         color=gray, pos = vec(0, 15, 0), height=18, box=False, line=False)
  157.  
  158.     # scene.select()
  159.  
  160. #--------------------------------------------------------------------------
  161.  
  162.     g.scene2.bind("mousedown", mousedown)
  163.     ev = g.scene2.waitfor('mousedown')
  164.     # if ev.event == 'mousedown': ...
  165.  
  166.     for i in l1,l2,l3,l4: i.visible = False
  167.  
  168.     yaw_rate = 0.01
  169.     move_rate = 0.02
  170.     tr = 0.0
  171.     tr_increment = 0.0003
  172.  
  173.     while 1:
  174.  
  175.         rate(60)
  176.  
  177.         k = keysdown()
  178.  
  179.         if 'up' in k:
  180.             tr += tr_increment
  181.         if 'down' in k:
  182.             tr -= tr_increment
  183.  
  184.         # (note: break = Shift + Pause, Enter = '\n')
  185.         names = ['delete', 'end', 'home', 'esc', '\n', '/', '@',
  186.                 'insert', 'backspace', 'break']
  187.         if set(names) & set(k):
  188.             tr = 0.0
  189.  
  190.         if 'left' in k:
  191.             yaw(yaw_rate)
  192.         if 'right' in k:
  193.             yaw(-yaw_rate)
  194.         if 'pageup' in k:
  195.             updown(move_rate)
  196.         if 'pagedown' in k:
  197.             updown(-move_rate)
  198.         if ',' in k:
  199.             slew(-move_rate*2.1)
  200.         if '.' in k:
  201.             slew(move_rate*2.1)
  202.  
  203.         idle_function()
  204.         roll(-g.rho)
  205.         yaw(-g.theta)
  206.         pitch(g.phi)
  207.         translate(tr)
  208.  
  209.         # scene.waitfor('redraw')
  210.  
  211. #--------------------------------------------------------------------------
  212.  
  213. def roll(rho):
  214.     # +rho = cw, -rho = ccw
  215.     scene.up = scene.up.rotate(angle=rho, axis=scene.forward)
  216.     scene.right.value = vpcross(scene.forward, scene.up)
  217.  
  218. def yaw(theta):
  219.     # +theta = left, -theta = right
  220.     scene.camera.axis = scene.camera.axis.rotate(angle=theta, axis=scene.up)
  221.     scene.right.value = vpcross(scene.forward, scene.up)
  222.  
  223. def pitch(phi):
  224.     # +phi = up, -phi = down
  225.     scene.camera.axis = scene.camera.axis.rotate(angle=phi, axis=scene.right)
  226.  
  227. def translate(d):
  228.     '''scene.forward = norm(scene.camera.axis)'''
  229.     scene.camera.pos += scene.forward * d
  230.  
  231. def slew(d):
  232.     ''' +d = right, -d = left '''
  233.     scene.camera.pos += scene.right * d
  234.  
  235. def updown(d):
  236.     ''' +d = up, -d = down '''
  237.     scene.camera.pos += scene.up * d
  238.  
  239.  
  240. def mousedown():
  241.     g.x0, g.y0 = g.scene2.mouse.pos.x, g.scene2.mouse.pos.y
  242.  
  243.  
  244. def idle_function():
  245.  
  246.     mx, my = g.scene2.mouse.pos.x, g.scene2.mouse.pos.y
  247.  
  248.     # UP, DOWN
  249.     #
  250.     y = my - g.y0
  251.     g.phi = y / (1e04 / (abs(y) + SAFE_DIV))
  252.  
  253.     # LEFT, RIGHT
  254.     #
  255.     x = mx - g.x0
  256.     g.theta = x / (5e03 / (abs(x) + SAFE_DIV))
  257.  
  258. #--------------------------------------------------------------------------
  259.  
  260. # FAST FUNCTIONS:
  261.  
  262. def vpnorm(v):
  263.     mag = sqrt(v.x*v.x + v.y*v.y + v.z*v.z)
  264.     return v / mag
  265.  
  266. def vpcross(u, v):
  267.     x = u.y*v.z - u.z*v.y
  268.     y = u.z*v.x - u.x*v.z
  269.     z = u.x*v.y - u.y*v.x
  270.     return vec(x, y, z)
  271.  
  272. def vpmul(u, v):
  273.     x = u.x * v.x
  274.     y = u.y * v.y
  275.     z = u.z * v.z
  276.     return vec(x, y, z)
  277.  
  278. # Rodrigues Rotation Formula (Wikipedia)
  279. # John Vince, Rotation Transforms for Computer Graphics (2011) p.139
  280. # Goldstein et al. p. 162 ("the rotation formula")
  281. #
  282. # P is the position vector to rotate
  283. # n is the direction vector about which to rotate
  284. # 𝚹  is the rotation angle in radians
  285. #
  286. def vprotate(P, n, 𝚹):
  287.  
  288.     n /= sqrt(n.x*n.x + n.y*n.y + n.z*n.z)
  289.  
  290.     x = n.y*P.z - n.z*P.y
  291.     y = n.z*P.x - n.x*P.z
  292.     z = n.x*P.y - n.y*P.x
  293.     cross_nP = vec(x, y, z)
  294.  
  295.     dot_nP = n.x*P.x + n.y*P.y + n.z*P.z
  296.  
  297.     cos𝚹 = cos(𝚹)
  298.  
  299.     return P * cos𝚹 + n * dot_nP * (1.0-cos𝚹) + cross_nP * sin(𝚹)
  300.  
  301.  
  302. class g: ...
  303.  
  304. if __name__ == '__main__':
  305.  
  306.  
  307.     sys.exit(main())
  308.  
  309.  
  310. b = box( pos=vec(-1,2,0),
  311.       texture={'file':textures.stucco,
  312.                'bumpmap':bumpmaps.stucco,
  313.                'place':['right', 'sides'],
  314.                'flipx':True,
  315.                'flipy':True,
  316.                'turn':-3} )
  317.  
  318.  
RAW Paste Data