home *** CD-ROM | disk | FTP | other *** search
/ HOT Scene Stuff / hotscenestuffzyklop1996.iso / demos / sunknown / voxel.cpp < prev    next >
C/C++ Source or Header  |  1994-04-07  |  10KB  |  553 lines

  1. // VOXEL.CPP ////////////////////////////////////////////////////////////////
  2.  
  3. // Thomas H.
  4.  
  5. // INCLUDES /////////////////////////////////////////////////////////////////
  6.  
  7. #include <stdio.h>
  8. #include <math.h>
  9. #include <dos.h>
  10. #include <stdlib.h>
  11.  
  12. #include "template.h"
  13. #include "voxel.h"
  14. #include "timer.h"
  15. #include "global.h"
  16. #include "seg.h"
  17. #include "xgraf.h"
  18.  
  19. // DEFINES //////////////////////////////////////////////////////////////////
  20.  
  21. #define VOXX    128
  22. #define VOXY    96
  23.  
  24. #define BACK  0.5
  25. #define HFAC    0.3
  26. #define SENTERY 145
  27.  
  28. #define ZOOMFAC 1.5873
  29. #define HITIT   125600
  30. #define REALLY    1
  31. #define RANFAC  127
  32. #define RANDOMIZE 1
  33.  
  34. //#define CHEAT
  35. //#define OVERVIEW
  36.  
  37. // EXTERNALS ////////////////////////////////////////////////////////////////
  38.  
  39. // FUNCTIONS
  40.  
  41. extern "C" void asmvox(void);
  42. extern "C" void interpolate(void);
  43. extern "C" void asmsky(void);
  44.  
  45. // VARS
  46.  
  47. extern timer_C timer;
  48. extern long plustimer;
  49. extern long far copper_32,
  50.        far leapright;
  51. extern word far sseg,
  52.        far hseg,
  53.        far fseg,
  54.        far lseg,
  55.        far voxpos,
  56.        far voypos;
  57. extern byte far alti,
  58.        far ofraction,
  59.        far interlimit,
  60.        *pressed,
  61.        rgb[];
  62. extern ulong musicstart;
  63. extern int relspeed;
  64.  
  65. // VARS /////////////////////////////////////////////////////////////////////
  66.  
  67. word *ra1,
  68.      *ra2,
  69.      *ra3,
  70.      *ra4;
  71. float zoomfac=ZOOMFAC;
  72.  
  73. // RECURSE
  74.  
  75. void recurse(word *ra,word pos,word len)
  76. {
  77.     long l=ra[pos];
  78.     long r=ra[pos+len];
  79.     long maxr=(long)len*RANFAC;
  80.     long ny=random(2l*maxr+1)-maxr;
  81.     if (len>=32)
  82.         ny*=4;
  83.     long her=(long)l+r+ny;
  84.     her>>=1;
  85.     if (her<0)
  86.         her=0;
  87.     if (her>16383)
  88.         her=16383l;
  89.     int plus=len>>1;
  90.     ra[pos+plus]=her;
  91.     if (plus==1)
  92.         return;
  93.     recurse(ra,pos,plus);
  94.     recurse(ra,pos+plus,plus);
  95. }
  96.  
  97. // MAKERAND
  98.  
  99. void makerand(word *ra)
  100. {
  101.     ra[0]=ra[256]=8192;
  102.     recurse(ra,0,256);
  103. }
  104.  
  105. // DIST
  106.  
  107. float dist(int no)
  108. {
  109.     float fac=no/96.0;
  110.     fac=pow(fac,1.05);
  111.     return fac*256.0;
  112. }
  113.  
  114. // PRECALC
  115.  
  116. void precalc(byte *lookup, byte *copper)
  117. {
  118.     byte *olookup=lookup;
  119.     byte *ocopper=copper;
  120.     FILE *fp=fopen(VOXPRE,"rb");
  121.     byte *leap=(byte*)leapright;
  122.  
  123.     for (int o=0; o<64; o++)
  124.     {
  125.         float vekk=64.0-o;
  126.         float s=max((double)0,64-sqrt(4096.0-vekk*vekk));
  127.         leap[o]=leap[127-o]=2*s;
  128.     }
  129.  
  130.     for (word cut=0; cut<128l*96l*3; cut++)
  131.     {
  132.         byte temp;
  133.         fread(&temp,1,1,fp);
  134.         ocopper[cut]=temp;
  135.     }
  136.  
  137.     for (cut=0; cut<96l*256l; cut++)
  138.     {
  139.         byte temp;
  140.         fread(&temp,1,1,fp);
  141.         olookup[cut]=temp;
  142.     }
  143.  
  144.     fclose(fp);
  145. }
  146.  
  147. // VISVOX
  148.  
  149. void visvox(byte *page,float camv, word screenseg,int step)
  150. {
  151. #ifndef CHEAT
  152.     int tempdx,tempcx;
  153.     byte count;
  154.  
  155.     float co=cos(camv);
  156.     float si=sin(camv);
  157.  
  158.     float hx=co*256/zoomfac;
  159.     float hy=si*256/zoomfac;
  160.  
  161.     int x0=32768-160*hx+120*hy;
  162.     int y0=32768-160*hy-120*hx;
  163.  
  164.     int plx=hx;
  165.     int ply=hy;
  166.     int plx4=hx*4;
  167.     int ply4=hy*4;
  168.  
  169.     asm pusha
  170.     asm push ds
  171.     asm lds si,page
  172.  
  173.     int mapval=MAP_MASK-256+512*step;
  174.  
  175.     for (byte plan=0; plan<4; plan+=step)
  176.     {
  177.         asm mov ax,screenseg
  178.         asm mov es,ax
  179.  
  180.         asm mov ax,mapval
  181.         asm mov cl,plan
  182.         asm shl ah,cl
  183.         asm mov dx,SC_INDEX
  184.         asm out dx,ax
  185.  
  186.         asm xor di,di
  187.         asm mov cx,x0
  188.         asm mov dx,y0
  189.  
  190.         asm mov count,80
  191.         asm mov si,plx
  192. next_right:
  193.         asm mov tempcx,cx
  194.         asm mov tempdx,dx
  195.         asm mov ah,60
  196. next_down:
  197.         asm mov bl,ch
  198.         asm mov bh,dh
  199.         asm mov al,[bx]
  200.         asm mov [es:di],al
  201.         asm add di,80
  202.         asm sub cx,ply
  203.         asm add dx,si
  204.         asm mov bl,ch
  205.         asm mov bh,dh
  206.         asm mov al,[bx]
  207.         asm mov [es:di],al
  208.         asm add di,80
  209.         asm sub cx,ply
  210.         asm add dx,si
  211.         asm mov bl,ch
  212.         asm mov bh,dh
  213.         asm mov al,[bx]
  214.         asm mov [es:di],al
  215.         asm add di,80
  216.         asm sub cx,ply
  217.         asm add dx,si
  218.         asm mov bl,ch
  219.         asm mov bh,dh
  220.         asm mov al,[bx]
  221.         asm mov [es:di],al
  222.         asm add di,80
  223.  
  224.         asm sub cx,ply
  225.         asm add dx,si
  226.         asm dec ah
  227.         asm jnz next_down
  228.  
  229.         asm add di,1-240*80
  230.         asm mov cx,tempcx
  231.         asm mov dx,tempdx
  232.         asm add cx,plx4
  233.         asm add dx,ply4
  234.         asm dec count
  235.         asm jnz next_right
  236.  
  237.         asm mov cx,plx
  238.         asm add x0,cx
  239.         asm mov cx,ply
  240.         asm add y0,cx
  241.  
  242.     }
  243.     asm pop ds
  244.     asm popa
  245. #else
  246.     asm pusha
  247.     asm push ds
  248.  
  249.     asm lds si,page
  250.     asm mov ax,0xa000
  251.     asm mov es,ax
  252.     asm xor si,si
  253.     asm xor di,di
  254.  
  255.     asm mov dx,200
  256. l1:
  257.     asm mov cx,128
  258.     asm rep movsw
  259.     asm add di,320-256
  260.     asm dec dx
  261.     asm jnz l1
  262.     asm pop ds
  263.     asm popa
  264.  
  265. #endif
  266. }
  267.  
  268. // LOADSKY
  269.  
  270. void loadsky(byte *sky)
  271. {
  272.     FILE *infile=fopen(CLOUDFILE,"rb");
  273.     for (int qy=0; qy<256; qy++)
  274.     for (int qx=0; qx<256; qx++)
  275.     {
  276.         byte col;
  277.         fread(&col,1,1,infile);
  278.         sky[qx+(qy<<8)]=col;
  279.     }
  280.     fclose(infile);
  281. }
  282.  
  283. // FIXSKY
  284.  
  285. void fixsky(byte *page, byte *sky, char skyx, byte skyy)
  286. {
  287.   asm pusha
  288.   asm push ds
  289.  
  290.   asm les di,page
  291.   asm lds bx,sky
  292.   asm add bh,skyx
  293.   asm add bl,skyy
  294.  
  295.   asmsky();
  296.  
  297.   asm pop ds
  298.   asm popa
  299. }
  300.  
  301. // COLORSTRIP
  302.  
  303. void colorstrip(byte *height, byte *color, byte stripe)
  304. {
  305.     for (int x=0; x<256; x++)
  306.     {
  307.         byte xx=x+2;
  308.         byte xxx=x-2;
  309.         int her=height[x+(stripe<<8)];
  310.         int der=height[xx+(stripe<<8)];
  311.         int tre=height[xxx+(stripe<<8)];
  312.         int diff=3*der-2*her-1*tre;
  313.         diff+=64;
  314.         fit(2,diff,127);
  315.         color[x+(stripe<<8)]=diff;
  316.     }
  317. }
  318.  
  319. // MAKESTRIP
  320.  
  321. void makestrip(byte *ptr,int no,float pro)
  322. {
  323.     int minus=384-no;
  324.     if (pro>0.65)
  325.         minus=(pro-0.65)*250;
  326.     fit(0,minus,127);
  327.     float f1=1+sin(no/23.0+2.5);
  328.     float f2=1+sin(no/21.0+1.5+f1/3);
  329.     float f3=1+sin(no/34.0+2.5);
  330.     float f4=1+sin(no/16.0+1.5+f3/2);
  331.  
  332.     int pl1=f1*4+no/2.0;
  333.     int pl2=f2*8-no/1.5;
  334.     int pl3=f3*7+no/4;
  335.     int pl4=f4*3-no/3;
  336.  
  337.     byte *optr=&ptr[no<<8];
  338.  
  339.     for (int count=0; count<256; count++)
  340.     {
  341.         word v1=ra1[(count+pl1)&255];
  342.         word v2=ra2[(count+pl2)&255];
  343.         word v3=ra3[(count+pl3)&255];
  344.         word v4=ra4[(count+pl4)&255];
  345.         word vv=v1+v2+v3+v4;
  346.         long sumb=vv>>9;
  347.         sumb-=minus;
  348.         if (sumb<0)
  349.             sumb=random(2);
  350.         *(optr++)=128+sumb;
  351.     }
  352. }
  353.  
  354. // VOXEL
  355.  
  356. void voxel(void)
  357. {
  358. #if RANDOMIZE
  359.     randomize();
  360. #endif
  361.     zoomfac=ZOOMFAC;
  362. #ifdef CHEAT
  363.     asm mov ax,0x13
  364.     asm int 0x10
  365. #endif
  366.  
  367.     setpalette(VOXELPAL);
  368.  
  369.     seg_C height;
  370.     seg_C color;
  371.     seg_C lookup;
  372.     seg_C sky;
  373.     seg_C randata(4096);
  374.  
  375.     ra1=(word*)&randata.ptr[0];
  376.     ra2=(word*)&randata.ptr[512];
  377.     ra3=(word*)&randata.ptr[1024];
  378.     ra4=(word*)&randata.ptr[1536];
  379.  
  380.     hseg=(long)height.ptr>>16;
  381.     fseg=(long)color.ptr>>16;
  382.     lseg=(long)lookup.ptr>>16; 
  383.  
  384. #ifndef OVERVIEW
  385.     loadsky(sky.ptr);
  386. #endif
  387.  
  388. #ifndef OVERVIEW
  389.     precalc(lookup.ptr, (byte*)copper_32);
  390. #endif
  391.     makerand(ra1);
  392.     makerand(ra2);
  393.     makerand(ra3);
  394.     makerand(ra4);
  395.  
  396.     for (long e=0; e<65536l; e++)
  397.     {
  398.         height.ptr[e]=random(2)+128;
  399.         height.ptr[e]=128;
  400.         color.ptr[e]=59+random(10);
  401.     }
  402.  
  403.     int s;
  404.     seg_C page;
  405.     sseg=(long)page.ptr>>16;
  406.  
  407.     for (s=0; s<1280; s++)
  408.     {
  409.         height.ptr[s]=(s>=4*256)?138:128;
  410.         color.ptr[s]=0;
  411.     }
  412.  
  413.     word screenseg=0xa000;
  414.  
  415.     word xpos=0,ypos=0;
  416.     long dxpos=0,dypos=0;
  417.     float frames=0,dvin=0,vin=M_PI_2,alt=0;
  418.     int done=0,pixsize=1+(relspeed<300);
  419.  
  420.     long estart=timer.readtimer();
  421.     long sofar=timer.elapsed(musicstart,estart);
  422.     long left=END_VOXEL-sofar-1100l;
  423.     int sretn=1-2*(random(100)>20);
  424.     do
  425.     {
  426.         long gaatt=timer.readtimer();
  427.         gaatt=timer.elapsed(estart,gaatt);
  428.         float pro=(float)gaatt/left;
  429.         if (pro>0.90)
  430.         {
  431.             float zfac=1+(pro-0.90)*150;
  432.             zoomfac=ZOOMFAC*zfac*zfac;
  433.         }
  434.  
  435.         if (pressed[59])
  436.             pixsize=1;
  437.         if (pressed[60])
  438.             pixsize=2;
  439.         if (pressed[61])
  440.             interlimit++;
  441.         if (pressed[62])
  442.             interlimit--;
  443.         voypos=ypos>>8;
  444.         voxpos=xpos;
  445.         ofraction=voxpos;
  446.         frames++;
  447.         if (pro>0.80)
  448.             vin+=(pro-0.80)*4.6*sretn;
  449.         dvin+=(pressed[77]-pressed[75])*0.007;
  450.         vin+=dvin;
  451.  
  452.         if (dypos>512)
  453.             dypos=512;
  454.  
  455.         long hit=timer.elapsed(musicstart,timer.readtimer());
  456.         if (hit<HITIT)
  457.             dypos=0;
  458.  
  459.         byte test1=xpos>>8;
  460.         byte test0=test1-15;
  461.         byte test2=test2+15;
  462.         byte testy=voypos+30;
  463.         word testypos=256*testy;
  464.  
  465.         int der0=height.ptr[test0+testypos];
  466.         int der2=height.ptr[test2+testypos];
  467.  
  468.         float vfacx=sin(vin);
  469.         float vkorr=cos(vin);
  470.         if (dypos)
  471.             dvin+=vkorr*0.01;
  472.         dvin*=0.975;
  473.         if (vfacx<0)
  474.             vfacx=0;
  475.         int hdiff=(der2-der0);
  476.         if (dypos)
  477.             dvin+=-hdiff*0.00009;
  478.  
  479.         if (dypos)
  480.             xpos+=dxpos-1000*cos(vin);
  481.         ypos+=dypos;
  482.  
  483.         static word voypos2;
  484.  
  485.         makestrip(height.ptr,voypos2+254,pro);
  486.         makestrip(height.ptr,voypos2+255,pro);
  487.         colorstrip(height.ptr,color.ptr,(voypos2+252)&255);
  488.         colorstrip(height.ptr,color.ptr,(voypos2+253)&255);
  489.  
  490.         alt+=2*(pressed[72]-pressed[80]);
  491.  
  492.         voypos2+=dypos/256;
  493.         testy=voypos2;
  494.         byte xx=(xpos>>8);
  495.         xx+=4;
  496.         float inall=height.ptr[xx+256*testy];
  497.         float inall2=height.ptr[xx+256*testy+5120];
  498.         if (pro>0.8)
  499.             inall2=128+((pro-0.8)*1270);
  500.         if (inall2>inall)
  501.             inall=inall2;
  502.         inall-=110;
  503.         if (inall>127)
  504.             inall=127;
  505.         float qq=20;
  506.         if (inall>alt)
  507.             qq=3;
  508.         if (!dypos)
  509.             qq=3;
  510.         float diff=(inall-alt)/qq;
  511.         alt+=diff;
  512.         if (diff<0.5 && !dypos)
  513.             dypos=512;
  514.  
  515.         fit((float)0,alt,(float)127);
  516.         alti=alt;
  517.     asmvox();
  518.         interpolate();
  519.         fixsky(page.ptr,sky.ptr,(xpos+50000000l)/256l,0);
  520. #ifdef OVERVIEW
  521.         visvox(color.ptr,vin,screenseg,pixsize);
  522. #else
  523.         visvox(page.ptr,vin,screenseg,pixsize);
  524. #endif
  525.  
  526. #ifndef CHEAT
  527.         yoffset((screenseg-0xa000)/5);
  528.         screenseg+=240*5;
  529.         if (screenseg==240*5*3l+0xa000)
  530.             screenseg=0xa000;
  531. #endif
  532.  
  533.         done|=(pro>=1.0);
  534.     }while (!done);
  535.  
  536.     byte col=getpixel(160,100);
  537.     float rr=rgb[col*3];
  538.     float rg=rgb[col*3+1];
  539.     float rb=rgb[col*3+2];
  540.     for (int count=0; count<70; count++)
  541.     {
  542.         float fac=1-count/69.0;
  543.         byte r=rr*fac;
  544.         byte g=rg*fac;
  545.         byte b=rb*fac;
  546.         frame();
  547.         setrgb(col,r,g,b);
  548.     }
  549. }
  550.  
  551.  
  552.  
  553.