from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.Tk import *
from math import *
import Pmw
def display(canvas):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
materialColor = (0.0,0.05,0.05,0.0) #the light scattered off the environment
diffuseColor = (0.4,0.5,0.5,1.0) #the light rays coming directly from source
specularColor = (0.04,0.7,0.7,0.0) #the light reflected off a shiny surface
shininess = .078125
glMaterialfv(GL_FRONT, GL_AMBIENT, materialColor)
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseColor)
glMaterialfv(GL_FRONT, GL_SPECULAR,specularColor)
glMaterialf(GL_FRONT, GL_SHININESS, shininess)
glPushMatrix()
glTranslated(points[0][0],points[0][1],points[0][2])
gluSphere(obj, 3.0, 50, 50);
glPopMatrix()
glPushMatrix()
glTranslated(points[1][0],points[1][1],points[1][2])
gluSphere(obj, 3.0, 50, 50);
glPopMatrix()
vx = points[1][0] - points[0][0]
vy = points[1][1] - points[0][2]
vz = points[1][2] - points[0][2]
v = sqrt( (vx*vx) + (vy*vy) + (vz*vz) )
ax = (180.0/pi)*acos( vz/v )
if ( vz <= 0.0 ):
ax = -ax
rx = -vy*vz
ry = +vx*vz
glPushMatrix()
glTranslated(points[0][0],points[0][1],points[0][2])
glRotated(ax, rx, ry, 0.0)
gluCylinder(obj,1.0,1.0,v,50,50)
glPopMatrix()
glFlush()
def animate(canvas):
global points
while 1:
for n in range(1,20+1):
points[1][0] = 2 * points[1][0] * cos(2*pi*n/20)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
materialColor = (0.0,0.05,0.05,0.0) #the light scattered off the environment
diffuseColor = (0.4,0.5,0.5,1.0) #the light rays coming directly from source
specularColor = (0.04,0.7,0.7,0.0) #the light reflected off a shiny surface
shininess = .078125
glMaterialfv(GL_FRONT, GL_AMBIENT, materialColor)
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseColor)
glMaterialfv(GL_FRONT, GL_SPECULAR,specularColor)
glMaterialf(GL_FRONT, GL_SHININESS, shininess)
glPushMatrix()
glTranslated(points[0][0],points[0][1],points[0][2])
gluSphere(obj, 3.0, 50, 50);
glPopMatrix()
glPushMatrix()
glTranslated(points[1][0],points[1][1],points[1][2])
gluSphere(obj, 3.0, 50, 50);
glPopMatrix()
vx = points[1][0] - points[0][0]
vy = points[1][1] - points[0][2]
vz = points[1][2] - points[0][2]
v = sqrt( (vx*vx) + (vy*vy) + (vz*vz) )
ax = (180.0/pi)*acos( vz/v )
if ( vz <= 0.0 ):
ax = -ax
rx = -vy*vz
ry = +vx*vz
glPushMatrix()
glTranslated(points[0][0],points[0][1],points[0][2])
glRotated(ax, rx, ry, 0.0)
gluCylinder(obj,1.0,1.0,v,50,50)
glPopMatrix()
canvas.after(200,glFlush())
def run():
canvas.redraw = animate
if __name__ == "__main__":
obj = gluNewQuadric()
canvas = Opengl(width = 250, height = 250, double = 1)
points = [[-1.0,1.0,1.0],[2.0,1.0,10.0]]
canvas.redraw = display
canvas.pack(side = 'top', expand = 1, fill = 'both')
root = Pmw.initialise()
b1 = Pmw.ButtonBox(root,labelpos = 'nw',label_text = '',frame_borderwidth = 2,frame_relief = 'groove')
b1.pack(padx = 10, pady = 10)
b1.add('Quit', command = quit)
b1.add('Animate',command = run)
canvas.mainloop()
|