from OpenGL.GL import * from OpenGL.GLUT import * from OpenGL.GLU import * import sys infile = "morecubes.obj" def cube_in_triangles(ps): fa = ps[3], ps[2], ps[1], ps[0] fb = ps[4], ps[7], ps[3], ps[0] fc = ps[0], ps[1], ps[5], ps[4] fd = ps[1], ps[2], ps[6], ps[5] fe = ps[2], ps[3], ps[7], ps[6] ff = ps[4], ps[5], ps[6], ps[7] def draw_face(f): return [f[2], f[1], f[0], f[3], f[2], f[0]] triangle_list = [] for face in [fa, fb, fc, fd, fe, ff]: triangle_list.extend(draw_face(face)) return triangle_list list_of_positions = [] file_lines = open(infile).read().strip().split("\n") vcount = 0 ccube = [] list_of_cubes = [] for line in file_lines: if line[0] == 'o': name = line.split()[1] if "cube" in name.lower(): # we found a cube print("We're starting to process ", name) if line[0] == 'v': x, y, z = [float(thing) for thing in line.split() if thing != 'v'] list_of_positions.append((x, y, z)) vcount += 1 ccube.append((x, y, z)) if vcount == 8: print len(ccube) list_of_cubes.append(ccube) ccube = [] vcount = 0 angle = .03 def drawscene(): global angle glClear(GL_COLOR_BUFFER_BIT) # glLoadIdentity() # Cumulative vs. not glScalef(.995, .995, .995) # angle += 1 # print angle if angle > 360: angle = 0 glRotatef(angle*3, 1, 1, 1) glCallList(1) glutSwapBuffers() def keypress(key, x, y): print "Keypress: ", key glLoadIdentity() if key == 'q' or key == 'Q': sys.exit() # For lab 13: # Add a key so that the rotation can switch directions print "sys.argv is: ", sys.argv glutInit(sys.argv) glutInitDisplayMode(GLUT_DOUBLE) glutInitWindowSize(1024, 768) glutCreateWindow("Opengl Blender File Viewer") glutDisplayFunc(drawscene) glutIdleFunc(drawscene) glutSpecialFunc(keypress) glutKeyboardFunc(keypress) glNewList(1, GL_COMPILE) color = 0 glBegin(GL_TRIANGLES) for ccube in list_of_cubes: for pos in cube_in_triangles(ccube): color += 0.01 if color >= 1.0: color = 0 glColor3f(1 - color, color, 0) x, y, z = pos glVertex3f(x, y, z) glEnd() glEndList() glMatrixMode(GL_MODELVIEW) #glLoadIdentity() glutMainLoop() # Maybe set color to something, with color=list_of_colors