Files
@ 5d2b119443f2
Branch filter:
Location: light9/lightsim/openglsim.py - annotation
5d2b119443f2
4.9 KiB
text/x-python
keyboardcomposer: fix (one) refresh bug
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | a08882a05d29 45b12307c695 45b12307c695 a08882a05d29 f866d4dec57b 839545f174d3 839545f174d3 f866d4dec57b 941cfe1e1691 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 f866d4dec57b 45b12307c695 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 839545f174d3 839545f174d3 839545f174d3 839545f174d3 839545f174d3 839545f174d3 a08882a05d29 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 45b12307c695 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 941cfe1e1691 941cfe1e1691 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 45b12307c695 a08882a05d29 45b12307c695 a08882a05d29 a08882a05d29 45b12307c695 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 941cfe1e1691 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 941cfe1e1691 941cfe1e1691 941cfe1e1691 45b12307c695 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 a08882a05d29 45b12307c695 45b12307c695 a08882a05d29 45b12307c695 45b12307c695 | #!/usr/bin/python2.4
# see http://www.sgi.com/software/opengl/advanced97/notes/node57.html for accum notes
from __future__ import division
import sys, time
import numarray as num
import Tkinter as tk
import Image
from louie import dispatcher
try:
from OpenGL import Tk as Togl
from OpenGL.GL import *
except ImportError:
sys.path.append("/usr/lib/python2.4/site-packages/OpenGL/Tk/linux2-tk8.4")
from OpenGL.GL import *
import Togl
def xxxdrawWithAlpha(imgString, w, h, alpha):
# this one should be faster because GL does the alpha adjust, but
# i don't think it works yet
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glClear(GL_COLOR_BUFFER_BIT)
#glBlendColor(1, 1, 1, mag) # needs ARB_imaging
glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, imgString)
class Surface(Togl.Opengl):
"""widget that adds multiple image files together with adjustable scales"""
def __init__(self, master, filenames, width=512, height=270,
imgRescaleTo=None):
"""
imgRescaleTo can be a length of pixels to reduce all the input
images into. Try 64 for a low res drawing.
"""
Togl.Opengl.__init__(self, master=master, width=width,
height=height, double=True, depth=0)
self.width, self.height = width, height
self.levels = {} # filename : brightness
self.image = {} # filename : imgstr
for filename in filenames:
im = Image.open(filename)
if imgRescaleTo:
im.thumbnail((imgRescaleTo, imgRescaleTo))
im = im.transpose(Image.FLIP_TOP_BOTTOM)
self.imageWidth = im.size[0]
self.imageHeight = im.size[1]
self.image[filename] = im.convert("RGBA").tostring()
self.set_centerpoint(0, 0, 0)
glDisable(GL_CULL_FACE)
glShadeModel(GL_FLAT)
print 'GL_ARB_imaging', 'GL_ARB_imaging' in glGetString(GL_EXTENSIONS)
import OpenGL
print OpenGL.__version__
self.bind("<Configure>", self.configure)
def configure(self, ev):
self.width, self.height = ev.width, ev.height
def redraw(self, event=None):
"""you set self.levels to dict and call tkRedraw"""
assert 'GL_ARB_imaging' in glGetString(GL_EXTENSIONS).split()
start = time.time()
glClearColor(0.0, 0.0, 0.0, 0)
glClear( GL_COLOR_BUFFER_BIT |GL_ACCUM_BUFFER_BIT)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE) # add
# l=glGenLists(1)
# glNewList(l,GL_COMPILE)
# glEndList()
# drawing to glAccum might be good
layerTimes = []
for filename, mag in self.levels.items():
#print "pic %s at %f" % (filename, mag)
t = time.time()
self.drawWithAlpha(self.image[filename],
self.imageWidth, self.imageHeight, mag)
layerTimes.append(time.time() - t)
dispatcher.send("status", key="redraw",
value="%.1fms" % ((time.time() - start) * 1000))
def drawWithAlpha(self, imgString, w, h, alpha):
"""without opengl extensions"""
if alpha == 0:
return
t = time.time()
ar = num.reshape(num.fromstring(imgString, dtype='uint8'),
(w * h, 4))
#print " tonum", time.time() - t
if alpha != 1:
ar[:,3] *= alpha
#print " scl", time.time() - t
# this might be a good way to scale the color channels too,
# but the blend might take two steps. Anyway,
# GL_CONSTANT_COLOR seems not to work, so i'm not exploring
# this right now.
#glBlendFunc(GL_CONSTANT_COLOR, GL_ONE)
#glBlendColor(.8, .5, .5, .5)
glPixelZoom(self.width / w, self.height / h)
glDrawPixels(w, h,
GL_RGBA, GL_UNSIGNED_BYTE, ar.tostring())
#print " draw", time.time() - t
def newLevels(self, event=None, levels=None):
if levels != self.levels:
self.levels = levels
self.tkRedraw()
def main():
root = tk.Frame()
root.pack(expand=True, fill='both')
QuitButton = tk.Button(root, {'text':'Quit'})
QuitButton.bind('<ButtonRelease-1>', sys.exit)
QuitButton.pack()
filenames=['skyline/bg.png',
'skyline/cyc-lo-red.png',
'skyline/cyc-lo-grn.png',
]
scales = {} # filename : scale
for f in filenames:
scales[f] = tk.Scale(
root, label=f, from_=0, to=1, res=.05, orient='horiz',
command=lambda *args: ogl.newLevels(
levels=dict([(f, s.get()) for f,s in scales.items()])))
scales[f].pack()
ogl = Surface(root, filenames)
ogl.pack(side='top', expand=True, fill='both')
ogl.mainloop()
if __name__ == '__main__':
main()
demo = Surface
|