Statistics
| Revision:

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

History | View | Annotate | Download (1.6 kB)

1
"""GLUT replacement for the original checker.py demonstration code
2

3
Note:
4
        Has no navigation code ATM.
5
"""
6

    
7
# This is statement is required by the build system to query build info
8
if __name__ == '__build__':
9
        raise Exception
10

    
11
__version__='$Revision: 1.2 $'[11:-2]
12
__date__ = '$Date: 2008/09/05 20:20:57 $'[6:-2]
13
import OpenGL 
14
OpenGL.ERROR_ON_COPY = True 
15

    
16
import cone
17

    
18
from OpenGL.GL import *
19
from OpenGL.GLU import *
20
from OpenGL.GLUT import *
21
import time, sys, os
22

    
23
def click( button, state, x, y ):
24
        """Handler for click on the screen"""
25
        if state == GLUT_UP:
26
                saveBuffer( )
27

    
28
def saveBuffer( filename="test.jpg", format="JPEG" ):
29
        """Save current buffer to filename in format"""
30
        import Image # get PIL's functionality...
31
        x,y,width,height = glGetDoublev(GL_VIEWPORT)
32
        width,height = int(width),int(height)
33
        glPixelStorei(GL_PACK_ALIGNMENT, 1)
34
        data = glReadPixels(x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE)
35
        image = Image.fromstring( "RGB", (width, height), data )
36
        image = image.transpose( Image.FLIP_TOP_BOTTOM)
37
        image.save( filename, format )
38
        print 'Saved image to %s'% (os.path.abspath( filename))
39
        return image
40
def key_pressed(*args):
41
        # If escape is pressed, kill everything.
42
        if args[0] == '\033':
43
                sys.exit()
44

    
45
def main():
46
        print """You should see a cone rotating slowly, click to save to test.jpg"""
47
        import sys
48
        glutInit(sys.argv)
49
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
50
        glutCreateWindow('Image Saving Demo')
51
        glutDisplayFunc(cone.display)
52
        glutIdleFunc(cone.display)
53
        glutKeyboardFunc(key_pressed)
54
        glutMouseFunc( click )
55
        # note need to do this to properly render faceted geometry
56
        glutMainLoop()
57

    
58
if __name__ == "__main__":
59
    main()