home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Zone / VRZONE.ISO / mac / PC / REND386 / JIREND / COLORMAP.C < prev    next >
C/C++ Source or Header  |  1993-04-11  |  5KB  |  164 lines

  1. /* Routine to setup and compute colors */
  2. /* for 256-color mode             */
  3.   
  4. /* Written by Dave Stampe Mar 21 1992 */
  5. /* Modified by Bernie Roehl, April 2, 1992 */
  6. /* Substantially upgraded by Dave Stampe, August '92 */
  7.   
  8. /* Copyright 1992 by Dave Stampe and Bernie Roehl.
  9.    May be freely used to write software for release into the public domain;
  10.    all commercial endeavours MUST contact Bernie Roehl and Dave Stampe
  11.    for permission to incorporate any part of this software into their
  12.    products!
  13.  */
  14.   
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <dos.h>
  18.  
  19. #include "rend386.h"
  20. #include "f3dkitd.h"
  21.   
  22. /* colors to use on screen */
  23.   
  24. int screen_clear_color = -1;
  25. int sky_color = -1;
  26. int ground_color = -1;
  27. int wireframe_color = -1;
  28. int highlight_color = -1;
  29. int highest_color = -1;
  30.   
  31. extern int do_horizon;
  32.   
  33. void preset_default_colors(void)
  34. {
  35.    if (highest_color > 254)
  36.    {
  37.       if (screen_clear_color == -1) screen_clear_color = 3;
  38.       if (sky_color == -1) sky_color = 3;
  39.       if (ground_color == -1) ground_color = 0x88;
  40.       if (wireframe_color == -1) wireframe_color = 13;
  41.       if (highlight_color == -1) highlight_color = 15;
  42.    }
  43.    else /* if (highest_color > 14) */
  44.    {
  45.       if (screen_clear_color == -1) screen_clear_color = 10;
  46.       if (sky_color == -1) sky_color = 10;
  47.       if (ground_color == -1) ground_color = 5;
  48.       if (wireframe_color == -1) wireframe_color = 12;
  49.       if (highlight_color == -1) highlight_color = 15;
  50.    }
  51.    do_horizon = (sky_color==ground_color) ? 0 : 1 ;
  52. }
  53.   
  54. void set_colors(int bw)
  55. {
  56.    load_DAC_colors(NULL, highest_color+1, bw);
  57. }
  58.   
  59.   
  60. /* USER POLYGON LIGHTING ROUTINE: DETERMINES POLY COLOR # */
  61.   
  62. /* The 16-bit color the user specifies for a polygon is broken down as
  63.    follows:
  64.                  H R SS CCCC BBBBBBBB
  65.   
  66.    H is the highlight flag (the polygon should be highlighted in
  67.    some way, usually by outlining it in the highlight_color given above).
  68.   
  69.    R is a reserved bit, which should be set to zero
  70.   
  71.    SS is a two-bit field specifying one of four surface types:
  72.   
  73.       00 is a constant-color surface; the 4-bit field CCCC is ignored, and the
  74.          8-bit field BBBBBBBB is used as an absolute color number
  75.   
  76.       01 is a cosine-lit surface; the 4-bit field CCCC specifies one of 16
  77.          basic colors, and the 8-bit brightness field BBBBBBBB is multiplied
  78.          by the cosine of the angle between the light source and the polygon's
  79.          surface normal to provide a 4-bit shading value.
  80.   
  81.       10 is a pseudo-metallic surface; the CCCC field gives the starting hue,
  82.          and the BBBBBBBB value is ignored.  The color will cycle through
  83.          the different shades to give a 'metallic' effect.
  84.   
  85.       11 is a pseudo-transparent surface made up of alternating rows of
  86.          spaced dots; other than that, it behaves like a pseudo-metallic
  87.          surface.
  88.   
  89.    This routine maps the above into an 8-bit color number in the low byte
  90.    of its return value, and passes through the top four bits.
  91.   
  92.  */
  93.   
  94. extern int ambient_light;
  95.   
  96. int user_poly_color(POLY *p, int pcolor)
  97. {
  98.    int hilite = pcolor & 0xF000; /* highlight flag  (MSB) */
  99.    int bright = pcolor & 0xFF; /* mask out albedo (7 bits) */
  100.    int hue = (pcolor & 0x0F00) >> 4; /* 16 * basis color */
  101.    signed int color;
  102.   
  103.    switch(highest_color)
  104.    {
  105.       case 15:
  106.       {
  107.          if (hue == 0) return ((bright & 15) | hilite); /* abs. color */
  108.   
  109.          if ((pcolor & 0x3000) == 0) /* fixed (unlit) color */
  110.          {
  111.             if (bright > 15) bright = 15;
  112.             return (hilite | bright);
  113.          }
  114.   
  115.          color = poly_cosine(p);
  116.   
  117.          if (pcolor & 0x2000) /* develop offset for metal/glass cycle */
  118.          {
  119.             color = (bright >> 6) - (color >> 5) + ((hue+320) >> 6);
  120.             if (color < 0) color = 0;
  121.             if (color > 15) color = 15;
  122.             return (hilite | color);
  123.          }
  124.             /* compute brightness index */
  125.          if (color < 0) color = 0;
  126.   
  127.          color = (( ((color*(128-ambient_light)) >> 7) + ambient_light)
  128.          * (bright>>1) );
  129.          color = ((color>>5)*((hue+768) >> 4))/2300;
  130.   
  131.          if (color < 0) return( hilite );
  132.          if (color > 15) return (15 | hilite);
  133.          return (color | hilite);
  134.       }
  135.   
  136.       case 255:
  137.       {
  138.          if ((pcolor & 0x3000) == 0) /* fixed (unlit) color */
  139.             return hue ? (hilite | hue | ((bright >> 4) & 0x0F)) : (bright | hilite);
  140.   
  141.          color = poly_cosine(p);
  142.   
  143.          if (pcolor & 0x2000) /* develop offset for metal/glass cycle */
  144.          {
  145.             color = (-(color >> 1)) + bright >> 3;
  146.             color = (color & 16) ? (color & 15) | 0x100 : color & 15;
  147.             return (hilite | hue | color);
  148.          }/* compute brightness index */
  149.   
  150.          if (color < 0) color = 0;
  151.          color = (( ((color*(128-ambient_light)) >> 7) + ambient_light)
  152.          * (bright >> 1) ) / 0x440;
  153.          if (color < 1) /* absolute zero: black */
  154.             return (hilite & 0x2000) ? (hilite | hue) : hilite;
  155.          if (color > 15)
  156.             return (15 | hue | hilite);
  157.          return (color | hue | hilite);
  158.       }
  159.   
  160.       default:
  161.          return pcolor;
  162.    }
  163. }
  164.