Advertisement
Guest User

Slow ray caster (demo with cones)

a guest
Feb 20th, 2012
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.05 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <math.h>
  3. #include <stdio.h>
  4. #include <SDL.h>
  5.  
  6. int bsize = 2;
  7.  
  8. int scw = 1280;
  9. int sch = 1024;
  10.  
  11. int mapa = 32;
  12. int mapb = 32;
  13. int mapc = 32;
  14.  
  15. void filldata (int *hei, int *col)
  16. {
  17.     int idx;
  18.     int x,y,z;
  19.     for (y=0; y<mapb; y++)
  20.     {
  21.         for (x=0; x<mapa; x++)
  22.         {
  23.             int val;
  24.             for (z=0; z<mapc; z++)
  25.             {
  26.                 idx = y*mapa*mapb + x*mapa + z;
  27.                 double d = sqrt((x-10)*(x-10) + (y-10)*(y-10));
  28.                 int h = 30 - 3*d;
  29.                 if ((h>0)&&(z<h)) val = z;
  30.                 else val = -1;
  31.  
  32.                 hei[idx] = val;
  33.                 col[idx] = 3*sqrt(x*x+y*y+z*z);
  34.             }
  35.         }
  36.     }
  37. }
  38.  
  39. void fillpalette (SDL_Surface *screen)
  40. {
  41.     SDL_Color colors[256];
  42.     int i;
  43.     /* Fill colors with color information */
  44.     for(i=0;i<=256;i++)
  45.     {
  46.         colors[i].r=i;
  47.         colors[i].g=i/3;
  48.         colors[i].b=i/3;
  49.     }
  50.     SDL_SetPalette(screen, SDL_LOGPAL|SDL_PHYSPAL, colors, 0, 256);
  51. }
  52.  
  53. void printdata (int *hei)
  54. {
  55.     int x,y,z;
  56.     FILE *out = fopen ("testout.dat", "w");
  57.     for (y=0; y<mapb; y++)
  58.     {
  59.         for (x=0; x<mapa; x++)
  60.         {
  61.             for (z=0; z<mapc; z++)
  62.             {
  63.                 int idx = y*mapa*mapb + x*mapa + z;
  64.                 fprintf (out, "%i %i %i\n", x, y, hei[idx]);
  65.             }
  66.         }
  67.     }
  68.     fclose (out);
  69. }
  70.  
  71. void blacken_screen (SDL_Surface *screen)
  72. {
  73.     SDL_Rect rect;
  74.     rect.x = 0;
  75.     rect.y = 0;
  76.     rect.w = scw;
  77.     rect.h = sch;
  78.  
  79.     SDL_FillRect (screen, &rect, 0);
  80. }
  81.  
  82. void checkevents(double *ang, int *x, int *y, int *k)
  83. {
  84.     SDL_Event event;
  85.     if (SDL_PollEvent(&event))
  86.     {
  87.         switch (event.type) {
  88.         case SDL_QUIT:
  89.             exit(0);
  90.         }
  91.     }
  92.        Uint8 *keystate = SDL_GetKeyState (NULL);
  93.     if (keystate[SDLK_LEFT]) *ang += 0.02;
  94.     if (keystate[SDLK_RIGHT]) *ang -= 0.02;
  95.     if (keystate[SDLK_UP]) *k -= 1;
  96.     if (keystate[SDLK_DOWN]) *k += 1;
  97.     if (keystate[SDLK_a] || keystate[SDLK_d] || keystate[SDLK_w] || keystate[SDLK_s])
  98.     {
  99.         int xd, yd;
  100.         xd = 0;
  101.         yd = 0;
  102.         if (keystate[SDLK_a]) xd = -3;
  103.         if (keystate[SDLK_d]) xd = 3;
  104.         if (keystate[SDLK_w]) yd = 3;
  105.         if (keystate[SDLK_s]) yd = -3;
  106.        
  107.         *x += yd*cos(*ang) + xd*sin(*ang);
  108.         *y += -yd*sin(*ang) + xd*cos(*ang);
  109.     }
  110. }
  111.  
  112. int main ()
  113. {
  114.     int hei[mapa*mapb*mapc-1];
  115.     int col[mapa*mapb*mapc-1];
  116.  
  117.     filldata (hei, col);
  118.     printdata (hei);
  119.  
  120.     if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
  121.     {
  122.         fprintf(stderr, "Cannot initialize SDL: %s\n", SDL_GetError());
  123.         exit(1);
  124.     }
  125.  
  126.     SDL_Surface *screen;
  127.     screen = SDL_SetVideoMode(scw, sch, 8, SDL_SWSURFACE|SDL_HWPALETTE);
  128.     if ( screen == NULL )
  129.     {// Если установить разрешение не удалось
  130.         fprintf(stderr, "Cannot set resolution %ix%i: %s\n", scw, sch, SDL_GetError());
  131.         exit(1);
  132.     }
  133.  
  134.     fillpalette (screen);
  135.  
  136.     int posx = 0;
  137.     int posy = 0;
  138.     int posz = 40;
  139.  
  140.     double ang = 0;
  141.     int k = 1;
  142.  
  143.     double dd = 1;
  144.     double de = dd * 128; // 128 is the max "distance"
  145.     int x,y,z;
  146.  
  147.     double amax = M_PI/3.4; // FOV
  148.     double a;
  149.  
  150.     int bin1 = mapb*mapb*mapb-mapa*mapa;
  151.     int bin2 = mapa*mapa-mapa;
  152.     int bin3 = mapc-1;
  153.  
  154.     int sx;
  155.    
  156.     while (1)
  157.     {
  158.         SDL_LockSurface(screen);
  159.        
  160.         blacken_screen (screen);
  161.  
  162.         double sinang = sin(ang);
  163.         double cosang = cos(ang);
  164.  
  165.         for (sx=0;sx<scw;sx++)
  166.         {
  167.             double d;
  168.             a = -amax + 2*amax*sx/scw;
  169.             double tana = tan (a);
  170.             for (d=de;d>=0;d-=dd)
  171.             {
  172.                 double y1 = d*/*cos(amax)**/tana;
  173.                 double x1 = d;//*cos(amax);
  174.  
  175.                 x = posx + x1*cosang+y1*sinang;
  176.                 y = posy - x1*sinang+y1*cosang;
  177.                 z = posz - k*d;//*sin(amax);
  178.  
  179.                 x = abs(x);
  180.                 y = abs(y);
  181.                
  182.                 int idx = ((y*mapa*mapb)&bin1) + ((x*mapa)&bin2);
  183.                 int zi;
  184.                 for (zi=0; zi<mapc; zi++)
  185.                 {
  186.                     int idx2 = idx+zi;
  187.                     int h = hei[idx2];
  188.                     if (h==-1) break;
  189.  
  190.                     int sy = sch/2 + 150*(z-h)/(d+1);
  191.                     int sy2 = sch/2 + 150*(z-h-bsize)/(d+1);
  192.  
  193.                     if (sy2<sch)
  194.                     {
  195.                         if (sy>=sch) sy = sch-1;
  196.                         if (sy2<0) sy2 = 0;
  197.                         int syi;
  198.                         for (syi=sy; syi>=sy2; syi--)
  199.                         {
  200.                             *((Uint8*)screen->pixels+sx+scw*syi) = col[idx2];
  201.                         }
  202.                     }
  203.                 }
  204.             }
  205.         }
  206.  
  207.         SDL_UnlockSurface (screen);
  208.         SDL_Flip (screen);
  209.         checkevents(&ang, &posx, &posy, &k);
  210.     }
  211.  
  212.    
  213.     return atexit(SDL_Quit);
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement