home *** CD-ROM | disk | FTP | other *** search
- /*********************************************************************
- *
- * File: rtramp.c
- *
- * Abstract: Simple module to assist with generating custom ramps
- * for RenderWare smooth shading.
- *
- * Author: Colin McCartney
- *
- * Date: 02/03/95
- *
- *********************************************************************/
-
- /*********************************************************************
- *
- * Include files.
- *
- *********************************************************************/
-
- #include "rtramp.h"
-
- /*********************************************************************
- *
- * Functions.
- *
- *********************************************************************/
-
- /*********************************************************************/
- /* convert from HSV to RED, GREEN AND BLUE */
- void
- RtHSVToRGB(RwReal h, RwReal s, RwReal v, RwByte *r, RwByte *g, RwByte *b)
- {
- RwReal f;
- RwReal p1;
- RwReal p2;
- RwReal p3;
- int i;
-
- if (h < CREAL(0.0))
- h = CREAL(0.0);
- else if (h > CREAL(1.0))
- h = CREAL(1.0);
-
- h = RMul(h, CREAL(6.0));
- i = (int)REAL2INT(h);
- f = RSub(h, INT2REAL(i));
- p1 = RMul(v, RSub(CREAL(1.0), s));
- p2 = RMul(v, RSub(CREAL(1.0), RMul(s, f)));
- p3 = RMul(v, RSub(CREAL(1.0), RMul(s, RSub(CREAL(1.0), f))));
- switch (i)
- {
- case 0:
- *r = (RwByte)REAL2INT(RMul(v, CREAL(255)));
- *g = (RwByte)REAL2INT(RMul(p3, CREAL(255)));
- *b = (RwByte)REAL2INT(RMul(p1, CREAL(255)));
- break;
- case 1:
- *r = (RwByte)REAL2INT(RMul(p2, CREAL(255)));
- *g = (RwByte)REAL2INT(RMul(v, CREAL(255)));
- *b = (RwByte)REAL2INT(RMul(p1, CREAL(255)));
- break;
- case 2:
- *r = (RwByte)REAL2INT(RMul(p1, CREAL(255)));
- *g = (RwByte)REAL2INT(RMul(v, CREAL(255)));
- *b = (RwByte)REAL2INT(RMul(p3, CREAL(255)));
- break;
- case 3:
- *r = (RwByte)REAL2INT(RMul(p1, CREAL(255)));
- *g = (RwByte)REAL2INT(RMul(p2, CREAL(255)));
- *b = (RwByte)REAL2INT(RMul(v, CREAL(255)));
- break;
- case 4:
- *r = (RwByte)REAL2INT(RMul(p3, CREAL(255)));
- *g = (RwByte)REAL2INT(RMul(p1, CREAL(255)));
- *b = (RwByte)REAL2INT(RMul(v, CREAL(255)));
- break;
- case 5:
- *r = (RwByte)REAL2INT(RMul(v, CREAL(255)));
- *g = (RwByte)REAL2INT(RMul(p1, CREAL(255)));
- *b = (RwByte)REAL2INT(RMul(p2, CREAL(255)));
- break;
- }
- }
-
- /*********************************************************************/
- /* Generate the ramp. A turn point is created that is 4/5 through the ramp. The first 4/5 are built with an
- incrementing brightness. Assuming MAXLUM=23, the turnpoint would equal 5.5. Now we subtract this
- from MAXLUM (23-5.5=17). So the turn point is at index 17.
-
- The first 4/5 of the ramp ranges from 0 to 16. Now the brightness is incremented as the index ranges
- from 0 to 16 by .8/17. Consequently, the ramp is incremented such that the beginning of the brightness
- ramp is 0.0 and the end (after 16 increments) is 0.8.
-
- The next ramp, from 4/5 to 1 (of MAXLUM). The saturation increment is set to a negative value such that
- the saturation decreases. The saturation decreases at a rate dependent of the saturation value such that the
- saturation decreases to 0.0 at the end of the ramp. The brightness starts at 0.8 and it increments by
- 0.2/17. Consequently, the brightness ranges from 0.8 to 1.0 in this section of the ramp.
- */
-
- void
- RtGenRamp(RwPaletteEntry* entry, RwReal hue, RwReal sat)
- {
- RwReal bri, sb, ss;
- int range;
- int turnpoint;
-
- // turnpoint = 0.5 + MAXLUM/4
- turnpoint = REAL2INT(RAdd(RDiv(INT2REAL(MAXLUM), CREAL(4.0)),
- CREAL(0.5)));
-
-
- /* ****************** Build the first 4/5 of the ramp *********************** */
- //saturation = 0
- ss = CREAL(0.0);
-
- // brightness increment = 0.8 / (MAXLUM - (0.5 + MAXLUM/4)
- sb = RDiv(CREAL(0.8), INT2REAL(MAXLUM - turnpoint));
-
- // brightness = 0
- bri = CREAL(0.0);
-
- // Modify brightness: do for 0 to MAXLUM-turnpoint
- for (range = 0; range < MAXLUM - turnpoint; range++)
- {
- // convert from HSV to RED, GREEN AND BLUE
- RtHSVToRGB(hue, sat, bri,
- &entry[range].r, &entry[range].g, &entry[range].b);
-
- // brightness += brightness increment
- bri = RAdd(bri, sb);
- }
-
- /* ****************** Build the second 1/5 of the ramp *********************** */
-
- // (-saturation / turnpoint)
- ss = (RwReal) RDiv(-sat, INT2REAL(turnpoint));
-
- // brightness = .8
- bri = CREAL(0.8);
-
- // saturation = 0.2 / turnpoint
- sb = RDiv(CREAL(0.2), INT2REAL(turnpoint));
-
- /* modify saturation and brightness: do for turnpoint to end. Saturation falls from sat to 0.0.
- * Brightness increases from 0.8 to 1.0
- */
- for (; range < MAXLUM; range++)
- {
- // convert from HSV to RED, GREEN AND BLUE
- RtHSVToRGB(hue, sat, bri,
- &entry[range].r, &entry[range].g, &entry[range].b);
-
- // saturation += saturation increment
- sat = RAdd(sat, ss);
- // brightness += brightness increment
- bri = RAdd(bri, sb);
- }
- }
-
- /*********************************************************************/
- /*
- * Build a ramp (#ramp) in the palette array beginning at location (ramp# * MAXLUM) for MAXLUM
- * values based on the hue and saturation (sat) values.
- */
- void
- RtSetRamp(RwPaletteEntry* palette, int ramp, RwReal hue, RwReal sat)
- {
- RwPaletteEntry* rampStart;
-
- rampStart = &palette[ramp * MAXLUM];
- RtGenRamp(rampStart, hue, sat);
- }
-
- /*********************************************************************/
- /* since MAXLUM is 23, we generate ten ramps each ranging from 0-23. The ramp therefore ranges
- * from palette entry 0 to 230
- */
- void
- RtDefaultPalette(RwPaletteEntry* palette)
- {
- RtSetRamp(palette, 0, CREAL(-1.0000), CREAL(0.0));
- RtSetRamp(palette, 1, CREAL(0.00000), CREAL(1.0));
- RtSetRamp(palette, 2, CREAL(0.00000), CREAL(0.5));
- RtSetRamp(palette, 3, CREAL(0.08333), CREAL(1.0));
- RtSetRamp(palette, 4, CREAL(0.16666), CREAL(1.0));
- RtSetRamp(palette, 5, CREAL(0.33333), CREAL(1.0));
- RtSetRamp(palette, 6, CREAL(0.33333), CREAL(0.5));
- RtSetRamp(palette, 7, CREAL(0.50000), CREAL(1.0));
- RtSetRamp(palette, 8, CREAL(0.66666), CREAL(1.0));
- RtSetRamp(palette, 9, CREAL(0.83333), CREAL(1.0));
- }
-
- /*********************************************************************/
-