home *** CD-ROM | disk | FTP | other *** search
/ Cutting-Edge 3D Game Programming with C++ / CE3DC++.ISO / BOOK / CHAP11 / MORPH / PALSHADE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-30  |  2.8 KB  |  111 lines

  1. //
  2. // File name: PalShade.CPP
  3. //
  4. // Description: The support file for a palette shading class
  5. //
  6. // Author: John De Goes
  7. //
  8. // Project: Cutting Edge 3D Game Programming
  9. //
  10.  
  11. #define DOS
  12.  
  13. #ifdef DOS
  14.   #include <IOStream.H>
  15. #endif
  16.  
  17. #include <Math.H>
  18.  
  19. #ifndef PALSHADEHPP
  20.   #define PALSHADEHPP
  21.   #include "PalShade.HPP"
  22. #endif
  23.  
  24. void ShadeDat::GenTable ( double R2, double G2, double B2, 
  25.                           RGBQUAD *Pal )
  26.    {
  27.    long Count, N;
  28.    double R, G, B, R1, G1, B1, DeltaR, DeltaG, 
  29.           DeltaB, StepR, StepG, StepB;
  30.  
  31.    #ifdef DOS
  32.    double IPer;
  33.    cout << "\nGenerating shade table:\n";
  34.    #endif
  35.  
  36.    for ( Count = 0; Count < COLOR_COUNT; Count++ )
  37.        {
  38.        #ifdef DOS
  39.        IPer = ( double ) Count / ( double ) COLOR_COUNT * 101.0F;
  40.        cout << ( long ) IPer << "%\r";
  41.        #endif
  42.        R1 = Pal [ Count ].rgbRed;
  43.        G1 = Pal [ Count ].rgbGreen;
  44.        B1 = Pal [ Count ].rgbBlue;
  45.        DeltaR = R2 - R1;
  46.        DeltaG = G2 - G1;
  47.        DeltaB = B2 - B1;
  48.        R = R1;
  49.        G = G1;
  50.        B = B1;
  51.        StepR = DeltaR / ( double ) SHADE_COUNT;
  52.        StepG = DeltaG / ( double ) SHADE_COUNT;
  53.        StepB = DeltaB / ( double ) SHADE_COUNT;
  54.        for ( N = 0; N < SHADE_COUNT; N++ )
  55.            {
  56.            Shade [ ( N * COLOR_COUNT ) + Count ] = ( BYTE ) 
  57.                                  GetColor ( ( long ) R, 
  58.                                             ( long ) G, 
  59.                                             ( long ) B, Pal );
  60.            R += StepR;
  61.            G += StepG;
  62.            B += StepB;
  63.            }
  64.        }
  65.    }
  66.  
  67. int ShadeDat::LoadTable ( char *FileName )
  68.   {
  69.   FILE *InFile;
  70.   if ( ( InFile = fopen ( FileName, "rb" ) ) == 0 )
  71.      return 0;
  72.   fread ( &Shade, sizeof Shade, 1, InFile );
  73.   fclose ( InFile );
  74.   return 1;
  75.   }
  76.  
  77. int ShadeDat::SaveTable ( char *FileName )
  78.   {
  79.   FILE *OutFile;
  80.   if ( ( OutFile = fopen ( FileName, "wb" ) ) == 0 )
  81.      return 0;
  82.   fwrite ( &Shade, sizeof Shade, 1, OutFile );
  83.   fclose ( OutFile );
  84.   return 1;
  85.   }
  86.  
  87. int GetColor ( int Red, int Green, int Blue, RGBQUAD *Pal )
  88.   {
  89.   double Dist [ COLOR_COUNT ], DRed, DGreen, DBlue, DistC;
  90.   int Count, CloseC;
  91.   for ( Count = 0; Count < COLOR_COUNT; Count++ )
  92.       {
  93.       DRed   = ( Red   - Pal [ Count ].rgbRed );
  94.       DGreen = ( Green - Pal [ Count ].rgbGreen );
  95.       DBlue  = ( Blue  - Pal [ Count ].rgbBlue );
  96.       Dist [ Count ] = sqrt ( DRed   * DRed +
  97.                               DGreen * DGreen +
  98.                               DBlue  * DBlue );
  99.       }
  100.   CloseC = 0;
  101.   DistC  = Dist [ 0 ];
  102.   for ( Count = 0; Count < COLOR_COUNT; Count++ )
  103.       {
  104.       if ( Dist [ Count ] < DistC )
  105.          {
  106.          DistC  = Dist [ Count ];
  107.          CloseC = Count;
  108.          }
  109.       }
  110.   return CloseC;
  111.   }