~mcfletch/openglcontext/trunk : /tests/gldrawpixelssynth.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
	GLUT, PIL, GLUTContext
6
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
	def OnInit( self, ):
20
		"""Initialisation"""
21
		print """Demonstrates drawing a synthetically-generated image 
22
23
	Note: bitmap is drawn in screen coordinates, so does not
24
	respond to moving around or rescaling the window as would
25
	a piece of geometry."""
26
		import numpy
27
		width,height = 200,50
28
		self.width, self.height, self.data = width,height,numpy.arange(
29
			0, 1.0, 1.0/(width*height*3),
30
			dtype='f',
31
		)
32
		
33
	def Render( self, mode = 0):
34
		BaseContext.Render( self, mode )
35
		format = GL_RGB
36
		type = GL_FLOAT
37
		glPixelStorei(GL_PACK_ALIGNMENT, 1)
38
		glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
39
40
		width, height = self.getViewPort()
41
		glMatrixMode(GL_PROJECTION);
42
		# For some reason the GL_PROJECTION_MATRIX is overflowing with a single push!
43
		# glPushMatrix()
44
		matrix = glGetDouble( GL_PROJECTION_MATRIX )
45
		
46
		glLoadIdentity();
47
		glOrtho(0.0, height or 32, 0.0, width or 32, -1.0, 1.0)
48
		glMatrixMode(GL_MODELVIEW);
49
		glPushMatrix();
50
		glLoadIdentity();
51
		glRasterPos2i(40,40);
52
53
		glDrawPixels(
54
			self.width,
55
			self.height,
56
			format,
57
			type,
58
			self.data,
59
		)
60
		
61
		glPopMatrix();
62
		glMatrixMode(GL_PROJECTION);
63
		# For some reason the GL_PROJECTION_MATRIX is overflowing with a single push!
64
		# glPopMatrix();
65
		glLoadMatrixd( matrix ) # should have un-decorated alias for this...
66
		
67
		glMatrixMode(GL_MODELVIEW);
68
69
	def Background(self, mode = 0):
70
		'''Clear the background for a particular rendering mode'''
71
		glClearColor(0.0,0.0,1.0,1.0)
72
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )
73
		
74
if __name__ == "__main__":
75 296
	TestContext.ContextMainLoop()

Loggerhead 1.10 is a web-based interface for Bazaar branches