Created
December 19, 2010 17:00
-
-
Save amiller/747477 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
sys.path += ['..'] | |
import cv | |
import numpy as np | |
from visuals.pykinectwindow import Window | |
from OpenGL.GL import * | |
from OpenGL.GL.ARB.vertex_buffer_object import * | |
import calibkinect | |
# I probably need more help with these! Which graphics cards | |
# support OpenGL2.0 textures and which support the earlier ARB rect? | |
try: | |
TEXTURE_TARGET = GL_TEXTURE_RECTANGLE | |
except: | |
TEXTURE_TARGET = GL_TEXTURE_RECTANGLE_ARB | |
if not 'window' in globals(): | |
window = Window() | |
# Always create fresh textures when we run the script | |
def create_textures(): | |
global textures | |
if 'textures' in globals(): glDeleteTextures(textures) | |
textures = glGenTextures(3) | |
# Read in the mask image, separate into channels | |
png = np.asarray(cv.LoadImageM('mask.png')) | |
h,w = png.shape[:2] | |
global R,G,B | |
R,G,B = np.rollaxis(png,2)>120 | |
from scipy.ndimage import uniform_filter | |
m1 = np.dstack(4*[~R|~B]).astype('f') # Start with back layer | |
m1 = uniform_filter(m1, 45) # Blur it to get halo effect | |
m1 *= [0.6,0.6,1.2,5.0] # Set the base color | |
m1 = m1.clip(0,1) # Constrain | |
# Add in the solid button backgrounds | |
m1[~G,:] = [0.1,0.1,0.5,1.0] | |
m1[R&~G,:] = [0.5,0.5,0.8,1.0] | |
# Make the mask for the cursor strip | |
m2 = np.dstack(4*[B&~R]).astype('f') | |
# Make the mask for the button icons | |
m3 = np.dstack(4*[R&~G]).astype('f') | |
glBindTexture(TEXTURE_TARGET, textures[0]) | |
glTexImage2D(TEXTURE_TARGET,0,GL_RGBA,w,h,0,GL_RGBA,GL_FLOAT,m1) | |
glBindTexture(TEXTURE_TARGET, textures[1]) | |
glTexImage2D(TEXTURE_TARGET,0,GL_RGBA,w,h,0,GL_RGBA,GL_FLOAT,m2) | |
glBindTexture(TEXTURE_TARGET, textures[2]) | |
glTexImage2D(TEXTURE_TARGET,0,GL_RGBA,w,h,0,GL_RGBA,GL_FLOAT,m3) | |
create_textures() | |
cursorpos = 320 | |
cursorpick = None | |
def init(): | |
pass | |
init() | |
@window.event | |
def on_draw(): | |
glClearColor(0,0,0,0) | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) | |
glMatrixMode(GL_PROJECTION) | |
glLoadIdentity() | |
glOrtho(0,640,0,480,-1,1) | |
glMatrixMode(GL_MODELVIEW) | |
glLoadIdentity() | |
# Draw the glowy background and buttons | |
glEnable (GL_BLEND); | |
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
glEnable(TEXTURE_TARGET) | |
glBindTexture(TEXTURE_TARGET, textures[0]) | |
glColor(1,1,1) | |
glBegin(GL_QUADS) | |
glTexCoord(0,240); glVertex(0,0); | |
glTexCoord(0,0); glVertex(0,240); | |
glTexCoord(640,0); glVertex(640,240); | |
glTexCoord(640,240); glVertex(640,0) | |
glEnd() | |
# Draw the cursor only where the texture is | |
x = cursorpos | |
glBindTexture(TEXTURE_TARGET, textures[1]) | |
glBegin(GL_QUAD_STRIP) | |
glColor(0,0,0.6); | |
glTexCoord(x-50,240); glVertex(x-50,0); glTexCoord(x-50,0); glVertex(x-50,240) | |
glColor(1,1,1); | |
glTexCoord(x ,240); glVertex(x ,0); glTexCoord(x ,0); glVertex(x ,240) | |
glColor(0,0,0.6); | |
glTexCoord(x+50,240); glVertex(x+50,0); glTexCoord(x+50,0); glVertex(x+50,240) | |
glEnd() | |
glDisable(GL_STENCIL_TEST) | |
if not cursorpick is None: | |
glEnable(TEXTURE_TARGET) | |
glBindTexture(TEXTURE_TARGET, textures[2]) | |
glColor(1,1,1) | |
x = 100*(1+cursorpick) | |
glBegin(GL_QUADS) | |
glTexCoord(x-50,240); glVertex(x-50,0); | |
glTexCoord(x-50,0); glVertex(x-50,240); | |
glTexCoord(x+50,0); glVertex(x+50,240); | |
glTexCoord(x+50,240); glVertex(x+50,0) | |
glEnd() | |
@window.eventx | |
def EVT_MOTION(event): | |
# Control the sliding cursor using the mouse X coordinate | |
x,_ = event.Position | |
global cursorpos, cursorpick | |
cursorpos = x | |
pick = np.round(float(x)/100) | |
dist = np.abs(x-pick*100) | |
cursorpick = pick-1 if dist<30 else None | |
window.Refresh() | |
import freenect | |
import scipy.ndimage as nd | |
@window.eventx | |
def EVT_IDLE(event): | |
global depth, cursorpos, cursorpick | |
depth,_ = freenect.sync_get_depth() | |
thresh = depth[::2,::2] < 600 | |
if thresh.sum() > 30: | |
v,u = np.mgrid[:480/2,:640/2] | |
x = cursorpos = cursorpos*0.8 + (640 - u[thresh].mean()*2)*0.2 | |
pick = np.round(float(x)/100) | |
dist = np.abs(x-pick*100) | |
cursorpick = pick-1 if dist<30 else None | |
else: | |
cursorpos = 0 | |
cursorpick = None | |
event.RequestMore() | |
window.Refresh() | |
window.Refresh() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment