Changeset - f866d4dec57b
[Not reviewed]
default
0 1 7
drewp@bigasterisk.com - 18 years ago 2007-05-29 18:20:15
drewp@bigasterisk.com
openglsim now mixes images
6 files changed:
0 comments (0 inline, 0 general)
lightsim/openglsim.py
Show inline comments
 
# see http://www.sgi.com/software/opengl/advanced97/notes/node57.html for accum notes
 

	
 
import sys
 
from Image import *
 
import sys, time
 
import numpy as num
 
from OpenGL.GL import *
 
from OpenGL.Tk import *
 
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
 
  
 

	
 
class Surface:
 
  def Display(self, event=None):
 
    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_ONE_MINUS_SRC_ALPHA)
 
    glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA)
 
    l=glGenLists(1)
 
    glNewList(l,GL_COMPILE)
 
    glEndList()
 

	
 
#    glDrawBuffer(GL_BACK)
 
                
 
    for x in range(1,2):
 

	
 
    for x, img in enumerate(self.image):
 
      mag = self.scales[x].get()
 
      print "pic %i at %f" % (x,mag)
 
      glClear(GL_COLOR_BUFFER_BIT)
 
      glDrawPixels(self.imageWidth, self.imageHeight, GL_RGB, GL_UNSIGNED_BYTE, self.image[x])
 
#      glClear(GL_COLOR_BUFFER_BIT)
 
      #glBlendColor(1, 1, 1, mag) # needs ARB_imaging
 
      drawWithAlpha(img, self.imageWidth, self.imageHeight, mag)
 

	
 

	
 
      if x==0:
 
        glAccum(GL_LOAD,mag)
 
      else:
 
        glAccum(GL_ACCUM,mag)
 
##       if x==0:
 
##         glAccum(GL_LOAD,mag)
 
##       else:
 
##         glAccum(GL_ACCUM,mag)
 

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

	
 
  def SetupWindow(self):
 
    self.OglFrame = Frame()
 
    self.OglFrame.pack(side = 'top',fill='both',expand=1)
 
    self.QuitButton = Button(self.OglFrame, {'text':'Quit'})
 
    self.QuitButton.bind('<ButtonRelease-1>', sys.exit)
 
    self.QuitButton.pack({'side':'top'})
 
  
 
  
 
  def SetupOpenGL(self):
 
    self.ogl = Opengl(master=self.OglFrame, width = 270, height = 270, double = 1, depth = 0)
 
    self.ogl = 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
 
  
 
    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()
 
  
 
    self.image=[]
 
    for filename in ('pic1.ppm','pic2.ppm'):
 
      im = open(filename)
 
    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.tostring("raw", "RGB", 0, -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()
 
  
 
    glDisable(GL_CULL_FACE)
 
    #         glEnable(GL_DEPTH_TEST)
 
    #         glEnable(GL_NORMALIZE)
 
    glShadeModel(GL_FLAT)
 
  
 
    self.ogl.tkRedraw()
 
    self.ogl.mainloop()
 

	
 
if __name__ == '__main__':
 
  Surface()
 
                
 
demo = Surface
lightsim/sim2
Show inline comments
 
new file 100644
 
#!/usr/bin/python2.4
 
from __future__ import division
 
import sys, time, math
 
import Image, ImageFilter
 
import Numeric as num
 
from OpenGL import GL, GLUT, GLU
 
from OpenGL.GL import *
 

	
 
def openglSetup(width=512, height=256):
 
    global cardList
 

	
 
    glClearColor (0.0, 0.0, 0.0, 0.0)
 
    glShadeModel (GL_SMOOTH)
 
    glEnable(GL_COLOR_MATERIAL)
 

	
 
    glLightfv(GL_LIGHT0, GL_AMBIENT, [0.0, 0.0, 0.0, 1.0])
 
    glLightfv(GL_LIGHT0, GL_DIFFUSE, [1.0, 1.0, 1.0, 1.0])
 
    glLightfv(GL_LIGHT0, GL_POSITION, [0.0, 3.0, 3.0, 0.0])
 
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, [0.2, 0.2, 0.2, 1.0])
 
    glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, [0])
 

	
 
    #glFrontFace(GL_CW)
 
    glEnable(GL_LIGHTING)
 
    glEnable(GL_LIGHT0)
 
    #glEnable(GL_AUTO_NORMAL)
 
    #glEnable(GL_NORMALIZE)
 
    glEnable(GL_DEPTH_TEST) 
 

	
 

	
 
    glViewport (0, 0, width, height)
 
    glMatrixMode (GL_PROJECTION)
 
    glLoadIdentity ()
 
    glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0)
 
    glMatrixMode (GL_MODELVIEW)
 

	
 
    cardList = glGenLists(1)
 
    glNewList(cardList, GL_COMPILE)
 
    glColor3f(1,1,1)
 
    glBegin(GL_QUADS)
 
    glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 0.0,  1.0)
 
    glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 0.0,  1.0)
 
    glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, 1.0,  1.0)
 
    glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, 1.0,  1.0)
 
    glEnd()
 
    glEndList()
 

	
 

	
 

	
 
def imageCard(img):
 
    """card facing +Z from -1<x<1 -1<y<1"""
 
    textureData = img.convert(mode="RGBA").tostring()
 

	
 
    glBindTexture(GL_TEXTURE_2D, 0)
 
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA,
 
                  512, #multiImage.size()[0],
 
                  256, #multiImage.size()[1],
 
                  0,
 
                  GL_RGBA, GL_UNSIGNED_BYTE, textureData)
 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
 

	
 
    glCallList(cardList)
 

	
 
def setupDraw():
 
    """per-frame setup"""
 
    glClearColor(0.0, 0.0, 0.0, 0.0)
 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
 
    glLoadIdentity ()
 
    GLU.gluLookAt (0.5, 0.5, 8.0,
 
                   0.5, 0.5, 0.0,
 
                   0.0, 1.0, 0.0)
 

	
 
    glDisable(GL_TEXTURE_2D)
 

	
 
correctFrameStart = None
 
def drawFrame():
 
    global correctFrameStart
 
    t1 = time.time()
 
    setupDraw()
 
    
 
    matching = False
 
    glPushMatrix()
 
    try:
 
        glTranslatef(1, -.9, 3.2)
 
        # flip to compensate for the camera facing the user
 
        glScalef(-1, 1, 1)
 
        
 
        glDisable(GL_LIGHTING)
 
        glEnable(GL_TEXTURE_2D)
 

	
 
#        imageCard(Image.open("skyline/bg.png"))
 
        imageCard(Image.open("skyline/cyc-lo-red.png"))
 

	
 
##         if grab.lastFrame:
 
##             imageCard(grab.lastFrame)
 
##             glEnable(GL_LIGHTING)
 
##             glDisable(GL_TEXTURE_2D)
 
##             if grab.seenPose:
 
##                 for colorName, pos in grab.seenPose.pos.items():
 
##                     cube(color=(x / 255 for x in vision.colors[colorName]),
 
##                          center=((1-pos[0]) * 2 - 1, pos[1] * 2 - 1, 1),
 
##                          side=.1, wire=False)
 
##                 matching = game.pose.compare(grab.seenPose)
 
        glEnable(GL_LIGHTING)
 
        glDisable(GL_TEXTURE_2D)
 
    finally:
 
        glPopMatrix()
 

	
 
    
 
    glFlush()
 
    #print "draw", time.time() - t1
 

	
 
GLUT.glutInit(sys.argv)
 
window = GLUT.glutCreateWindow("sim2")
 
openglSetup()
 
GLUT.glutDisplayFunc(drawFrame)
 
def menu(arg):
 
    print "hi", arg
 
GLUT.glutCreateMenu(menu)
 
GLUT.glutAddMenuEntry("hi", 1)
 
GLUT.glutAttachMenu(GLUT.GLUT_RIGHT_BUTTON)
 

	
 
GLUT.glutMainLoop()
lightsim/skyline/bg.png
Show inline comments
 
new file 100644
 
binary diff not shown
Show images
lightsim/skyline/cyc-lo-blu.png
Show inline comments
 
new file 100644
 
binary diff not shown
Show images
lightsim/skyline/cyc-lo-grn.png
Show inline comments
 
new file 100644
 
binary diff not shown
Show images
lightsim/skyline/cyc-lo-or.png
Show inline comments
 
new file 100644
 
binary diff not shown
Show images

Changeset was too big and was cut off... Show full diff anyway

0 comments (0 inline, 0 general)