Statistics
| Revision:

root / PyOpenGL-Demo / tom / conesave.py @ 1

History | View | Annotate | Download (2.3 kB)

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:38 $')[1:3], ' ')
11
__author__ = 'Tarn Weisner Burton <twburton@users.sourceforge.net>'
12

    
13
from OpenGL.GL import *
14
from OpenGL.Tk import *
15
from OpenGL.GLUT import *
16
from Tkinter import *
17
import sys
18

    
19
class MyApp(Frame):
20

    
21
        def init(self):
22
                glutInit([])
23
                glMaterialfv(GL_FRONT, GL_AMBIENT, [0.2, 0.2, 0.2, 1.0])
24
                glMaterialfv(GL_FRONT, GL_DIFFUSE, [0.8, 0.8, 0.8, 1.0])
25
                glMaterialfv(GL_FRONT, GL_SPECULAR, [1.0, 0.0, 1.0, 1.0])
26
                glMaterialfv(GL_FRONT, GL_SHININESS, 50.0)
27
                glLightfv(GL_LIGHT0, GL_AMBIENT, [0.0, 1.0, 0.0, 1.0])
28
                glLightfv(GL_LIGHT0, GL_DIFFUSE, [1.0, 1.0, 1.0, 1.0])
29
                glLightfv(GL_LIGHT0, GL_SPECULAR, [1.0, 1.0, 1.0, 1.0])
30
                glLightfv(GL_LIGHT0, GL_POSITION, [1.0, 1.0, 1.0, 0.0]);   
31
                glLightModelfv(GL_LIGHT_MODEL_AMBIENT, [0.2, 0.2, 0.2, 1.0])
32
                glEnable(GL_LIGHTING)
33
                glEnable(GL_LIGHT0)
34

    
35

    
36
        def redraw(self, o):
37
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
38
                glPushMatrix()
39
                glTranslatef(0, -1, 0)
40
                glRotatef(250, 1, 0, 0)
41
                glutSolidCone(1, 2, 50, 10)
42
                glPopMatrix()
43

    
44
        def save( self, filename='test.jpg', format="JPEG" ):
45
                import Image # get PIL's functionality...
46
                width, height = 400,400
47
                glPixelStorei(GL_PACK_ALIGNMENT, 1)
48
                data = glReadPixelsub(0, 0, width, height, GL_RGB)
49
                assert data.shape == (width,height,3), """Got back array of shape %r, expected %r"""%(
50
                        data.shape,
51
                        (width,height,3),
52
                )
53
                image = Image.fromstring( "RGB", (width, height), data.tostring() )
54
                image = image.transpose( Image.FLIP_TOP_BOTTOM)
55
                image.save( filename, format )
56
                print 'Saved image to %s'% (os.path.abspath( filename))
57
                return image
58

    
59
        def __init__(self):
60
                self.f = Frame()
61
                self.f.pack()
62

    
63
                self.gl = Opengl(width = 400, height = 400, double = 1, depth = 1)
64
                self.gl.redraw = self.redraw
65
                self.gl.autospin_allowed = 1
66
                self.gl.pack(side = TOP, expand = YES, fill = BOTH)
67
                self.gl.set_background(255,255,255)
68
                self.init()
69

    
70
                self.b = Button(self.f, text="Save", command=self.save)
71
                self.b.pack(side='top')
72
                self.quit = Button(self.f, text = 'Quit', command = sys.exit)
73
                self.quit.pack(side = 'top')
74
                self.gl.mainloop()
75

    
76
app = MyApp()