~mcfletch/openglcontext/trunk : /tests/arbwindowpos.py (revision 351)

Line Revision Contents
1 1
#! /usr/bin/env python
2
'''Test of the ARB window_pos extension...
3
4
Requires:
5 308
    PIL
6 1
7
ARB.window_pos provides for specifying on-screen
8
raster location without needing to carefully specify
9
the model-view and perspective matrices.
10
11
This module uses the glWindowPos2dARB and glWindowPos2dvARB
12
functions, as well as testing for proper operation under
13
malformed parameters to the glWindowPos2dvARB function.
14
'''
15
from OpenGLContext import testingcontext
16 296
BaseContext = testingcontext.getInteractive()
17 1
from OpenGL.GL import *
18 175
from OpenGL.constants import *
19
from OpenGL import error
20 1
import math, random, traceback, sys
21
from OpenGLContext.events.timer import Timer
22
23
class TestContext( BaseContext ):
24 308
    def loadImage( self, imageName = 'gldrawpixels.png' ):
25
        """Load an image from a file using PIL.
26
        This is closer to what you really want to do than the
27
        original port's crammed-together stuff that set global
28
        state in the loading method.  Note the process of binding
29
        the texture to an ID then loading the texture into memory.
30
        This didn't seem clear to me somehow in the tutorial.
31
        """
32 310
        try:
33
            from PIL.Image import open
34
        except ImportError, err:
35
            from Image import open
36 308
        im = open(imageName)
37
        try:
38
            ix, iy, image = im.size[0], im.size[1], im.tostring("raw", "RGBA", 0, -1)
39
        except SystemError:
40
            ix, iy, image = im.size[0], im.size[1], im.tostring("raw", "RGBX", 0, -1)
41
        return ix,iy, image
42
    def OnInit( self, ):
43
        """Initialisation"""
44
        print """You should see two bitmap images traversing the screen
45
    diagonally.  If the GL.ARB.window_pos extension is not available
46
    then you will exit immediately.
47
    """
48
        self.width, self.height, self.data = self.loadImage()
49
        global window_pos
50
        window_pos = self.extensions.initExtension( "GL.ARB.window_pos")
51
        if not window_pos:
52
            print 'GL_ARB_window_pos not supported!'
53
            sys.exit( testingcontext.REQUIRED_EXTENSION_MISSING )
54
        self.time = Timer( duration = 8.0, repeating = 1 )
55
        self.time.addEventHandler( "fraction", self.OnTimerFraction )
56
        self.time.register (self)
57
        self.time.start ()
58
        self.x = 0
59
        self.y = 0
60
        
61
        try:
62
            window_pos.glWindowPos2dvARB(())
63
        except (error.CopyError,GLerror,ValueError), err:
64
            print 'Correct handling of incorrect parameters', err
65
        except Exception, err:
66
            traceback.print_exc()
67
            print 'Incorrect handling of incorrect parameters'
68
        try:
69
            window_pos.glWindowPos3dvARB(())
70
        except (error.CopyError,GLerror, ValueError), err:
71
            print 'Correct handling of incorrect parameters', err
72
        except Exception, err:
73
            traceback.print_exc()
74
            print 'Incorrect handling of incorrect parameters'
75
        
76
    def OnTimerFraction( self, event ):
77
        """Set new position..."""
78
        width, height = self.getViewPort()
79
        self.x = width * event.fraction()
80
        self.y = height * event.fraction()
81
82
    def Render( self, mode = None):
83
        BaseContext.Render( self, mode )
84
        # we aren't affected by the matrices (which is the point)
85
        glTranslate( 100,0,0 )
86
        if mode.visible and not mode.transparent:
87
            format = GL_RGBA
88
            type = GL_UNSIGNED_BYTE
89
            glEnable(GL_ALPHA_TEST);
90
            glAlphaFunc(GL_GREATER,0);
91
            glPixelStorei(GL_PACK_ALIGNMENT, 1)
92
            glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
93
94
            width, height = self.getViewPort()
95
            window_pos.glWindowPos2dARB(self.x,self.y)
96
97
            glDrawPixels(
98
                self.width,
99
                self.height,
100
                format,
101
                type,
102
                self.data,
103
            )
104
            window_pos.glWindowPos2fvARB(
105
                GLfloat_2(self.x,self.getViewPort()[1]-self.y)
106
            )
107
            glDrawPixels(
108
                self.width,
109
                self.height,
110
                format,
111
                type,
112
                self.data,
113
            )
114
        
115
        
116 1
if __name__ == "__main__":
117 308
    TestContext.ContextMainLoop()

Loggerhead 1.10 is a web-based interface for Bazaar branches