root / PyOpenGL-Demo / broken / dek / OglSurface / OglFrame.py
Historique | Voir | Annoter | Télécharger (2,35 ko)
| 1 |
#!/usr/bin/env 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 sys |
| 9 |
import string |
| 10 |
from OpenGL.GL import * |
| 11 |
from OpenGL.Tk import * |
| 12 |
try:
|
| 13 |
from numpy import * |
| 14 |
except ImportError, err: |
| 15 |
try:
|
| 16 |
from Numeric import * |
| 17 |
except ImportError, err: |
| 18 |
print "This demo requires the numpy or Numeric extension, sorry" |
| 19 |
import sys |
| 20 |
sys.exit() |
| 21 |
import Image |
| 22 |
|
| 23 |
WIDTH=800
|
| 24 |
HEIGHT=600
|
| 25 |
|
| 26 |
class OglFrame: |
| 27 |
def Redraw(self, event=None): |
| 28 |
glDisable(GL_LIGHTING) |
| 29 |
glBegin(GL_LINES) |
| 30 |
|
| 31 |
## +x axis points left
|
| 32 |
glColor3f(1,0,0) |
| 33 |
glVertex3fv([0,0,0]) |
| 34 |
glVertex3fv(self.axispoints[0].tolist()) |
| 35 |
|
| 36 |
## +y axis points up
|
| 37 |
glColor3f(0,1,0) |
| 38 |
glVertex3fv([0,0,0]) |
| 39 |
glVertex3fv(self.axispoints[1].tolist()) |
| 40 |
|
| 41 |
## +z-axis points away
|
| 42 |
glColor3f(0,0,1) |
| 43 |
glVertex3fv([0,0,0]) |
| 44 |
glVertex3fv(self.axispoints[2].tolist()) |
| 45 |
|
| 46 |
glEnd() |
| 47 |
|
| 48 |
def Photo(self, event=None): |
| 49 |
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
|
| 50 |
pixels = glReadPixels(0, 0, |
| 51 |
self.keywords['width'], self.keywords['height'], |
| 52 |
GL_RGBA, GL_UNSIGNED_BYTE) |
| 53 |
im = Image.new("RGB", (self.keywords['width'], self.keywords['height'])) |
| 54 |
im.fromstring(pixels) |
| 55 |
im.save(self.imagename)
|
| 56 |
|
| 57 |
def __init__(self, master=None, redraw=None, *arguments, **keywords): |
| 58 |
self.imagename="photo.ppm" |
| 59 |
self.master=master
|
| 60 |
self.keywords = keywords
|
| 61 |
self.OglFrame = Frame(self.master, width=320, height=200) |
| 62 |
self.OglFrame.pack(side = 'top', expand = 1, fill = 'both') |
| 63 |
self.keywords['double'] = 1 |
| 64 |
self.ogl = Opengl(self.OglFrame, self.keywords) |
| 65 |
self.ogl.pack(side = 'top', expand = 1, fill = 'both') |
| 66 |
self.ogl.bind('<Shift-Button-2>', self.Photo) |
| 67 |
self.ogl.bind('<Button-2>',self.ogl.tkRecordMouse) |
| 68 |
self.ogl.bind('<B2-Motion>', self.ogl.tkTranslate) |
| 69 |
self.ogl.bind('<Button-1>', self.ogl.StartRotate) |
| 70 |
self.ogl.bind('<B1-Motion>', self.ogl.tkRotate) |
| 71 |
self.ogl.bind('<Button-3>', self.ogl.tkRecordMouse) |
| 72 |
self.ogl.bind('<B3-Motion>', self.ogl.tkScale) |
| 73 |
|
| 74 |
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 75 |
glEnable(GL_BLEND); |
| 76 |
glEnable(GL_LINE_SMOOTH); |
| 77 |
|
| 78 |
self.ogl.set_background(0,0,0) |
| 79 |
|
| 80 |
if redraw == None: |
| 81 |
self.ogl.redraw=self.Redraw |
| 82 |
else:
|
| 83 |
self.ogl.redraw=redraw
|
| 84 |
|
| 85 |
self.mainloop=self.ogl.mainloop |
| 86 |
|
| 87 |
|
| 88 |
self.axispoints = identity((3)) |
| 89 |
|
| 90 |
if __name__ == '__main__': |
| 91 |
x= OglFrame(None,
|
| 92 |
None,
|
| 93 |
width=320,
|
| 94 |
height=200,
|
| 95 |
double=1,
|
| 96 |
depth=1)
|
| 97 |
x.mainloop() |