#!/bin/env python # Rotating cube with color interpolation # Demonstration of use of homogeneous coordinate # transformations and simple data structure for representing # cube from Chapter 4 # Both normals and colors are assigned to the vertices # Cube is centered at origin so (unnormalized) normals # are the same as the vertex values # This version has been hacked up to illustrate: # -- Two rotating cubes, both displaced from the origin. # -- Axes, for orientation purposes # -- Perspective projection (instead of orthographic) # -- Keyboard interaction (moving the eye x-coordinate with # the arrow keys), 'q' to quit). # from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * import sys vertices = [[-1.0,-1.0,-1.0],[1.0,-1.0,-1.0], [1.0,1.0,-1.0], [-1.0,1.0,-1.0], [-1.0,-1.0,1.0], [1.0,-1.0,1.0], [1.0,1.0,1.0], [-1.0,1.0,1.0]] normals = [[-1.0,-1.0,-1.0],[1.0,-1.0,-1.0], [1.0,1.0,-1.0], [-1.0,1.0,-1.0], [-1.0,-1.0,1.0], [1.0,-1.0,1.0], [1.0,1.0,1.0], [-1.0,1.0,1.0]] colors = [[0.0,0.0,0.0],[1.0,0.0,0.0], [1.0,1.0,0.0], [0.0,1.0,0.0], [0.0,0.0,1.0], [1.0,0.0,1.0], [1.0,1.0,1.0], [0.0,1.0,1.0]]; theta = [0.0, 0.0, 0.0] axis = 2 eyex = 3.0 eyey = 3.0 eyez = 7.0 # draw a polygon via list of vertices def polygon(*vert_numbs): glBegin(GL_POLYGON) for vert_num in vert_numbs: glColor3fv(colors[vert_num]) glNormal3fv(normals[vert_num]) glVertex3fv(vertices[vert_num]) glEnd() # draw the six faces of the cube as six polygons def colorcube(): polygon(0,3,2,1) polygon(2,3,7,6) polygon(0,4,7,3) polygon(1,2,6,5) polygon(4,5,6,7) polygon(0,1,5,4) # Draw one axis: # axno 0=x, 1=y, 2=z # leng length from origin to each end of axis # def draw_axis(axno, leng): # Set color colors = [0.0, 0.0, 0.0] colors[axno] = 1.0 glColor3fv(colors) # Draw line endpt = [0.0, 0.0, 0.0] glBegin(GL_LINE) endpt[axno] = -leng glVertex3fv(endpt) endpt[axno] = leng glVertex3fv(endpt) glEnd() glPointSize(4.0) glBegin(GL_POINTS) glColor3f(1.0, 1.0, 1.0) for x in range(-int(leng), int(leng)+1): pt = [0.0, 0.0, 0.0] pt[axno] = x glVertex3fv(pt) glEnd() def axes(): draw_axis(0, 2.0) draw_axis(1, 2.0) draw_axis(2, 2.0) # display callback, clear frame buffer and z buffer, # rotate cube and draw, swap buffers def display(): global eyex, eyey, eyez glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() gluLookAt(eyex, eyey, eyez, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) axes() # Move the first cube up and to the right glPushMatrix() glTranslatef(1.1, 1.1, 0.0) # Rotate and draw first cube glRotatef(theta[0], 1.0, 0.0, 0.0) glRotatef(theta[1], 0.0, 1.0, 0.0) glRotatef(theta[2], 0.0, 0.0, 1.0) colorcube() glPopMatrix() # Move the second cube down and to the left glPushMatrix() glTranslatef(-1.1, -1.1, 0.0) # Rotate and draw second cube glRotatef(theta[0], 1.0, 0.0, 0.0) glRotatef(theta[1], 0.0, 1.0, 0.0) glRotatef(theta[2], 0.0, 0.0, 1.0) colorcube() glPopMatrix() glFlush() glutSwapBuffers() # Idle callback, spin cube 1 degrees about selected axis def spinCube(): global theta theta[axis] += 1.0 if theta[axis] >= 360: theta[axis] -= 360 glutPostRedisplay() # mouse callback, selects an axis about which to rotate def mouse(btn, state, x, y): global axis if btn==GLUT_LEFT_BUTTON and state == GLUT_DOWN: axis = 0 if btn==GLUT_MIDDLE_BUTTON and state == GLUT_DOWN: axis = 1 if btn==GLUT_RIGHT_BUTTON and state == GLUT_DOWN: axis = 2 # Special key callback def specialkey(keypressed, x, y): global eyex, eyey, eyez if keypressed == GLUT_KEY_LEFT: eyex -= 0.5 elif keypressed == GLUT_KEY_RIGHT: eyex += 0.5 # Regular key callback def key(keypressed, x, y): if keypressed == 'q' or keypressed == 'Q': sys.exit() def myReshape(w, h): dim = min(w, h) glViewport(0, 0, dim, dim) glMatrixMode(GL_PROJECTION) glLoadIdentity() #glOrtho(-4.0, 4.0, -4.0, 4.0, -10.0, 10.0) glFrustum(-4.0, 4.0, -4.0, 4.0, 5.0, 15.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity() if __name__ == "__main__": glutInit(sys.argv) # need both double buffering and z buffer glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH) glutInitWindowSize(500, 500) glutCreateWindow("colorcube") glutReshapeFunc(myReshape) glutDisplayFunc(display) glutIdleFunc(spinCube) glutMouseFunc(mouse) glutSpecialFunc(specialkey) glutKeyboardFunc(key) glEnable(GL_DEPTH_TEST) # Enable hidden--surface--removal glutMainLoop()