Advertisement
Guest User

Untitled

a guest
Sep 9th, 2013
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.54 KB | None | 0 0
  1. #include <limits.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <malloc.h>
  5. #include <math.h>
  6. #include <SDL/SDL.h>
  7.  
  8. #define SECONDARY_SORTING_SPEED 300000
  9. #define width 1024
  10. #define height 768
  11.  
  12. SDL_Surface *sdl_scr;
  13. int *scr, *zbf, *data, tmp[4];
  14. const int widthheight = width*height;
  15. const int halfwidth = width/2;
  16. const int halfheight = height/2;
  17. static unsigned int seed=1000;
  18. __inline int _rand() {seed=(214013*seed+2531011); return((seed>>16)&0x7FFF);}
  19.  
  20. void SWP(int A, int B) {
  21.     int as;
  22.     for(as=0; as<4; as++){
  23.         tmp[as]=data[A+as];
  24.         data[A+as]=data[B+as];
  25.         data[B+as]=tmp[as];
  26.     }
  27. }
  28.  
  29. int load_points(const char* filename) {
  30.     FILE *f = fopen(filename, "rb");
  31.     fseek(f, 0, SEEK_END);
  32.     const int count = (ftell(f)/9);
  33.     fseek(f, 0, SEEK_SET);
  34.     data = (int*)calloc(count, 4*4);
  35.     int i = 0;
  36.     while (!feof(f)) {
  37.         unsigned short xyz[4];
  38.         unsigned char rgb[3];
  39.         fread(&xyz, 3, 2, f);
  40.         fread(&rgb, 3, 1, f);
  41.         data[i+0] = xyz[0];
  42.         data[i+1] = xyz[2];
  43.         data[i+2] = xyz[1];
  44.         data[i+3] = (rgb[0]<<16)|(rgb[1]<<8)|rgb[2];
  45.         i+=4;
  46.     }
  47.     fclose(f);
  48. // FIRST SORTING. IT IS SIMPLE, BUT VERY IMPORTANT STAGE FOR THIS ALGORITHM.
  49.     for (i = 0; i < count*4; i+=4) {
  50.         int ri = ((_rand()*_rand())%count)*4;
  51.         SWP(i, ri);
  52.     }
  53.     return count;
  54. }
  55.  
  56. __inline void rot2d(float sf, float cf, float *x, float *y) {
  57.     float tx = (*x) * cf - (*y) * sf; *y = (*y) * cf + (*x) * sf; *x = tx;
  58. }
  59.  
  60. int main(int argc, char **argv) {
  61.     // PREPARATIONS (MEMORY ALLOCATION, POINT CLOUD DATA LOADING):
  62.     if (argc==1) return 0;
  63.     const int count = load_points(argv[1]);
  64.     sdl_scr = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE);
  65.     scr = (int*)calloc(width * height, 4);
  66.     zbf = (int*)calloc(width * height, 4);
  67.     float camx = 512, camy = 512, camz = 512, yaw = 0;
  68.     float yx = 0, dx = 0, dy = 0, dz = 0;
  69.  
  70.     int thresh = 0, lasti = 0, limit = count, f = 0, i4 = 0, i = 0;
  71.  
  72.  
  73.     // MAIN LOOP:
  74.     for(;;) {
  75.         // 1. CAMERA MOTION
  76.         SDL_PumpEvents();
  77.         static unsigned lt = 0; float d = SDL_GetTicks() - lt; lt += d; float dt = d<1?1:d;
  78.         if (dt<16) { SDL_Delay(16 - dt); dt = 16; }
  79.         unsigned char *key = SDL_GetKeyState(NULL);
  80.         if (key[SDLK_ESCAPE]) return 0;
  81.         yx = -(key[SDLK_LEFT] - key[SDLK_RIGHT]) * dt/500.0;
  82.         dx += (key[SDLK_d] - key[SDLK_a]) * dt/48.0;
  83.         dy += (key[SDLK_SPACE] - key[SDLK_LCTRL]) * dt/48.0;
  84.         dz += (key[SDLK_w] - key[SDLK_s]) * dt/48.0;
  85.         yaw += yx; float sf = sinf(yaw), cf = cosf(yaw);
  86.         float tdz = dz, tdx = dx; rot2d(sf, cf, &tdz, &tdx);
  87.         camx += tdx; camy += dy; camz += tdz;
  88.         float dimv = (1.0 - dt/500.0); if (dimv<0) dimv = 0; if (dimv>1) dimv = 1;
  89.         dx = dx * dimv; dy = dy * dimv; dz = dz * dimv;
  90.         if (fabs(dx)<0.1) dx = 0; if (fabs(dy)<0.1) dy = 0; if (fabs(dz)<0.1) dz = 0;
  91.  
  92.         // 2. CLEARING SCREEN AND ZBUFFER
  93.         for (i = 0; i < widthheight; i++) { scr[i] = 0; zbf[i] = INT_MAX; }
  94.  
  95.         // 3. ATOMS DRAWING ALONG WITH _SORTING_
  96.         f = 0;
  97.         for (i = 0; i < limit; i++) {
  98.             if (i == thresh) {
  99.                 i = lasti;
  100.                 limit = i + SECONDARY_SORTING_SPEED;
  101.                 if (limit > count) limit = count;
  102.             }
  103.             i4 = i*4;
  104.             int x = data[i4 + 0] - camx, y = data[i4 + 1] - camy, z = data[i4 + 2] - camz;
  105.             float fx = x, fz = z; rot2d(sf, cf, &fx, &fz); x = fx; z = fz;
  106.             if (z <= 0) continue;
  107.             const int sx = halfwidth  + (x*halfwidth) / z;
  108.             if (sx < 0 || sx >= width) continue;
  109.             const int sy = halfheight - (y*halfwidth) / z;
  110.             if (sy < 0 || sy >= height) continue;
  111.             const int sind = sx + sy * width;
  112.             if (zbf[sind] <= z+1) continue;
  113.             scr[sind] = data[i4 + 3];// if (f > thresh) scr[sind] = 0xff;
  114.             zbf[sind] = z;
  115.             if (f < thresh) SWP(i4, f*4);
  116.             f++;
  117.         }
  118.         thresh = f + 1000; // Adaptive threshold
  119.         lasti = i;
  120.         if (lasti >= count) lasti = thresh;
  121.  
  122.         // 4. FLIPPING SCREEN, SHOWING FPS
  123.         if(SDL_MUSTLOCK(sdl_scr)) if(SDL_LockSurface(sdl_scr)<0) return 0;
  124.         memcpy(sdl_scr->pixels, scr, width*height*4);
  125.         if(SDL_MUSTLOCK(sdl_scr)) SDL_UnlockSurface(sdl_scr);
  126.         SDL_Flip(sdl_scr);
  127.         char buf[256]; sprintf(buf, "fps: %.2f, thresh: %d", 1000/dt, thresh ); SDL_WM_SetCaption(buf, NULL);
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement