Mercurial > code > home > repos > light9
comparison lightsim/openglsim.py @ 346:a08882a05d29
openglsim refactor. now dims a few lights
author | drewp@bigasterisk.com |
---|---|
date | Sat, 09 Jun 2007 07:09:21 +0000 |
parents | 839545f174d3 |
children | 941cfe1e1691 |
comparison
equal
deleted
inserted
replaced
345:839545f174d3 | 346:a08882a05d29 |
---|---|
1 #!/usr/bin/python2.4 | |
1 # see http://www.sgi.com/software/opengl/advanced97/notes/node57.html for accum notes | 2 # see http://www.sgi.com/software/opengl/advanced97/notes/node57.html for accum notes |
2 | 3 |
4 from __future__ import division | |
3 import sys, time | 5 import sys, time |
4 import numarray as num | 6 import numarray as num |
5 | |
6 | |
7 sys.path.append("/usr/lib/python2.4/site-packages/OpenGL/Tk/linux2-tk8.4") | |
8 from OpenGL.GL import * | |
9 import Togl | |
10 import Tkinter as tk | 7 import Tkinter as tk |
11 | |
12 import Image | 8 import Image |
13 | 9 |
14 def drawWithAlpha(imgString, w, h, alpha): | 10 try: |
15 """without opengl extensions""" | 11 from OpenGL import Tk as Togl |
16 t = time.time() | 12 from OpenGL.GL import * |
17 ar = num.reshape(num.fromstring(imgString, dtype='uint8'), | 13 except ImportError: |
18 (w * h, 4)) | 14 sys.path.append("/usr/lib/python2.4/site-packages/OpenGL/Tk/linux2-tk8.4") |
19 print " tonum", time.time() - t | 15 from OpenGL.GL import * |
20 ar[:,3] *= alpha | 16 import Togl |
21 | |
22 print " scl", time.time() - t | |
23 glDrawPixels(w, h, | |
24 GL_RGBA, GL_UNSIGNED_BYTE, ar.tostring()) | |
25 print " draw", time.time() - t | |
26 | 17 |
27 | 18 |
28 def drawWithAlpha(imgString, w, h, alpha): | 19 def xxxdrawWithAlpha(imgString, w, h, alpha): |
20 # this one should be faster because GL does the alpha adjust, but | |
21 # i don't think it works yet | |
22 | |
29 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) | 23 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) |
30 | 24 |
31 glClear(GL_COLOR_BUFFER_BIT) | 25 glClear(GL_COLOR_BUFFER_BIT) |
32 #glBlendColor(1, 1, 1, mag) # needs ARB_imaging | 26 #glBlendColor(1, 1, 1, mag) # needs ARB_imaging |
33 glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, imgString) | 27 glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, imgString) |
28 | |
29 class Surface(Togl.Opengl): | |
30 width = 512 | |
31 height = 270 | |
32 imgRescaleTo = 100 | |
33 def __init__(self, master, filenames): | |
34 Togl.Opengl.__init__(self, master=master, width = self.width, | |
35 height = self.height, double = True, depth = 0) | |
36 | |
37 self.levels = {} # filename : brightness | |
38 | |
39 self.image = {} # filename : imgstr | |
40 for filename in filenames: | |
41 im = Image.open(filename) | |
42 if self.imgRescaleTo: | |
43 im.thumbnail((self.imgRescaleTo, self.imgRescaleTo)) | |
44 im = im.transpose(Image.FLIP_TOP_BOTTOM) | |
45 self.imageWidth = im.size[0] | |
46 self.imageHeight = im.size[1] | |
47 self.image[filename] = im.convert("RGBA").tostring() | |
48 | |
49 self.set_centerpoint(0, 0, 0) | |
50 | |
51 glDisable(GL_CULL_FACE) | |
52 glShadeModel(GL_FLAT) | |
53 print 'GL_ARB_imaging', 'GL_ARB_imaging' in glGetString(GL_EXTENSIONS) | |
54 import OpenGL | |
55 print OpenGL.__version__ | |
56 | |
57 self.bind("<Configure>", self.configure) | |
58 | |
59 def configure(self, ev): | |
60 # import pdb; pdb.set_trace() | |
61 self.width, self.height = ev.width, ev.height | |
62 | |
63 def drawWithAlpha(self, imgString, w, h, alpha): | |
64 """without opengl extensions""" | |
65 if alpha == 0: | |
66 return | |
67 t = time.time() | |
68 ar = num.reshape(num.fromstring(imgString, dtype='uint8'), | |
69 (w * h, 4)) | |
70 #print " tonum", time.time() - t | |
71 if alpha != 1: | |
72 ar[:,3] *= alpha | |
73 | |
74 #print " scl", time.time() - t | |
75 glPixelZoom(self.width / w, self.height / h) | |
76 glDrawPixels(w, h, | |
77 GL_RGBA, GL_UNSIGNED_BYTE, ar.tostring()) | |
78 #print " draw", time.time() - t | |
79 | |
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() | |
34 | 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() | |
35 | 91 |
36 class Surface: | 92 # drawing to glAccum might be good |
37 def Display(self, event=None): | 93 for filename, mag in self.levels.items(): |
38 assert 'GL_ARB_imaging' in glGetString(GL_EXTENSIONS).split() | 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): | |
99 self.levels = levels | |
100 self.tkRedraw() | |
101 | |
102 def main(): | |
103 root = tk.Frame() | |
104 root.pack(expand=True, fill='both') | |
105 QuitButton = tk.Button(root, {'text':'Quit'}) | |
106 QuitButton.bind('<ButtonRelease-1>', sys.exit) | |
107 QuitButton.pack() | |
108 | |
109 filenames=['skyline/bg.png', | |
110 'skyline/cyc-lo-red.png', | |
111 'skyline/cyc-lo-grn.png', | |
112 ] | |
39 | 113 |
40 glClearColor(0.0, 0.0, 0.0, 0) | 114 scales = {} # filename : scale |
41 glClear( GL_COLOR_BUFFER_BIT |GL_ACCUM_BUFFER_BIT) | 115 for f in filenames: |
42 glEnable(GL_BLEND) | 116 scales[f] = tk.Scale( |
43 glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA) | 117 root, label=f, from_=0, to=1, res=.05, orient='horiz', |
44 l=glGenLists(1) | 118 command=lambda *args: ogl.newLevels( |
45 glNewList(l,GL_COMPILE) | 119 levels=dict([(f, s.get()) for f,s in scales.items()]))) |
46 glEndList() | 120 scales[f].pack() |
121 ogl = Surface(root, filenames) | |
122 ogl.pack(side='top', expand=True, fill='both') | |
47 | 123 |
48 # glDrawBuffer(GL_BACK) | 124 ogl.mainloop() |
49 | |
50 for x, img in enumerate(self.image): | |
51 mag = self.scales[x].get() | |
52 print "pic %i at %f" % (x,mag) | |
53 drawWithAlpha(img, self.imageWidth, self.imageHeight, mag) | |
54 | |
55 | |
56 ## if x==0: | |
57 ## glAccum(GL_LOAD,mag) | |
58 ## else: | |
59 ## glAccum(GL_ACCUM,mag) | |
60 | |
61 # glAccum(GL_ADD,self.x) | |
62 self.x=(self.x+.1)%2.0 | |
63 ## glAccum(GL_RETURN,1) | |
64 | |
65 def SetupWindow(self): | |
66 self.OglFrame = tk.Frame() | |
67 self.OglFrame.pack(side = 'top',fill='both',expand=1) | |
68 self.QuitButton = tk.Button(self.OglFrame, {'text':'Quit'}) | |
69 self.QuitButton.bind('<ButtonRelease-1>', sys.exit) | |
70 self.QuitButton.pack({'side':'top'}) | |
71 | |
72 | |
73 def SetupOpenGL(self): | |
74 self.ogl = Togl.Opengl(master=self.OglFrame, width = 512, height = 270, double = 1, depth = 0) | |
75 self.ogl.pack(side = 'top', expand = 1, fill = 'both') | |
76 self.ogl.set_centerpoint(0, 0, 0) | |
77 self.ogl.redraw = self.Display | |
78 | |
79 for x in range(0,2): | |
80 self.scales[x] = Scale(self.OglFrame,label="s%i"%x,from_=0,to=1,res=.05,orient='horiz',command=self.ogl.tkRedraw) | |
81 self.scales[x].pack() | |
82 | |
83 | |
84 def __init__(self): | |
85 self.x=0 | |
86 self.scales=[None,None] | |
87 | |
88 self.SetupWindow() | |
89 | |
90 self.image=[] | |
91 for filename in ('skyline/bg.png', 'skyline/cyc-lo-red.png'): | |
92 im = Image.open(filename) | |
93 #im.thumbnail((200, 200)) | |
94 self.imageWidth = im.size[0] | |
95 self.imageHeight = im.size[1] | |
96 self.image.append(im.convert("RGBA").tostring())#"raw", "RGB", 0, -1)) | |
97 print self.imageWidth, self.imageHeight, self.imageWidth * self.imageHeight*4, len(self.image) | |
98 | |
99 self.SetupOpenGL() | |
100 | |
101 glDisable(GL_CULL_FACE) | |
102 # glEnable(GL_DEPTH_TEST) | |
103 # glEnable(GL_NORMALIZE) | |
104 glShadeModel(GL_FLAT) | |
105 print 'GL_ARB_imaging', 'GL_ARB_imaging' in glGetString(GL_EXTENSIONS) | |
106 import OpenGL | |
107 print OpenGL.__version__ | |
108 | |
109 self.ogl.tkRedraw() | |
110 self.ogl.mainloop() | |
111 | 125 |
112 if __name__ == '__main__': | 126 if __name__ == '__main__': |
113 Surface() | 127 main() |
114 | 128 |
115 demo = Surface | 129 demo = Surface |