comparison lightsim/openglsim.py @ 353:941cfe1e1691

lightsim now reads levels from dmxserver
author drewp@bigasterisk.com
date Mon, 11 Jun 2007 00:53:16 +0000
parents a08882a05d29
children 60b49f6c2027
comparison
equal deleted inserted replaced
352:9d1f323fb3d3 353:941cfe1e1691
4 from __future__ import division 4 from __future__ import division
5 import sys, time 5 import sys, time
6 import numarray as num 6 import numarray as num
7 import Tkinter as tk 7 import Tkinter as tk
8 import Image 8 import Image
9 9 from louie import dispatcher
10 try: 10 try:
11 from OpenGL import Tk as Togl 11 from OpenGL import Tk as Togl
12 from OpenGL.GL import * 12 from OpenGL.GL import *
13 except ImportError: 13 except ImportError:
14 sys.path.append("/usr/lib/python2.4/site-packages/OpenGL/Tk/linux2-tk8.4") 14 sys.path.append("/usr/lib/python2.4/site-packages/OpenGL/Tk/linux2-tk8.4")
25 glClear(GL_COLOR_BUFFER_BIT) 25 glClear(GL_COLOR_BUFFER_BIT)
26 #glBlendColor(1, 1, 1, mag) # needs ARB_imaging 26 #glBlendColor(1, 1, 1, mag) # needs ARB_imaging
27 glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, imgString) 27 glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, imgString)
28 28
29 class Surface(Togl.Opengl): 29 class Surface(Togl.Opengl):
30 width = 512 30 """widget that adds multiple image files together with adjustable scales"""
31 height = 270 31 def __init__(self, master, filenames, width=512, height=270,
32 imgRescaleTo = 100 32 imgRescaleTo=None):
33 def __init__(self, master, filenames): 33 """
34 Togl.Opengl.__init__(self, master=master, width = self.width, 34 imgRescaleTo can be a length of pixels to reduce all the input
35 height = self.height, double = True, depth = 0) 35 images into. Try 64 for a low res drawing.
36 """
37 Togl.Opengl.__init__(self, master=master, width=width,
38 height=height, double=True, depth=0)
39 self.width, self.height = width, height
36 40
37 self.levels = {} # filename : brightness 41 self.levels = {} # filename : brightness
38 42
39 self.image = {} # filename : imgstr 43 self.image = {} # filename : imgstr
40 for filename in filenames: 44 for filename in filenames:
41 im = Image.open(filename) 45 im = Image.open(filename)
42 if self.imgRescaleTo: 46 if imgRescaleTo:
43 im.thumbnail((self.imgRescaleTo, self.imgRescaleTo)) 47 im.thumbnail((imgRescaleTo, imgRescaleTo))
44 im = im.transpose(Image.FLIP_TOP_BOTTOM) 48 im = im.transpose(Image.FLIP_TOP_BOTTOM)
45 self.imageWidth = im.size[0] 49 self.imageWidth = im.size[0]
46 self.imageHeight = im.size[1] 50 self.imageHeight = im.size[1]
47 self.image[filename] = im.convert("RGBA").tostring() 51 self.image[filename] = im.convert("RGBA").tostring()
48 52
55 print OpenGL.__version__ 59 print OpenGL.__version__
56 60
57 self.bind("<Configure>", self.configure) 61 self.bind("<Configure>", self.configure)
58 62
59 def configure(self, ev): 63 def configure(self, ev):
60 # import pdb; pdb.set_trace()
61 self.width, self.height = ev.width, ev.height 64 self.width, self.height = ev.width, ev.height
62 65
66 def redraw(self, event=None):
67 """you set self.levels to dict and call tkRedraw"""
68 assert 'GL_ARB_imaging' in glGetString(GL_EXTENSIONS).split()
69 start = time.time()
70
71 glClearColor(0.0, 0.0, 0.0, 0)
72 glClear( GL_COLOR_BUFFER_BIT |GL_ACCUM_BUFFER_BIT)
73 glEnable(GL_BLEND)
74 glBlendFunc(GL_SRC_ALPHA, GL_ONE) # add
75
76 # l=glGenLists(1)
77 # glNewList(l,GL_COMPILE)
78 # glEndList()
79
80 # drawing to glAccum might be good
81 layerTimes = []
82 for filename, mag in self.levels.items():
83 #print "pic %s at %f" % (filename, mag)
84 t = time.time()
85 self.drawWithAlpha(self.image[filename],
86 self.imageWidth, self.imageHeight, mag)
87 layerTimes.append(time.time() - t)
88
89 dispatcher.send("status", key="redraw",
90 value="%.1fms" % ((time.time() - start) * 1000))
91
92
63 def drawWithAlpha(self, imgString, w, h, alpha): 93 def drawWithAlpha(self, imgString, w, h, alpha):
64 """without opengl extensions""" 94 """without opengl extensions"""
65 if alpha == 0: 95 if alpha == 0:
66 return 96 return
67 t = time.time() 97 t = time.time()
70 #print " tonum", time.time() - t 100 #print " tonum", time.time() - t
71 if alpha != 1: 101 if alpha != 1:
72 ar[:,3] *= alpha 102 ar[:,3] *= alpha
73 103
74 #print " scl", time.time() - t 104 #print " scl", time.time() - t
105
106 # this might be a good way to scale the color channels too,
107 # but the blend might take two steps. Anyway,
108 # GL_CONSTANT_COLOR seems not to work, so i'm not exploring
109 # this right now.
110 #glBlendFunc(GL_CONSTANT_COLOR, GL_ONE)
111 #glBlendColor(.8, .5, .5, .5)
112
75 glPixelZoom(self.width / w, self.height / h) 113 glPixelZoom(self.width / w, self.height / h)
76 glDrawPixels(w, h, 114 glDrawPixels(w, h,
77 GL_RGBA, GL_UNSIGNED_BYTE, ar.tostring()) 115 GL_RGBA, GL_UNSIGNED_BYTE, ar.tostring())
78 #print " draw", time.time() - t 116 #print " draw", time.time() - t
79 117
80 def redraw(self, event=None):
81 """you set self.levels to dict and call tkRedraw"""
82 assert 'GL_ARB_imaging' in glGetString(GL_EXTENSIONS).split()
83
84 glClearColor(0.0, 0.0, 0.0, 0)
85 glClear( GL_COLOR_BUFFER_BIT |GL_ACCUM_BUFFER_BIT)
86 glEnable(GL_BLEND)
87 glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA)
88 # l=glGenLists(1)
89 # glNewList(l,GL_COMPILE)
90 # glEndList()
91
92 # drawing to glAccum might be good
93 for filename, mag in self.levels.items():
94 #print "pic %s at %f" % (filename, mag)
95 self.drawWithAlpha(self.image[filename],
96 self.imageWidth, self.imageHeight, mag)
97
98 def newLevels(self, event=None, levels=None): 118 def newLevels(self, event=None, levels=None):
99 self.levels = levels 119 if levels != self.levels:
100 self.tkRedraw() 120 self.levels = levels
121 self.tkRedraw()
101 122
102 def main(): 123 def main():
103 root = tk.Frame() 124 root = tk.Frame()
104 root.pack(expand=True, fill='both') 125 root.pack(expand=True, fill='both')
105 QuitButton = tk.Button(root, {'text':'Quit'}) 126 QuitButton = tk.Button(root, {'text':'Quit'})