Last active
December 29, 2015 18:18
-
-
Save mandel59/7709562 to your computer and use it in GitHub Desktop.
春学期の実験にはPixilangを使った
This file contains 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
include "kinect_control.pixi" | |
N_PLAYERS = 6 | |
near_mode = 0 | |
if near_mode | |
{ | |
depth_flag = NUI_IMAGE_STREAM_FLAG_ENABLE_NEAR_MODE | |
skeleton_flag | |
= NUI_SKELETON_TRACKING_FLAG_ENABLE_SEATED_SUPPORT | |
| NUI_SKELETON_TRACKING_FLAG_ENABLE_IN_NEAR_RANGE | |
} | |
else | |
{ | |
depth_flag = 0 | |
skeleton_flag = 0 | |
} | |
fn init() | |
{ | |
width = dlcall( kc, kc.width, 0 ) | |
height = dlcall( kc, kc.height, 0 ) | |
scale_factor = WINDOW_XSIZE / width | |
color_image = new( width, height ) | |
depth_image = new( width, height, INT32 ) | |
player = new( width, height, INT32 ) | |
coordX = new( width, height, INT32 ) | |
coordY = new( width, height, INT32 ) | |
skeletonTracked = new( NUI_SKELETON_COUNT, INT ) | |
positionX = new( N_PLAYERS, FLOAT32 ) | |
positionY = new( N_PLAYERS, FLOAT32 ) | |
positionZ = new( N_PLAYERS, FLOAT32 ) | |
positionCX = new( N_PLAYERS, INT32 ) | |
positionCY = new( N_PLAYERS, INT32 ) | |
positionD = new( N_PLAYERS, INT16 ) | |
player_rot = new( N_PLAYERS, INT ) | |
player_rot_t = new( N_PLAYERS, INT ) | |
skeletonX = new( NUI_SKELETON_POSITION_COUNT, NUI_SKELETON_COUNT, FLOAT32 ) | |
skeletonY = new( NUI_SKELETON_POSITION_COUNT, NUI_SKELETON_COUNT, FLOAT32 ) | |
skeletonZ = new( NUI_SKELETON_POSITION_COUNT, NUI_SKELETON_COUNT, FLOAT32 ) | |
skeleton_colorX = new( NUI_SKELETON_POSITION_COUNT, NUI_SKELETON_COUNT, INT32 ) | |
skeleton_colorY = new( NUI_SKELETON_POSITION_COUNT, NUI_SKELETON_COUNT, INT32 ) | |
skeleton_depth = new( NUI_SKELETON_POSITION_COUNT, NUI_SKELETON_COUNT, INT16 ) | |
color_work_image = new( width, height ) | |
background_image = new( width, height ) | |
background_mask_image = new( width, height ) | |
mask_color_image = new( width, height ) | |
mask_image = new( width, height, INT8 ) | |
zbuf = new( WINDOW_XSIZE, WINDOW_YSIZE, INT32 ) | |
set_zbuf( zbuf ) | |
visible = new( N_PLAYERS, INT ) | |
player_region = new( N_PLAYERS, INT ) | |
player_mask = new( N_PLAYERS, INT ) | |
$i = 0 | |
while $i < N_PLAYERS { | |
player_region[ $i ] = new( width, height ) | |
player_mask[ $i ] = new( width, height, INT8 ) | |
set_alpha( player_region[ $i ], player_mask[ $i ] ) | |
$i + 1 | |
} | |
state_draw_skeleton = 0 | |
} | |
fn screen_resize() | |
{ | |
resize( get_screen(), WINDOW_XSIZE, WINDOW_YSIZE ) | |
resize( get_zbuf(), WINDOW_XSIZE, WINDOW_YSIZE, INT32 ) | |
scale_factor = WINDOW_XSIZE / width | |
} | |
fn dump( $x ) | |
{ | |
$i = 0 | |
while $i < NUI_SKELETON_POSITION_COUNT { | |
printf("%d %g %g\n", $i, $x[$i], $x[$i]) | |
$i + 1 | |
} | |
} | |
fn get_frame() | |
{ | |
dlcall( kc, kc.color_image, color_image ) | |
dlcall( kc, kc.depth_image, depth_image, player, coordX, coordY ) | |
dlcall( kc, kc.skeleton_frame, 0 ) | |
$i = 0 | |
$p = new( 3, FLOAT32 ) | |
$sx = new( NUI_SKELETON_POSITION_COUNT, FLOAT32 ) | |
$sy = new( NUI_SKELETON_POSITION_COUNT, FLOAT32 ) | |
$sz = new( NUI_SKELETON_POSITION_COUNT, FLOAT32 ) | |
$skeleton_colorX = new( NUI_SKELETON_POSITION_COUNT, INT32 ) | |
$skeleton_colorY = new( NUI_SKELETON_POSITION_COUNT, INT32 ) | |
$skeleton_depth = new( NUI_SKELETON_POSITION_COUNT, INT16 ) | |
while $i < NUI_SKELETON_COUNT | |
{ | |
skeletonTracked[$i] = dlcall( kc, kc.skeleton_tracking_state, $i ) | |
if skeletonTracked[$i] >= 1 | |
{ | |
dlcall( kc, kc.skeleton_position, $i, $p ) | |
positionX[ $i ] = $p[ 0 ] | |
positionY[ $i ] = $p[ 1 ] | |
positionZ[ $i ] = $p[ 2 ] | |
} | |
if skeletonTracked[$i] == 2 | |
{ | |
dlcall( kc, kc.skeleton_positions, $i, $sx, $sy, $sz ) | |
dlcall( kc, kc.transform_skeleton_to_color_image, NUI_SKELETON_POSITION_COUNT, $sx, $sy, $sz, $skeleton_colorX, $skeleton_colorY, $skeleton_depth ) | |
copy( skeletonX, $sx, $i * NUI_SKELETON_POSITION_COUNT, 0, NUI_SKELETON_POSITION_COUNT ) | |
copy( skeletonY, $sy, $i * NUI_SKELETON_POSITION_COUNT, 0, NUI_SKELETON_POSITION_COUNT ) | |
copy( skeletonZ, $sz, $i * NUI_SKELETON_POSITION_COUNT, 0, NUI_SKELETON_POSITION_COUNT ) | |
copy( skeleton_colorX, $skeleton_colorX, $i * NUI_SKELETON_POSITION_COUNT, 0, NUI_SKELETON_POSITION_COUNT ) | |
copy( skeleton_colorY, $skeleton_colorY, $i * NUI_SKELETON_POSITION_COUNT, 0, NUI_SKELETON_POSITION_COUNT ) | |
copy( skeleton_depth, $skeleton_depth, $i * NUI_SKELETON_POSITION_COUNT, 0, NUI_SKELETON_POSITION_COUNT ) | |
} | |
$i + 1 | |
} | |
dlcall( kc, kc.transform_skeleton_to_color_image, N_PLAYERS, positionX, positionY, positionZ, positionCX, positionCY, positionD ) | |
remove( $p ) | |
remove( $sx ) | |
remove( $sy ) | |
remove( $sz ) | |
remove( $skeleton_colorX ) | |
remove( $skeleton_colorY ) | |
remove( $skeleton_depth ) | |
} | |
fn update_mask( $mask, $mask_image, $mask_color_image ) | |
{ | |
clean( $mask_image ) | |
dlcall( kc, kc.player_area, $mask, player, coordX, coordY, $mask_image ) | |
dlcall( kc, kc.util_dilate, $mask_image ) | |
split_rgb( 1, $mask_color_image, $mask_image, $mask_image, $mask_image ) | |
} | |
fn fbox_image( $image, $x, $y, $xsize, $ysize, $color ) | |
{ | |
$width = get_xsize( $image ) | |
$height = get_ysize( $image ) | |
$x_end = $x + $xsize | |
$y_end = $y + $ysize | |
if $x < 0 { $x = 0 } | |
if $y < 0 { $y = 0 } | |
if $x_end >= $width { $x_end = $width } | |
if $y_end >= $height { $y_end = $height } | |
while $y < $y_end { | |
$xx = $x | |
while $xx < $x_end { | |
$image[$xx, $y] = $color | |
$xx + 1 | |
} | |
$y + 1 | |
} | |
} | |
fn draw_boxes_on_face( $image, $color ) | |
{ | |
$i = 0 | |
while $i < NUI_SKELETON_COUNT { | |
if skeletonTracked[$i] == 2 | |
{ | |
$hx = skeleton_colorX[NUI_SKELETON_POSITION_HEAD, $i] | |
$hy = skeleton_colorY[NUI_SKELETON_POSITION_HEAD, $i] | |
$sx = skeleton_colorX[NUI_SKELETON_POSITION_SHOULDER_CENTER, $i] | |
$sy = skeleton_colorY[NUI_SKELETON_POSITION_SHOULDER_CENTER, $i] | |
$dx = $sx - $hx | |
$dy = $sy - $hy | |
$r = floor( sqrt( $dx * $dx + $dy * $dy ) ) | |
$size = 2 * $r | |
$screen = get_screen() | |
fbox_image( $image, $hx - $r, $hy - $r, $size, $size, WHITE ) | |
} | |
$i + 1 | |
} | |
} | |
fn update_background() | |
{ | |
update_mask( 1 | 2 | 4 | 8 | 16 | 32, mask_image, mask_color_image ) | |
draw_boxes_on_face( mask_color_image, WHITE ) | |
$rnd = floor(rand() / 32767 * 12 + 0x20) * #010101 | |
copy( color_work_image, color_image ) | |
op_cn( OP_COLOR_MUL, color_work_image, $rnd ) | |
copy( background_mask_image, background_image ) | |
op_cc( OP_AND, background_mask_image, mask_color_image ) | |
op_cn( OP_COLOR_MUL, background_image, #FFFFFF - $rnd ) | |
op_cc( OP_COLOR_ADD, background_image, color_work_image ) | |
op_cc( OP_COLOR_SUB, background_image, mask_color_image ) | |
op_cc( OP_COLOR_ADD, background_image, background_mask_image ) | |
remove_gl_data( background_image ) | |
} | |
fn update_players() | |
{ | |
$i = 0 | |
while $i < N_PLAYERS { | |
if skeletonTracked[ $i ] == 0 { | |
visible[ $i ] = 1 | |
player_rot[ $i ] = 0 | |
player_rot_t[ $i ] = 0 | |
$i + 1 | |
continue | |
} | |
player_rot[ $i ] / 2 | |
if player_rot_t[ $i ] > 0 | |
{ | |
player_rot_t[ $i ] - 1 | |
} | |
else | |
{ | |
if player_rot[ $i ] == 0 && d_hands( $i ) < 400 { | |
switch_visible( $i ) | |
} | |
} | |
update_mask( 1 << $i, player_mask[ $i ], mask_color_image ) | |
copy( player_region[ $i ], color_image ) | |
remove_gl_data( player_region[ $i ] ) | |
$i + 1 | |
} | |
} | |
fn update_fps() | |
{ | |
fps_s = "" | |
sprintf(fps_s, "%d", FPS) | |
} | |
fn update() | |
{ | |
update_background() | |
update_players() | |
update_fps() | |
} | |
fn draw_skeleton( $color, $skeletonX, $skeletonY ) | |
{ | |
line( | |
$skeletonX[NUI_SKELETON_POSITION_HEAD], $skeletonY[NUI_SKELETON_POSITION_HEAD], | |
$skeletonX[NUI_SKELETON_POSITION_SHOULDER_CENTER], $skeletonY[NUI_SKELETON_POSITION_SHOULDER_CENTER], | |
$color) | |
line( | |
$skeletonX[NUI_SKELETON_POSITION_SHOULDER_CENTER], $skeletonY[NUI_SKELETON_POSITION_SHOULDER_CENTER], | |
$skeletonX[NUI_SKELETON_POSITION_SHOULDER_LEFT], $skeletonY[NUI_SKELETON_POSITION_SHOULDER_LEFT], | |
$color) | |
line( | |
$skeletonX[NUI_SKELETON_POSITION_SHOULDER_LEFT], $skeletonY[NUI_SKELETON_POSITION_SHOULDER_LEFT], | |
$skeletonX[NUI_SKELETON_POSITION_ELBOW_LEFT], $skeletonY[NUI_SKELETON_POSITION_ELBOW_LEFT], | |
$color) | |
line( | |
$skeletonX[NUI_SKELETON_POSITION_ELBOW_LEFT], $skeletonY[NUI_SKELETON_POSITION_ELBOW_LEFT], | |
$skeletonX[NUI_SKELETON_POSITION_WRIST_LEFT], $skeletonY[NUI_SKELETON_POSITION_WRIST_LEFT], | |
$color) | |
line( | |
$skeletonX[NUI_SKELETON_POSITION_WRIST_LEFT], $skeletonY[NUI_SKELETON_POSITION_WRIST_LEFT], | |
$skeletonX[NUI_SKELETON_POSITION_HAND_LEFT], $skeletonY[NUI_SKELETON_POSITION_HAND_LEFT], | |
$color) | |
line( | |
$skeletonX[NUI_SKELETON_POSITION_SHOULDER_CENTER], $skeletonY[NUI_SKELETON_POSITION_SHOULDER_CENTER], | |
$skeletonX[NUI_SKELETON_POSITION_SHOULDER_RIGHT], $skeletonY[NUI_SKELETON_POSITION_SHOULDER_RIGHT], | |
$color) | |
line( | |
$skeletonX[NUI_SKELETON_POSITION_SHOULDER_RIGHT], $skeletonY[NUI_SKELETON_POSITION_SHOULDER_RIGHT], | |
$skeletonX[NUI_SKELETON_POSITION_ELBOW_RIGHT], $skeletonY[NUI_SKELETON_POSITION_ELBOW_RIGHT], | |
$color) | |
line( | |
$skeletonX[NUI_SKELETON_POSITION_ELBOW_RIGHT], $skeletonY[NUI_SKELETON_POSITION_ELBOW_RIGHT], | |
$skeletonX[NUI_SKELETON_POSITION_WRIST_RIGHT], $skeletonY[NUI_SKELETON_POSITION_WRIST_RIGHT], | |
$color) | |
line( | |
$skeletonX[NUI_SKELETON_POSITION_WRIST_RIGHT], $skeletonY[NUI_SKELETON_POSITION_WRIST_RIGHT], | |
$skeletonX[NUI_SKELETON_POSITION_HAND_RIGHT], $skeletonY[NUI_SKELETON_POSITION_HAND_RIGHT], | |
$color) | |
if near_mode { ret } | |
line( | |
$skeletonX[NUI_SKELETON_POSITION_SHOULDER_CENTER], $skeletonY[NUI_SKELETON_POSITION_SHOULDER_CENTER], | |
$skeletonX[NUI_SKELETON_POSITION_SPINE], $skeletonY[NUI_SKELETON_POSITION_SPINE], | |
$color) | |
line( | |
$skeletonX[NUI_SKELETON_POSITION_SPINE], $skeletonY[NUI_SKELETON_POSITION_SPINE], | |
$skeletonX[NUI_SKELETON_POSITION_HIP_CENTER], $skeletonY[NUI_SKELETON_POSITION_HIP_CENTER], | |
$color) | |
line( | |
$skeletonX[NUI_SKELETON_POSITION_HIP_CENTER], $skeletonY[NUI_SKELETON_POSITION_HIP_CENTER], | |
$skeletonX[NUI_SKELETON_POSITION_HIP_LEFT], $skeletonY[NUI_SKELETON_POSITION_HIP_LEFT], | |
$color) | |
line( | |
$skeletonX[NUI_SKELETON_POSITION_HIP_LEFT], $skeletonY[NUI_SKELETON_POSITION_HIP_LEFT], | |
$skeletonX[NUI_SKELETON_POSITION_KNEE_LEFT], $skeletonY[NUI_SKELETON_POSITION_KNEE_LEFT], | |
$color) | |
line( | |
$skeletonX[NUI_SKELETON_POSITION_KNEE_LEFT], $skeletonY[NUI_SKELETON_POSITION_KNEE_LEFT], | |
$skeletonX[NUI_SKELETON_POSITION_ANKLE_LEFT], $skeletonY[NUI_SKELETON_POSITION_ANKLE_LEFT], | |
$color) | |
line( | |
$skeletonX[NUI_SKELETON_POSITION_ANKLE_LEFT], $skeletonY[NUI_SKELETON_POSITION_ANKLE_LEFT], | |
$skeletonX[NUI_SKELETON_POSITION_FOOT_LEFT], $skeletonY[NUI_SKELETON_POSITION_FOOT_LEFT], | |
$color) | |
line( | |
$skeletonX[NUI_SKELETON_POSITION_HIP_CENTER], $skeletonY[NUI_SKELETON_POSITION_HIP_CENTER], | |
$skeletonX[NUI_SKELETON_POSITION_HIP_RIGHT], $skeletonY[NUI_SKELETON_POSITION_HIP_RIGHT], | |
$color) | |
line( | |
$skeletonX[NUI_SKELETON_POSITION_HIP_RIGHT], $skeletonY[NUI_SKELETON_POSITION_HIP_RIGHT], | |
$skeletonX[NUI_SKELETON_POSITION_KNEE_RIGHT], $skeletonY[NUI_SKELETON_POSITION_KNEE_RIGHT], | |
$color) | |
line( | |
$skeletonX[NUI_SKELETON_POSITION_KNEE_RIGHT], $skeletonY[NUI_SKELETON_POSITION_KNEE_RIGHT], | |
$skeletonX[NUI_SKELETON_POSITION_ANKLE_RIGHT], $skeletonY[NUI_SKELETON_POSITION_ANKLE_RIGHT], | |
$color) | |
line( | |
$skeletonX[NUI_SKELETON_POSITION_ANKLE_RIGHT], $skeletonY[NUI_SKELETON_POSITION_ANKLE_RIGHT], | |
$skeletonX[NUI_SKELETON_POSITION_FOOT_RIGHT], $skeletonY[NUI_SKELETON_POSITION_FOOT_RIGHT], | |
$color) | |
} | |
fn draw_skeleton3d( $color, $skeletonX, $skeletonY, $skeletonZ ) | |
{ | |
line3d( | |
$skeletonX[NUI_SKELETON_POSITION_HEAD], $skeletonY[NUI_SKELETON_POSITION_HEAD], $skeletonZ[NUI_SKELETON_POSITION_HEAD], | |
$skeletonX[NUI_SKELETON_POSITION_SHOULDER_CENTER], $skeletonY[NUI_SKELETON_POSITION_SHOULDER_CENTER], $skeletonZ[NUI_SKELETON_POSITION_SHOULDER_CENTER], | |
$color) | |
line3d( | |
$skeletonX[NUI_SKELETON_POSITION_SHOULDER_CENTER], $skeletonY[NUI_SKELETON_POSITION_SHOULDER_CENTER], $skeletonZ[NUI_SKELETON_POSITION_SHOULDER_CENTER], | |
$skeletonX[NUI_SKELETON_POSITION_SHOULDER_LEFT], $skeletonY[NUI_SKELETON_POSITION_SHOULDER_LEFT], $skeletonZ[NUI_SKELETON_POSITION_SHOULDER_LEFT], | |
$color) | |
line3d( | |
$skeletonX[NUI_SKELETON_POSITION_SHOULDER_LEFT], $skeletonY[NUI_SKELETON_POSITION_SHOULDER_LEFT], $skeletonZ[NUI_SKELETON_POSITION_SHOULDER_LEFT], | |
$skeletonX[NUI_SKELETON_POSITION_ELBOW_LEFT], $skeletonY[NUI_SKELETON_POSITION_ELBOW_LEFT], $skeletonZ[NUI_SKELETON_POSITION_ELBOW_LEFT], | |
$color) | |
line3d( | |
$skeletonX[NUI_SKELETON_POSITION_ELBOW_LEFT], $skeletonY[NUI_SKELETON_POSITION_ELBOW_LEFT], $skeletonZ[NUI_SKELETON_POSITION_ELBOW_LEFT], | |
$skeletonX[NUI_SKELETON_POSITION_WRIST_LEFT], $skeletonY[NUI_SKELETON_POSITION_WRIST_LEFT], $skeletonZ[NUI_SKELETON_POSITION_WRIST_LEFT], | |
$color) | |
line3d( | |
$skeletonX[NUI_SKELETON_POSITION_WRIST_LEFT], $skeletonY[NUI_SKELETON_POSITION_WRIST_LEFT], $skeletonZ[NUI_SKELETON_POSITION_WRIST_LEFT], | |
$skeletonX[NUI_SKELETON_POSITION_HAND_LEFT], $skeletonY[NUI_SKELETON_POSITION_HAND_LEFT], $skeletonZ[NUI_SKELETON_POSITION_HAND_LEFT], | |
$color) | |
line3d( | |
$skeletonX[NUI_SKELETON_POSITION_SHOULDER_CENTER], $skeletonY[NUI_SKELETON_POSITION_SHOULDER_CENTER], $skeletonZ[NUI_SKELETON_POSITION_SHOULDER_CENTER], | |
$skeletonX[NUI_SKELETON_POSITION_SHOULDER_RIGHT], $skeletonY[NUI_SKELETON_POSITION_SHOULDER_RIGHT], $skeletonZ[NUI_SKELETON_POSITION_SHOULDER_RIGHT], | |
$color) | |
line3d( | |
$skeletonX[NUI_SKELETON_POSITION_SHOULDER_RIGHT], $skeletonY[NUI_SKELETON_POSITION_SHOULDER_RIGHT], $skeletonZ[NUI_SKELETON_POSITION_SHOULDER_RIGHT], | |
$skeletonX[NUI_SKELETON_POSITION_ELBOW_RIGHT], $skeletonY[NUI_SKELETON_POSITION_ELBOW_RIGHT], $skeletonZ[NUI_SKELETON_POSITION_ELBOW_RIGHT], | |
$color) | |
line3d( | |
$skeletonX[NUI_SKELETON_POSITION_ELBOW_RIGHT], $skeletonY[NUI_SKELETON_POSITION_ELBOW_RIGHT], $skeletonZ[NUI_SKELETON_POSITION_ELBOW_RIGHT], | |
$skeletonX[NUI_SKELETON_POSITION_WRIST_RIGHT], $skeletonY[NUI_SKELETON_POSITION_WRIST_RIGHT], $skeletonZ[NUI_SKELETON_POSITION_WRIST_RIGHT], | |
$color) | |
line3d( | |
$skeletonX[NUI_SKELETON_POSITION_WRIST_RIGHT], $skeletonY[NUI_SKELETON_POSITION_WRIST_RIGHT], $skeletonZ[NUI_SKELETON_POSITION_WRIST_RIGHT], | |
$skeletonX[NUI_SKELETON_POSITION_HAND_RIGHT], $skeletonY[NUI_SKELETON_POSITION_HAND_RIGHT], $skeletonZ[NUI_SKELETON_POSITION_HAND_RIGHT], | |
$color) | |
if near_mode { ret } | |
line3d( | |
$skeletonX[NUI_SKELETON_POSITION_SHOULDER_CENTER], $skeletonY[NUI_SKELETON_POSITION_SHOULDER_CENTER], $skeletonZ[NUI_SKELETON_POSITION_SHOULDER_CENTER], | |
$skeletonX[NUI_SKELETON_POSITION_SPINE], $skeletonY[NUI_SKELETON_POSITION_SPINE], $skeletonZ[NUI_SKELETON_POSITION_SPINE], | |
$color) | |
line3d( | |
$skeletonX[NUI_SKELETON_POSITION_SPINE], $skeletonY[NUI_SKELETON_POSITION_SPINE], $skeletonZ[NUI_SKELETON_POSITION_SPINE], | |
$skeletonX[NUI_SKELETON_POSITION_HIP_CENTER], $skeletonY[NUI_SKELETON_POSITION_HIP_CENTER], $skeletonZ[NUI_SKELETON_POSITION_HIP_CENTER], | |
$color) | |
line3d( | |
$skeletonX[NUI_SKELETON_POSITION_HIP_CENTER], $skeletonY[NUI_SKELETON_POSITION_HIP_CENTER], $skeletonZ[NUI_SKELETON_POSITION_HIP_CENTER], | |
$skeletonX[NUI_SKELETON_POSITION_HIP_LEFT], $skeletonY[NUI_SKELETON_POSITION_HIP_LEFT], $skeletonZ[NUI_SKELETON_POSITION_HIP_LEFT], | |
$color) | |
line3d( | |
$skeletonX[NUI_SKELETON_POSITION_HIP_LEFT], $skeletonY[NUI_SKELETON_POSITION_HIP_LEFT], $skeletonZ[NUI_SKELETON_POSITION_HIP_LEFT], | |
$skeletonX[NUI_SKELETON_POSITION_KNEE_LEFT], $skeletonY[NUI_SKELETON_POSITION_KNEE_LEFT], $skeletonZ[NUI_SKELETON_POSITION_KNEE_LEFT], | |
$color) | |
line3d( | |
$skeletonX[NUI_SKELETON_POSITION_KNEE_LEFT], $skeletonY[NUI_SKELETON_POSITION_KNEE_LEFT], $skeletonZ[NUI_SKELETON_POSITION_KNEE_LEFT], | |
$skeletonX[NUI_SKELETON_POSITION_ANKLE_LEFT], $skeletonY[NUI_SKELETON_POSITION_ANKLE_LEFT], $skeletonZ[NUI_SKELETON_POSITION_ANKLE_LEFT], | |
$color) | |
line3d( | |
$skeletonX[NUI_SKELETON_POSITION_ANKLE_LEFT], $skeletonY[NUI_SKELETON_POSITION_ANKLE_LEFT], $skeletonZ[NUI_SKELETON_POSITION_ANKLE_LEFT], | |
$skeletonX[NUI_SKELETON_POSITION_FOOT_LEFT], $skeletonY[NUI_SKELETON_POSITION_FOOT_LEFT], $skeletonZ[NUI_SKELETON_POSITION_FOOT_LEFT], | |
$color) | |
line3d( | |
$skeletonX[NUI_SKELETON_POSITION_HIP_CENTER], $skeletonY[NUI_SKELETON_POSITION_HIP_CENTER], $skeletonZ[NUI_SKELETON_POSITION_HIP_CENTER], | |
$skeletonX[NUI_SKELETON_POSITION_HIP_RIGHT], $skeletonY[NUI_SKELETON_POSITION_HIP_RIGHT], $skeletonZ[NUI_SKELETON_POSITION_HIP_RIGHT], | |
$color) | |
line3d( | |
$skeletonX[NUI_SKELETON_POSITION_HIP_RIGHT], $skeletonY[NUI_SKELETON_POSITION_HIP_RIGHT], $skeletonZ[NUI_SKELETON_POSITION_HIP_RIGHT], | |
$skeletonX[NUI_SKELETON_POSITION_KNEE_RIGHT], $skeletonY[NUI_SKELETON_POSITION_KNEE_RIGHT], $skeletonZ[NUI_SKELETON_POSITION_KNEE_RIGHT], | |
$color) | |
line3d( | |
$skeletonX[NUI_SKELETON_POSITION_KNEE_RIGHT], $skeletonY[NUI_SKELETON_POSITION_KNEE_RIGHT], $skeletonZ[NUI_SKELETON_POSITION_KNEE_RIGHT], | |
$skeletonX[NUI_SKELETON_POSITION_ANKLE_RIGHT], $skeletonY[NUI_SKELETON_POSITION_ANKLE_RIGHT], $skeletonZ[NUI_SKELETON_POSITION_ANKLE_RIGHT], | |
$color) | |
line3d( | |
$skeletonX[NUI_SKELETON_POSITION_ANKLE_RIGHT], $skeletonY[NUI_SKELETON_POSITION_ANKLE_RIGHT], $skeletonZ[NUI_SKELETON_POSITION_ANKLE_RIGHT], | |
$skeletonX[NUI_SKELETON_POSITION_FOOT_RIGHT], $skeletonY[NUI_SKELETON_POSITION_FOOT_RIGHT], $skeletonZ[NUI_SKELETON_POSITION_FOOT_RIGHT], | |
$color) | |
} | |
fn tofloat( $x ) | |
{ | |
$e = (($x >> 23) & 0xFF) - 127 | |
$f = $x & 0x7FFFFF | |
$z = $f * pow(10, $e) | |
if $x < 0 { | |
ret(-$z) | |
} else { | |
ret($z) | |
} | |
} | |
fn d_hands( $i ) | |
{ | |
$cx1 = skeleton_colorX[NUI_SKELETON_POSITION_HAND_LEFT, $i] | |
$cx2 = skeleton_colorX[NUI_SKELETON_POSITION_HAND_RIGHT, $i] | |
$cy1 = skeleton_colorY[NUI_SKELETON_POSITION_HAND_LEFT, $i] | |
$cy2 = skeleton_colorY[NUI_SKELETON_POSITION_HAND_RIGHT, $i] | |
$dp1 = skeleton_depth[NUI_SKELETON_POSITION_HAND_LEFT, $i] | |
$dp2 = skeleton_depth[NUI_SKELETON_POSITION_HAND_RIGHT, $i] | |
$cdx = $cx2 - $cx1 | |
$cdy = $cy2 - $cy1 | |
$ddp = ($dp2 - $dp1) / 2000 | |
$d = $cdx * $cdx + $cdy * $cdy + $ddp * $ddp | |
ret( $d ) | |
} | |
fn draw_player_bones() | |
{ | |
$i = 0 | |
$skeleton_colorX = new( NUI_SKELETON_POSITION_COUNT, INT32 ) | |
$skeleton_colorY = new( NUI_SKELETON_POSITION_COUNT, INT32 ) | |
t_push_matrix() | |
t_scale( scale_factor, scale_factor, scale_factor ) | |
t_translate( - width / 2, - height / 2, 10 ) | |
while $i < NUI_SKELETON_COUNT { | |
if skeletonTracked[$i] == 0 { $i + 1 continue } | |
if skeletonTracked[$i] == 2 | |
{ | |
copy( $skeleton_colorX, skeleton_colorX, 0, $i * NUI_SKELETON_POSITION_COUNT, NUI_SKELETON_POSITION_COUNT ) | |
copy( $skeleton_colorY, skeleton_colorY, 0, $i * NUI_SKELETON_POSITION_COUNT, NUI_SKELETON_POSITION_COUNT ) | |
copy( $skeleton_depth, skeleton_depth, 0, $i * NUI_SKELETON_POSITION_COUNT, NUI_SKELETON_POSITION_COUNT ) | |
draw_skeleton( GREEN, $skeleton_colorX, $skeleton_colorY ) | |
} | |
$s = "" | |
sprintf( $s, "%d", $i ) | |
print( $s, positionCX[ $i ], positionCY[ $i ], WHITE, BOTTOM ) | |
$i + 1 | |
} | |
t_pop_matrix() | |
remove( $skeleton_colorX ) | |
remove( $skeleton_colorY ) | |
} | |
fn gl_callback( $userdata ) | |
{ | |
set_screen( GL_SCREEN ) | |
draw() | |
set_screen( 0 ) | |
} | |
fn print_fps() | |
{ | |
print( fps_s, WINDOW_XSIZE / 2 - 16, WINDOW_YSIZE / 2 - 16, WHITE, BOTTOM | RIGHT ) | |
} | |
fn draw() | |
{ | |
clear() | |
clear_zbuf() | |
t_reset() | |
pixi( background_image, 0, 0, WHITE, scale_factor, scale_factor ) | |
t_translate( 0, 0, 100 ) | |
draw_players() | |
t_translate( 0, 0, 100 ) | |
if state_draw_skeleton { | |
draw_player_bones() | |
} | |
t_translate( 0, 0, 100 ) | |
print_fps() | |
} | |
fn switch_visible( $i ) | |
{ | |
visible[$i] = 1 - visible[$i] | |
player_rot[ $i ] = 90 | |
player_rot_t[ $i ] = 20 | |
} | |
fn draw_players() | |
{ | |
$i = 0 | |
while $i < N_PLAYERS { | |
if skeletonTracked[ $i ] == 0 { $i + 1 continue } | |
$dx = scale_factor * (positionCX[ $i ] - width / 2) | |
$dy = scale_factor * (positionCY[ $i ] - height / 2) | |
if visible[ $i ] == 0 { $rot = 90 - player_rot[ $i ] } | |
else { $rot = player_rot[ $i ] } | |
$scale = 1 - $rot / 180 | |
t_push_matrix() | |
t_translate( $dx, $dy, 0 ) | |
t_rotate( $rot, 0, 1, 0 ) | |
t_scale( $scale, $scale, $scale ) | |
t_translate( -$dx, -$dy, 0 ) | |
pixi( player_region[ $i ], 0, 0, WHITE, scale_factor, scale_factor ) | |
t_pop_matrix() | |
t_translate( 0, 0, 100 ) | |
$i + 1 | |
} | |
} | |
fn screenshot() | |
{ | |
draw() | |
$filename = "" | |
sprintf( $filename, "screenshot_%02d-%02d-%02d_%02d-%02d-%02d.png", | |
get_year(), get_month(), get_day(), get_hours(), get_minutes(), get_seconds() ) | |
save( get_screen(), $filename, FORMAT_PNG ) | |
printf( "screenshot: %s\n", $filename ) | |
} | |
fn screencast_start() | |
{ | |
if screencast == 0 { | |
create_anim( get_screen() ) | |
screencast = 1 | |
printf( "screencast start\n" ) | |
} | |
} | |
fn screencast_capture() | |
{ | |
if screencast == 1 { | |
$s = get_screen() | |
clone_frame( $s ) | |
$s.frame + 1 | |
draw() | |
pack_frame( $s ) | |
} | |
} | |
fn screencast_end() | |
{ | |
if screencast == 1 { | |
printf( "screencast end\n" ) | |
$s = get_screen() | |
$filename = "" | |
sprintf( $filename, "screencast_%02d-%02d-%02d_%02d-%02d-%02d.%s", | |
get_year(), get_month(), get_day(), get_hours(), get_minutes(), get_seconds(), "gif" ) | |
save( $s, $filename, FORMAT_GIF, GIF_DITHER ) | |
remove_anim( $s ) | |
screencast = 0 | |
printf( "screencast: %s\n", $filename ) | |
} | |
} | |
// main routine | |
kc = kc_load_dll() | |
if kc == -1 | |
{ | |
fputs( "could not load dll\n", STDERR ) | |
halt | |
} | |
ct = dlcall( kc, kc.sensor_count, 0 ) | |
if ct == -1 | |
{ | |
fputs( "something wrong\n", STDERR ) | |
go finalize | |
} | |
if ct == 0 | |
{ | |
fputs( "please connect kinect\n", STDERR ) | |
go finalize | |
} | |
if dlcall( kc, kc.initialize, 0, | |
NUI_INITIALIZE_FLAG_USES_COLOR | |
| NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX | |
| NUI_INITIALIZE_FLAG_USES_SKELETON ) != 0 | |
{ | |
fputs( "could not initialize kinect\n", STDERR ) | |
go finalize | |
} | |
if dlcall( kc, kc.color_stream_open, 0 ) != 0 | |
{ | |
fputs( "could not open color image stream\n", STDERR ) | |
go finalize | |
} | |
if dlcall( kc, kc.depth_stream_open, depth_flag ) != 0 | |
{ | |
fputs( "could not open depth image stream\n", STDERR ) | |
go finalize | |
} | |
if dlcall( kc, kc.skeleton_tracking_enable, skeleton_flag) != 0 | |
{ | |
fputs( "could not enable skeleton tracking\n", STDERR ) | |
go finalize | |
} | |
if dlcall( kc, kc.frame_end_event, 0 ) != 0 | |
{ | |
fputs( "could not set frame end event\n", STDERR ) | |
go finalize | |
} | |
dlcall( kc, kc.camera_set_angle, -5 ) | |
init() | |
set_quit_action( QA_NONE ) | |
set_gl_callback( gl_callback ) | |
screencast = 0 | |
while 1 | |
{ | |
if dlcall( kc, kc.wait_stream_event, 10 ) { | |
get_frame() | |
} | |
while get_event() | |
{ | |
if EVT[ EVT_TYPE ] == EVT_QUIT { break2 } | |
if EVT[ EVT_TYPE ] == EVT_SCREENRESIZE { screen_resize() } | |
if EVT[ EVT_TYPE ] == EVT_BUTTONDOWN | |
{ | |
$k = EVT[ EVT_KEY ] | |
if $k == KEY_F9 { | |
screenshot() | |
} | |
if $k == 0x73 /* s */ { | |
state_draw_skeleton = 1 - state_draw_skeleton | |
} | |
if $k == 0x72 /* r */ { | |
if screencast == 0 { | |
screencast_start() | |
} else { | |
screencast_end() | |
} | |
} | |
if 0x30 <= $k && $k <= 0x35 { | |
$i = $k - 0x30 | |
switch_visible( $i ) | |
} | |
} | |
} | |
update() | |
screencast_capture() | |
frame() | |
} | |
screencast_end() | |
set_gl_callback( -1 ) | |
dlcall( kc, kc.camera_set_angle, 0 ) | |
finalize: | |
dlcall( kc, kc.finalize, 0 ) | |
kc_unload_dll() |
This file contains 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
_kc_dll = -1 | |
NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX = 0x1 | |
NUI_INITIALIZE_FLAG_USES_COLOR = 0x2 | |
NUI_INITIALIZE_FLAG_USES_SKELETON = 0x8 | |
NUI_IMAGE_STREAM_FLAG_ENABLE_NEAR_MODE = 0x20000 | |
NUI_SKELETON_TRACKING_FLAG_ENABLE_SEATED_SUPPORT = 0x4 | |
NUI_SKELETON_TRACKING_FLAG_ENABLE_IN_NEAR_RANGE = 0x8 | |
NUI_SKELETON_COUNT = 6 | |
NUI_SKELETON_POSITION_HIP_CENTER = 0 | |
NUI_SKELETON_POSITION_SPINE = 1 | |
NUI_SKELETON_POSITION_SHOULDER_CENTER = 2 | |
NUI_SKELETON_POSITION_HEAD = 3 | |
NUI_SKELETON_POSITION_SHOULDER_LEFT = 4 | |
NUI_SKELETON_POSITION_ELBOW_LEFT = 5 | |
NUI_SKELETON_POSITION_WRIST_LEFT = 6 | |
NUI_SKELETON_POSITION_HAND_LEFT = 7 | |
NUI_SKELETON_POSITION_SHOULDER_RIGHT = 8 | |
NUI_SKELETON_POSITION_ELBOW_RIGHT = 9 | |
NUI_SKELETON_POSITION_WRIST_RIGHT = 10 | |
NUI_SKELETON_POSITION_HAND_RIGHT = 11 | |
NUI_SKELETON_POSITION_HIP_LEFT = 12 | |
NUI_SKELETON_POSITION_KNEE_LEFT = 13 | |
NUI_SKELETON_POSITION_ANKLE_LEFT = 14 | |
NUI_SKELETON_POSITION_FOOT_LEFT = 15 | |
NUI_SKELETON_POSITION_HIP_RIGHT = 16 | |
NUI_SKELETON_POSITION_KNEE_RIGHT = 17 | |
NUI_SKELETON_POSITION_ANKLE_RIGHT = 18 | |
NUI_SKELETON_POSITION_FOOT_RIGHT = 19 | |
NUI_SKELETON_POSITION_COUNT = 20 | |
INFINITE = 0xFFFFFFFF | |
fn kc_load_dll() | |
{ | |
if _kc_dll != -1 { | |
ret( _kc_dll ) | |
} | |
if strstr( OS_NAME, "win32" ) >= 0 | |
{ | |
_kc_dll = dlopen( "KinectControl.dll" ) | |
} | |
$fns = new(23, 2) | |
$fns[0, 0] = "initialize" | |
$fns[0, 1] = "i(iI)" | |
$fns[1, 0] = "finalize" | |
$fns[1, 1] = "i(i)" | |
$fns[2, 0] = "sensor_count" | |
$fns[2, 1] = "i(i)" | |
$fns[3, 0] = "color_stream_open" | |
$fns[3, 1] = "i(I)" | |
$fns[4, 0] = "frame_end_event" | |
$fns[4, 1] = "i(i)" | |
$fns[5, 0] = "width" | |
$fns[5, 1] = "I(i)" | |
$fns[6, 0] = "height" | |
$fns[6, 1] = "I(i)" | |
$fns[7, 0] = "wait_stream_event" | |
$fns[7, 1] = "i(I)" | |
$fns[8, 0] = "color_image" | |
$fns[8, 1] = "i(p)" | |
$fns[9, 0] = "depth_stream_open" | |
$fns[9, 1] = "i(I)" | |
$fns[10, 0] = "depth_image" | |
$fns[10, 1] = "i(pppp)" | |
$fns[11, 0] = "skeleton_tracking_enable" | |
$fns[11, 1] = "i(I)" | |
$fns[12, 0] = "skeleton_frame" | |
$fns[12, 1] = "i(I)" | |
$fns[13, 0] = "player_area" | |
$fns[13, 1] = "i(Ipppp)" | |
$fns[14, 0] = "depth_color_map" | |
$fns[14, 1] = "i(pppp)" | |
$fns[15, 0] = "skeleton_tracking_state" | |
$fns[15, 1] = "i(i)" | |
$fns[16, 0] = "skeleton_positions" | |
$fns[16, 1] = "i(ippp)" | |
$fns[17, 0] = "transform_skeleton_to_color_image" | |
$fns[17, 1] = "i(ipppppp)" | |
$fns[18, 0] = "camera_angle" | |
$fns[18, 1] = "i(i)" | |
$fns[19, 0] = "camera_set_angle" | |
$fns[19, 1] = "i(i)" | |
$fns[20, 0] = "util_dilate" | |
$fns[20, 1] = "i(p)" | |
$fns[21, 0] = "util_erode" | |
$fns[21, 1] = "i(p)" | |
$fns[22, 0] = "skeleton_position" | |
$fns[22, 1] = "i(ip)" | |
$xsize = get_xsize( $fns ) | |
$i = 0 | |
$fn = "" | |
while $i < $xsize | |
{ | |
sprintf( $fn, "kc_%s", $fns[$i, 0] ) | |
$f = dlsym( _kc_dll, $fn, $fns[$i, 1] ) | |
if $f == -1 | |
{ | |
kc_unload_dll() | |
_kc_dll = -1 | |
break | |
} | |
set_prop( _kc_dll, $fns[$i, 0], $f ) | |
$i + 1 | |
} | |
ret( _kc_dll ) | |
} | |
fn kc_unload_dll() | |
{ | |
if _kc_dll != -1 | |
{ | |
dlclose( _kc_dll ) | |
} | |
_kc_dll = -1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment