#!/usr/bin/env python #RGB Cube by Lauro Moura - lauromoura at gmail dot com #Based on Nehe Productions tutorials from OpenGL.GL import * from OpenGL.GLUT import * from OpenGL.GLU import * import sys ESCAPE = '\033' window = 0 def InitGL(Width, Height): glClearColor(0.0, 0.0, 0.0, 0.0) glClearDepth(1.0) glDepthFunc(GL_LESS) glEnable(GL_DEPTH_TEST) glShadeModel(GL_SMOOTH) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0) glMatrixMode(GL_MODELVIEW) def ReSizeGLScene(Width, Height): if Height == 0: # Prevent A Divide By Zero If The Window Is Too Small Height = 1 glViewport(0, 0, Width, Height) # Reset The Current Viewport And Perspective Transformation glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0) glMatrixMode(GL_MODELVIEW) rquad = 0.0 def DrawGLScene(): global rquad glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() # Reset The View glTranslatef(-0.5,-0.5,-5.0) glRotate(rquad, 1.0, 1.0, 1.0) drawAxis(True) drawCube() rquad += 0.35 glutSwapBuffers() def drawAxis(label): glColor3f(1.0, 1.0, 1.0) if label: glRasterPos3f(2.1, 0, 0) glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, 0x58) glRasterPos3f(0, 2.1, 0) glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, 0x59) glRasterPos3f(0, 0, 2.1) glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, 0x5A) glBegin(GL_LINES) glVertex3f(0.0,0.0,0.0) glVertex3f(2.0,0.0,0.0) glVertex3f(0.0,0.0,0.0) glVertex3f(0.0,2.0,0.0) glVertex3f(0.0,0.0,0.0) glVertex3f(0.0,0.0,2.0) glEnd() def drawCube(): glBegin(GL_QUADS) #X - Red #Y - Green #Z - Blue #front glColor(1.0, 1.0, 1.0) glVertex3f(1.0, 1.0, 1.0) glColor(0.0, 1.0, 1.0) glVertex3f(0.0, 1.0, 1.0) glColor(0.0, 0.0, 1.0) glVertex3f(0.0, 0.0, 1.0) glColor(1.0, 0.0, 1.0) glVertex3f(1.0, 0.0, 1.0) #right glColor(1.0, 1.0, 0.0) glVertex3f(1.0, 1.0, 0.0) glColor(1.0, 1.0, 1.0) glVertex3f(1.0, 1.0, 1.0) glColor(1.0, 0.0, 1.0) glVertex3f(1.0, 0.0, 1.0) glColor(1.0, 0.0, 0.0) glVertex3f(1.0, 0.0, 0.0) #back glColor(1.0, 1.0, 0.0) glVertex3f(1.0, 1.0, 0.0) glColor(0.0, 1.0, 0.0) glVertex3f(0.0, 1.0, 0.0) glColor(0.0, 0.0, 0.0) glVertex3f(0.0, 0.0, 0.0) glColor(1.0, 0.0, 0.0) glVertex3f(1.0, 0.0, 0.0) #left glColor(0.0, 1.0, 1.0) glVertex3f(0.0, 1.0, 1.0) glColor(0.0, 1.0, 0.0) glVertex3f(0.0, 1.0, 0.0) glColor(0.0, 0.0, 0.0) glVertex3f(0.0, 0.0, 0.0) glColor(0.0, 0.0, 1.0) glVertex3f(0.0, 0.0, 1.0) #top glColor(1.0, 1.0, 0.0) glVertex3f(1.0, 1.0, 0.0) glColor(0.0, 1.0, 0.0) glVertex3f(0.0, 1.0, 0.0) glColor(0.0, 1.0, 1.0) glVertex3f(0.0, 1.0, 1.0) glColor(1.0, 1.0, 1.0) glVertex3f(1.0, 1.0, 1.0) #botton glColor(1.0, 0.0, 0.0) glVertex3f(1.0, 0.0, 0.0) glColor(0.0, 0.0, 0.0) glVertex3f(0.0, 0.0, 0.0) glColor(0.0, 0.0, 1.0) glVertex3f(0.0, 0.0, 1.0) glColor(1.0, 0.0, 1.0) glVertex3f(1.0, 0.0, 1.0) glEnd() def keyPressed(*args): if args[0] == ESCAPE: glutDestroyWindow(window) sys.exit() def main(): global window glutInit(sys.argv) glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(640, 480) glutInitWindowPosition(0, 0) window = glutCreateWindow("RGB Cube") glutDisplayFunc(DrawGLScene) glutIdleFunc(DrawGLScene) glutReshapeFunc(ReSizeGLScene) glutKeyboardFunc(keyPressed) InitGL(640, 480) glutMainLoop() print "Hit ESC key to quit." main()