home *** CD-ROM | disk | FTP | other *** search
/ CD Action 16 B / cdactioncoverdisc / dcolony / data.z / EXPLOS~1.TXT < prev    next >
Text File  |  1997-07-03  |  4KB  |  89 lines

  1. Okay, the explosion problem:
  2.  
  3. We have either a (5 6 5) or a (5 5 5) <r g b> value. We have to
  4. mix this colour with a colour <r_mix g_mix b_mix>. A scalar from
  5. 0 to 1, alpha_mix, describes how much each colour contributes to
  6. the final colour. The final colour, <r' g' b'>, is computed using 
  7. the following formula:
  8.  
  9. <r' g' b'> = <r g b> * alpha_mix + <r_mix g_mix b_mix> * (1 - alpha_mix)
  10.  
  11. This is a simple, but expensive operation.  Here is one proposed method
  12. for optmizing this operation.
  13.  
  14. The value (1 - alpha_mix) * <r_mix g_mix b_mix> can be precomputed and 
  15. converted to a (5 6 5) or (5 5 5) colour value.
  16.  
  17. The result of remaining operation, <r g b> * alpha_mix, will be pre-computed
  18. and stored in a 128k table (64k table entries * 2 bytes per entry). The
  19. table is constructed as follows:
  20.  
  21. There are 64k table entries, so each value in the table can be indexed with
  22. a 16 bit integer. This integer will be constructed using the 15 or 16 bit
  23. colour value, <r g b>, and the alpha_mix value. First, the alpha_mix value
  24. into an integer, alpha_int, using the formula 
  25.  
  26. alpha_int = floor(alpha_mix*15). 
  27.  
  28.  
  29. alpha_int can have values from 0 to 15, so it can be represented in 4 bits.
  30. cann this bits a3, a2, a1, and a0.
  31.  
  32. In the case of the (5 6 5) rgb value, 5 bits are used to represent red,
  33. 6 bits green, and 5 bits blue. Call these bits r4, r3, r2, r1, r0,
  34. g5, g4, g3, g2, g1, g0, b4, b3, b2, b1, b0. 
  35.  
  36.  
  37. 15   14   13   12   11   10    9    8    7    6    5    4    3    2    1    0
  38. -----------------------------------------------------------------------------
  39. r4 | r3 | r2 | r1 | r0 | g5 | g4 | g3 | g2 | g1 | g0 | b4 | b3 | b2 | b1 | b0
  40. -----------------------------------------------------------------------------
  41.  
  42. Bits r0, g1, g0, and b0 are replaced by a3, a2, a1, and a0. The information
  43. that was contained in these bits is lost, so the bit values have been chosen
  44. to mimimalize the error in the colour. The new bit pattern is
  45.  
  46. 15   14   13   12   11   10    9    8    7    6    5    4    3    2    1    0
  47. -----------------------------------------------------------------------------
  48. r4 | r3 | r2 | r1 | a3 | g5 | g4 | g3 | g2 | a2 | a1 | b4 | b3 | b2 | b1 | a0
  49. -----------------------------------------------------------------------------
  50.  
  51. This is the bit pattern used for the table lookup.
  52.  
  53. following psudio code algorithm shows how a pixel would be drawn on a 5 6 5
  54. display.
  55.  
  56. pval = explosion_sprite_pixel_value
  57. aval = special_effects_palette[pval] . add_colour;
  58. rval = special_effects_palette[pval] . alpha_value;
  59. pixel = screen_pixel_value
  60. pixel &= 0xf79e            /* 1111 0111 1001 1110 */
  61. pixel |= rval
  62. pixel = alpha_remap[pixel]
  63. pixel + = aval
  64. screen_pixel_value = pixel
  65.  
  66.  
  67. Explosion_sprite_pixel_value is a sprite with pixels that consist of 8 bit
  68. offsets into the special effects palette. In the 16 bit version, each entry
  69. in this palette has two fields; these fields are add_colour, the pre-
  70. computed (1 - alpha_mix) * <r_mix g_mix b_mix> values, and alpha_value,
  71. a 16 bit integer that contains bits a3, a2, a1, and a0 in the following
  72. format:
  73.  
  74. 15   14   13   12   11   10    9    8    7    6    5    4    3    2    1    0
  75. -----------------------------------------------------------------------------
  76.  0 |  0 |  0 |  0 | a3 |  0 |  0 |  0 |  0 | a2 | a1 |  0 |  0 |  0 |  0 | a0
  77. -----------------------------------------------------------------------------
  78.  
  79. Bits 11, 6, 5, and 0 and masked off of the pixel value, the alpha value is
  80. ored with the pixel value, this value is used to look up the value
  81. <r g b> * alpha_mix, and then add_colour, which represents 
  82. (1 - alpha_mix) * <r_mix g_mix b_mix>, is added to the result.
  83.  
  84. Both the alpha_remap and special_effects_palette tables are relatively
  85. inexpensive to compute, so niether need to be computed and stored on disk.
  86. This means the algorithm should be versatile enough to handle other 16 bit 
  87. colour formats, such as (5 5 5).
  88.  
  89.