Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SDL/SDL.h>
- #include <libfreenect.h>
- #include <libfreenect_sync.h>
- #include <stdio.h>
- #include <unistd.h> //tcgetattr and sleep
- //g++ -g -Wall -o "ppplotKinect" "ppplotKinect.cpp" -lSDL -lSDL_ttf -lSDL_mixer -lSDL_image -lfreenect -lfreenect_sync
- SDL_Surface* Backbuffer = NULL;
- freenect_context *f_ctx;
- freenect_device *f_dev;
- #define WIDTH 640
- #define HEIGHT 480
- int die = 0;
- int kInit(); //initiallize / test kinect library and device connection
- void kClose(); //close the kinect device
- void depth_cb(freenect_device *dev, void *depth, uint32_t timestamp);
- void no_kinect_quit(void);
- bool ProgramIsRunning();
- void DrawPixel(SDL_Surface *surface, int x, int y , unsigned int depth_value);
- void IR(freenect_device *dev, uint16_t brightness);
- int main(int argc, char* args[]){
- if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
- printf("Failed to initialize SDL!\n");
- return 0;
- }
- kInit(); //init kinect lib, and device
- int ire = freenect_get_ir_brightness(f_dev);
- printf("%d brightness\n", ire);
- //freenect_start_ir(f_dev);
- IR(f_dev,30); //brightness 1 to 50 may effect resolution depending on conditions.
- freenect_set_depth_callback(f_dev, depth_cb);
- // Start the depth stream
- freenect_start_depth(f_dev);
- // Backbuffer = SDL_SetVideoMode(640 ,480, 32, SDL_FULLSCREEN | SDL_SWSURFACE);
- Backbuffer = SDL_SetVideoMode(1280 ,960, 32, SDL_SWSURFACE);
- SDL_WM_SetCaption("Ghost Data", NULL);
- while(ProgramIsRunning()) {
- if (freenect_process_events(f_ctx) < 0){
- break;
- }
- }
- SDL_Quit();
- kClose();
- return 0;
- }
- void depth_cb(freenect_device *dev, void *depth, uint32_t timestamp){
- // Cast the depth data to unsigned short (16-bit) xbox uses 11 bits of depth
- uint16_t *depth_data = (uint16_t *)depth;
- unsigned int depth_value =0;
- int x = 0;
- int y = 0;
- int index = 0; //index 0 to 307840
- for (x=0; x<=WIDTH; x++){
- index = y*WIDTH+x;
- depth_value = depth_data[index];
- DrawPixel(Backbuffer, x, y, depth_value);
- if(x>=WIDTH){
- if(y>=HEIGHT){
- x=0;
- y++;
- SDL_Flip(Backbuffer);
- return;
- }
- y++; //goto next line
- x=0;
- }
- }
- }
- bool ProgramIsRunning(){
- SDL_Event event;
- int brightness = freenect_get_ir_brightness(f_dev);
- bool running = true;
- while(SDL_PollEvent(&event)) {
- if(event.type == SDL_QUIT)
- running = false;
- }
- if(event.type==SDL_KEYDOWN){
- switch(event.key.keysym.sym){
- case SDLK_ESCAPE :
- running=false;
- break;
- case SDLK_a :
- //get brightness add 1
- brightness = freenect_get_ir_brightness(f_dev);
- freenect_set_ir_brightness(f_dev, brightness+1);
- printf("brightness: %d \n", brightness);
- if (brightness >=50){
- brightness=50;
- break;
- }
- break;
- case SDLK_d :
- //get brightness sub 1
- brightness = freenect_get_ir_brightness(f_dev);
- freenect_set_ir_brightness(f_dev, brightness-1);
- printf("brightness: %d \n", brightness);
- if (brightness <=1){
- brightness=1;
- break;
- }
- break;
- default :
- break;
- }
- }
- return running;
- }
- void DrawPixel(SDL_Surface *surface,int x , int y , unsigned int depth_value){
- if(SDL_MUSTLOCK(surface)){
- if(SDL_LockSurface(surface) < 0)
- return;
- }
- Uint32 *buffer;
- Uint32 color;
- color = SDL_MapRGB(surface->format, 0, 0, 0);
- if(x >= surface->w || x < 0 || y >= surface->h || y < 0)
- return;
- //write a switch to convert depth to rgb values.
- switch (depth_value) {
- //depth value fckery
- case 0 ... 400:
- color = SDL_MapRGB(surface->format, depth_value+100,0,0); //red
- break;
- case 401 ... 624:
- color = SDL_MapRGB(surface->format, 255,depth_value/3,0); //orange scale
- break;
- case 625 ... 824:
- color = SDL_MapRGB(surface->format, depth_value-300,depth_value-300,0); //yello
- break;
- case 825 ... 925:
- color = SDL_MapRGB(surface->format, 0,depth_value/5,0); //green scale
- break;
- case 926 ... 1480:
- color = SDL_MapRGB(surface->format, depth_value/20,0,depth_value/7); //indiogo
- break;
- case 1481 ... 1999:
- color = SDL_MapRGB(surface->format, 0,255, 255);
- break;
- /*
- case 1666 ... 1850:
- color = SDL_MapRGB(surface->format, 128,0,depth_value);
- break;
- case 1851 ... 1999:
- color = SDL_MapRGB(surface->format, 255,0,depth_value/12);
- break;
- case 2036 ... 2047:
- color = SDL_MapRGB(surface->format, 0,0,0);
- break;
- **/
- case 2000 ... 2047:
- color = SDL_MapRGB(surface->format, depth_value,depth_value,depth_value);
- break;
- default:
- color = SDL_MapRGB(surface->format, 0,0,0);
- break;
- }
- buffer = (Uint32*)surface->pixels + y*surface->pitch/4 + x;
- (*buffer) = color;
- if(SDL_MUSTLOCK(surface))
- SDL_UnlockSurface(surface);
- }
- //Kinect Stuff
- int kInit(){
- // Initialize libfreenect
- if (freenect_init(&f_ctx, NULL) < 0) {
- printf("Failed to initialize libfreenect!\n");
- exit(1);
- }
- // Set the log level (optional)
- // freenect_set_log_level(f_ctx, FREENECT_LOG_DEBUG);
- // Open the Kinect device
- if (freenect_open_device(f_ctx, &f_dev, 0) < 0) {
- printf("Failed to open Kinect device!\n");
- exit(1);
- }
- return 0;
- }
- void IR(freenect_device *dev, uint16_t brightness){
- int ire = freenect_get_ir_brightness(f_dev);
- freenect_set_ir_brightness(f_dev, brightness);
- printf("%d brightness\n", ire);
- //int freenect_set_ir_brightness(f_dev, brightness);
- }
- void kClose(){
- //shutdown functions
- freenect_stop_depth(f_dev);
- freenect_close_device(f_dev);
- freenect_shutdown(f_ctx);
- // SDL_Quit();
- }
- void no_kinect_quit(void){
- printf("Error: Kinect not connected?\n");
- exit(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement