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

Line Revision Contents
1 1
#! /usr/bin/env python
2
'''Test of the glDrawPixels function including alpha blending
3
4
Requires:
5 310
    GLUT, PIL, GLUTContext
6 1
7
You should see a black 128x128 box in the lower-left corner of the screen
8
with the words "Hello From Alpha-ville!" in blue (the background showing
9
through) and a scrawled "regular image stuff" in red (just part of the
10
RGB image).
11
'''
12
13
from OpenGLContext import testingcontext
14 296
BaseContext = testingcontext.getInteractive()
15 1
from OpenGL.GL import *
16
import string
17
18
class TestContext( BaseContext ):
19 310
    def loadImage( self, imageName = 'gldrawpixels.png' ):
20
        """Load an image from a file using PIL.
21
        This is closer to what you really want to do than the
22
        original port's crammed-together stuff that set global
23
        state in the loading method.  Note the process of binding
24
        the texture to an ID then loading the texture into memory.
25
        This didn't seem clear to me somehow in the tutorial.
26
        """
27
        try:
28
            from PIL.Image import open
29
        except ImportError, err:
30
            from Image import open
31
        im = open(imageName)
32
        try:
33
            ix, iy, image = im.size[0], im.size[1], im.tostring("raw", "RGBA", 0, -1)
34
        except SystemError:
35
            ix, iy, image = im.size[0], im.size[1], im.tostring("raw", "RGBX", 0, -1)
36
        return ix,iy, image
37
    def OnInit( self, ):
38
        """Initialisation"""
39
        print """Should see black bitmap/square in lower left quadrant over blue background
40 1
Should see scrawled "regular image stuff" at top of black square.
41
Should see typed "Hello From Alpha-ville" in the middle of the
42
black square.
43
44 310
    Note: bitmap is drawn in screen coordinates, so does not
45
    respond to moving around or rescaling the window as would
46
    a piece of geometry."""
47
        self.width, self.height, self.data = self.loadImage()
48
        
49
    def Render( self, mode = 0):
50
        BaseContext.Render( self, mode )
51
        format = GL_RGBA
52
        type = GL_UNSIGNED_BYTE
53
        glEnable(GL_ALPHA_TEST);
54
        glAlphaFunc(GL_GREATER,0);
55 1
##		glEnable(GL_BLEND);
56
##		glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
57 310
        glPixelStorei(GL_PACK_ALIGNMENT, 1)
58
        glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
59
60
        width, height = self.getViewPort()
61
        glMatrixMode(GL_PROJECTION);
62
        # For some reason the GL_PROJECTION_MATRIX is overflowing with a single push!
63
        # glPushMatrix()
64
        matrix = glGetDouble( GL_PROJECTION_MATRIX )
65
        
66
        glLoadIdentity();
67
        glOrtho(0.0, height or 32, 0.0, width or 32, -1.0, 1.0)
68
        glMatrixMode(GL_MODELVIEW);
69
        glPushMatrix();
70
        glLoadIdentity();
71
        glRasterPos2i(40,40);
72
73
        glDrawPixels(
74
            self.width,
75
            self.height,
76
            format,
77
            type,
78
            self.data,
79
        )
80
        
81
        glPopMatrix();
82
        glMatrixMode(GL_PROJECTION);
83
        # For some reason the GL_PROJECTION_MATRIX is overflowing with a single push!
84
        # glPopMatrix();
85
        glLoadMatrixd( matrix ) # should have un-decorated alias for this...
86
        
87
        glMatrixMode(GL_MODELVIEW);
88
89
    def Background(self, mode = 0):
90
        '''Clear the background for a particular rendering mode'''
91
        glClearColor(0.0,0.0,1.0,1.0)
92
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )
93
        
94 1
if __name__ == "__main__":
95 310
    TestContext.ContextMainLoop()

Loggerhead 1.10 is a web-based interface for Bazaar branches