#tr.py mogrified as in Brinkmann #took about an hour to mogrify ... all those selfishes # gkf 12apr05 # there is still a python type error in here but it works as intended. from OpenGL.GL import * from OpenGL.GLUT import * from OpenGL.GLU import * from math import * import sys # Bryn Keller's version of (cond?expr1:expr2) def iif(cond, expr1, expr2): if cond: return float(expr1) else: float(expr2) def DOT(a,b): return float(a[0]*b[0]+a[1]*b[1]+a[2]*b[2]) def C(u): return cos(u*0.01745) def S(u): return sin(u*0.01745) class Scene: #constructor def __init__(self, argv): self.lux =[0.3, 0.5, 0.8] self.lu = [1,0,0] # this should not be necessary self.pwr = 10. self.aff = [1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0] self.wd = 800 self.ht = 800 self.MouseX = self.wd/2 self.MouseY = self.ht/2 self.brake = 512 self.initGlut(argv) # why is this necessary? def drawvert(self,th,ta): n0= C(th)*C(ta) n1= S(th)*C(ta) n2= S(ta) nn=(n0,n1,n2) lmb = nn[0]*self.lu[0]+nn[1]*self.lu[1]+nn[2]*self.lu[2] lmb = min(max(lmb, 0.0), 1.0) #clamp spec=min(1., 1. - self.pwr + self.pwr*lmb) dog = (th- 10.)/(346.-10.) cat = (ta- 10.)/(346.-10.) rr = dog*lmb gg = (.25 + abs(cat-.5))*lmb bb = (1-cat)*lmb glColor3f(max(rr,spec), max(gg,spec), max(bb,spec)) glVertex3f(C(th)+.5*n0, S(th)+.5*n1, .5*n2) #end drawvert def drawtor(self): for th in range(0,346,15): glBegin(GL_TRIANGLE_STRIP) for ta in range (0,346, 15): self.drawvert(th,ta); self.drawvert(th+12,ta) glEnd() #end drawtor def calculite(self,aff): for ii in range(3): self.lu[ii]= 0 for jj in range(3): self.lu[ii] +=self.aff[ii][jj]*self.lux[jj] #end calculite def display(self): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glMatrixMode(GL_MODELVIEW) glLoadIdentity() glMultMatrixf(self.aff) self.drawtor() glutSwapBuffers() self.calculite(self.aff) #end display() def keyboard(self,key, x, y): if key == chr(27) or key == 'q': sys.exit(0) glutPostRedisplay() #end keyboard() #adjust to resizing of the window def reshape(self,width, height): glClearColor(0.0, 0.0, 0.0, 0.0) if height == 0: height = 1 self.wd = width self.ht = height glViewport(0,0,self.wd,self.ht) glMatrixMode(GL_PROJECTION) glLoadIdentity() if self.wd<=self.ht: glOrtho(-2.0,2.0,-2.0*self.ht/self.wd,2.0*self.ht/self.wd,-2.0,2.0) else: glOrtho(-2.0*self.wd/self.ht,2.0*self.wd/self.ht,-2.0,2.0,-2.0,2.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity() #end reshape() def chaptrack(self): dx = (self.MouseX-self.wd/2)/self.brake dy = (self.MouseY-self.ht/2)/self.brake glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() glRotatef(dx,0,1.0,0.0) glRotatef(dy,1.0,0.0,0.0) glMultMatrixf(self.aff) self.aff = glGetFloatv(GL_MODELVIEW_MATRIX) glPopMatrix() #end chaptrack() def idle(self): self.chaptrack() glutPostRedisplay() #end idle() def mousemotion(self,x,y): self.MouseX = x self.MouseY = y #inLoop() #end mousemotion() def initGlut(self,argv): glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE) glutInitWindowPosition(50, 50) glutInitWindowSize(self.wd, self.ht) glutInit(argv) glutCreateWindow("Torus") glutKeyboardFunc(self.keyboard) glutDisplayFunc(self.display) glutReshapeFunc(self.reshape) glutIdleFunc(self.idle) glutPassiveMotionFunc(self.mousemotion) glEnable(GL_DEPTH_TEST) glShadeModel(GL_SMOOTH) glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(-2.0,2.0,-2.0,2.0,-2.0,2.0) #end class if __name__ == '__main__': scene=Scene(sys.argv) glutMainLoop()