ulvis.paste.net

Paste Search Dynamic
Recent pastes
3D cube in OpenGL (Mobile)
  1. import appuifw, key_codes, glcanvas, e32
  2. from gles import *
  3.  
  4. vertices = array(GL_BYTE, 3, [
  5. -1,1,1, 1,1,1, 1,-1,1, -1,-1,1,
  6. -1,1,-1, 1,1,-1, 1,-1,-1, -1,-1,-1 ])
  7. triangles = array(GL_UNSIGNED_BYTE, 3, [
  8. 1,0,3, 1,3,2, 2,6,5, 2,5,1, 7,4,5, 7,5,6,
  9. 0,4,7, 0,7,3, 5,4,0, 5,0,1, 3,7,6, 3,6,2 ])
  10. colors = array(GL_UNSIGNED_BYTE, 4, [
  11. 0,255,0,255, 0,0,255,255, 0,255,0,255, 255,0,0,255,
  12. 0,0,255,255, 255,0,0,255, 0,0,255,255, 0,255,0,255 ])
  13.  
  14. running = 1
  15. turning_axis = 0
  16.  
  17. def resize():
  18. #Resize handler
  19. glViewport(0, 0, canvas.size[0], canvas.size[1])
  20. aspect = float(canvas.size[1]) / float(canvas.size[0])
  21. glMatrixMode( GL_PROJECTION )
  22. glLoadIdentity()
  23. glFrustumf( -1.0, 1.0, -1.0*aspect, 1.0*aspect, 3.0, 1000.0 )
  24.  
  25. def initgl():
  26. global render
  27. #Initializes OpenGL and sets up the rendering environment
  28. glClearColor( 0.0, 0.0, 0.0, 1.0 )# Set the screen background color.
  29. glEnable( GL_CULL_FACE )# Enable back face culling.
  30. resize()# Initialize viewport and projection.
  31. glMatrixMode( GL_MODELVIEW )
  32. glEnableClientState( GL_VERTEX_ARRAY )# Enable vertex arrays.
  33. glVertexPointerb(vertices)# Set array pointers.
  34. glEnableClientState( GL_COLOR_ARRAY ) # Enable color arrays.
  35. glColorPointerub(colors )# Set color pointers.
  36. glShadeModel( GL_SMOOTH )# Set the initial shading mode
  37. glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST )# no perspective correction
  38.  
  39. def drawbox(aSizeX, aSizeY, aSizeZ):
  40. #Draws a box with triangles.Scales the box to the given size using glScalef.
  41. glScalef( aSizeX, aSizeY, aSizeZ )
  42. glDrawElementsub( GL_TRIANGLES, triangles )
  43.  
  44. def redraw(frame):
  45. global turning_axis
  46. #Draws & animates the objects. The frame number determines the amount of rotation.
  47. iFrame = frame
  48. glMatrixMode( GL_MODELVIEW )
  49. cameraDistance = 100
  50. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )
  51.  
  52. # Animate and draw box
  53. glLoadIdentity()
  54. glTranslatex( 0 , 0 , -cameraDistance << 16 )
  55. if turning_axis == 0:
  56. glRotatex( iFrame << 16,1 << 16,0,0)
  57. glRotatex( iFrame << 16,0,1 << 16,0)
  58. glRotatex( iFrame << 16,0,0,1 << 16)
  59. elif turning_axis == 1:
  60. glRotatex( iFrame << 16,1 << 16,0,0)
  61. elif turning_axis == 2:
  62. glRotatex( iFrame << 16,0,1 << 16,0)
  63. elif turning_axis == 3:
  64. glRotatex( iFrame << 16,0,0,1 << 16)
  65. drawbox( 15.0, 15.0, 15.0 )
  66.  
  67.  
  68.  
  69.  
  70.  
  71. def set_exit():
  72. global running, canvas
  73. canvas=none
  74. running = 0
  75.  
  76. def keys(event):
  77. global turning_axis
  78. if event['keycode'] == key_codes.EKeyDownArrow: turning_axis = 1
  79. elif event['keycode'] == key_codes.EKeyRightArrow: turning_axis = 2
  80. elif event['keycode'] == key_codes.EKeyLeftArrow: turning_axis = 3
  81. elif event['keycode'] == key_codes.EKeyUpArrow: turning_axis = 0
  82.  
  83. appuifw.app.exit_key_handler=set_exit
  84. appuifw.app.screen = 'normal'
  85. canvas=glcanvas.GLCanvas(redraw_callback=redraw, event_callback=keys, resize_callback=resize)
  86. appuifw.app.body=canvas
  87. initgl()
  88.  
  89. while running:
  90. canvas.drawNow()
  91. e32.ao_sleep(0.0001)
  92.  
  93.  
Parsed in 0.023 seconds