- # -*- coding: utf-8 -*-
- from OpenGL.GL import *
- from OpenGL.GLUT import *
- from OpenGL.GLU import *
- import serial
- import os
- import threading
- ESCAPE = '\033'
- KEY_a = '\141'
- KEY_b = '\142'
- KEY_c = '\143'
- KEY_d = '\144'
- KEY_e = '\145'
- KEY_f = '\146'
- #Initiate the glut window
- window = 0
- # Rotation angle for the quadrilateral.
- X_AXIS = 0.0
- Y_AXIS = 0.0
- Z_AXIS = 0.0
- #AXIS direction
- DIRECTION = 1
- # A general OpenGL initialization function. Sets all of the initial parameters.
- # We call this right after our OpenGL window is created.
- def InitGL(Width, Height):
- # This Will Clear The Background Color To Black
- glClearColor(0.0, 0.0, 0.0, 0.0)
- # Enables Clearing Of The Depth Buffer
- glClearDepth(1.0)
- # The Type Of Depth Test To Do
- glDepthFunc(GL_LESS)
- # Enables Depth Testing
- glEnable(GL_DEPTH_TEST)
- # Enables Smooth Color Shading
- glShadeModel(GL_SMOOTH)
- glMatrixMode(GL_PROJECTION)
- # Reset The Projection Matrix
- glLoadIdentity()
- # Calculate The Aspect Ratio Of The Window
- gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
- glMatrixMode(GL_MODELVIEW)
- # The function called whenever a key is pressed.
- # Note the use of Python tuples to pass in: (key, x, y)
- def keyPressed(*args):
- global X_AXIS,Y_AXIS,Z_AXIS
- global DIRECTION
- # If escape is pressed, kill everything.
- if args[0] == ESCAPE:
- sys.exit()
- elif args[0] == KEY_a:
- DIRECTION = 1
- X_AXIS = X_AXIS + 0.30
- elif args[0] == KEY_b:
- DIRECTION = 1
- Y_AXIS = Y_AXIS + 0.30
- elif args[0] == KEY_c:
- DIRECTION = 1
- Z_AXIS = Z_AXIS + 0.30
- elif args[0] == KEY_d:
- DIRECTION = -1
- X_AXIS = X_AXIS - 0.30
- elif args[0] == KEY_e:
- DIRECTION = -1
- Y_AXIS = Y_AXIS - 0.30
- elif args[0] == KEY_f:
- DIRECTION = -1
- Z_AXIS = Z_AXIS - 0.30
- def DrawGLScene():
- global X_AXIS,Y_AXIS,Z_AXIS
- global DIRECTION
- #clear the screen and the depth buffer
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
- # Reset The View
- glLoadIdentity()
- # Move Right And Into The Screen
- glTranslatef(0.0,0.0,-6.0)
- # Rotate The Cube On X
- glRotatef(X_AXIS,1.0,0.0,0.0)
- # Rotate The Cube On Y
- glRotatef(Y_AXIS,0.0,1.0,0.0)
- # Rotate The Cube On Z
- glRotatef(Z_AXIS,0.0,0.0,1.0)
- # Start Drawing The Cube
- glBegin(GL_QUADS)
- # Set The Color To Blue
- glColor3f(0.0,1.0,0.0)
- # Top Right Of The Quad (Top)
- glVertex3f( 1.0, 1.0,-1.0)
- # Top Left Of The Quad (Top)
- glVertex3f(-1.0, 1.0,-1.0)
- # Bottom Left Of The Quad (Top)
- glVertex3f(-1.0, 1.0, 1.0)
- # Bottom Right Of The Quad (Top)
- glVertex3f( 1.0, 1.0, 1.0)
- # Set The Color To Orange
- glColor3f(1.0,0.5,0.0)
- # Top Right Of The Quad (Bottom)
- glVertex3f( 1.0,-1.0, 1.0)
- # Top Left Of The Quad (Bottom)
- glVertex3f(-1.0,-1.0, 1.0)
- # Bottom Left Of The Quad (Bottom)
- glVertex3f(-1.0,-1.0,-1.0)
- # Bottom Right Of The Quad (Bottom)
- glVertex3f( 1.0,-1.0,-1.0)
- # Set The Color To Red
- glColor3f(1.0,0.0,0.0)
- # Top Right Of The Quad (Front)
- glVertex3f( 1.0, 1.0, 1.0)
- # Top Left Of The Quad (Front)
- glVertex3f(-1.0, 1.0, 1.0)
- # Bottom Left Of The Quad (Front)
- glVertex3f(-1.0,-1.0, 1.0)
- # Bottom Right Of The Quad (Front)
- glVertex3f( 1.0,-1.0, 1.0)
- # Set The Color To Yellow
- glColor3f(1.0,1.0,0.0)
- # Bottom Left Of The Quad (Back)
- glVertex3f( 1.0,-1.0,-1.0)
- # Bottom Right Of The Quad (Back)
- glVertex3f(-1.0,-1.0,-1.0)
- # Top Right Of The Quad (Back)
- glVertex3f(-1.0, 1.0,-1.0)
- # Top Left Of The Quad (Back)
- glVertex3f( 1.0, 1.0,-1.0)
- # Set The Color To Blue
- glColor3f(0.0,0.0,1.0)
- # Top Right Of The Quad (Left)
- glVertex3f(-1.0, 1.0, 1.0)
- # Top Left Of The Quad (Left)
- glVertex3f(-1.0, 1.0,-1.0)
- # Bottom Left Of The Quad (Left)
- glVertex3f(-1.0,-1.0,-1.0)
- # Bottom Right Of The Quad (Left)
- glVertex3f(-1.0,-1.0, 1.0)
- # Set The Color To Violet
- glColor3f(1.0,0.0,1.0)
- # Top Right Of The Quad (Right)
- glVertex3f( 1.0, 1.0,-1.0)
- # Top Left Of The Quad (Right)
- glVertex3f( 1.0, 1.0, 1.0)
- # Bottom Left Of The Quad (Right)
- glVertex3f( 1.0,-1.0, 1.0)
- # Bottom Right Of The Quad (Right)
- glVertex3f( 1.0,-1.0,-1.0)
- # Done Drawing The Quad
- glEnd()
- if X_AXIS > 360 or X_AXIS == 0 or X_AXIS < -360:
- print X_AXIS
- X_AXIS = 0.0
- else:
- print X_AXIS
- print DIRECTION
- X_AXIS = X_AXIS + 0.3*(DIRECTION)
- if Y_AXIS > 360 or Y_AXIS == 0 or Y_AXIS < -360:
- print Y_AXIS
- Y_AXIS = 0.0
- else:
- print Y_AXIS
- print DIRECTION
- Y_AXIS = Y_AXIS + 0.3*(DIRECTION)
- if Z_AXIS > 360 or Z_AXIS == 0 or Z_AXIS < -360:
- print Z_AXIS
- Z_AXIS = 0.0
- else:
- print Z_AXIS
- print DIRECTION
- Z_AXIS = Z_AXIS + 0.3*(DIRECTION)
- # since this is double buffered, swap the buffers to display what just got drawn.
- glutSwapBuffers()
- def main():
- global window
- #initialize the GLUT library
- glutInit(sys.argv)
- #select display mode:
- #Bit mask to select an RGBA mode window
- #Bit mask to select a double buffered window
- #Bit mask to request a window with a depth buffer
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
- #get a window size
- glutInitWindowSize(640,480)
- #the window starts at the upper left corner of the screen
- glutInitWindowPosition(200,200)
- #create a window and set its title
- window = glutCreateWindow('Pointer Demo')
- # Register the drawing function with glut, BUT in Python land,
- # at least using PyOpenGL, we need to set the function pointer and
- # invoke a function to actually register the callback,
- # otherwise it would be very much like the C version of the code.
- glutDisplayFunc(DrawGLScene)
- # When we are doing nothing, redraw the scene.
- glutIdleFunc(DrawGLScene)
- # Register the function called when the keyboard is pressed.
- glutKeyboardFunc(keyPressed)
- # Initialize our window.
- InitGL(640, 480)
- #start event processing engine
- glutMainLoop()
- if __name__ == "__main__":
- main()
Monday, May 05, 2008
Python OpenGL cube rotated by keypress
Subscribe to:
Post Comments (Atom)
1 意見:
nice...
Post a Comment