import sys, time, math, os, random from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * startDir = os.getcwd() class Square: def __init__(self, width=1.0, height=1.0, xpos=0.0, ypos=0.0, color=[1,1,1]): self.width = width self.height = height self.xpos = xpos self.ypos = ypos self.color = color def draw(self): glColor3fv(self.color) glPushMatrix() glTranslatef(self.xpos, self.ypos, 0) glBegin(GL_QUADS) glVertex2f(-self.width/2.0, -self.height/2.0) glVertex2f(self.width/2.0, -self.height/2.0) glVertex2f(self.width/2.0, self.height/2.0) glVertex2f(-self.width/2.0, self.height/2.0) glEnd() glPopMatrix() class AlphaWheel: def __init__(self, radius=1.0, xpos=0.0, ypos=0.0, color=[1,1,1]): self.radius = radius self.xpos = xpos self.ypos = ypos self.color = color def draw(self): # glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glPushMatrix() glTranslatef(self.xpos, self.ypos, 0) glBegin(GL_TRIANGLE_FAN) glColor4f(self.color[0],self.color[1],self.color[2],1) glVertex2f(0,0) glColor4f(self.color[0],self.color[1],self.color[2],0) for angle in range(0,361, 10): glVertex2f(math.sin(angle*math.pi/180.0)*self.radius, math.cos(angle*math.pi/180.0)*self.radius) glEnd() glPopMatrix() glDisable(GL_BLEND) objects = [] def createObjects(): global objects square1 = Square(3, 3) objects.append(square1) wheel = AlphaWheel(radius=3, xpos=2, ypos=0, color=[1,0,0]) objects.append(wheel) def draw(): glClearColor(0, 0.3, 0.5, 0) glClear(GL_COLOR_BUFFER_BIT) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(-5, 5, -5, 5) glMatrixMode(GL_MODELVIEW) for o in objects: o.draw() glutSwapBuffers() def keyboard(key, x, y): if key == chr(27): sys.exit(0) def specialkey(key,x,y): global objects if key == GLUT_KEY_LEFT: objects[0].xpos -= 0.25 elif key == GLUT_KEY_RIGHT: objects[0].xpos += 0.25 elif key == GLUT_KEY_UP: objects[0].ypos += 0.25 elif key == GLUT_KEY_DOWN: objects[0].ypos -= 0.25 def update(dummy): glutTimerFunc(16, update, 0) global objects objects[1].xpos = 2 * math.sin(time.time()) objects[1].ypos = 2 * math.cos(time.time()) glutPostRedisplay() glutInit([]) glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE) glutInitWindowSize(400,400) glutCreateWindow(sys.argv[0]) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) glutSpecialFunc(specialkey) glutTimerFunc(1, update, 0) os.chdir(startDir) createObjects() glutMainLoop()