POINT.C 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. /****************************************************************************
  2. * point.c
  3. *
  4. * This module implements the point & spot light source primitive.
  5. *
  6. * from Persistence of Vision(tm) Ray Tracer
  7. * Copyright 1996,1999 Persistence of Vision Team
  8. *---------------------------------------------------------------------------
  9. * NOTICE: This source code file is provided so that users may experiment
  10. * with enhancements to POV-Ray and to port the software to platforms other
  11. * than those supported by the POV-Ray Team. There are strict rules under
  12. * which you are permitted to use this file. The rules are in the file
  13. * named POVLEGAL.DOC which should be distributed with this file.
  14. * If POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  15. * Team Coordinator by email to team-coord@povray.org or visit us on the web at
  16. * http://www.povray.org. The latest version of POV-Ray may be found at this site.
  17. *
  18. * This program is based on the popular DKB raytracer version 2.12.
  19. * DKBTrace was originally written by David K. Buck.
  20. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  21. *
  22. *****************************************************************************/
  23. #include "frame.h"
  24. #include "vector.h"
  25. #include "povproto.h"
  26. #include "point.h"
  27. #include "matrices.h"
  28. #include "objects.h"
  29. #include "povray.h"
  30. /*****************************************************************************
  31. * Local preprocessor defines
  32. ******************************************************************************/
  33. /*****************************************************************************
  34. * Local typedefs
  35. ******************************************************************************/
  36. /*****************************************************************************
  37. * Static functions
  38. ******************************************************************************/
  39. static DBL cubic_spline ( DBL low,DBL high,DBL pos);
  40. static int All_Light_Source_Intersections (OBJECT *Object, RAY *Ray, ISTACK *Depth_Stack);
  41. static int Inside_Light_Source (VECTOR point, OBJECT *Object);
  42. static void Light_Source_Normal (VECTOR Result, OBJECT *Object, INTERSECTION *Inter);
  43. static void Translate_Light_Source (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans);
  44. static void Rotate_Light_Source (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans);
  45. static void Scale_Light_Source (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans);
  46. static void Transform_Light_Source (OBJECT *Object, TRANSFORM *Trans);
  47. static void Invert_Light_Source (OBJECT *Object);
  48. static LIGHT_SOURCE *Copy_Light_Source (OBJECT *Object);
  49. static void Destroy_Light_Source (OBJECT *Object);
  50. /*****************************************************************************
  51. * Local variables
  52. ******************************************************************************/
  53. static METHODS Light_Source_Methods =
  54. {
  55. All_Light_Source_Intersections,
  56. Inside_Light_Source, Light_Source_Normal,
  57. (COPY_METHOD)Copy_Light_Source,
  58. Translate_Light_Source, Rotate_Light_Source,
  59. Scale_Light_Source, Transform_Light_Source, Invert_Light_Source,
  60. Destroy_Light_Source
  61. };
  62. /*****************************************************************************
  63. *
  64. * FUNCTION
  65. *
  66. * All_Light_Source_Intersections
  67. *
  68. * INPUT
  69. *
  70. * OUTPUT
  71. *
  72. * RETURNS
  73. *
  74. * AUTHOR
  75. *
  76. * POV-Ray Team
  77. *
  78. * DESCRIPTION
  79. *
  80. * -
  81. *
  82. * CHANGES
  83. *
  84. * -
  85. *
  86. ******************************************************************************/
  87. static int All_Light_Source_Intersections (OBJECT *Object, RAY *Ray, ISTACK *Depth_Stack)
  88. {
  89. if (((LIGHT_SOURCE *)Object)->Children != NULL)
  90. {
  91. if (Ray_In_Bound (Ray, ((LIGHT_SOURCE *)Object)->Children->Bound))
  92. {
  93. if (All_Intersections (((LIGHT_SOURCE *)Object)->Children, Ray, Depth_Stack))
  94. {
  95. return(TRUE);
  96. }
  97. }
  98. }
  99. return(FALSE);
  100. }
  101. /*****************************************************************************
  102. *
  103. * FUNCTION
  104. *
  105. * Inside_Light_Source
  106. *
  107. * INPUT
  108. *
  109. * OUTPUT
  110. *
  111. * RETURNS
  112. *
  113. * AUTHOR
  114. *
  115. * POV-Ray Team
  116. *
  117. * DESCRIPTION
  118. *
  119. * -
  120. *
  121. * CHANGES
  122. *
  123. * -
  124. *
  125. ******************************************************************************/
  126. static int Inside_Light_Source (VECTOR IPoint, OBJECT *Object)
  127. {
  128. if (((LIGHT_SOURCE *)Object)->Children != NULL)
  129. {
  130. if (Inside_Object (IPoint, ((LIGHT_SOURCE *)Object)->Children))
  131. {
  132. return (TRUE);
  133. }
  134. }
  135. return (FALSE);
  136. }
  137. /*****************************************************************************
  138. *
  139. * FUNCTION
  140. *
  141. * Light_Source_Normal
  142. *
  143. * INPUT
  144. *
  145. * OUTPUT
  146. *
  147. * RETURNS
  148. *
  149. * AUTHOR
  150. *
  151. * POV-Ray Team
  152. *
  153. * DESCRIPTION
  154. *
  155. * -
  156. *
  157. * CHANGES
  158. *
  159. * -
  160. *
  161. ******************************************************************************/
  162. static void Light_Source_Normal (VECTOR Result, OBJECT *Object, INTERSECTION *Inter)
  163. {
  164. if (((LIGHT_SOURCE *)Object)->Children != NULL)
  165. {
  166. Normal (Result, ((LIGHT_SOURCE *)Object)->Children,Inter);
  167. }
  168. }
  169. /*****************************************************************************
  170. *
  171. * FUNCTION
  172. *
  173. * Translate_Light_Source
  174. *
  175. * INPUT
  176. *
  177. * OUTPUT
  178. *
  179. * RETURNS
  180. *
  181. * AUTHOR
  182. *
  183. * POV-Ray Team
  184. *
  185. * DESCRIPTION
  186. *
  187. * -
  188. *
  189. * CHANGES
  190. *
  191. * -
  192. *
  193. ******************************************************************************/
  194. static void Translate_Light_Source (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans)
  195. {
  196. LIGHT_SOURCE *Light = (LIGHT_SOURCE *)Object;
  197. VAddEq (Light->Center, Vector);
  198. VAddEq (Light->Points_At, Vector);
  199. if (Light->Children != NULL)
  200. {
  201. Translate_Object (Light->Children, Vector, Trans);
  202. }
  203. }
  204. /*****************************************************************************
  205. *
  206. * FUNCTION
  207. *
  208. * Rotate_Light_Source
  209. *
  210. * INPUT
  211. *
  212. * OUTPUT
  213. *
  214. * RETURNS
  215. *
  216. * AUTHOR
  217. *
  218. * POV-Ray Team
  219. *
  220. * DESCRIPTION
  221. *
  222. * -
  223. *
  224. * CHANGES
  225. *
  226. * -
  227. *
  228. ******************************************************************************/
  229. static void Rotate_Light_Source (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans)
  230. {
  231. Transform_Light_Source(Object, Trans);
  232. }
  233. /*****************************************************************************
  234. *
  235. * FUNCTION
  236. *
  237. * Scale_Light_Source
  238. *
  239. * INPUT
  240. *
  241. * OUTPUT
  242. *
  243. * RETURNS
  244. *
  245. * AUTHOR
  246. *
  247. * POV-Ray Team
  248. *
  249. * DESCRIPTION
  250. *
  251. * -
  252. *
  253. * CHANGES
  254. *
  255. * -
  256. *
  257. ******************************************************************************/
  258. static void Scale_Light_Source (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans)
  259. {
  260. Transform_Light_Source(Object, Trans);
  261. }
  262. /*****************************************************************************
  263. *
  264. * FUNCTION
  265. *
  266. * Transform_Light_Source
  267. *
  268. * INPUT
  269. *
  270. * OUTPUT
  271. *
  272. * RETURNS
  273. *
  274. * AUTHOR
  275. *
  276. * POV-Ray Team
  277. *
  278. * DESCRIPTION
  279. *
  280. * -
  281. *
  282. * CHANGES
  283. *
  284. * -
  285. *
  286. ******************************************************************************/
  287. static void Transform_Light_Source (OBJECT *Object, TRANSFORM *Trans)
  288. {
  289. DBL len;
  290. LIGHT_SOURCE *Light = (LIGHT_SOURCE *)Object;
  291. MTransPoint (Light->Center, Light->Center, Trans);
  292. MTransPoint (Light->Points_At, Light->Points_At, Trans);
  293. MTransPoint (Light->Axis1, Light->Axis1, Trans);
  294. MTransPoint (Light->Axis2, Light->Axis2, Trans);
  295. MTransDirection (Light->Direction, Light->Direction, Trans);
  296. /* Make sure direction has unit length. */
  297. VLength(len, Light->Direction);
  298. if (len > EPSILON)
  299. {
  300. VInverseScaleEq(Light->Direction, len);
  301. }
  302. if (Light->Children != NULL)
  303. {
  304. Transform_Object (Light->Children, Trans);
  305. }
  306. }
  307. /*****************************************************************************
  308. *
  309. * FUNCTION
  310. *
  311. * Invert_Light_Source
  312. *
  313. * INPUT
  314. *
  315. * OUTPUT
  316. *
  317. * RETURNS
  318. *
  319. * AUTHOR
  320. *
  321. * POV-Ray Team
  322. *
  323. * DESCRIPTION
  324. *
  325. * -
  326. *
  327. * CHANGES
  328. *
  329. * -
  330. *
  331. ******************************************************************************/
  332. static void Invert_Light_Source (OBJECT *Object)
  333. {
  334. LIGHT_SOURCE *Light = (LIGHT_SOURCE *)Object;
  335. if (Light->Children != NULL)
  336. {
  337. Invert_Object (Light->Children);
  338. }
  339. }
  340. /*****************************************************************************
  341. *
  342. * FUNCTION
  343. *
  344. * Create_Light_Source
  345. *
  346. * INPUT
  347. *
  348. * OUTPUT
  349. *
  350. * RETURNS
  351. *
  352. * AUTHOR
  353. *
  354. * POV-Ray Team
  355. *
  356. * DESCRIPTION
  357. *
  358. * -
  359. *
  360. * CHANGES
  361. *
  362. * -
  363. *
  364. ******************************************************************************/
  365. LIGHT_SOURCE *Create_Light_Source ()
  366. {
  367. int i;
  368. LIGHT_SOURCE *New;
  369. New = (LIGHT_SOURCE *)POV_MALLOC(sizeof (LIGHT_SOURCE), "light_source");
  370. INIT_OBJECT_FIELDS(New, LIGHT_OBJECT, &Light_Source_Methods)
  371. New->Children = NULL;
  372. Set_Flag(New, NO_SHADOW_FLAG);
  373. Make_Colour(New->Colour, 1.0, 1.0, 1.0);
  374. Make_Vector(New->Direction, 0.0, 0.0, 0.0);
  375. Make_Vector(New->Center, 0.0, 0.0, 0.0);
  376. Make_Vector(New->Points_At, 0.0, 0.0, 1.0);
  377. Make_Vector(New->Axis1, 0.0, 0.0, 1.0);
  378. Make_Vector(New->Axis2, 0.0, 1.0, 0.0);
  379. New->Coeff = 10.0;
  380. New->Radius = 0.35;
  381. New->Falloff = 0.35;
  382. New->Fade_Distance = 0.0;
  383. New->Fade_Power = 0.0;
  384. New->Next_Light_Source = NULL;
  385. New->Light_Grid = NULL;
  386. New->Shadow_Cached_Object = NULL;
  387. New->Light_Type = POINT_SOURCE;
  388. New->Area_Light = FALSE;
  389. New->Jitter = FALSE;
  390. New->Track = FALSE;
  391. New->Area_Size1 = 0;
  392. New->Area_Size2 = 0;
  393. New->Adaptive_Level = 100;
  394. New->Media_Attenuation = FALSE;
  395. New->Media_Interaction = TRUE;
  396. for (i = 0; i < 6; i++)
  397. {
  398. New->Light_Buffer[i] = NULL;
  399. }
  400. return (New);
  401. }
  402. /*****************************************************************************
  403. *
  404. * FUNCTION
  405. *
  406. * Copy_Light_Source
  407. *
  408. * INPUT
  409. *
  410. * OUTPUT
  411. *
  412. * RETURNS
  413. *
  414. * AUTHOR
  415. *
  416. * POV-Ray Team
  417. *
  418. * DESCRIPTION
  419. *
  420. * -
  421. *
  422. * CHANGES
  423. *
  424. * -
  425. *
  426. ******************************************************************************/
  427. static LIGHT_SOURCE *Copy_Light_Source (OBJECT *Old)
  428. {
  429. int i, j;
  430. LIGHT_SOURCE *New;
  431. LIGHT_SOURCE *Light = (LIGHT_SOURCE *)Old;
  432. New = Create_Light_Source();
  433. /* Copy light source. */
  434. *New = *(LIGHT_SOURCE *)Old;
  435. New->Next_Light_Source = NULL;
  436. New->Children = Copy_Object (((LIGHT_SOURCE *)Old)->Children);
  437. if (Light->Light_Grid != NULL)
  438. {
  439. New->Light_Grid = Create_Light_Grid(Light->Area_Size1, Light->Area_Size2);
  440. for (i = 0; i < Light->Area_Size1; i++)
  441. {
  442. for (j = 0; j < Light->Area_Size2; j++)
  443. {
  444. Assign_Colour(New->Light_Grid[i][j], Light->Light_Grid[i][j]);
  445. }
  446. }
  447. }
  448. return (New);
  449. }
  450. /*****************************************************************************
  451. *
  452. * FUNCTION
  453. *
  454. * Destroy_Light_Source
  455. *
  456. * INPUT
  457. *
  458. * OUTPUT
  459. *
  460. * RETURNS
  461. *
  462. * AUTHOR
  463. *
  464. * POV-Ray Team
  465. *
  466. * DESCRIPTION
  467. *
  468. * -
  469. *
  470. * CHANGES
  471. *
  472. * -
  473. *
  474. ******************************************************************************/
  475. static void Destroy_Light_Source (OBJECT *Object)
  476. {
  477. int i;
  478. LIGHT_SOURCE *Light = (LIGHT_SOURCE *)Object;
  479. if (Light->Light_Grid != NULL)
  480. {
  481. for (i = 0; i < Light->Area_Size1; i++)
  482. {
  483. POV_FREE(Light->Light_Grid[i]);
  484. }
  485. POV_FREE(Light->Light_Grid);
  486. }
  487. Destroy_Object(Light->Children);
  488. POV_FREE(Object);
  489. }
  490. /*****************************************************************************
  491. *
  492. * FUNCTION
  493. *
  494. * Create_Light_Grid
  495. *
  496. * INPUT
  497. *
  498. * OUTPUT
  499. *
  500. * RETURNS
  501. *
  502. * AUTHOR
  503. *
  504. * POV-Ray Team
  505. *
  506. * DESCRIPTION
  507. *
  508. * -
  509. *
  510. * CHANGES
  511. *
  512. * -
  513. *
  514. ******************************************************************************/
  515. COLOUR **Create_Light_Grid (int Size1, int Size2)
  516. {
  517. int i;
  518. COLOUR **New;
  519. New = (COLOUR **)POV_MALLOC(Size1 * sizeof (COLOUR *), "area light");
  520. for (i = 0; i < Size1; i++)
  521. {
  522. New[i] = (COLOUR *)POV_MALLOC(Size2 * sizeof (COLOUR), "area light");
  523. }
  524. return (New);
  525. }
  526. /*****************************************************************************
  527. *
  528. * FUNCTION
  529. *
  530. * cubic_spline
  531. *
  532. * INPUT
  533. *
  534. * OUTPUT
  535. *
  536. * RETURNS
  537. *
  538. * AUTHOR
  539. *
  540. * POV-Ray Team
  541. *
  542. * DESCRIPTION
  543. *
  544. * Cubic spline that has tangents of slope 0 at x == low and at x == high.
  545. * For a given value "pos" between low and high the spline value is returned.
  546. *
  547. * CHANGES
  548. *
  549. * -
  550. *
  551. ******************************************************************************/
  552. static DBL cubic_spline(DBL low, DBL high, DBL pos)
  553. {
  554. /* Check to see if the position is within the proper boundaries. */
  555. if (pos < low)
  556. {
  557. return(0.0);
  558. }
  559. else
  560. {
  561. if (pos >= high)
  562. {
  563. return(1.0);
  564. }
  565. }
  566. /* This never happens. [DB] */
  567. /*
  568. if (high == low)
  569. {
  570. return(0.0);
  571. }
  572. */
  573. /* Normalize to the interval [0...1]. */
  574. pos = (pos - low) / (high - low);
  575. /* See where it is on the cubic curve. */
  576. return(3 - 2 * pos) * pos * pos;
  577. }
  578. /*****************************************************************************
  579. *
  580. * FUNCTION
  581. *
  582. * Attenuate_Light
  583. *
  584. * INPUT
  585. *
  586. * OUTPUT
  587. *
  588. * RETURNS
  589. *
  590. * AUTHOR
  591. *
  592. * POV-Ray Team
  593. *
  594. * DESCRIPTION
  595. *
  596. * -
  597. *
  598. * CHANGES
  599. *
  600. * Jan 1995 : Added attenuation due to atmospheric scattering and light
  601. * source distance. Added cylindrical light source. [DB]
  602. *
  603. ******************************************************************************/
  604. DBL Attenuate_Light (LIGHT_SOURCE *Light, RAY *Ray, DBL Distance)
  605. {
  606. DBL len, k, costheta;
  607. DBL Attenuation = 1.0;
  608. VECTOR P, V1;
  609. /* If this is a spotlight then attenuate based on the incidence angle. */
  610. switch (Light->Light_Type)
  611. {
  612. case SPOT_SOURCE:
  613. VDot(costheta, Ray->Direction, Light->Direction);
  614. costheta *= -1.0;
  615. if (costheta > 0.0)
  616. {
  617. Attenuation = pow(costheta, Light->Coeff);
  618. /*
  619. * If there is a soft falloff region associated with the light then
  620. * do an interpolation of values between the hot center and the
  621. * direction at which light falls to nothing.
  622. */
  623. if (Light->Radius > 0.0)
  624. {
  625. Attenuation *= cubic_spline(Light->Falloff, Light->Radius, costheta);
  626. }
  627. /*
  628. Debug_Info("Atten: %lg\n", Attenuation);
  629. */
  630. }
  631. else
  632. {
  633. Attenuation = 0.0;
  634. }
  635. break;
  636. case CYLINDER_SOURCE:
  637. VSub(V1, Ray->Initial, Light->Center);
  638. VDot(k, V1, Light->Direction);
  639. if (k > 0.0)
  640. {
  641. VLinComb2(P, 1.0, V1, -k, Light->Direction);
  642. VLength(len, P);
  643. if (len < Light->Falloff)
  644. {
  645. len = 1.0 - len / Light->Falloff;
  646. Attenuation = pow(len, Light->Coeff);
  647. if (Light->Radius > 0.0)
  648. {
  649. Attenuation *= cubic_spline(1.0 - Light->Radius / Light->Falloff, 1.0, len);
  650. }
  651. }
  652. else
  653. {
  654. Attenuation = 0.0;
  655. }
  656. }
  657. else
  658. {
  659. Attenuation = 0.0;
  660. }
  661. break;
  662. }
  663. if (Attenuation > 0.0)
  664. {
  665. /* Attenuate light due to light source distance. */
  666. if ((Light->Fade_Power > 0.0) && (fabs(Light->Fade_Distance) > EPSILON))
  667. {
  668. Attenuation *= 2.0 / (1.0 + pow(Distance / Light->Fade_Distance, Light->Fade_Power));
  669. }
  670. }
  671. return(Attenuation);
  672. }