Advertisement
j0h

now with real pixels!

j0h
Jul 31st, 2023
2,722
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.00 KB | None | 0 0
  1. #include <SDL/SDL.h>
  2. #include <libfreenect.h>
  3. #include <libfreenect_sync.h>
  4. #include <stdio.h>
  5. #include <unistd.h>  //tcgetattr and sleep
  6.  
  7. //g++ -g -Wall -o "ppplotKinect" "ppplotKinect.cpp"  -lSDL -lSDL_ttf -lSDL_mixer -lSDL_image   -lfreenect -lfreenect_sync
  8. SDL_Surface* Backbuffer = NULL;
  9.  
  10. freenect_context *f_ctx;
  11. freenect_device *f_dev;
  12.  
  13. #define WIDTH 640
  14. #define HEIGHT 480
  15.  
  16.      int die = 0;
  17.  
  18. int kInit();                 //initiallize / test kinect library and device connection
  19. void kClose();         //close the kinect device
  20. void depth_cb(freenect_device *dev, void *depth, uint32_t timestamp);
  21. void no_kinect_quit(void);    
  22. bool ProgramIsRunning();
  23. void DrawPixel(SDL_Surface *surface, int x, int y , unsigned int depth_value);
  24. void IR(freenect_device *dev, uint16_t brightness);
  25. int main(int argc, char* args[]){
  26.     if(SDL_Init(SDL_INIT_EVERYTHING) < 0)    {
  27.         printf("Failed to initialize SDL!\n");
  28.         return 0;
  29.     }
  30.     kInit();  //init kinect lib, and device
  31.         int ire = freenect_get_ir_brightness(f_dev);
  32.         printf("%d brightness\n", ire);
  33.     //freenect_start_ir(f_dev);
  34.     IR(f_dev,30);  //brightness 1 to 50 may effect resolution depending on conditions.
  35.  
  36.     freenect_set_depth_callback(f_dev, depth_cb);
  37.     // Start the depth stream
  38.     freenect_start_depth(f_dev);
  39.    // Backbuffer = SDL_SetVideoMode(640 ,480, 32, SDL_FULLSCREEN | SDL_SWSURFACE);
  40.     Backbuffer = SDL_SetVideoMode(1280 ,960, 32, SDL_SWSURFACE);
  41.     SDL_WM_SetCaption("Ghost Data", NULL);
  42.     while(ProgramIsRunning())    {
  43.                 if (freenect_process_events(f_ctx) < 0){
  44.             break;
  45.         }
  46.     }
  47.    SDL_Quit();
  48.     kClose();
  49.     return 0;
  50. }
  51.  
  52. void depth_cb(freenect_device *dev, void *depth, uint32_t timestamp){
  53.     // Cast the depth data to unsigned short (16-bit) xbox uses 11 bits of depth
  54.     uint16_t *depth_data = (uint16_t *)depth;
  55.  
  56. unsigned int depth_value =0;
  57.     int x = 0;
  58.     int y = 0;
  59.     int index = 0; //index 0 to 307840
  60.  
  61. for (x=0; x<=WIDTH; x++){  
  62.      index = y*WIDTH+x;  
  63.     depth_value = depth_data[index];  
  64.     DrawPixel(Backbuffer, x, y, depth_value);
  65.  
  66.     if(x>=WIDTH){
  67.         if(y>=HEIGHT){
  68.         x=0;
  69.         y++;
  70.         SDL_Flip(Backbuffer);  
  71.         return;
  72.         }
  73.         y++;  //goto next line
  74.         x=0;
  75.         }    
  76.     }
  77. }
  78. bool ProgramIsRunning(){
  79.     SDL_Event event;
  80.     int brightness = freenect_get_ir_brightness(f_dev);
  81.  
  82.     bool running = true;
  83.     while(SDL_PollEvent(&event))   {
  84.         if(event.type == SDL_QUIT)
  85.             running = false;
  86.     }
  87.     if(event.type==SDL_KEYDOWN){
  88.         switch(event.key.keysym.sym){
  89.             case SDLK_ESCAPE :
  90.                     running=false;
  91.                     break;
  92.                    
  93.             case SDLK_a :
  94.             //get brightness add 1
  95.             brightness = freenect_get_ir_brightness(f_dev);
  96.             freenect_set_ir_brightness(f_dev, brightness+1);
  97.             printf("brightness: %d \n", brightness);
  98.             if (brightness >=50){
  99.                 brightness=50;
  100.                 break;
  101.                 }
  102.             break;
  103.             case SDLK_d :
  104.             //get brightness sub 1
  105.             brightness = freenect_get_ir_brightness(f_dev);
  106.             freenect_set_ir_brightness(f_dev, brightness-1);
  107.             printf("brightness: %d \n", brightness);
  108.             if (brightness <=1){
  109.                 brightness=1;
  110.                 break;
  111.                 }
  112.             break;
  113.                
  114.              default :
  115.                     break;         
  116.             }
  117.         }
  118.  
  119.     return running;
  120. }
  121.  
  122. void DrawPixel(SDL_Surface *surface,int x , int y , unsigned  int depth_value){
  123.     if(SDL_MUSTLOCK(surface)){
  124.         if(SDL_LockSurface(surface) < 0)
  125.             return;
  126.     }
  127.     Uint32 *buffer;
  128.     Uint32 color;
  129.  
  130.     color = SDL_MapRGB(surface->format, 0, 0, 0);
  131.     if(x >= surface->w || x < 0 || y >= surface->h || y < 0)
  132.         return;
  133. //write a switch to convert depth to rgb values.
  134. switch (depth_value) {
  135.     //depth value fckery
  136.     case 0 ... 400:
  137.     color = SDL_MapRGB(surface->format, depth_value+100,0,0);  //red
  138.     break;
  139.    
  140.     case 401 ... 624:
  141.   color = SDL_MapRGB(surface->format, 255,depth_value/3,0); //orange scale
  142.     break;
  143.    
  144.     case 625 ... 824:
  145.     color = SDL_MapRGB(surface->format, depth_value-300,depth_value-300,0);  //yello
  146.     break;
  147.    
  148.     case  825 ... 925:
  149.     color = SDL_MapRGB(surface->format, 0,depth_value/5,0); //green scale
  150.     break;
  151.      
  152.     case 926 ... 1480:
  153.     color = SDL_MapRGB(surface->format, depth_value/20,0,depth_value/7); //indiogo
  154.     break;
  155.    
  156.     case 1481 ... 1999:
  157.     color = SDL_MapRGB(surface->format, 0,255, 255);
  158.     break;
  159.       /*  
  160.     case 1666 ... 1850:
  161.     color = SDL_MapRGB(surface->format, 128,0,depth_value);
  162.     break;
  163.    
  164.     case 1851 ... 1999:
  165.     color = SDL_MapRGB(surface->format, 255,0,depth_value/12);
  166.     break;
  167.  
  168.     case 2036 ... 2047:
  169.     color = SDL_MapRGB(surface->format, 0,0,0);
  170.     break;
  171. **/
  172.    case 2000 ... 2047:
  173.         color = SDL_MapRGB(surface->format, depth_value,depth_value,depth_value);
  174.      break;
  175.     default:
  176. color = SDL_MapRGB(surface->format, 0,0,0);
  177.     break;
  178.         }
  179.  
  180.     buffer = (Uint32*)surface->pixels  + y*surface->pitch/4 + x;
  181.     (*buffer) = color;
  182.  
  183.     if(SDL_MUSTLOCK(surface))
  184.         SDL_UnlockSurface(surface);
  185. }
  186. //Kinect Stuff
  187. int kInit(){
  188.         // Initialize libfreenect
  189.     if (freenect_init(&f_ctx, NULL) < 0)    {
  190.         printf("Failed to initialize libfreenect!\n");
  191.         exit(1);
  192.     }
  193.  
  194.     // Set the log level (optional)
  195.   //  freenect_set_log_level(f_ctx, FREENECT_LOG_DEBUG);
  196.  
  197.     // Open the Kinect device
  198.     if (freenect_open_device(f_ctx, &f_dev, 0) < 0)    {
  199.         printf("Failed to open Kinect device!\n");
  200.        exit(1);
  201.     }
  202.     return 0;
  203.     }
  204.  
  205. void IR(freenect_device *dev, uint16_t brightness){
  206.        
  207.         int ire = freenect_get_ir_brightness(f_dev);
  208.         freenect_set_ir_brightness(f_dev, brightness);
  209.         printf("%d brightness\n", ire);
  210.         //int freenect_set_ir_brightness(f_dev,  brightness);
  211.         }
  212. void kClose(){
  213.         //shutdown functions
  214.     freenect_stop_depth(f_dev);
  215.     freenect_close_device(f_dev);
  216.     freenect_shutdown(f_ctx);
  217.    // SDL_Quit();
  218.         }
  219. void no_kinect_quit(void){
  220.     printf("Error: Kinect not connected?\n");
  221.     exit(1);
  222. }
  223.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement