root / PyOpenGL-Demo / broken / tom / pick.py
Historique | Voir | Annoter | Télécharger (2,64 ko)
| 1 |
#!/usr/bin/python
|
|---|---|
| 2 |
|
| 3 |
# This is statement is required by the build system to query build info
|
| 4 |
if __name__ == '__build__': |
| 5 |
raise Exception |
| 6 |
|
| 7 |
|
| 8 |
import string |
| 9 |
__version__ = string.split('$Revision: 1.1.1.1 $')[1] |
| 10 |
__date__ = string.join(string.split('$Date: 2007/02/15 19:25:40 $')[1:3], ' ') |
| 11 |
__author__ = 'Tarn Weisner Burton <twburton@users.sourceforge.net>'
|
| 12 |
|
| 13 |
from OpenGL.GL import * |
| 14 |
from OpenGL.GLUT import * |
| 15 |
from OpenGL.Tk import * |
| 16 |
|
| 17 |
def redraw(o): |
| 18 |
"""The main scene redraw function."""
|
| 19 |
|
| 20 |
# Clear the background and depth buffer.
|
| 21 |
|
| 22 |
for i in range(0, len(o.spheres)): |
| 23 |
if i == o.picked_sphere:
|
| 24 |
glMaterialfv(GL_FRONT, GL_DIFFUSE, [1., 1., 0., 0.]) |
| 25 |
else:
|
| 26 |
glMaterialfv(GL_FRONT, GL_DIFFUSE, [0.3, 0.9, 0.3, 0.]) |
| 27 |
s = o.spheres[i] |
| 28 |
glPushMatrix() |
| 29 |
glTranslatef(s[0], s[1], s[2]) |
| 30 |
glutSolidSphere(1.4,20,20) |
| 31 |
glPopMatrix() |
| 32 |
|
| 33 |
def pick(o, p1, p2, event=None): |
| 34 |
"""A pick function.
|
| 35 |
The end points of the picked line are passed as p1 and p2.
|
| 36 |
glDistFromLine can be used to measure distances from the line."""
|
| 37 |
|
| 38 |
inear = -1
|
| 39 |
dmin = 1.e10
|
| 40 |
|
| 41 |
for i in range(0, len(o.spheres)): |
| 42 |
s = o.spheres[i] |
| 43 |
d = glDistFromLine(s, p1, p2) |
| 44 |
|
| 45 |
if d < dmin:
|
| 46 |
inear = i |
| 47 |
dmin = d |
| 48 |
|
| 49 |
o.picked_sphere = inear |
| 50 |
|
| 51 |
"""If we want the viewer to redraw we return a true value."""
|
| 52 |
|
| 53 |
return 1 |
| 54 |
|
| 55 |
#
|
| 56 |
# Demo starts here really.
|
| 57 |
def main(): |
| 58 |
import Tkinter, sys |
| 59 |
|
| 60 |
o = Opengl(None, width = 200, height = 200, double = 1, depth = 1) |
| 61 |
glutInit( [] ) |
| 62 |
o.pack(expand = 1, fill = 'both') |
| 63 |
|
| 64 |
o.redraw = redraw |
| 65 |
o.pick = pick |
| 66 |
o.set_centerpoint(-2., 35., 24.) |
| 67 |
o.set_eyepoint(30.)
|
| 68 |
|
| 69 |
o.spheres = [ |
| 70 |
[-4.322, 30.55, 21.011], |
| 71 |
[-4.394, 29.355, 21.739], |
| 72 |
[-5.645, 28.807, 21.86], |
| 73 |
[-3.288, 28.783, 22.252], |
| 74 |
[-2.019, 29.274, 22.234], |
| 75 |
[-0.974, 28.708, 22.785], |
| 76 |
[-1.933, 30.569, 21.533], |
| 77 |
[-0.758, 31.235, 21.496], |
| 78 |
[-0.765, 32.405, 20.842], |
| 79 |
[-1.944, 32.803, 20.217], |
| 80 |
[-3.054, 32.324, 20.235], |
| 81 |
[-3.083, 31.116, 20.898], |
| 82 |
[0.64, 33.021, 20.684], |
| 83 |
[0.578, 34.384, 20.488], |
| 84 |
[1.308, 34.813, 19.227], |
| 85 |
[-1.279, 37.203, 22.934], |
| 86 |
[-0.862, 37.551, 21.655], |
| 87 |
[-0.194, 36.854, 20.712], |
| 88 |
[0.032, 35.404, 21.207], |
| 89 |
[-0.553, 35.049, 22.533], |
| 90 |
[-1.229, 35.834, 23.289], |
| 91 |
[-1.965, 38.13, 23.541], |
| 92 |
[-2.138, 39.337, 23.429], |
| 93 |
[-2.925, 37.974, 24.419], |
| 94 |
[-3.571, 38.572, 25.455], |
| 95 |
[-3.41, 37.769, 26.744], |
| 96 |
[-3.349, 36.58, 27.005], |
| 97 |
[-3.607, 38.634, 27.799], |
| 98 |
[-5.12, 38.852, 25.539], |
| 99 |
[-5.745, 39.848, 24.643], |
| 100 |
[-7.132, 39.91, 23.719], |
| 101 |
[-7.29, 40.706, 22.617], |
| 102 |
[-7.707, 39.325, 24.755]] |
| 103 |
|
| 104 |
o.picked_sphere = -1
|
| 105 |
o.autospin_allowed = 1
|
| 106 |
|
| 107 |
l = Tkinter.Label(None, text = 'Press Shift-Button-1 over an\natom to highlight') |
| 108 |
l.pack(side = 'top', expand = 1, fill = 'both') |
| 109 |
|
| 110 |
# Enter the tk mainloop.
|
| 111 |
Tkinter.mainloop() |
| 112 |
|
| 113 |
|
| 114 |
if __name__ == "__main__": |
| 115 |
main() |
| 116 |
|
| 117 |
|