import rs274.OpenGLTk, Tkinter, hal from minigl import * class Collection: def __init__(self, parts): self.parts = parts def traverse(self): for p in self.parts: if hasattr(p, "apply"): p.apply() if hasattr(p, "draw"): p.draw() if hasattr(p, "traverse"): p.traverse() if hasattr(p, "unapply"): p.unapply() class LinearTransformation(Collection): def __init__(self, parts, x, y, z): self.parts = parts self.where = x, y, z def apply(self): glPushMatrix() glTranslatef(*self.where) def unapply(self): glPopMatrix() class LinearHalTransformation(Collection): def __init__(self, parts, comp, var, x, y, z): self.parts = parts self.where = x, y, z self.comp = comp self.var = var def apply(self): x, y, z = self.where v = self.comp[self.var] glPushMatrix() glTranslatef(x*v, y*v, z*v) def unapply(self): glPopMatrix() class AngularHalTransformation(Collection): def __init__(self, parts, comp, var, th, x, y, z, dx, dy, dz): self.parts = parts self.where = th, x, y, z, dx, dy, dz self.comp = comp self.var = var def apply(self): th, x, y, z, dx, dy, dz = self.where glPushMatrix() glTranslatef(dx, dy, dz) glRotatef(th * self.comp[self.var], x, y, z) def unapply(self): glPopMatrix() class AngularTransformation(Collection): def __init__(self, parts, th, x, y, z): self.parts = parts self.where = th, x, y, z def apply(self): glPushMatrix() glTranslatef(*self.where) def unapply(self): glPopMatrix() class CappedCylinder: def __init__(self, base, top, height): self.coords = base, top, height self.q = gluNewQuadric() def draw(self): base, top, height = self.coords gluCylinder(self.q, base, top, height, 32, 1) gluDisk(self.q, 0, base, 32, 1) glPushMatrix() glTranslatef(0,0,height) gluDisk(self.q, 0, top, 32, 1) glPopMatrix() class O(rs274.OpenGLTk.Opengl): def __init__(self, *args, **kw): rs274.OpenGLTk.Opengl.__init__(self, *args, **kw) self.r_back = self.g_back = self.b_back = 0 def basic_lighting(self): self.activate() glLightfv(GL_LIGHT0, GL_POSITION, (1, -1, .5, 0)) glLightfv(GL_LIGHT0, GL_AMBIENT, (.2,.2,.2,0)) glLightfv(GL_LIGHT0, GL_DIFFUSE, (.6,.6,.4,0)) glLightfv(GL_LIGHT0+1, GL_POSITION, (-1, -1, .5, 0)) glLightfv(GL_LIGHT0+1, GL_AMBIENT, (.0,.0,.0,0)) glLightfv(GL_LIGHT0+1, GL_DIFFUSE, (.0,.0,.4,0)) glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, (1,1,1,0)) glEnable(GL_LIGHTING) glEnable(GL_LIGHT0) glEnable(GL_LIGHT0+1) glDepthFunc(GL_LESS) glEnable(GL_DEPTH_TEST) glMatrixMode(GL_MODELVIEW) glLoadIdentity() def redraw(self, *args): if self.winfo_width() == 1: return self.model.traverse() c = hal.component("r") c.newpin("a", hal.HAL_FLOAT, hal.HAL_IN) c.newpin("b", hal.HAL_FLOAT, hal.HAL_IN) c.newpin("c", hal.HAL_FLOAT, hal.HAL_IN) c.ready() app = Tkinter.Tk() t = O(app, double=1, depth=1) t.model = Collection([ CappedCylinder(1, .5, 2), AngularHalTransformation( [ CappedCylinder(1, 1, 2), AngularHalTransformation( [ CappedCylinder(1, 1, 2), AngularHalTransformation( [ CappedCylinder(1, 1, 2) ], c, 'c', 360, 1, 0, 0, 0, 0, 2) ], c, 'b', 360, 0, 1, 0, 0, 0, 2) ], c, 'a', 360, 1, 0, 0, 0, 0, 2) ]) t.distance = 20 t.pack(fill="both", expand=1) def update(): t.tkRedraw() t.after(100, update) update() app.mainloop()