Colour.cpp
上传用户:lhwx1029
上传日期:2013-03-07
资源大小:1173k
文件大小:4k
源码类别:

3D图形编程

开发平台:

Visual C++

  1. /** 3DGPL *************************************************
  2.  * ()                                                     *
  3.  * Handling of colour and lighting.                       *
  4.  *                                                        *
  5.  * Ifdefs:                                                *
  6.  *  _CI_                     Colour/Intensity model;      *
  7.  *  _RGB_                    RGB model;                   *
  8.  *                                                        *
  9.  * Defines:                                               *
  10.  *  CL_init_colours          Initialization;              *
  11.  *  CL_colour                Composing a colour;          *
  12.  *  CL_light                 Lighting a colour.           *
  13.  *                                                        *
  14.  * (c) 1995-98 Sergei Savchenko, (savs@cs.mcgill.ca)      *
  15. **********************************************************/
  16. #include "Colour.h"               /* colour models. */
  17. int CL_div[CL_COLOUR_LEVELS][CL_LIGHT_LEVELS];
  18. HW_pixel *CL_intensities;                   /* intensity table */
  19. #if defined(_CI_)
  20. struct CL_palette *CL_colours;              /* saved for hardware access */
  21. #endif
  22. /**********************************************************
  23.  * Initializing internal lookup tables.                   *
  24.  *                                                        *
  25.  * SETS: CL_intensity (when _CI_) CL_div (when _RGB_)     *
  26.  * -----                                                  *
  27. **********************************************************/
  28. #if defined(_CI_)
  29. void CL_init_colour(struct CL_palette *palette)
  30. {
  31.  CL_colours=palette;
  32.  CL_intensities=palette->cl_intensity_table;/* for palette based models */
  33. }
  34. #endif
  35. //初始化内部查找表,具体不是特别清楚
  36. #if defined(_RGB_)
  37. void CL_init_colour(void)
  38. {
  39.  int i,j;                                   /* for RGB modes */
  40.  for(i=0;i<CL_COLOUR_LEVELS;i++)
  41.  {
  42.   for(j=0;j<CL_LIGHT_LEVELS;j++)
  43.   {
  44. //CL_LIGHT_MASK is for clamping light???? 0xff???
  45.    CL_div[i][j]=(i*j)/CL_LIGHT_MASK;
  46.   }
  47.  }
  48. }
  49. #endif
  50. /**********************************************************
  51.  * Composing a colour.                                    *
  52.  *                                                        *
  53.  * RETURNS: Packed colour, storable in a colourmap.       *
  54.  * --------                                               *
  55. **********************************************************/
  56. #if defined(_RGB_)
  57. HW_pixel CL_colour(int red_light,
  58.                    int green_light,
  59.                    int blue_light
  60.                   )
  61. {
  62.  return((CL_clamp_red(red_light)<<CL_RED_SHIFT) |
  63.         (CL_clamp_green(green_light)<<CL_GREEN_SHIFT) |
  64.         (CL_clamp_blue(blue_light)<<CL_BLUE_SHIFT)
  65.        );
  66. }
  67. #endif
  68. /**********************************************************
  69.  * Lighting a single pixel.                               *
  70.  *                                                        *
  71.  * RETURNS: Packed colour, storable in a colourmap.       *
  72.  * --------                                               *
  73. **********************************************************/
  74. #if defined(_CI_)
  75. HW_pixel CL_light(HW_pixel colour,int light)
  76. {
  77.  return(CL_intensities[colour+(CL_clamp_light(light)<<CL_LOG_COLOUR_LEVELS)]);
  78. }
  79. #endif
  80. #if defined(_RGB_)
  81. HW_pixel CL_light(HW_pixel colour,int red_light,
  82.                                   int green_light,
  83.                                   int blue_light
  84.                   )
  85. {
  86.  return((CL_div[CL_red(colour)][CL_clamp_light(red_light)]<<CL_RED_SHIFT)       |
  87.         (CL_div[CL_green(colour)][CL_clamp_light(green_light)]<<CL_GREEN_SHIFT) |
  88.         (CL_div[CL_blue(colour)][CL_clamp_light(blue_light)]<<CL_BLUE_SHIFT)
  89.        );
  90. }
  91. #endif
  92. /**********************************************************/