home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Graphics / Graphics.zip / povsrc31.zip / glow.c < prev    next >
C/C++ Source or Header  |  2001-01-06  |  8KB  |  356 lines

  1. /****************************************************************************
  2. *                   glow.c
  3. *
  4. *  This module contains functions for the glow feature.
  5. *
  6. *****************************************************************************/
  7.  
  8. #include "frame.h"
  9.  
  10. #ifdef GlowPatch
  11.  
  12. #include "povray.h"
  13. #include "vector.h"
  14. #include "matrices.h"
  15. #include "povproto.h"
  16. #include "colour.h"
  17. #include "express.h"
  18. #include "pigment.h"
  19. #include "warps.h"
  20. #include "glow.h"
  21.  
  22. #include "parse.h"
  23. #include "parstxtr.h"
  24. #include "tokenize.h" 
  25. #include "texture.h"
  26.  
  27. /*****************************************************************************
  28. * Local preprocessor defines
  29. ******************************************************************************/
  30.  
  31. /*****************************************************************************
  32. * Local typedefs
  33. ******************************************************************************/
  34.  
  35. /*****************************************************************************
  36. * Static functions
  37. ******************************************************************************/
  38.  
  39. static void Compute_Glow(GLOW * Glow, INTERSECTION * Isect, RAY * Ray, COLOUR Colour);
  40.  
  41. /*****************************************************************************
  42. * Local variables
  43. ******************************************************************************/
  44.  
  45. /*****************************************************************************
  46. *
  47. * FUNCTION   Create_Glow()
  48. *
  49. *   
  50. *
  51. * INPUT
  52. *
  53. * OUTPUT
  54. *
  55. * RETURNS   pointer to a GLOW struct
  56. *
  57. * AUTHOR
  58. *
  59. *   Chris Huff
  60. *
  61. * DESCRIPTION
  62. *
  63. *   Creates and initializes a glow.
  64. *
  65. * CHANGES
  66. *
  67. *   2000.9.02: Creation.
  68. *
  69. ******************************************************************************/
  70. GLOW * Create_Glow()
  71. {
  72.     GLOW * New = (GLOW *)POV_MALLOC(sizeof(GLOW), "glow");
  73.     Make_Vector(New->Center, 0, 0, 0);
  74.     Make_Colour(New->Colour, 1, 1, 1);
  75.     New->Glow_Type = 1;
  76.     New->Size = 1;
  77.     New->Cutoff_Radius = 0;
  78.     New->fade_pow = 1;
  79.     New->Warps = NULL;
  80.     New->next = NULL;
  81.  
  82.     return New;
  83. }
  84.  
  85. /*****************************************************************************
  86. *
  87. * FUNCTION   Add_Glow()
  88. *
  89. *   
  90. *
  91. * INPUT
  92. *
  93. * OUTPUT
  94. *
  95. * RETURNS
  96. *
  97. * AUTHOR
  98. *
  99. *   Chris Huff
  100. *
  101. * DESCRIPTION
  102. *
  103. *   Adds a glow to the list in Frame.
  104. *
  105. * CHANGES
  106. *
  107. *   2000.9.02: Creation.
  108. *
  109. ******************************************************************************/
  110. void Add_Glow(GLOW * Glow)
  111. {
  112.     Glow->next = Frame.Glows;
  113.     Frame.Glows = Glow;
  114. /*    GLOW_PTR * newGlowList = NULL;
  115.     if(Frame.NumOfGlows == 0)
  116.     {
  117.         Frame.Glows = POV_MALLOC(sizeof(GLOW_PTR), "glow pointer array");
  118.         Frame.Glows[0] = Glow;
  119.     }
  120.     else
  121.     {
  122.         Frame.Glows = POV_REALLOC(Frame.Glows, (Frame.NumOfGlows+2)*sizeof(GLOW_PTR), "glow pointer array");
  123. *//*        newGlowList = POV_MALLOC((Frame.NumOfGlows+2)*sizeof(GLOW_PTR), "glow pointer array");
  124.         memcpy(newGlowList, Frame.Glows, (Frame.NumOfGlows+1)*sizeof(GLOW_PTR));
  125.         POV_FREE(Frame.Glows);
  126.         Frame.Glows = newGlowList;*//*
  127.         Frame.Glows[Frame.NumOfGlows] = Glow;
  128.     }*/
  129.     Frame.NumOfGlows++;
  130. }
  131.  
  132. /*****************************************************************************
  133. *
  134. * FUNCTION   Destroy_Glow()
  135. *
  136. *   
  137. *
  138. * INPUT
  139. *
  140. * OUTPUT
  141. *
  142. * RETURNS
  143. *
  144. * AUTHOR
  145. *
  146. *   Chris Huff
  147. *
  148. * DESCRIPTION
  149. *
  150. *   Destroys a glow.
  151. *
  152. * CHANGES
  153. *
  154. *   2000.9.02: Creation.
  155. *
  156. ******************************************************************************/
  157. void Destroy_Glow(GLOW * Glow)
  158. {
  159.     if(Glow->Warps != NULL)
  160.     {
  161.         Destroy_TPat_Fields(Glow->Warps);
  162.         POV_FREE(Glow->Warps);
  163.     }
  164.     POV_FREE(Glow);
  165. }
  166.  
  167. /*****************************************************************************
  168. *
  169. * FUNCTION   Destroy_Glow_List()
  170. *
  171. *   
  172. *
  173. * INPUT
  174. *
  175. * OUTPUT
  176. *
  177. * RETURNS
  178. *
  179. * AUTHOR
  180. *
  181. *   Chris Huff
  182. *
  183. * DESCRIPTION
  184. *
  185. *   Destroys a list of glows.
  186. *
  187. * CHANGES
  188. *
  189. *   2000.9.02: Creation.
  190. *
  191. ******************************************************************************/
  192. void Destroy_Glow_List(GLOW * Glow_List)
  193. {
  194.     GLOW * Glow = Glow_List;
  195.     GLOW * Next_Glow = NULL;
  196.     while(Glow != NULL)
  197.     {
  198.         Next_Glow = Glow->next;
  199.         Destroy_Glow(Glow);
  200.         Glow = Next_Glow;
  201.     }
  202. /*    unsigned int j;
  203.     for(j=0; j<Frame.NumOfGlows; j++)
  204.     {
  205.         Destroy_Glow(Frame.Glows[j]);
  206.         Frame.Glows[j] = NULL;
  207.         POV_FREE(Frame.Glows);
  208.     }*/
  209. }
  210.  
  211. /*****************************************************************************
  212. *
  213. * FUNCTION   Do_Glow()
  214. *
  215. *   
  216. *
  217. * INPUT
  218. *
  219. * OUTPUT
  220. *
  221. * RETURNS 
  222. *
  223. * AUTHOR
  224. *
  225. *   Chris Huff
  226. *
  227. * DESCRIPTION
  228. *
  229. *   Calculates effect of a single glow.
  230. *
  231. * CHANGES
  232. *
  233. *   2000.9.02: Creation.
  234. *
  235. ******************************************************************************/
  236. static void Compute_Glow(GLOW * Glow, INTERSECTION * Isect, RAY * Ray, COLOUR Colour)
  237. {
  238.     VECTOR Pt;/*Point on plane perpendicular to ray and passing through glow*/
  239.     VECTOR lightDir;/*direction of glow source*/
  240.     COLOUR scattered_light;
  241.     DBL Depth = Isect->Depth;/*Distance to intersection*/
  242.     DBL scattering = 0;
  243.     DBL cosA;/*cosine of the angle between the ray and the direction of the glow source*/
  244.     DBL Dist;
  245.     
  246.     /* cosA Computing */
  247.     VSub(lightDir, Ray->Initial, Glow->Center);
  248.     VDot(cosA, lightDir, Ray->Direction);
  249.  
  250.     /* d0 Computing */
  251.     VScale(Pt, Ray->Direction, -cosA);
  252.     VAddEq(Pt, Ray->Initial);
  253.     if(Glow->Warps != NULL)
  254.         Warp_EPoint(Pt, Pt, Glow->Warps, FALSE);
  255.         
  256.     VSubEq(Pt, Glow->Center);
  257.     VLength(Dist, Pt);
  258.     if(Glow->Cutoff_Radius == 0 || Dist < Glow->Cutoff_Radius)
  259.     {
  260.         /* scattered energy integration along ray */
  261.         switch(Glow->Glow_Type)
  262.         {
  263.             case 0:/* A model, I(d) = 1/d^2 */
  264.                 Dist /= Glow->Size;
  265.                 scattering = (atan((Depth+cosA)/Dist) - atan(cosA/Dist))/Dist;
  266. /*                scattering *= Glow->Size;*/
  267.             break;
  268.  
  269.             case 1:/* B model, I(d) = 1/(d^2+1) */
  270.             {
  271.                 DBL d0;
  272.                 DBL denom = 1;
  273.                 VDot(d0, Pt, Pt);
  274.                 denom = sqrt(d0 + 1)/Glow->Size;
  275.                 if(Depth >= Max_Distance) /* Optimization */
  276.                     scattering = (M_PI_2 - atan(cosA/denom))/denom;
  277.                 else
  278.                     scattering = (atan((Depth+cosA)/denom) - atan(cosA/denom))/denom;
  279. /*                scattering *= Glow->Size;*/
  280.             }
  281.             break;
  282.  
  283.             case 2:/*exp() falloff*/
  284.                 if(cosA < 0)
  285.                     scattering = exp(-Dist/Glow->Size);
  286.                 /*    scattered_intensity = exp(-Dist/(Glow->Size+Depth/Glow->Size));*/
  287.             break;
  288.  
  289.             case 3:/*Cosine falloff*/
  290.             {
  291.                 DBL d = Dist/Glow->Size;
  292.                 if(d < 1 && cosA < 0)
  293.                     scattering = sin(max(0, 1-d)*M_PI_2);
  294.             }
  295.             break;
  296.         }
  297.         if(Glow->fade_pow != 1)
  298.         {
  299.             if(Glow->fade_pow == 2)
  300.                 scattering *= scattering;
  301.             else
  302.                 scattering = pow(scattering, Glow->fade_pow);
  303.         }
  304.         Scale_Colour(scattered_light, Glow->Colour, scattering);
  305.         Add_Colour(Colour, Colour, scattered_light);
  306.     }
  307. }
  308.  
  309. /*****************************************************************************
  310. *
  311. * FUNCTION   Do_Glow()
  312. *
  313. *   
  314. *
  315. * INPUT
  316. *
  317. * OUTPUT
  318. *
  319. * RETURNS 
  320. *
  321. * AUTHOR
  322. *
  323. *   Chris Huff
  324. *
  325. * DESCRIPTION
  326. *
  327. *   Calculates glow effect.
  328. *
  329. * CHANGES
  330. *
  331. *   2000.9.02: Creation.
  332. *
  333. ******************************************************************************/
  334. void Do_Glow(INTERSECTION * Isect, RAY * Ray, COLOUR Colour)
  335. {
  336.     GLOW * Glow = Frame.Glows;
  337.     while(Glow != NULL)
  338.     {
  339.         Compute_Glow(Glow, Isect, Ray, Colour);
  340.         Glow = Glow->next;
  341.     }
  342. /*    unsigned int j;
  343.     for(j=0; j<Frame.NumOfGlows; j++) {;}*/
  344. //        Compute_Glow(Frame.Glows[j], Isect, Ray, Colour);
  345. }
  346.  
  347.  
  348.  
  349. void Transform_Glow(GLOW * Glow, TRANSFORM * Trans)
  350. {
  351.     MTransPoint(Glow->Center, Glow->Center, Trans);
  352. }
  353.  
  354. #endif
  355.  
  356.