from OpenGL.GL import * # import the main gl library, e.g., glViewport from OpenGL.GLU import * # import the gl utility library, e.g., gluOrtho2D from OpenGL.GLUT import * # import GL Utility Toolkit, windowing lib, e.g., glutCreateWindow def display(): print 'display' # clear up the color array glClear( GL_COLOR_BUFFER_BIT ) # give an array of 2D vertices glVertexPointerf( [[0.1, 0.9], [0.9, 0.1]] ) # draw lines, starting from zero, use two vertices glDrawArrays( GL_LINES, 0, 2 ) # same for a different array glVertexPointerf( [[0.1, 0.1], [0.9, 0.9]] ) glDrawArrays( GL_LINES, 0, 2 ) # tell opengl to draw all the commands that were passed in glFlush() def myInit(): # set up the background color to red, alpha zero glClearColor( 1,0,0,0 ) # set the current drawing color to white glColor3( 1,1,1 ) # enable the use of vertex arrays glEnableClientState( GL_VERTEX_ARRAY ) def reshape(w,h): print 'reshape' # set up the viewport to be the full window glViewport( 0,0,w,h ) # start changing the projection matrix glMatrixMode( GL_PROJECTION ) # clear it to identity matrix glLoadIdentity() # multiply the identity matrix with a matrix that uses # orthographic projection, sets the coordinate system # from 0 to 1 in both x and y gluOrtho2D( 0,1,0,1 ) # usually we only modify modelview matrix in the programs, # so set the matrixmode to modelview so don't accidentally # change the projection matrix glMatrixMode( GL_MODELVIEW ) def mouse(button, state, x, y): print 'mouse', button, state, x, y def motion(x, y): print 'motion', x, y def kb(key, x, y): print 'kb', key, ord(key), x, y def main(): # initialized glut library glutInit( [] ) # prepare for the window you are going to ask for glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB ) glutInitWindowSize( 300,300 ) glutInitWindowPosition( 300,100 ) # get the window, give it a title string glutCreateWindow( 'Hello GLUT!' ) # set up the callback functions for various events glutReshapeFunc( reshape ) glutDisplayFunc( display ) glutMouseFunc( mouse ) glutMotionFunc( motion ) glutKeyboardFunc( kb ) # call my own init to set up basic opengl state myInit() # release control to the glut main event loop glutMainLoop() # after all the functions have been declared, just call the main() function # note that main is not a special name like in C or Java main()