home *** CD-ROM | disk | FTP | other *** search
/ Learn 3D Graphics Programming on the PC / Learn_3D_Graphics_Programming_on_the_PC_Ferraro.iso / rwwin / rtramp.c_ / rtramp.bin
Text File  |  1995-11-14  |  7KB  |  194 lines

  1. /*********************************************************************
  2.  *
  3.  * File:     rtramp.c
  4.  *
  5.  * Abstract: Simple module to assist with generating custom ramps
  6.  *           for RenderWare smooth shading.
  7.  *
  8.  * Author:   Colin McCartney
  9.  *
  10.  * Date:     02/03/95
  11.  *
  12.  *********************************************************************/
  13.  
  14. /*********************************************************************
  15.  *
  16.  * Include files.
  17.  *
  18.  *********************************************************************/
  19.  
  20. #include "rtramp.h"
  21.  
  22. /*********************************************************************
  23.  *
  24.  * Functions.
  25.  *
  26.  *********************************************************************/
  27.  
  28. /*********************************************************************/
  29. /* convert from HSV to RED, GREEN AND BLUE */
  30. void
  31. RtHSVToRGB(RwReal h, RwReal s, RwReal v, RwByte *r, RwByte *g, RwByte *b)
  32. {
  33.     RwReal f;
  34.     RwReal p1;
  35.     RwReal p2;
  36.     RwReal p3;
  37.     int    i;
  38.  
  39.     if (h < CREAL(0.0))
  40.     h = CREAL(0.0);
  41.     else if (h > CREAL(1.0))
  42.     h = CREAL(1.0);
  43.  
  44.     h  = RMul(h, CREAL(6.0));
  45.     i  = (int)REAL2INT(h);
  46.     f  = RSub(h, INT2REAL(i));
  47.     p1 = RMul(v, RSub(CREAL(1.0), s));
  48.     p2 = RMul(v, RSub(CREAL(1.0), RMul(s, f)));
  49.     p3 = RMul(v, RSub(CREAL(1.0), RMul(s, RSub(CREAL(1.0), f))));
  50.     switch (i)          
  51.     {
  52.     case 0:
  53.         *r = (RwByte)REAL2INT(RMul(v,  CREAL(255)));
  54.         *g = (RwByte)REAL2INT(RMul(p3, CREAL(255)));
  55.         *b = (RwByte)REAL2INT(RMul(p1, CREAL(255)));
  56.         break;
  57.     case 1:
  58.         *r = (RwByte)REAL2INT(RMul(p2, CREAL(255)));
  59.         *g = (RwByte)REAL2INT(RMul(v,  CREAL(255)));
  60.         *b = (RwByte)REAL2INT(RMul(p1, CREAL(255)));
  61.         break;
  62.     case 2:
  63.         *r = (RwByte)REAL2INT(RMul(p1, CREAL(255)));
  64.         *g = (RwByte)REAL2INT(RMul(v,  CREAL(255)));
  65.         *b = (RwByte)REAL2INT(RMul(p3, CREAL(255)));
  66.         break;
  67.     case 3:
  68.         *r = (RwByte)REAL2INT(RMul(p1, CREAL(255)));
  69.         *g = (RwByte)REAL2INT(RMul(p2, CREAL(255)));
  70.         *b = (RwByte)REAL2INT(RMul(v,  CREAL(255)));
  71.         break;
  72.     case 4:
  73.         *r = (RwByte)REAL2INT(RMul(p3, CREAL(255)));
  74.         *g = (RwByte)REAL2INT(RMul(p1, CREAL(255)));
  75.         *b = (RwByte)REAL2INT(RMul(v,  CREAL(255)));
  76.         break;
  77.     case 5:
  78.         *r = (RwByte)REAL2INT(RMul(v,  CREAL(255)));
  79.         *g = (RwByte)REAL2INT(RMul(p1, CREAL(255)));
  80.         *b = (RwByte)REAL2INT(RMul(p2, CREAL(255)));
  81.         break;
  82.     }
  83. }
  84.  
  85. /*********************************************************************/
  86. /* Generate the ramp.   A turn point is created that is 4/5 through the ramp. The first 4/5 are built with an
  87.  incrementing brightness. Assuming MAXLUM=23, the turnpoint would equal 5.5. Now we subtract this 
  88. from MAXLUM (23-5.5=17).  So the turn point is at index 17.
  89.  
  90. The first 4/5 of the ramp ranges from 0 to 16.  Now the brightness is incremented as the index ranges 
  91. from 0 to 16 by .8/17.  Consequently, the ramp is incremented such that the beginning of the brightness 
  92. ramp is 0.0 and the end (after 16 increments) is 0.8.
  93.  
  94. The next ramp, from 4/5 to 1 (of MAXLUM). The saturation increment is set to a negative value such that 
  95. the saturation decreases.  The saturation decreases at a rate dependent of the saturation value such that the 
  96. saturation decreases to 0.0 at the end of the ramp.  The brightness starts at 0.8 and it increments by 
  97. 0.2/17.  Consequently, the brightness ranges from 0.8 to 1.0 in this section of the ramp. 
  98.  */
  99.  
  100. void
  101. RtGenRamp(RwPaletteEntry* entry, RwReal hue, RwReal sat)
  102. {
  103.     RwReal bri, sb, ss;
  104.     int range;
  105.     int turnpoint;
  106.  
  107.    // turnpoint = 0.5 + MAXLUM/4
  108.     turnpoint = REAL2INT(RAdd(RDiv(INT2REAL(MAXLUM), CREAL(4.0)),
  109.                   CREAL(0.5)));
  110.  
  111.  
  112.  /* ****************** Build the first 4/5 of the ramp *********************** */  
  113.    //saturation = 0
  114.    ss = CREAL(0.0);
  115.  
  116.     // brightness increment = 0.8 / (MAXLUM - (0.5 + MAXLUM/4)
  117.     sb = RDiv(CREAL(0.8), INT2REAL(MAXLUM - turnpoint));
  118.  
  119.    // brightness = 0
  120.     bri = CREAL(0.0);
  121.  
  122.     // Modify brightness:   do for 0 to MAXLUM-turnpoint
  123.     for (range = 0; range < MAXLUM - turnpoint; range++)
  124.     {
  125.     // convert from HSV to RED, GREEN AND BLUE
  126.               RtHSVToRGB(hue, sat, bri,
  127.            &entry[range].r, &entry[range].g, &entry[range].b);
  128.  
  129.               // brightness += brightness increment
  130.     bri = RAdd(bri, sb);
  131.     }
  132.  
  133.    /* ****************** Build the second 1/5 of the ramp *********************** */  
  134.  
  135.     //  (-saturation / turnpoint)
  136.     ss = (RwReal) RDiv(-sat, INT2REAL(turnpoint));
  137.  
  138.     // brightness = .8
  139.     bri = CREAL(0.8);
  140.  
  141.    // saturation = 0.2 / turnpoint
  142.     sb = RDiv(CREAL(0.2), INT2REAL(turnpoint));
  143.     
  144.     /* modify saturation and brightness:  do for turnpoint to end.  Saturation falls from sat to 0.0.
  145.      * Brightness increases from 0.8 to 1.0
  146.      */
  147.     for (; range < MAXLUM; range++)
  148.     {
  149.     // convert from HSV to RED, GREEN AND BLUE
  150.     RtHSVToRGB(hue, sat, bri,
  151.            &entry[range].r, &entry[range].g, &entry[range].b);
  152.  
  153.     // saturation += saturation increment
  154.               sat = RAdd(sat, ss);
  155.     // brightness += brightness increment
  156.     bri = RAdd(bri, sb);
  157.     }
  158. }
  159.  
  160. /*********************************************************************/
  161. /*
  162.  * Build a ramp (#ramp) in the palette array beginning at location (ramp# * MAXLUM) for MAXLUM 
  163.  * values based on the hue and saturation (sat) values.
  164.  */
  165. void
  166. RtSetRamp(RwPaletteEntry* palette, int ramp, RwReal hue, RwReal sat)
  167. {
  168.     RwPaletteEntry* rampStart;
  169.     
  170.     rampStart = &palette[ramp * MAXLUM];
  171.     RtGenRamp(rampStart, hue, sat);
  172. }
  173.  
  174. /*********************************************************************/
  175. /* since MAXLUM is 23, we generate ten ramps each ranging from 0-23.  The ramp therefore ranges 
  176. * from palette entry 0 to 230
  177. */
  178. void
  179. RtDefaultPalette(RwPaletteEntry* palette)
  180. {
  181.     RtSetRamp(palette, 0, CREAL(-1.0000), CREAL(0.0));
  182.     RtSetRamp(palette, 1, CREAL(0.00000), CREAL(1.0));
  183.     RtSetRamp(palette, 2, CREAL(0.00000), CREAL(0.5));
  184.     RtSetRamp(palette, 3, CREAL(0.08333), CREAL(1.0));
  185.     RtSetRamp(palette, 4, CREAL(0.16666), CREAL(1.0));
  186.     RtSetRamp(palette, 5, CREAL(0.33333), CREAL(1.0));
  187.     RtSetRamp(palette, 6, CREAL(0.33333), CREAL(0.5));
  188.     RtSetRamp(palette, 7, CREAL(0.50000), CREAL(1.0));
  189.     RtSetRamp(palette, 8, CREAL(0.66666), CREAL(1.0));
  190.     RtSetRamp(palette, 9, CREAL(0.83333), CREAL(1.0));
  191. }
  192.  
  193. /*********************************************************************/
  194.