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