SHARE
TWEET

Untitled

a guest Aug 25th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pygame
  2. from pygame.locals import *
  3. from OpenGL.GL import *
  4. from OpenGL.GLU import *
  5.        
  6. surfaces = (
  7.     (1, 2, 3, 4),
  8.     (3, 2, 7, 6),
  9.     (6, 7, 5, 4),
  10.     (4, 5, 1, 0),
  11.     (1, 5, 7, 2),
  12.     (4, 0, 3, 6)
  13. )
  14.  
  15. vertices = (
  16.     ( 1, -1, -1),
  17.     ( 1,  1, -1),
  18.     (-1,  1, -1),
  19.     (-1, -1, -1),
  20.     ( 1, -1,  1),
  21.     ( 1,  1,  1),
  22.     (-1, -1,  1),
  23.     (-1,  1,  1)
  24. )
  25.  
  26. edges = (
  27.     (0, 1),
  28.     (0, 3),
  29.     (0, 4),
  30.     (2, 1),
  31.     (2, 3),
  32.     (2, 7),
  33.     (6, 3),
  34.     (6, 4),
  35.     (6, 7),
  36.     (5, 1),
  37.     (5, 4),
  38.     (5, 7)
  39. )
  40.  
  41. # -------------------------------------------------------------
  42. btn_square = 0
  43. btn_cross = 1
  44. btn_circle = 2
  45. btn_tri = 3
  46.  
  47. btn_l1 = 4
  48. btn_l2 = 6
  49. btn_l3 = 10
  50.  
  51. btn_r1 = 5
  52. btn_r2 = 7
  53. btn_r3 = 11
  54.  
  55. btn_share = 8
  56. btn_option = 9
  57.  
  58. btn_ps4 = 12
  59. btn_touch = 13
  60.  
  61. axis_x = 9
  62. axis_y = 11
  63. #axis_z = ??? #don't think there's any more accelerometers
  64.  
  65. axis_l3_x = 0
  66. axis_l3_y = 1
  67. axis_r3_x = 2
  68. axis_r3_y = 5
  69.  
  70. axis_l2 = 3
  71. axis_r2 = 4
  72.  
  73. axis_touch_x = 13
  74. axis_touch_y = 14
  75. # -------------------------------------------------------------
  76.  
  77. def mixColors(colors):
  78.     reds =   [ c[0] for c in colors ]
  79.     greens = [ c[1] for c in colors ]
  80.     blues =  [ c[2] for c in colors ]
  81.        
  82.     r = sum(reds) / len(colors)
  83.     g = sum(greens) / len(colors)
  84.     b = sum(blues) / len(colors)
  85.  
  86.     return (r, g, b)
  87.  
  88. def getColorFromButtonsPressed(buttons):
  89.     colors = {
  90.         btn_square: (1.00, 0.41, 0.97),
  91.         btn_cross:  (0.49, 0.69, 0.97),
  92.         btn_circle: (1.00, 0.40, 0.40),
  93.         btn_tri:    (0.25, 0.88, 0.62)
  94.         }
  95.  
  96.     pressedButtons = [ b for b in buttons.keys() if buttons[b] and b in colors.keys() ]
  97.  
  98.     if not pressedButtons:
  99.         return (0, 0, 0)
  100.  
  101.     return mixColors([ colors[b] for b in pressedButtons ])
  102.  
  103. def shiftCube(axis_x, axis_y):
  104.     translate_x = 2.0 * axis_x
  105.     translate_y = 2.0 * axis_y  
  106.    
  107.     glTranslatef(axis_x, axis_y, 0.0)    
  108.    
  109.  
  110. def Cube(color):
  111.     glBegin(GL_QUADS)
  112.     for surface in surfaces:
  113.         for vertex in surface:          
  114.             glColor3fv(color)
  115.             glVertex3fv(vertices[vertex])
  116.     glEnd()
  117.  
  118.     glBegin(GL_LINES)
  119.     for edge in edges:
  120.         for vertex in edge:
  121.             glColor3fv((1,1,1))
  122.             glVertex3fv(vertices[vertex])
  123.     glEnd()
  124.  
  125.  
  126. def getZrotation(l2_axis, l2_pressed, r2_axis, r2_pressed):
  127.     # if buttons not pressed their axis is 0.0, but
  128.     # -1 makes more sense for us here
  129.     if not l2_pressed:
  130.         l2_axis = -1.0
  131.     if not r2_pressed:
  132.         r2_axis = -1.0
  133.  
  134.     # next scale it so we're working with values [0..2] instead of [-1..1]
  135.     l2_axis += 1.0
  136.     r2_axis += 1.0
  137.  
  138.     # finally subtract axes, and scale back to [0..1]
  139.     return (l2_axis - r2_axis) / 2
  140.  
  141. def main():
  142.     pygame.init()
  143.     display = (800,600)
  144.     pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
  145.    
  146.     gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
  147.     glTranslatef(0.0,0.0, -5)
  148.  
  149.     pygame.init()
  150.     pygame.joystick.init()
  151.     controller = pygame.joystick.Joystick(0)
  152.     controller.init()
  153.    
  154.     axis = {}
  155.  
  156.     button = {}
  157.     for i in range(controller.get_numbuttons()):
  158.         button[i] = False
  159.  
  160.     # set some default value
  161.     rot_x = 0.0
  162.     rot_y = 0.0
  163.     rot_z = 0.0
  164.     rot_z_l2 = 0.0
  165.     rot_z_r2 = 0.0
  166.  
  167.     trans_x = 0
  168.     trans_y = 0
  169.  
  170.     # fix scaling - the rotz
  171.     rot_scale = 60.0
  172.     rotx_scale =  1 * rot_scale
  173.     roty_scale = -1 * rot_scale
  174.     rotz_scale = -1 * 180
  175.    
  176.     while True:
  177.         # load previous values, in case there's no joy events this iteration
  178.         axis[axis_x] = rot_x
  179.         axis[axis_y] = rot_y
  180.         axis[axis_l2] = rot_z_l2
  181.         axis[axis_r2] = rot_z_r2
  182.  
  183.         axis[axis_touch_x] = trans_x
  184.         axis[axis_touch_y] = trans_y
  185.        
  186.         for event in pygame.event.get():
  187.             if event.type == pygame.JOYAXISMOTION:
  188.                 axis[event.axis] = round(event.value,2)
  189.             elif event.type == pygame.JOYBUTTONDOWN:
  190.                 button[event.button] = True
  191.             elif event.type == pygame.JOYBUTTONUP:
  192.                 button[event.button] = False
  193.        
  194.         rot_x = axis[axis_x]
  195.         rot_y = axis[axis_y]
  196.         rot_z_l2 = axis[axis_l2]
  197.         rot_z_r2 = axis[axis_r2]
  198.         rot_z = getZrotation(rot_z_l2, button[btn_l2], rot_z_r2, button[btn_r2])
  199.         trans_x = axis[axis_touch_x]
  200.         trans_y = axis[axis_touch_y]
  201.  
  202.         glPushMatrix()  
  203.         shiftCube(trans_x, -1 * trans_y)
  204.  
  205.         glRotatef(roty_scale * rot_y, 1, 0, 0)
  206.         glRotatef(rotz_scale * rot_z, 0, 1, 0)
  207.         glRotatef(rotx_scale * rot_x, 0, 0, 1)
  208.  
  209.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  210.         Cube(getColorFromButtonsPressed(button))
  211.         glPopMatrix()        
  212.         pygame.display.flip()
  213.  
  214. main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top