comparison lightsim/openglsim.py @ 341:f866d4dec57b

openglsim now mixes images
author drewp@bigasterisk.com
date Tue, 29 May 2007 18:20:15 +0000
parents 45b12307c695
children 839545f174d3
comparison
equal deleted inserted replaced
340:88352cff403d 341:f866d4dec57b
1 # see http://www.sgi.com/software/opengl/advanced97/notes/node57.html for accum notes 1 # see http://www.sgi.com/software/opengl/advanced97/notes/node57.html for accum notes
2 2
3 import sys 3 import sys, time
4 from Image import * 4 import numpy as num
5 from OpenGL.GL import * 5 from OpenGL.GL import *
6 from OpenGL.Tk import * 6 from OpenGL.Tk import *
7 import Image
8
9 def drawWithAlpha(imgString, w, h, alpha):
10 """without opengl extensions"""
11 t = time.time()
12 ar = num.reshape(num.fromstring(imgString, dtype='uint8'),
13 (w * h, 4))
14 print " tonum", time.time() - t
15 ar[:,3] *= alpha
16
17 print " scl", time.time() - t
18 glDrawPixels(w, h,
19 GL_RGBA, GL_UNSIGNED_BYTE, ar.tostring())
20 print " draw", time.time() - t
21
7 22
8 class Surface: 23 class Surface:
9 def Display(self, event=None): 24 def Display(self, event=None):
10 25 assert 'GL_ARB_imaging' in glGetString(GL_EXTENSIONS).split()
26
11 glClearColor(0.0, 0.0, 0.0, 0) 27 glClearColor(0.0, 0.0, 0.0, 0)
12 glClear( GL_COLOR_BUFFER_BIT |GL_ACCUM_BUFFER_BIT) 28 glClear( GL_COLOR_BUFFER_BIT |GL_ACCUM_BUFFER_BIT)
13 29 glEnable(GL_BLEND)
30 #glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
31 glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA)
14 l=glGenLists(1) 32 l=glGenLists(1)
15 glNewList(l,GL_COMPILE) 33 glNewList(l,GL_COMPILE)
16 glEndList() 34 glEndList()
17 35
18 # glDrawBuffer(GL_BACK) 36 # glDrawBuffer(GL_BACK)
19 37
20 for x in range(1,2): 38 for x, img in enumerate(self.image):
21
22 mag = self.scales[x].get() 39 mag = self.scales[x].get()
23 print "pic %i at %f" % (x,mag) 40 print "pic %i at %f" % (x,mag)
24 glClear(GL_COLOR_BUFFER_BIT) 41 # glClear(GL_COLOR_BUFFER_BIT)
25 glDrawPixels(self.imageWidth, self.imageHeight, GL_RGB, GL_UNSIGNED_BYTE, self.image[x]) 42 #glBlendColor(1, 1, 1, mag) # needs ARB_imaging
43 drawWithAlpha(img, self.imageWidth, self.imageHeight, mag)
26 44
27 if x==0: 45
28 glAccum(GL_LOAD,mag) 46 ## if x==0:
29 else: 47 ## glAccum(GL_LOAD,mag)
30 glAccum(GL_ACCUM,mag) 48 ## else:
49 ## glAccum(GL_ACCUM,mag)
31 50
32 # glAccum(GL_ADD,self.x) 51 # glAccum(GL_ADD,self.x)
33 self.x=(self.x+.1)%2.0 52 self.x=(self.x+.1)%2.0
34 print "return" 53 ## glAccum(GL_RETURN,1)
35 glAccum(GL_RETURN,1)
36 54
37 def SetupWindow(self): 55 def SetupWindow(self):
38 self.OglFrame = Frame() 56 self.OglFrame = Frame()
39 self.OglFrame.pack(side = 'top',fill='both',expand=1) 57 self.OglFrame.pack(side = 'top',fill='both',expand=1)
40 self.QuitButton = Button(self.OglFrame, {'text':'Quit'}) 58 self.QuitButton = Button(self.OglFrame, {'text':'Quit'})
41 self.QuitButton.bind('<ButtonRelease-1>', sys.exit) 59 self.QuitButton.bind('<ButtonRelease-1>', sys.exit)
42 self.QuitButton.pack({'side':'top'}) 60 self.QuitButton.pack({'side':'top'})
43 61
44 62
45 def SetupOpenGL(self): 63 def SetupOpenGL(self):
46 self.ogl = Opengl(master=self.OglFrame, width = 270, height = 270, double = 1, depth = 0) 64 self.ogl = Opengl(master=self.OglFrame, width = 512, height = 270, double = 1, depth = 0)
47 self.ogl.pack(side = 'top', expand = 1, fill = 'both') 65 self.ogl.pack(side = 'top', expand = 1, fill = 'both')
48 self.ogl.set_centerpoint(0, 0, 0) 66 self.ogl.set_centerpoint(0, 0, 0)
49 self.ogl.redraw = self.Display 67 self.ogl.redraw = self.Display
50 68
51 for x in range(0,2): 69 for x in range(0,2):
58 self.scales=[None,None] 76 self.scales=[None,None]
59 77
60 self.SetupWindow() 78 self.SetupWindow()
61 79
62 self.image=[] 80 self.image=[]
63 for filename in ('pic1.ppm','pic2.ppm'): 81 for filename in ('skyline/bg.png', 'skyline/cyc-lo-red.png'):
64 im = open(filename) 82 im = Image.open(filename)
83 im.thumbnail((200, 200))
65 self.imageWidth = im.size[0] 84 self.imageWidth = im.size[0]
66 self.imageHeight = im.size[1] 85 self.imageHeight = im.size[1]
67 self.image.append(im.tostring("raw", "RGB", 0, -1)) 86 self.image.append(im.convert("RGBA").tostring())#"raw", "RGB", 0, -1))
68 print self.imageWidth, self.imageHeight, self.imageWidth * self.imageHeight*4, len(self.image) 87 print self.imageWidth, self.imageHeight, self.imageWidth * self.imageHeight*4, len(self.image)
69 88
70 self.SetupOpenGL() 89 self.SetupOpenGL()
71 90
72 glDisable(GL_CULL_FACE) 91 glDisable(GL_CULL_FACE)