home *** CD-ROM | disk | FTP | other *** search
/ Cutting-Edge 3D Game Programming with C++ / CE3DC++.ISO / BOOK / CHAP12 / PALSHADE.CPP < prev    next >
C/C++ Source or Header  |  1996-01-24  |  3KB  |  108 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. #include "PalShade.HPP"
  20.  
  21. void ShadeDat::GenTable ( double R2, double G2, double B2, 
  22.                           RGBQUAD *Pal )
  23.    {
  24.    long Count, N;
  25.    double R, G, B, R1, G1, B1, DeltaR, DeltaG, 
  26.           DeltaB, StepR, StepG, StepB;
  27.  
  28.    #ifdef DOS
  29.    double IPer;
  30.    cout << "\nGenerating shade table:\n";
  31.    #endif
  32.  
  33.    for ( Count = 0; Count < COLOR_COUNT; Count++ )
  34.        {
  35.        #ifdef DOS
  36.        IPer = ( double ) Count / ( double ) COLOR_COUNT * 101.0F;
  37.        cout << ( long ) IPer << "%\r";
  38.        #endif
  39.        R1 = Pal [ Count ].rgbRed;
  40.        G1 = Pal [ Count ].rgbGreen;
  41.        B1 = Pal [ Count ].rgbBlue;
  42.        DeltaR = R2 - R1;
  43.        DeltaG = G2 - G1;
  44.        DeltaB = B2 - B1;
  45.        R = R1;
  46.        G = G1;
  47.        B = B1;
  48.        StepR = DeltaR / ( double ) SHADE_COUNT;
  49.        StepG = DeltaG / ( double ) SHADE_COUNT;
  50.        StepB = DeltaB / ( double ) SHADE_COUNT;
  51.        for ( N = 0; N < SHADE_COUNT; N++ )
  52.            {
  53.            Shade [ ( N * COLOR_COUNT ) + Count ] = ( BYTE ) 
  54.                                  GetColor ( ( long ) R, 
  55.                                             ( long ) G, 
  56.                                             ( long ) B, Pal );
  57.            R += StepR;
  58.            G += StepG;
  59.            B += StepB;
  60.            }
  61.        }
  62.    }
  63.  
  64. int ShadeDat::LoadTable ( char *FileName )
  65.   {
  66.   FILE *InFile;
  67.   if ( ( InFile = fopen ( FileName, "rb" ) ) == 0 )
  68.      return 0;
  69.   fread ( &Shade, sizeof Shade, 1, InFile );
  70.   fclose ( InFile );
  71.   return 1;
  72.   }
  73.  
  74. int ShadeDat::SaveTable ( char *FileName )
  75.   {
  76.   FILE *OutFile;
  77.   if ( ( OutFile = fopen ( FileName, "wb" ) ) == 0 )
  78.      return 0;
  79.   fwrite ( &Shade, sizeof Shade, 1, OutFile );
  80.   fclose ( OutFile );
  81.   return 1;
  82.   }
  83.  
  84. int GetColor ( int Red, int Green, int Blue, RGBQUAD *Pal )
  85.   {
  86.   double Dist [ COLOR_COUNT ], DRed, DGreen, DBlue, DistC;
  87.   int Count, CloseC;
  88.   for ( Count = 0; Count < COLOR_COUNT; Count++ )
  89.       {
  90.       DRed   = ( Red   - Pal [ Count ].rgbRed );
  91.       DGreen = ( Green - Pal [ Count ].rgbGreen );
  92.       DBlue  = ( Blue  - Pal [ Count ].rgbBlue );
  93.       Dist [ Count ] = sqrt ( DRed   * DRed +
  94.                               DGreen * DGreen +
  95.                               DBlue  * DBlue );
  96.       }
  97.   CloseC = 0;
  98.   DistC  = Dist [ 0 ];
  99.   for ( Count = 0; Count < COLOR_COUNT; Count++ )
  100.       {
  101.       if ( Dist [ Count ] < DistC )
  102.          {
  103.          DistC  = Dist [ Count ];
  104.          CloseC = Count;
  105.          }
  106.       }
  107.   return CloseC;
  108.   }