from OpenGL.GL import * from OpenGL.GLUT import * from OpenGL.GLU import * _node_list = None wire = 1 solid = 1 rotate = 0 xrot = yrot = zrot = 0.0 light = 0 points = False def set_handler(handler): global _node_list _node_list = handler._node_list def draw(node_id): global _node_list, solid, wire, rotate, xrot, yrot, zrot, light, points glLoadIdentity() geom = _node_list[node_id] center = [0.0, 0.0, 0.0] center[0] = (geom.bbox[1][0] + geom.bbox[0][0])/2 center[1] = (geom.bbox[1][1] + geom.bbox[0][1])/2 center[2] = (geom.bbox[1][2] + geom.bbox[0][2])/2 glTranslatef(-center[0], -center[1], -center[2] - 5.0) scale = [0.0, 0.0, 0.0] scale[0] = (geom.bbox[1][0] - geom.bbox[0][0])/2 scale[1] = (geom.bbox[1][1] - geom.bbox[0][1])/2 scale[2] = (geom.bbox[1][2] - geom.bbox[0][2])/2 scale = max(scale) if scale != 0: scale = 1/scale glScalef(scale, scale, scale) glRotatef(xrot,1.0,0.0,0.0) glRotatef(yrot,0.0,1.0,0.0) glRotatef(zrot,0.0,0.0,1.0) if points: glBegin(GL_POINTS) for vid in geom.v: glVertex3fv(geom.v[vid].co) glEnd() else: if solid: if solid == 1: glColor3ub(255, 255, 255) glEnable(GL_TEXTURE_2D) else: glColor3ub(0, 0, 0) if wire: glEnable(GL_POLYGON_OFFSET_FILL); glPolygonOffset(1.0, 2.0); for f in geom.f.values(): xyzs, uvs = f.getCoords() if xyzs and uvs: if len(xyzs) == 4: glBegin(GL_POINTS) else: glBegin(GL_POINTS) if light: f.calcNorm() glNormal3fv(f.getNorm()) for i in range(0, len(xyzs)): glTexCoord2fv(uvs[i]) glVertex3fv(xyzs[i]) glEnd(); glDisable(GL_LIGHTING) glDisable(GL_POLYGON_OFFSET_FILL); glDisable(GL_TEXTURE_2D) if rotate: xrot = xrot + 0.2 yrot = yrot + 0.2 zrot = zrot + 0.2 if wire: glColor3ub(0, 150, 50) for f in geom.f.values(): xyzs, uvs = f.getCoords() if xyzs and uvs: glBegin(GL_LINE_LOOP) for i in range(0, len(xyzs)): glVertex3fv(xyzs[i]) glEnd(); def draw_all(): global _node_list, light if light: glLightfv(GL_LIGHT0, GL_POSITION, (10.0, 10, 20.0, 0.0)) glLightfv(GL_LIGHT0, GL_DIFFUSE, (0.99, 0.99, 0.99, 1.0)) glEnable(GL_LIGHT0) glLightfv(GL_LIGHT1, GL_POSITION, (-10.0, 10, -20.0, 0.0)) glLightfv(GL_LIGHT1, GL_DIFFUSE, (0.99, 0.99, 0.99, 1.0)) glEnable(GL_LIGHT1) glEnable(GL_LIGHTING) glShadeModel(GL_SMOOTH) for node_id in _node_list: draw(node_id) if light: glDisable(GL_LIGHTING) glShadeModel(GL_FLAT)