home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 15 / CD_ASCQ_15_070894.iso / vrac / texmpsrc.zip / TEXMAP.C < prev    next >
C/C++ Source or Header  |  1994-01-14  |  5KB  |  237 lines

  1. ///////////////////////////////////////////
  2. ///////////////////////////////////////////
  3. //                                       //
  4. // Slimy Texture Mapping.                //
  5. //                                       //
  6. // by F.Becker aka -=* SliQ *=-          //
  7. //                                       //
  8. // Greet me, if you use it...            //
  9. //                                       //
  10. //                                       //
  11. // C++ commenting style - I prefer it,   //
  12. // what can I say?                       //
  13. //                                       //
  14. ///////////////////////////////////////////
  15. ///////////////////////////////////////////
  16.  
  17. #include <stdio.h>
  18. #include <math.h>
  19.  
  20. // Fdegrees to radians rad=deg*Fd2r
  21. // Fdegrees range from 0 to 1023.
  22. #define        Fd2r    3.1415926535/512
  23.  
  24. #define         Xsize    160
  25. #define        Ysize   120
  26.  
  27. typedef struct {
  28.     unsigned char r;
  29.     unsigned char g;
  30.     unsigned char b;
  31. } rgbpal;
  32.  
  33.  
  34. // NOTE: longs represent 16.16 fixed point values
  35.  
  36. unsigned char far thepic[65535];
  37. rgbpal            pal[256];
  38. long          slimeS[128],slimeC[128];
  39. long           xo,yo;
  40.  
  41. void Scron(){
  42. asm{
  43.       //set mode 13h
  44.       pusha
  45.       mov    ax,0x13
  46.       int    0x10
  47.       popa
  48.    }
  49. }
  50.  
  51.  
  52. void Scroff(){
  53. asm{
  54.       //set mode 03h
  55.       pusha
  56.       mov    ax,0x03
  57.       int    0x10
  58.       popa
  59.    }
  60. }
  61.  
  62.  
  63. void SetCol(char ColReg, char Red, char Green, char Blue ){
  64. asm{
  65.       mov    dx,0x03c8
  66.       mov   al,[ColReg]
  67.       out   dx,al
  68.  
  69.       inc   dx
  70.  
  71.       mov   al,[Red]
  72.       out   dx,al
  73.       mov   al,[Green]
  74.       out   dx,al
  75.       mov   al,[Blue]
  76.       out   dx,al
  77.     }
  78. }
  79.  
  80.  
  81. void load_pic(){
  82.  
  83.   FILE *fp;
  84.   int i,x,y,gray;
  85.  
  86.   fp = fopen( "sliq.raw" , "rb" );
  87.   fread( thepic, 1, 32, fp );      // skip header
  88.   fread( pal   , 1, 768  , fp );   // load palette
  89.   fread( thepic, 1, 65535, fp );   // load picture
  90.   fclose( fp );
  91.  
  92.   // set palette
  93.  
  94.   for( i=0 ; i<256 ; i++ )
  95.     SetCol(i,pal[i].r>>2,pal[i].g>>2,pal[i].b>>2);
  96. }
  97.  
  98. void TexScreen( long dx , long dy,
  99.         long adx, long ady){
  100.  
  101.   // postiton on screen
  102.   int  x,y;
  103.  
  104.   // start position and current position in 16.16 fixed point
  105.   long ystart,xstart,xpos,ypos;
  106.  
  107.   // color!
  108.   unsigned int color;
  109.  
  110.   // slime adjustments for every vertical line
  111.   // NOTE: picture is drawn by columns not rows.
  112.   int  tmp[256];
  113.  
  114.   // lower precision versions of adx/ady and xpos/ypos ie 8.8 fixed point.
  115.   //   it's sufficient for the relative small (orig.) picture size of 256x256
  116.   int  adx2,ady2,xp2,yp2;
  117.  
  118.   // make a 8.8 version of ad..
  119.   // we dont worry about the upper 8 bits of adx/ady, because
  120.   // they are just sign extensions in this program...
  121.   adx2=adx>>8;
  122.   ady2=ady>>8;
  123.  
  124.   // precalculate slime values for vertical line (used in inner loop)
  125.   for( y=0; y<Ysize ; y++ ){
  126.     tmp[y]=ady2+(slimeS[y&127]>>8);
  127.   }
  128.  
  129.   //offset on screen - use register
  130.   _SI = 0;
  131.  
  132.   xstart=xo;
  133.   ystart=yo;
  134.   for( x=0; x<Xsize ; x++ ){
  135.     xpos=xstart;
  136.     ypos=ystart;
  137.  
  138.     // make a 8.8 version of .pos.
  139.     xp2=xpos>>8;
  140.     yp2=ypos>>8;
  141.  
  142.     for( y=0; y<Ysize ; y++ ){
  143.  
  144.       //calc position within original
  145.       //which has the convenient size of 256x256
  146.       //  use the hi-bytes which is the integer part of the 8.8 fixed pt.
  147.       //bh is y and bl is x.
  148.       asm{
  149.       mov    bx,[yp2]
  150.       mov    bl,byte ptr [xp2]+1
  151.       }
  152.  
  153.       // get the color from the original
  154.       color = thepic[ _BX ];
  155.  
  156.       //set the pixel
  157.       asm{
  158.       // Set es to video segment
  159.       mov    ax,0xa000
  160.       mov    es,ax
  161.       mov   ax,[color]
  162.       mov   es:[si],al
  163.       }
  164.  
  165.       //move to next position
  166.       xp2+=adx2;
  167.       yp2+=tmp[y];
  168.       //move one line down
  169.       _SI+=320;
  170.     }
  171.     // calc new start position for vertical line plus slime adjustment...
  172.     xstart+=dx+slimeC[x&127];
  173.     ystart+=dy;
  174.     // back to top and one right
  175.     _SI-=(320*Ysize)-1;
  176.   }
  177. }
  178.  
  179.  
  180. int main(){
  181.  
  182.   int deg;
  183.   long dx,dy;
  184.  
  185.   Scron();
  186.   load_pic();
  187.  
  188.   for( deg=0 ; deg <128; deg++ ){
  189.     slimeS[deg]=(sin((deg<<3)*Fd2r)*65536.0);
  190.     slimeC[deg]=(cos((deg<<3)*Fd2r)*65536.0);
  191.  
  192. //  Note: you dont have to use sin and cos to get a
  193. //        slime effect. Essentially any (cont.) function/curve
  194. //      which will start and end with the same Y-value
  195. //       will work. (ie. slime[0]=slime[127].) Play around with
  196. //      it - use a bezier curve, maybe. Be creative...
  197. //    slimeS[deg]=(127-deg)*log(deg+1)*536;
  198. //    slimeC[deg]=(127-deg)*log(deg*deg+1)*136;
  199.   }
  200.  
  201.   //tilt in Fdegrees
  202.   deg=0;
  203.  
  204.   //offset into (original) picture
  205.   xo=0;
  206.   yo=0;
  207.  
  208.   while( !kbhit() ){
  209.  
  210.     //calc delta X and delta Y (the slope)
  211.     //ie basic direction across (orig.) picture
  212.     //  in texscreen we'll follow a sine-like curve to
  213.     //  create slime effect.
  214.     dy=((sin(deg*Fd2r)*160000.0));
  215.     dx=((cos(deg*Fd2r)*160000.0));
  216.  
  217.     //the vector (-dy,dx) is perpendicular to (dx,dy)
  218.     //  the basic shape should be a rectangle.
  219.     //  (remove the minus sign and you'll see what I mean.-)
  220.  
  221.     TexScreen(  dx,dy,  -dy,dx  );
  222.  
  223.     //keep on turning
  224.     deg+=2;
  225.  
  226.     // move offset a bit across (orig.) picture
  227.     xo+=(1253L<<5);
  228.     yo+=(1534L<<5);
  229.   }
  230.  
  231.   // back to text
  232.   Scroff();
  233.  
  234.   return(0);
  235.  
  236. }
  237.