comparison lightsim/openglsim.py @ 463:60b49f6c2027

start porting lightsim to qt
author drewp@bigasterisk.com
date Mon, 01 Sep 2008 00:41:29 +0000
parents 941cfe1e1691
children f69ba5ac17c5
comparison
equal deleted inserted replaced
462:cd4d7b878550 463:60b49f6c2027
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 from louie import dispatcher 9 from louie import dispatcher
10 try: 10
11 from OpenGL import Tk as Togl 11 from PyQt4 import QtCore, QtOpenGL
12 from OpenGL.GL import * 12 from OpenGL.GL import *
13 except ImportError:
14 sys.path.append("/usr/lib/python2.4/site-packages/OpenGL/Tk/linux2-tk8.4")
15 from OpenGL.GL import *
16 import Togl
17
18 13
19 def xxxdrawWithAlpha(imgString, w, h, alpha): 14 def xxxdrawWithAlpha(imgString, w, h, alpha):
20 # this one should be faster because GL does the alpha adjust, but 15 # this one should be faster because GL does the alpha adjust, but
21 # i don't think it works yet 16 # i don't think it works yet
22 17
24 19
25 glClear(GL_COLOR_BUFFER_BIT) 20 glClear(GL_COLOR_BUFFER_BIT)
26 #glBlendColor(1, 1, 1, mag) # needs ARB_imaging 21 #glBlendColor(1, 1, 1, mag) # needs ARB_imaging
27 glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, imgString) 22 glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, imgString)
28 23
29 class Surface(Togl.Opengl): 24 class Surface(QtOpenGL.QGLWidget):
30 """widget that adds multiple image files together with adjustable scales""" 25 """widget that adds multiple image files together with adjustable scales"""
31 def __init__(self, master, filenames, width=512, height=270, 26 def __init__(self, parent, filenames, width=512, height=270,
32 imgRescaleTo=None): 27 imgRescaleTo=None):
33 """ 28 """
34 imgRescaleTo can be a length of pixels to reduce all the input 29 imgRescaleTo can be a length of pixels to reduce all the input
35 images into. Try 64 for a low res drawing. 30 images into. Try 64 for a low res drawing.
36 """ 31 """
37 Togl.Opengl.__init__(self, master=master, width=width, 32 QtOpenGL.QGLWidget.__init__(self, parent)
38 height=height, double=True, depth=0)
39 self.width, self.height = width, height
40 33
41 self.levels = {} # filename : brightness 34 self.levels = {} # filename : brightness
42 35
43 self.image = {} # filename : imgstr 36 self.image = {} # filename : imgstr
44 for filename in filenames: 37 for filename in filenames:
38 print "open", filename
45 im = Image.open(filename) 39 im = Image.open(filename)
46 if imgRescaleTo: 40 if imgRescaleTo:
47 im.thumbnail((imgRescaleTo, imgRescaleTo)) 41 im.thumbnail((imgRescaleTo, imgRescaleTo))
48 im = im.transpose(Image.FLIP_TOP_BOTTOM) 42 im = im.transpose(Image.FLIP_TOP_BOTTOM)
49 self.imageWidth = im.size[0] 43 self.imageWidth = im.size[0]
50 self.imageHeight = im.size[1] 44 self.imageHeight = im.size[1]
51 self.image[filename] = im.convert("RGBA").tostring() 45 self.image[filename] = im.convert("RGBA").tostring()
52 46
53 self.set_centerpoint(0, 0, 0) 47 #self.set_centerpoint(0, 0, 0)
54 48
49 def initializeGL(self):
55 glDisable(GL_CULL_FACE) 50 glDisable(GL_CULL_FACE)
56 glShadeModel(GL_FLAT) 51 glShadeModel(GL_FLAT)
57 print 'GL_ARB_imaging', 'GL_ARB_imaging' in glGetString(GL_EXTENSIONS) 52 print 'GL_ARB_imaging', 'GL_ARB_imaging' in glGetString(GL_EXTENSIONS)
58 import OpenGL 53 import OpenGL
59 print OpenGL.__version__ 54 print OpenGL.__version__
55
56 # def minimumSizeHint(self):
57 # return QtCore.QSize(512, 512)
60 58
61 self.bind("<Configure>", self.configure) 59 # def sizeHint(self):
60 # return QtCore.QSize(512, 512)
62 61
63 def configure(self, ev): 62 def paintGL(self):
64 self.width, self.height = ev.width, ev.height
65
66 def redraw(self, event=None):
67 """you set self.levels to dict and call tkRedraw""" 63 """you set self.levels to dict and call tkRedraw"""
68 assert 'GL_ARB_imaging' in glGetString(GL_EXTENSIONS).split() 64 assert 'GL_ARB_imaging' in glGetString(GL_EXTENSIONS).split()
69 start = time.time() 65 start = time.time()
70 66
71 glClearColor(0.0, 0.0, 0.0, 0) 67 glClearColor(0.0, 0.0, 0.0, 0)
78 # glEndList() 74 # glEndList()
79 75
80 # drawing to glAccum might be good 76 # drawing to glAccum might be good
81 layerTimes = [] 77 layerTimes = []
82 for filename, mag in self.levels.items(): 78 for filename, mag in self.levels.items():
83 #print "pic %s at %f" % (filename, mag)
84 t = time.time() 79 t = time.time()
85 self.drawWithAlpha(self.image[filename], 80 self.drawWithAlpha(self.image[filename],
86 self.imageWidth, self.imageHeight, mag) 81 self.imageWidth, self.imageHeight, mag)
87 layerTimes.append(time.time() - t) 82 layerTimes.append(time.time() - t)
88 83
107 # but the blend might take two steps. Anyway, 102 # but the blend might take two steps. Anyway,
108 # GL_CONSTANT_COLOR seems not to work, so i'm not exploring 103 # GL_CONSTANT_COLOR seems not to work, so i'm not exploring
109 # this right now. 104 # this right now.
110 #glBlendFunc(GL_CONSTANT_COLOR, GL_ONE) 105 #glBlendFunc(GL_CONSTANT_COLOR, GL_ONE)
111 #glBlendColor(.8, .5, .5, .5) 106 #glBlendColor(.8, .5, .5, .5)
112 107
113 glPixelZoom(self.width / w, self.height / h) 108 glPixelZoom(self.width() / w, self.height() / h)
114 glDrawPixels(w, h, 109 glDrawPixels(w, h,
115 GL_RGBA, GL_UNSIGNED_BYTE, ar.tostring()) 110 GL_RGBA, GL_UNSIGNED_BYTE, ar.tostring())
116 #print " draw", time.time() - t 111 #print " draw", time.time() - t
117 112
118 def newLevels(self, event=None, levels=None): 113 def newLevels(self, event=None, levels=None):
119 if levels != self.levels: 114 if levels != self.levels:
120 self.levels = levels 115 self.levels = levels
121 self.tkRedraw() 116 self.updateGL()
122 117
118
119 ## def mousePressEvent(self, event):
120 ## self.lastPos = QtCore.QPoint(event.pos())
121
122 ## def mouseMoveEvent(self, event):
123 ## dx = event.x() - self.lastPos.x()
124 ## dy = event.y() - self.lastPos.y()
125 ## rot = (.25*dy, .25*dx, 0)
126 ## if event.buttons() & QtCore.Qt.LeftButton:
127 ## self.emit(QtCore.SIGNAL("rotationChanged"), rot)
128 ## elif event.buttons() & QtCore.Qt.MidButton:
129 ## self.emit(QtCore.SIGNAL("camDistChanged"), .01*dy)
130
131 ## self.lastPos = QtCore.QPoint(event.pos())
132
123 def main(): 133 def main():
124 root = tk.Frame() 134 root = tk.Frame()
125 root.pack(expand=True, fill='both') 135 root.pack(expand=True, fill='both')
126 QuitButton = tk.Button(root, {'text':'Quit'}) 136 QuitButton = tk.Button(root, {'text':'Quit'})
127 QuitButton.bind('<ButtonRelease-1>', sys.exit) 137 QuitButton.bind('<ButtonRelease-1>', sys.exit)