Monday, May 05, 2008

Python OpenGL cube rotated by keypress

  1.  
  2. # -*- coding: utf-8 -*-
  3. from OpenGL.GL import *
  4. from OpenGL.GLUT import *
  5. from OpenGL.GLU import *
  6. import serial
  7. import os
  8. import threading
  9.  
  10. ESCAPE = '\033'
  11.  
  12. KEY_a = '\141'
  13.  
  14. KEY_b = '\142'
  15.  
  16. KEY_c = '\143'
  17.  
  18. KEY_d = '\144'
  19.  
  20. KEY_e = '\145'
  21.  
  22. KEY_f = '\146'
  23.  
  24. #Initiate the glut window
  25. window = 0
  26.  
  27. # Rotation angle for the quadrilateral.
  28. X_AXIS = 0.0
  29. Y_AXIS = 0.0
  30. Z_AXIS = 0.0
  31.  
  32. #AXIS direction
  33. DIRECTION = 1
  34.  
  35.  
  36. # A general OpenGL initialization function.  Sets all of the initial parameters.
  37. # We call this right after our OpenGL window is created.
  38. def InitGL(Width, Height)
  39.  
  40.         # This Will Clear The Background Color To Black
  41.         glClearColor(0.0, 0.0, 0.0, 0.0)
  42.  
  43.         # Enables Clearing Of The Depth Buffer
  44.         glClearDepth(1.0) 
  45.         # The Type Of Depth Test To Do
  46.         glDepthFunc(GL_LESS)
  47.         # Enables Depth Testing
  48.         glEnable(GL_DEPTH_TEST)
  49.         # Enables Smooth Color Shading
  50.         glShadeModel(GL_SMOOTH)   
  51.  
  52.         glMatrixMode(GL_PROJECTION)
  53.         # Reset The Projection Matrix
  54.         glLoadIdentity()
  55.  
  56.         # Calculate The Aspect Ratio Of The Window
  57.         gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
  58.  
  59.         glMatrixMode(GL_MODELVIEW)
  60.  
  61. # The function called whenever a key is pressed.
  62. # Note the use of Python tuples to pass in: (key, x, y) 
  63. def keyPressed(*args):
  64.         global X_AXIS,Y_AXIS,Z_AXIS
  65.         global DIRECTION
  66.         # If escape is pressed, kill everything.
  67.         if args[0] == ESCAPE:
  68.                 sys.exit()
  69.         elif args[0] == KEY_a:
  70.                 DIRECTION = 1
  71.                 X_AXIS = X_AXIS + 0.30
  72.         elif args[0] == KEY_b:
  73.                 DIRECTION = 1
  74.                 Y_AXIS = Y_AXIS + 0.30
  75.         elif args[0] == KEY_c:
  76.                 DIRECTION = 1
  77.                 Z_AXIS = Z_AXIS + 0.30
  78.         elif args[0] == KEY_d:
  79.                 DIRECTION = -1
  80.                 X_AXIS = X_AXIS - 0.30
  81.         elif args[0] == KEY_e:
  82.                 DIRECTION = -1
  83.                 Y_AXIS = Y_AXIS - 0.30
  84.         elif args[0] == KEY_f:
  85.                 DIRECTION = -1
  86.                 Z_AXIS = Z_AXIS - 0.30
  87.  
  88.  
  89. def DrawGLScene():
  90.         global X_AXIS,Y_AXIS,Z_AXIS
  91.         global DIRECTION
  92.  
  93.         #clear the screen and the depth buffer
  94.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  95.  
  96.         # Reset The View
  97.         glLoadIdentity()
  98.  
  99.         # Move Right And Into The Screen
  100.         glTranslatef(0.0,0.0,-6.0)
  101.  
  102.         # Rotate The Cube On X
  103.         glRotatef(X_AXIS,1.0,0.0,0.0)
  104.         # Rotate The Cube On Y
  105.         glRotatef(Y_AXIS,0.0,1.0,0.0)
  106.         # Rotate The Cube On Z
  107.         glRotatef(Z_AXIS,0.0,0.0,1.0)
  108.  
  109.         # Start Drawing The Cube
  110.         glBegin(GL_QUADS)
  111.  
  112.  
  113.         # Set The Color To Blue
  114.         glColor3f(0.0,1.0,0.0)
  115.         # Top Right Of The Quad (Top)
  116.         glVertex3f( 1.0, 1.0,-1.0)
  117.         # Top Left Of The Quad (Top)
  118.         glVertex3f(-1.0, 1.0,-1.0)
  119.         # Bottom Left Of The Quad (Top)
  120.         glVertex3f(-1.0, 1.0, 1.0)
  121.         # Bottom Right Of The Quad (Top)
  122.         glVertex3f( 1.0, 1.0, 1.0) 
  123.  
  124.  
  125.         # Set The Color To Orange
  126.         glColor3f(1.0,0.5,0.0)
  127.         # Top Right Of The Quad (Bottom)
  128.         glVertex3f( 1.0,-1.0, 1.0)
  129.         # Top Left Of The Quad (Bottom)
  130.         glVertex3f(-1.0,-1.0, 1.0)
  131.         # Bottom Left Of The Quad (Bottom)
  132.         glVertex3f(-1.0,-1.0,-1.0)
  133.         # Bottom Right Of The Quad (Bottom)
  134.         glVertex3f( 1.0,-1.0,-1.0) 
  135.  
  136.  
  137.         # Set The Color To Red
  138.         glColor3f(1.0,0.0,0.0)
  139.         # Top Right Of The Quad (Front)
  140.         glVertex3f( 1.0, 1.0, 1.0)
  141.         # Top Left Of The Quad (Front)
  142.         glVertex3f(-1.0, 1.0, 1.0)
  143.         # Bottom Left Of The Quad (Front)
  144.         glVertex3f(-1.0,-1.0, 1.0)
  145.         # Bottom Right Of The Quad (Front)
  146.         glVertex3f( 1.0,-1.0, 1.0)
  147.  
  148.  
  149.         # Set The Color To Yellow
  150.         glColor3f(1.0,1.0,0.0)
  151.         # Bottom Left Of The Quad (Back)
  152.         glVertex3f( 1.0,-1.0,-1.0)
  153.         # Bottom Right Of The Quad (Back)
  154.         glVertex3f(-1.0,-1.0,-1.0)
  155.         # Top Right Of The Quad (Back)
  156.         glVertex3f(-1.0, 1.0,-1.0)
  157.         # Top Left Of The Quad (Back)
  158.         glVertex3f( 1.0, 1.0,-1.0)
  159.  
  160.  
  161.         # Set The Color To Blue
  162.         glColor3f(0.0,0.0,1.0)
  163.         # Top Right Of The Quad (Left)
  164.         glVertex3f(-1.0, 1.0, 1.0) 
  165.         # Top Left Of The Quad (Left)
  166.         glVertex3f(-1.0, 1.0,-1.0)
  167.         # Bottom Left Of The Quad (Left)
  168.         glVertex3f(-1.0,-1.0,-1.0) 
  169.         # Bottom Right Of The Quad (Left)
  170.         glVertex3f(-1.0,-1.0, 1.0) 
  171.  
  172.  
  173.         # Set The Color To Violet
  174.         glColor3f(1.0,0.0,1.0)
  175.         # Top Right Of The Quad (Right)
  176.         glVertex3f( 1.0, 1.0,-1.0) 
  177.         # Top Left Of The Quad (Right)
  178.         glVertex3f( 1.0, 1.0, 1.0)
  179.         # Bottom Left Of The Quad (Right)
  180.         glVertex3f( 1.0,-1.0, 1.0)
  181.         # Bottom Right Of The Quad (Right)
  182.         glVertex3f( 1.0,-1.0,-1.0)
  183.         # Done Drawing The Quad
  184.         glEnd()
  185.  
  186.  
  187.         if X_AXIS > 360 or X_AXIS == 0 or X_AXIS < -360:
  188.                 print X_AXIS
  189.                 X_AXIS = 0.0
  190.         else:
  191.                 print X_AXIS
  192.                 print DIRECTION
  193.                 X_AXIS = X_AXIS + 0.3*(DIRECTION)
  194.  
  195.  
  196.         if Y_AXIS > 360 or Y_AXIS == 0 or Y_AXIS < -360:
  197.                 print Y_AXIS
  198.                 Y_AXIS = 0.0
  199.         else:
  200.                 print Y_AXIS
  201.                 print DIRECTION
  202.                 Y_AXIS = Y_AXIS + 0.3*(DIRECTION)
  203.  
  204.  
  205.         if Z_AXIS > 360 or Z_AXIS == 0 or Z_AXIS < -360:
  206.                 print Z_AXIS
  207.                 Z_AXIS = 0.0
  208.         else:
  209.                 print Z_AXIS
  210.                 print DIRECTION
  211.                 Z_AXIS = Z_AXIS + 0.3*(DIRECTION)
  212.  
  213.         #  since this is double buffered, swap the buffers to display what just got drawn.
  214.         glutSwapBuffers()
  215.  
  216.  
  217.  
  218. def main():
  219.  
  220.         global window
  221.  
  222.         #initialize the GLUT library
  223.         glutInit(sys.argv)
  224.  
  225.         #select display mode:
  226.         #Bit mask to select an RGBA mode window
  227.         #Bit mask to select a double buffered window
  228.         #Bit mask to request a window with a depth buffer
  229.         glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
  230.  
  231.         #get a window size
  232.         glutInitWindowSize(640,480)
  233.  
  234.         #the window starts at the upper left corner of the screen
  235.         glutInitWindowPosition(200,200)
  236.  
  237.         #create a window and set its title
  238.         window = glutCreateWindow('Pointer Demo')
  239.  
  240.         # Register the drawing function with glut, BUT in Python land,
  241.         # at least using PyOpenGL, we need to set the function pointer and
  242.         # invoke a function to actually register the callback,
  243.         # otherwise it would be very much like the C version of the code.
  244.         glutDisplayFunc(DrawGLScene)
  245.  
  246.         # When we are doing nothing, redraw the scene.
  247.         glutIdleFunc(DrawGLScene)
  248.  
  249.         # Register the function called when the keyboard is pressed. 
  250.         glutKeyboardFunc(keyPressed)
  251.  
  252.         # Initialize our window.
  253.         InitGL(640, 480)
  254.  
  255.  
  256.         #start event processing engine
  257.         glutMainLoop()
  258.  
  259. if __name__ == "__main__":
  260.         main()
  261.  

HEMiDEMi Technorati Del.icio.us MyShare個人書籤 Yahoo