Changeset - a08882a05d29
[Not reviewed]
default
0 1 0
drewp@bigasterisk.com - 18 years ago 2007-06-09 07:09:21
drewp@bigasterisk.com
openglsim refactor. now dims a few lights
1 file changed with 96 insertions and 82 deletions:
0 comments (0 inline, 0 general)
lightsim/openglsim.py
Show inline comments
 
#!/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
 

	
 

	
 
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
 
import Tkinter as tk
 

	
 
import Image
 

	
 
def drawWithAlpha(imgString, w, h, alpha):
 
    """without opengl extensions"""
 
    t = time.time()
 
    ar = num.reshape(num.fromstring(imgString, dtype='uint8'),
 
                     (w * h, 4))
 
    print "  tonum", time.time() - t
 
    ar[:,3] *= alpha
 

	
 
    print "  scl", time.time() - t
 
    glDrawPixels(w, h,
 
                 GL_RGBA, GL_UNSIGNED_BYTE, ar.tostring())
 
    print "  draw", time.time() - t
 
  
 

	
 
def drawWithAlpha(imgString, w, h, alpha):
 
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):
 
    width = 512
 
    height = 270
 
    imgRescaleTo = 100
 
    def __init__(self, master, filenames):
 
        Togl.Opengl.__init__(self, master=master, width = self.width,
 
                             height = self.height, double = True, depth = 0)
 

	
 
class Surface:
 
  def Display(self, event=None):
 
        self.levels = {} # filename : brightness
 
  
 
        self.image = {} # filename : imgstr
 
        for filename in filenames:
 
            im = Image.open(filename)
 
            if self.imgRescaleTo:
 
                im.thumbnail((self.imgRescaleTo, self.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):
 
#        import pdb; pdb.set_trace()
 
        self.width, self.height = ev.width, ev.height
 
  
 
    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
 
        glPixelZoom(self.width / w, self.height / h)
 
        glDrawPixels(w, h,
 
                     GL_RGBA, GL_UNSIGNED_BYTE, ar.tostring())
 
        #print "  draw", time.time() - t
 

	
 
    def redraw(self, event=None):
 
        """you set self.levels to dict and call tkRedraw"""
 
    assert 'GL_ARB_imaging' in glGetString(GL_EXTENSIONS).split()
 
    
 
    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_DST_ALPHA)
 
    l=glGenLists(1)
 
    glNewList(l,GL_COMPILE)
 
    glEndList()
 

	
 
#    glDrawBuffer(GL_BACK)
 
                
 
    for x, img in enumerate(self.image):
 
      mag = self.scales[x].get()
 
      print "pic %i at %f" % (x,mag)
 
      drawWithAlpha(img, self.imageWidth, self.imageHeight, mag)
 

	
 
#        l=glGenLists(1)
 
#        glNewList(l,GL_COMPILE)
 
#        glEndList()
 

	
 
##       if x==0:
 
##         glAccum(GL_LOAD,mag)
 
##       else:
 
##         glAccum(GL_ACCUM,mag)
 
        # drawing to glAccum might be good
 
        for filename, mag in self.levels.items():
 
            #print "pic %s at %f" % (filename, mag)
 
            self.drawWithAlpha(self.image[filename],
 
                               self.imageWidth, self.imageHeight, mag)
 

	
 
      # glAccum(GL_ADD,self.x)
 
      self.x=(self.x+.1)%2.0
 
##       glAccum(GL_RETURN,1)
 

	
 
  def SetupWindow(self):
 
    self.OglFrame = tk.Frame()
 
    self.OglFrame.pack(side = 'top',fill='both',expand=1)
 
    self.QuitButton = tk.Button(self.OglFrame, {'text':'Quit'})
 
    self.QuitButton.bind('<ButtonRelease-1>', sys.exit)
 
    self.QuitButton.pack({'side':'top'})
 
  
 
    def newLevels(self, event=None, levels=None):
 
        self.levels = levels
 
        self.tkRedraw()
 
  
 
  def SetupOpenGL(self):
 
    self.ogl = Togl.Opengl(master=self.OglFrame, width = 512, height = 270, double = 1, depth = 0)
 
    self.ogl.pack(side = 'top', expand = 1, fill = 'both')
 
    self.ogl.set_centerpoint(0, 0, 0)
 
    self.ogl.redraw = self.Display
 
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()
 
  
 
    for x in range(0,2):
 
      self.scales[x] = Scale(self.OglFrame,label="s%i"%x,from_=0,to=1,res=.05,orient='horiz',command=self.ogl.tkRedraw)
 
      self.scales[x].pack()
 
  
 
  
 
  def __init__(self):
 
    self.x=0
 
    self.scales=[None,None]
 
  
 
    self.SetupWindow()
 
    filenames=['skyline/bg.png',
 
               'skyline/cyc-lo-red.png',
 
               'skyline/cyc-lo-grn.png',
 
               ]
 
  
 
    self.image=[]
 
    for filename in ('skyline/bg.png', 'skyline/cyc-lo-red.png'):
 
      im = Image.open(filename)
 
      #im.thumbnail((200, 200))
 
      self.imageWidth = im.size[0]
 
      self.imageHeight = im.size[1]
 
      self.image.append(im.convert("RGBA").tostring())#"raw", "RGB", 0, -1))
 
      print self.imageWidth, self.imageHeight, self.imageWidth * self.imageHeight*4, len(self.image)
 
  
 
    self.SetupOpenGL()
 
    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')
 
  
 
    glDisable(GL_CULL_FACE)
 
    #         glEnable(GL_DEPTH_TEST)
 
    #         glEnable(GL_NORMALIZE)
 
    glShadeModel(GL_FLAT)
 
    print 'GL_ARB_imaging', 'GL_ARB_imaging' in glGetString(GL_EXTENSIONS)
 
    import OpenGL
 
    print OpenGL.__version__
 
  
 
    self.ogl.tkRedraw()
 
    self.ogl.mainloop()
 
    ogl.mainloop()
 

	
 
if __name__ == '__main__':
 
  Surface()
 
    main()
 
                
 
demo = Surface
0 comments (0 inline, 0 general)