home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Graphics / Graphics.zip / povsrc31.zip / photons.h < prev    next >
C/C++ Source or Header  |  2000-01-04  |  9KB  |  238 lines

  1. /*******************************************************
  2.  
  3. File:  photons.h
  4. Author: Nathan Kopp
  5.  
  6. This is part of the Photon Mapping custom version of
  7. POV-Ray.
  8.  
  9. ********************************************************/
  10.  
  11.  
  12. #include "point.h"
  13.  
  14. #define MEDIA_INTERACTION 1
  15.  
  16. /* this is for photon block allocation and array mapping functions */
  17. #define PHOTON_BLOCK_POWER 14
  18. /* PHOTON_BLOCK_SIZE must be equal to 2 raised to the power PHOTON_BLOCK_POWER */
  19. #define PHOTON_BLOCK_SIZE (16384)
  20. #define PHOTON_BLOCK_MASK (PHOTON_BLOCK_SIZE-1)
  21. #define INITIAL_BASE_ARRAY_SIZE 100
  22.  
  23. /* vlags for photon mapping */
  24. /* hidensity = object receives photons */
  25. /* passthru = this is pass through object */
  26. /* rfl_on = this object reflects photons */
  27. /* rfl_off = this object does not reflect photons */
  28. /* rfr_on = this object refracts photons */
  29. /* rfr_off = this object does not refract photons */
  30. /* ignore_photons = this object ignores photons */
  31. #define PH_FLAG_TARGET    1
  32. #define PH_FLAG_PASSTHRU  2
  33. #define PH_FLAG_RFL_ON    4
  34. #define PH_FLAG_RFL_OFF   8
  35. #define PH_FLAG_RFR_ON   16
  36. #define PH_FLAG_RFR_OFF  32
  37. #define PH_FLAG_IGNORE_PHOTONS 64
  38.  
  39. /* ------------------------------------------------------ */
  40. /* small colour */
  41. /* ------------------------------------------------------ */
  42. typedef struct small_colour_struct SMALL_COLOUR;
  43.  
  44. struct small_colour_struct {
  45.   unsigned char r,g,b,i;
  46. };
  47.  
  48. /* ------------------------------------------------------ */
  49. /* photon */
  50. /* ------------------------------------------------------ */
  51. typedef struct photon_struct PHOTON;
  52.  
  53. struct photon_struct {
  54.   SNGL_VECT Loc;        /* location */
  55.   SMALL_COLOUR Colour;  /* color & intensity (flux) */
  56.   unsigned char info;   /* info byte for kd-tree */
  57.   char theta, phi;      /* incoming direction */
  58. };
  59.  
  60. /* ------------------------------------------------------ */
  61. /* photon memory allocation stuff */
  62. /* ------------------------------------------------------ */
  63. typedef PHOTON *PHOTON_BLOCK;
  64.  
  65. /* ------------------------------------------------------ */
  66. /* more small_colour stuff */
  67. /* ------------------------------------------------------ */
  68.  
  69. /* ------------------------------------------------------ */
  70. #define UNPACK_COLOUR(LC, C, map) \
  71.   (LC)[0]=C.r/((C.i+1)*map.rangeSelector);\
  72.   (LC)[1]=C.g/((C.i+1)*map.rangeSelector);\
  73.   (LC)[2]=C.b/((C.i+1)*map.rangeSelector)
  74.  
  75. /* ------------------------------------------------------ */
  76. /* These are masks for the info byte - they were intended for
  77.    a threaded kd-tree (which turned out to be a failure.
  78.    They will be removed when I can make sure that they're not
  79.    needed anymore */
  80. #define PH_MASK_XYZ 7  /* 0000 0011 */
  81. #define PH_MASK_RT  8  /* 0000 0100 */
  82. #define PH_MASK_LT  16 /* 0000 1000 */
  83.  
  84. /* initialize the photon */
  85. #define INIT_PHOTON(ph, d)     ph->info=d;
  86.  
  87. /*
  88.   Photon array mapping function
  89.  
  90.   This converts a one-dimensional index into a two-dimensional address
  91.   in the photon array.
  92.  
  93.   Photon memory looks like this:
  94.  
  95.     # -> **********
  96.     # -> **********  <- blocks of photons
  97.     # -> **********
  98.     # -> /
  99.     # -> /
  100.     :
  101.     ^
  102.     |
  103.     Base array.
  104.  
  105.   The base array (which is dynamically resized as needed) contians pointers
  106.   to blocks of photons.  Blocks of photons (of fixed size of a power of two)
  107.   are allocated as needed.
  108.  
  109.   This mapping function converts a one-dimensional index and into a two-
  110.   dimensional address consisting of a block index and an offset within
  111.   that block.
  112.   
  113.   Note that, while this data structure allows easy allocation of photons,
  114.   it does not easily permit deallocation of photons.  Once photons are placed
  115.   into the photon map, they are not removed.
  116. */
  117. /* if this is changed, you must also change swapPhotons() and
  118.    allocatePhoton, both in photons.c */
  119. #define PHOTON_AMF(map, idx)   map[(idx)>>PHOTON_BLOCK_POWER][(idx) & PHOTON_BLOCK_MASK]
  120.  
  121. /* set/clear a photon flag in an object */
  122. #define SET_PH_FLAG(o, flon,floff) o->Ph_Flags |= flon; o->Ph_Flags &= ~floff
  123. #define CLEAR_PH_FLAG(o, flon,floff) o->Ph_Flags |= floff; o->Ph_Flags &= ~flon
  124.  
  125. /* ------------------------------------------------------ */
  126. typedef struct photon_map_struct PHOTON_MAP;
  127.  
  128. struct photon_map_struct {
  129.   /* these 3 are render-thread safe - NOT pre-process thread safe */
  130.   PHOTON_BLOCK *head;   /* the photon map - array of blocks of photons */
  131.   int numBlocks;        /* number of blocks in base array */
  132.   int numPhotons;       /* total number of photons used */
  133.  
  134.   DBL minGatherRad;       /* minimum gather radius */
  135.   DBL gatherRadStep;      /* step size for gather expansion */
  136.   int gatherNumSteps;     /* maximum times to perform 'gather' */
  137.  
  138.   DBL rangeSelector;
  139. };
  140.  
  141. /* ------------------------------------------------------ */
  142. typedef struct photon_options_struct PHOTON_OPTIONS;
  143.  
  144. struct photon_options_struct {
  145.   /* options */
  146.   /* these scene options are thread safe */
  147.   int photonsEnabled;     /* are photons enabled? */
  148.  
  149.   DBL surfaceSeparation;  /* surface photon separation */
  150.   DBL globalSeparation;   /* global photon separation */
  151.  
  152.   DBL expandTolerance; /* see paper for explanation */
  153.   int minExpandCount;     /* see paper for explanation */
  154.  
  155.   int Max_Trace_Level;    /* trace level for light-ray tracing step */
  156.   DBL ADC_Bailout;        /* adc bailout for light-ray tracing step */
  157.  
  158.   DBL jitter;             /* jitter amount */
  159.   DBL autoStopPercent;    /* percent at which to start using autostop feature */
  160.  
  161.   int minGatherCount;     /* minimum number of photons to gather */
  162.   int maxGatherCount;     /* maximum number to gather (size of priority queue) */
  163.  
  164.   char* fileName;         /* file name to load or save caustic photon map */
  165.   int saveFile;           /* do we save our photon map? (if not, we should load) */
  166.   int loadFile;           /* do we load instead of create? */
  167.                           /* load and save are mutually exculsive */
  168.  
  169.   /* dynamic variables */
  170.   /* not thread safe */
  171.   /* these 3 are render-thread safe - NOT pre-process thread safe */
  172.   PHOTON_MAP photonMap; /* the photon map - array of blocks of photons */
  173.  
  174.   /* not thread safe */ /* these 4 are not thread safe */
  175.   int hitObject;           /* did we hit the target object? (for autostop) */
  176.   DBL photonSpread;        /* photon spread (in radians) */
  177.   DBL photonDepth;         /* total distance from light to intersection */
  178.   OBJECT *photonObject;    /* object that we're shooting photons at.. NULL if global */
  179.  
  180.   /* speed optimization data - sin/cos stored in two arrays
  181.       these are only created if photon mapping is used
  182.   */
  183.   /* these are thread safe - used many times but not modified after initialization */
  184.   DBL *cosTheta;
  185.   DBL *sinTheta;
  186.  
  187.   /* global priority queue arrays used to conserve stack space */
  188.   /* not thread safe */ /* these 2 are not thread safe */
  189.   PHOTON **photonGatherList;  /* photons */
  190.   DBL *photonDistances;       /* priorities */
  191.  
  192.   /* the following variables are put here to avoid parameter passing to
  193.      conserve stack space */
  194.   /* not thread safe */ /* these 5 are not thread safe */
  195.   int passThruThis;           /* is this a pass-through object? */
  196.   int passThruPrev;           /* was the previous object pass-through? */
  197.   unsigned char lightFlags;   /* photon flags for the current light source */
  198.   unsigned char objectFlags;  /* photon flags for the current object */
  199.   LIGHT_SOURCE *Light;        /* the current light */
  200.  
  201.   /* ---------- global photon map ----------*/
  202.   PHOTON_MAP globalPhotonMap; /* the photon map - array of blocks of photons */
  203.   int globalPhotonsToShoot;      /* number of global photons to shoot */
  204.   DBL globalGatherRad;           /* minimum gather radius */
  205.  
  206.   /* media photons */
  207.   /* mediaPhotonMap is render-thread safe - NOT pre-process thread safe */
  208.   PHOTON_MAP mediaPhotonMap; /* the photon map - array of blocks of photons */
  209.  
  210.   DBL mediaSpacingFactor;
  211.   int maxMediaSteps;
  212.  
  213.   int surfaceCount;
  214.   int globalCount;
  215.  
  216.   int photonReflectionBlur;
  217. };
  218.  
  219.  
  220. /* ------------------------------------------------------ */
  221. /* global functions */
  222. /* for documentation of these functions, see photons.c */
  223. /* ------------------------------------------------------ */
  224. void CheckPassThru(OBJECT *o, int flag);
  225. void BuildPhotonMaps(void);
  226. void InitBacktraceEverything(void);
  227. void FreeBacktraceEverything(void);
  228. void addSurfacePhoton(VECTOR Point, VECTOR Origin, COLOUR LightCol, VECTOR RawNorm);
  229. void addMediaPhoton(VECTOR Point, VECTOR Origin, COLOUR LightCol, DBL depthDiff);
  230. int gatherPhotons(VECTOR pt, DBL Size, DBL *r, VECTOR norm, int flatten, PHOTON_MAP *map);
  231.  
  232. void ChooseRay(RAY *NewRay, VECTOR Normal, RAY *Ray, VECTOR Raw_Normal, int WhichRay);
  233.  
  234.  
  235. extern int backtraceFlag;
  236. extern PHOTON_OPTIONS photonOptions;
  237.  
  238.