HFIELD.C 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249
  1. /****************************************************************************
  2. * hfield.c
  3. *
  4. * This file implements the height field shape primitive. The shape is
  5. * implemented as a collection of triangles which are calculated as
  6. * needed. The basic intersection routine first computes the rays
  7. * intersection with the box marking the limits of the shape, then
  8. * follows the line from one intersection point to the other, testing the
  9. * two triangles which form the pixel for an intersection with the ray at
  10. * each step.
  11. *
  12. * height field added by Doug Muir with lots of advice and support
  13. * from David Buck and Drew Wells.
  14. *
  15. * from Persistence of Vision(tm) Ray Tracer
  16. * Copyright 1996,1999 Persistence of Vision Team
  17. *---------------------------------------------------------------------------
  18. * NOTICE: This source code file is provided so that users may experiment
  19. * with enhancements to POV-Ray and to port the software to platforms other
  20. * than those supported by the POV-Ray Team. There are strict rules under
  21. * which you are permitted to use this file. The rules are in the file
  22. * named POVLEGAL.DOC which should be distributed with this file.
  23. * If POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  24. * Team Coordinator by email to team-coord@povray.org or visit us on the web at
  25. * http://www.povray.org. The latest version of POV-Ray may be found at this site.
  26. *
  27. * This program is based on the popular DKB raytracer version 2.12.
  28. * DKBTrace was originally written by David K. Buck.
  29. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  30. *
  31. *****************************************************************************/
  32. /****************************************************************************
  33. *
  34. * Explanation:
  35. *
  36. * -
  37. *
  38. * Syntax:
  39. *
  40. * ---
  41. *
  42. * Aug 1994 : Merged functions for CSG height fields into functions for
  43. * non-CSG height fiels. Moved all height field map related
  44. * data into one data structure. Fixed memory problems. [DB]
  45. *
  46. * Feb 1995 : Major rewrite of the height field intersection tests. [DB]
  47. *
  48. *****************************************************************************/
  49. #include "frame.h"
  50. #include "povray.h"
  51. #include "vector.h"
  52. #include "povproto.h"
  53. #include "hfield.h"
  54. #include "matrices.h"
  55. #include "objects.h"
  56. /*****************************************************************************
  57. * Local preprocessor defines
  58. ******************************************************************************/
  59. #define sign(x) (((x) >= 0.0) ? 1.0 : -1.0)
  60. #define Get_Height(x, z, HField) ((DBL)(HField)->Data->Map[(z)][(x)])
  61. /* Small offest. */
  62. #define HFIELD_OFFSET 0.001
  63. /*****************************************************************************
  64. * Static functions
  65. ******************************************************************************/
  66. static DBL normalize (VECTOR A, VECTOR B);
  67. static DBL stretch (DBL x);
  68. static void smooth_height_field (HFIELD *HField, int xsize, int zsize);
  69. static int intersect_pixel (int x,int z,RAY *Ray,HFIELD *HField,DBL height1,DBL height2);
  70. static int add_single_normal (HF_VAL **data, int xsize, int zsize,
  71. int x0, int z0,int x1, int z1,int x2, int z2,VECTOR N);
  72. static int All_HField_Intersections (OBJECT *Object,RAY *Ray,ISTACK *Depth_Stack);
  73. static int Inside_HField (VECTOR IPoint,OBJECT *Object);
  74. static void HField_Normal (VECTOR Result,OBJECT *Object,INTERSECTION *Inter);
  75. static void Translate_HField (OBJECT *Object,VECTOR Vector, TRANSFORM *Trans);
  76. static void Rotate_HField (OBJECT *Object,VECTOR Vector, TRANSFORM *Trans);
  77. static void Scale_HField (OBJECT *Object,VECTOR Vector, TRANSFORM *Trans);
  78. static void Invert_HField (OBJECT *Object);
  79. static void Transform_HField (OBJECT *Object,TRANSFORM *Trans);
  80. static HFIELD *Copy_HField (OBJECT *Object);
  81. static void Destroy_HField (OBJECT *Object);
  82. static int dda_traversal (RAY *Ray, HFIELD *HField, VECTOR Start, HFIELD_BLOCK *Block);
  83. static int block_traversal (RAY *Ray, HFIELD *HField, VECTOR Start);
  84. static void build_hfield_blocks (HFIELD *HField);
  85. /*****************************************************************************
  86. * Local variables
  87. ******************************************************************************/
  88. METHODS HField_Methods =
  89. {
  90. All_HField_Intersections,
  91. Inside_HField, HField_Normal,
  92. (COPY_METHOD)Copy_HField, Translate_HField, Rotate_HField,
  93. Scale_HField, Transform_HField, Invert_HField, Destroy_HField
  94. };
  95. static ISTACK *HField_Stack;
  96. static RAY *RRay;
  97. static DBL mindist, maxdist;
  98. /*****************************************************************************
  99. *
  100. * FUNCTION
  101. *
  102. * All_HField_Intersections
  103. *
  104. * INPUT
  105. *
  106. * OUTPUT
  107. *
  108. * RETURNS
  109. *
  110. * AUTHOR
  111. *
  112. * Doug Muir, David Buck, Drew Wells
  113. *
  114. * DESCRIPTION
  115. *
  116. * -
  117. *
  118. * CHANGES
  119. *
  120. * Feb 1995 : Modified to work with new intersection functions. [DB]
  121. *
  122. ******************************************************************************/
  123. static int All_HField_Intersections(OBJECT *Object, RAY *Ray, ISTACK *Depth_Stack)
  124. {
  125. int Side1, Side2;
  126. VECTOR Start;
  127. RAY Temp_Ray;
  128. DBL depth1, depth2;
  129. HFIELD *HField = (HFIELD *) Object;
  130. Increase_Counter(stats[Ray_HField_Tests]);
  131. MInvTransPoint(Temp_Ray.Initial, Ray->Initial, HField->Trans);
  132. MInvTransDirection(Temp_Ray.Direction, Ray->Direction, HField->Trans);
  133. #ifdef HFIELD_EXTRA_STATS
  134. Increase_Counter(stats[Ray_HField_Box_Tests]);
  135. #endif
  136. if (!Intersect_Box(&Temp_Ray,HField->bounding_box,&depth1,&depth2,&Side1,&Side2))
  137. {
  138. return(FALSE);
  139. }
  140. #ifdef HFIELD_EXTRA_STATS
  141. Increase_Counter(stats[Ray_HField_Box_Tests_Succeeded]);
  142. #endif
  143. HField->Data->cache_pos = 0;
  144. if (depth1 < EPSILON)
  145. {
  146. depth1 = EPSILON;
  147. if (depth1 > depth2)
  148. {
  149. return(FALSE);
  150. }
  151. }
  152. VEvaluateRay(Start, Temp_Ray.Initial, depth1, Temp_Ray.Direction);
  153. mindist = depth1;
  154. maxdist = depth2;
  155. HField_Stack = Depth_Stack;
  156. RRay = Ray;
  157. if (block_traversal(&Temp_Ray, HField, Start))
  158. {
  159. Increase_Counter(stats[Ray_HField_Tests_Succeeded]);
  160. return(TRUE);
  161. }
  162. else
  163. {
  164. return(FALSE);
  165. }
  166. }
  167. /*****************************************************************************
  168. *
  169. * FUNCTION
  170. *
  171. * Inside_HField
  172. *
  173. * INPUT
  174. *
  175. * OUTPUT
  176. *
  177. * RETURNS
  178. *
  179. * AUTHOR
  180. *
  181. * Doug Muir, David Buck, Drew Wells
  182. *
  183. * DESCRIPTION
  184. *
  185. * -
  186. *
  187. * CHANGES
  188. *
  189. * -
  190. *
  191. ******************************************************************************/
  192. static int Inside_HField (VECTOR IPoint, OBJECT *Object)
  193. {
  194. HFIELD *HField = (HFIELD *) Object;
  195. int px, pz;
  196. DBL x,z,y1,y2,y3,water, dot1, dot2;
  197. VECTOR Local_Origin, H_Normal, Test;
  198. MInvTransPoint(Test, IPoint, HField->Trans);
  199. water = HField->bounding_box->bounds[0][Y];
  200. if ((Test[X] < 0.0) || (Test[X] >= HField->bounding_box->bounds[1][X]) ||
  201. (Test[Z] < 0.0) || (Test[Z] >= HField->bounding_box->bounds[1][Z]))
  202. {
  203. return(Test_Flag(HField, INVERTED_FLAG));
  204. }
  205. if (Test[Y] >= HField->bounding_box->bounds[1][Y])
  206. {
  207. return(Test_Flag(HField, INVERTED_FLAG));
  208. }
  209. if (Test[Y] < water)
  210. {
  211. return(!Test_Flag(HField, INVERTED_FLAG));
  212. }
  213. px = (int)Test[X];
  214. pz = (int)Test[Z];
  215. x = Test[X] - (DBL)px;
  216. z = Test[Z] - (DBL)pz;
  217. if ((x+z) < 1.0)
  218. {
  219. y1 = max(Get_Height(px, pz, HField), water);
  220. y2 = max(Get_Height(px+1, pz, HField), water);
  221. y3 = max(Get_Height(px, pz+1, HField), water);
  222. Make_Vector(Local_Origin,(DBL)px,y1,(DBL)pz);
  223. Make_Vector(H_Normal, y1-y2, 1.0, y1-y3);
  224. }
  225. else
  226. {
  227. px = (int)ceil(Test[X]);
  228. pz = (int)ceil(Test[Z]);
  229. y1 = max(Get_Height(px, pz, HField), water);
  230. y2 = max(Get_Height(px-1, pz, HField), water);
  231. y3 = max(Get_Height(px, pz-1, HField), water);
  232. Make_Vector(Local_Origin,(DBL)px,y1,(DBL)pz);
  233. Make_Vector(H_Normal, y2-y1, 1.0, y3-y1);
  234. }
  235. VDot(dot1, Test, H_Normal);
  236. VDot(dot2, Local_Origin, H_Normal);
  237. if (dot1 < dot2)
  238. {
  239. return(!Test_Flag(HField, INVERTED_FLAG));
  240. }
  241. return(Test_Flag(HField, INVERTED_FLAG));
  242. }
  243. /*****************************************************************************
  244. *
  245. * FUNCTION
  246. *
  247. * HField_Normal
  248. *
  249. * INPUT
  250. *
  251. * OUTPUT
  252. *
  253. * RETURNS
  254. *
  255. * AUTHOR
  256. *
  257. * Doug Muir, David Buck, Drew Wells
  258. *
  259. * DESCRIPTION
  260. *
  261. * -
  262. *
  263. * CHANGES
  264. *
  265. * -
  266. *
  267. ******************************************************************************/
  268. static void HField_Normal (VECTOR Result, OBJECT *Object, INTERSECTION *Inter)
  269. {
  270. HFIELD *HField = (HFIELD *) Object;
  271. int px,pz, i;
  272. DBL x,z,y1,y2,y3,u,v;
  273. VECTOR Local_Origin;
  274. VECTOR n[5];
  275. MInvTransPoint(Local_Origin, Inter->IPoint, HField->Trans);
  276. for (i = 0; i < HField->Data->cache_pos; i++)
  277. {
  278. if ((Local_Origin[X] == HField->Data->Normal_Cache[i].fx) &&
  279. (Local_Origin[Z] == HField->Data->Normal_Cache[i].fz))
  280. {
  281. Assign_Vector(Result,HField->Data->Normal_Cache[i].normal);
  282. MTransNormal(Result,Result,HField->Trans);
  283. VNormalize(Result,Result);
  284. return;
  285. }
  286. }
  287. px = (int)Local_Origin[X];
  288. pz = (int)Local_Origin[Z];
  289. x = Local_Origin[X] - (DBL)px;
  290. z = Local_Origin[Z] - (DBL)pz;
  291. if (Test_Flag(HField, SMOOTHED_FLAG))
  292. {
  293. n[0][X] = HField->Data->Normals[pz][px][0];
  294. n[0][Y] = HField->Data->Normals[pz][px][1];
  295. n[0][Z] = HField->Data->Normals[pz][px][2];
  296. n[1][X] = HField->Data->Normals[pz][px+1][0];
  297. n[1][Y] = HField->Data->Normals[pz][px+1][1];
  298. n[1][Z] = HField->Data->Normals[pz][px+1][2];
  299. n[2][X] = HField->Data->Normals[pz+1][px][0];
  300. n[2][Y] = HField->Data->Normals[pz+1][px][1];
  301. n[2][Z] = HField->Data->Normals[pz+1][px][2];
  302. n[3][X] = HField->Data->Normals[pz+1][px+1][0];
  303. n[3][Y] = HField->Data->Normals[pz+1][px+1][1];
  304. n[3][Z] = HField->Data->Normals[pz+1][px+1][2];
  305. x = stretch(x);
  306. z = stretch(z);
  307. u = (1.0 - x);
  308. v = (1.0 - z);
  309. Result[X] = v*(u*n[0][X] + x*n[1][X]) + z*(u*n[2][X] + x*n[3][X]);
  310. Result[Y] = v*(u*n[0][Y] + x*n[1][Y]) + z*(u*n[2][Y] + x*n[3][Y]);
  311. Result[Z] = v*(u*n[0][Z] + x*n[1][Z]) + z*(u*n[2][Z] + x*n[3][Z]);
  312. }
  313. else
  314. {
  315. if ((x+z) <= 1.0)
  316. {
  317. /* Lower triangle. */
  318. y1 = Get_Height(px, pz, HField);
  319. y2 = Get_Height(px+1, pz, HField);
  320. y3 = Get_Height(px, pz+1, HField);
  321. Make_Vector(Result, y1-y2, 1.0, y1-y3);
  322. }
  323. else
  324. {
  325. /* Upper triangle. */
  326. y1 = Get_Height(px+1, pz+1, HField);
  327. y2 = Get_Height(px, pz+1, HField);
  328. y3 = Get_Height(px+1, pz, HField);
  329. Make_Vector(Result, y2-y1, 1.0, y3-y1);
  330. }
  331. }
  332. MTransNormal(Result, Result, HField->Trans);
  333. VNormalize(Result, Result);
  334. }
  335. /*****************************************************************************
  336. *
  337. * FUNCTION
  338. *
  339. * stretch
  340. *
  341. * INPUT
  342. *
  343. * OUTPUT
  344. *
  345. * RETURNS
  346. *
  347. * AUTHOR
  348. *
  349. * Doug Muir, David Buck, Drew Wells
  350. *
  351. * DESCRIPTION
  352. *
  353. * -
  354. *
  355. * CHANGES
  356. *
  357. * -
  358. *
  359. ******************************************************************************/
  360. static DBL stretch (DBL x)
  361. {
  362. if (x <= 0.5)
  363. {
  364. x = 2.0 * x * x;
  365. }
  366. else
  367. {
  368. x = 1.0 - (2.0 * (1.0 - x) * (1.0 - x));
  369. }
  370. return(x);
  371. }
  372. /*****************************************************************************
  373. *
  374. * FUNCTION
  375. *
  376. * normalize
  377. *
  378. * INPUT
  379. *
  380. * OUTPUT
  381. *
  382. * RETURNS
  383. *
  384. * AUTHOR
  385. *
  386. * Doug Muir, David Buck, Drew Wells
  387. *
  388. * DESCRIPTION
  389. *
  390. * -
  391. *
  392. * CHANGES
  393. *
  394. * -
  395. *
  396. ******************************************************************************/
  397. static DBL normalize(VECTOR A, VECTOR B)
  398. {
  399. VLength(VTemp, B);
  400. if (fabs(VTemp) > EPSILON)
  401. {
  402. VInverseScale(A, B, VTemp);
  403. }
  404. else
  405. {
  406. Make_Vector(A, 0.0, 1.0, 0.0);
  407. }
  408. return(VTemp);
  409. }
  410. /*****************************************************************************
  411. *
  412. * FUNCTION
  413. *
  414. * intersect_pixel
  415. *
  416. * INPUT
  417. *
  418. * OUTPUT
  419. *
  420. * RETURNS
  421. *
  422. * AUTHOR
  423. *
  424. * Doug Muir, David Buck, Drew Wells
  425. *
  426. * DESCRIPTION
  427. *
  428. * -
  429. *
  430. * CHANGES
  431. *
  432. * -
  433. *
  434. ******************************************************************************/
  435. static int intersect_pixel(int x, int z, RAY *Ray, HFIELD *HField, DBL height1, DBL height2)
  436. {
  437. int Found;
  438. DBL dot, depth1, depth2;
  439. DBL s, t, y1, y2, y3, y4;
  440. DBL min_y2_y3, max_y2_y3;
  441. DBL max_height, min_height;
  442. VECTOR P, N, V1;
  443. #ifdef HFIELD_EXTRA_STATS
  444. Increase_Counter(stats[Ray_HField_Cell_Tests]);
  445. #endif
  446. y1 = Get_Height(x, z, HField);
  447. y2 = Get_Height(x+1, z, HField);
  448. y3 = Get_Height(x, z+1, HField);
  449. y4 = Get_Height(x+1, z+1, HField);
  450. /* Do we hit this cell at all? */
  451. if (y2 < y3)
  452. {
  453. min_y2_y3 = y2;
  454. max_y2_y3 = y3;
  455. }
  456. else
  457. {
  458. min_y2_y3 = y3;
  459. max_y2_y3 = y2;
  460. }
  461. min_height = min(min(y1, y4), min_y2_y3);
  462. max_height = max(max(y1, y4), max_y2_y3);
  463. if ((max_height < height1) || (min_height > height2))
  464. {
  465. return(FALSE);
  466. }
  467. #ifdef HFIELD_EXTRA_STATS
  468. Increase_Counter(stats[Ray_HField_Cell_Tests_Succeeded]);
  469. #endif
  470. Found = FALSE;
  471. /* Check if we'll hit first triangle. */
  472. min_height = min(y1, min_y2_y3);
  473. max_height = max(y1, max_y2_y3);
  474. if ((max_height >= height1) && (min_height <= height2))
  475. {
  476. #ifdef HFIELD_EXTRA_STATS
  477. Increase_Counter(stats[Ray_HField_Triangle_Tests]);
  478. #endif
  479. /* Set up triangle. */
  480. Make_Vector(P, (DBL)x, y1, (DBL)z);
  481. /*
  482. * Calculate the normal vector from:
  483. *
  484. * N = V2 x V1, with V1 = <1, y2-y1, 0>, V2 = <0, y3-y1, 1>.
  485. */
  486. Make_Vector(N, y1-y2, 1.0, y1-y3);
  487. /* Now intersect the triangle. */
  488. VDot(dot, N, Ray->Direction);
  489. if ((dot > EPSILON) || (dot < -EPSILON))
  490. {
  491. VSub(V1, P, Ray->Initial);
  492. VDot(depth1, N, V1);
  493. depth1 /= dot;
  494. if ((depth1 >= mindist) && (depth1 <= maxdist))
  495. {
  496. s = Ray->Initial[X] + depth1 * Ray->Direction[X] - (DBL)x;
  497. t = Ray->Initial[Z] + depth1 * Ray->Direction[Z] - (DBL)z;
  498. if ((s >= -0.0001) && (t >= -0.0001) && ((s+t) <= 1.0001))
  499. {
  500. #ifdef HFIELD_EXTRA_STATS
  501. Increase_Counter(stats[Ray_HField_Triangle_Tests_Succeeded]);
  502. #endif
  503. VEvaluateRay(P, RRay->Initial, depth1, RRay->Direction);
  504. if (Point_In_Clip(P, HField->Clip))
  505. {
  506. push_entry(depth1, P, (OBJECT *)HField, HField_Stack);
  507. Found = TRUE;
  508. /* Cache normal. */
  509. if (!Test_Flag(HField, SMOOTHED_FLAG))
  510. {
  511. if (HField->Data->cache_pos < HF_CACHE_SIZE)
  512. {
  513. Assign_Vector(HField->Data->Normal_Cache[HField->Data->cache_pos].normal, N);
  514. HField->Data->Normal_Cache[HField->Data->cache_pos].fx = x + s;
  515. HField->Data->Normal_Cache[HField->Data->cache_pos].fz = z + t;
  516. HField->Data->cache_pos++;
  517. }
  518. }
  519. }
  520. }
  521. }
  522. }
  523. }
  524. /* Check if we'll hit second triangle. */
  525. min_height = min(y4, min_y2_y3);
  526. max_height = max(y4, max_y2_y3);
  527. if ((max_height >= height1) && (min_height <= height2))
  528. {
  529. #ifdef HFIELD_EXTRA_STATS
  530. Increase_Counter(stats[Ray_HField_Triangle_Tests]);
  531. #endif
  532. /* Set up triangle. */
  533. Make_Vector(P, (DBL)(x+1), y4, (DBL)(z+1));
  534. /*
  535. * Calculate the normal vector from:
  536. *
  537. * N = V2 x V1, with V1 = <-1, y3-y4, 0>, V2 = <0, y2-y4, -1>.
  538. */
  539. Make_Vector(N, y3-y4, 1.0, y2-y4);
  540. /* Now intersect the triangle. */
  541. VDot(dot, N, Ray->Direction);
  542. if ((dot > EPSILON) || (dot < -EPSILON))
  543. {
  544. VSub(V1, P, Ray->Initial);
  545. VDot(depth2, N, V1);
  546. depth2 /= dot;
  547. if ((depth2 >= mindist) && (depth2 <= maxdist))
  548. {
  549. s = Ray->Initial[X] + depth2 * Ray->Direction[X] - (DBL)x;
  550. t = Ray->Initial[Z] + depth2 * Ray->Direction[Z] - (DBL)z;
  551. if ((s <= 1.0001) && (t <= 1.0001) && ((s+t) >= 0.9999))
  552. {
  553. #ifdef HFIELD_EXTRA_STATS
  554. Increase_Counter(stats[Ray_HField_Triangle_Tests_Succeeded]);
  555. #endif
  556. VEvaluateRay(P, RRay->Initial, depth2, RRay->Direction);
  557. if (Point_In_Clip(P, HField->Clip))
  558. {
  559. push_entry(depth2, P, (OBJECT *)HField, HField_Stack);
  560. Found = TRUE;
  561. /* Cache normal. */
  562. if (!Test_Flag(HField, SMOOTHED_FLAG))
  563. {
  564. if (HField->Data->cache_pos < HF_CACHE_SIZE)
  565. {
  566. Assign_Vector(HField->Data->Normal_Cache[HField->Data->cache_pos].normal, N);
  567. HField->Data->Normal_Cache[HField->Data->cache_pos].fx = x + s;
  568. HField->Data->Normal_Cache[HField->Data->cache_pos].fz = z + t;
  569. HField->Data->cache_pos++;
  570. }
  571. }
  572. }
  573. }
  574. }
  575. }
  576. }
  577. return(Found);
  578. }
  579. /*****************************************************************************
  580. *
  581. * FUNCTION
  582. *
  583. * add_single_normal
  584. *
  585. * INPUT
  586. *
  587. * OUTPUT
  588. *
  589. * RETURNS
  590. *
  591. * AUTHOR
  592. *
  593. * Doug Muir, David Buck, Drew Wells
  594. *
  595. * DESCRIPTION
  596. *
  597. * -
  598. *
  599. * CHANGES
  600. *
  601. * -
  602. *
  603. ******************************************************************************/
  604. static int add_single_normal(HF_VAL **data, int xsize, int zsize, int x0, int z0, int x1, int z1, int x2, int z2, VECTOR N)
  605. {
  606. VECTOR v0, v1, v2, t0, t1, Nt;
  607. if ((x0 < 0) || (z0 < 0) ||
  608. (x1 < 0) || (z1 < 0) ||
  609. (x2 < 0) || (z2 < 0) ||
  610. (x0 > xsize) || (z0 > zsize) ||
  611. (x1 > xsize) || (z1 > zsize) ||
  612. (x2 > xsize) || (z2 > zsize))
  613. {
  614. return(0);
  615. }
  616. else
  617. {
  618. Make_Vector(v0, x0, (DBL)data[z0][x0], z0);
  619. Make_Vector(v1, x1, (DBL)data[z1][x1], z1);
  620. Make_Vector(v2, x2, (DBL)data[z2][x2], z2);
  621. VSub(t0, v2, v0);
  622. VSub(t1, v1, v0);
  623. VCross(Nt, t0, t1);
  624. normalize(Nt, Nt);
  625. if (Nt[Y] < 0.0)
  626. {
  627. VScaleEq(Nt, -1.0);
  628. }
  629. VAddEq(N, Nt);
  630. return(1);
  631. }
  632. }
  633. /*****************************************************************************
  634. *
  635. * FUNCTION
  636. *
  637. * smooth_height_field
  638. *
  639. * INPUT
  640. *
  641. * OUTPUT
  642. *
  643. * RETURNS
  644. *
  645. * AUTHOR
  646. *
  647. * Doug Muir, David Buck, Drew Wells
  648. *
  649. * DESCRIPTION
  650. *
  651. * Given a height field that only contains an elevation grid, this
  652. * routine will walk through the data and produce averaged normals
  653. * for all points on the grid.
  654. *
  655. * CHANGES
  656. *
  657. * -
  658. *
  659. ******************************************************************************/
  660. static void smooth_height_field(HFIELD *HField, int xsize, int zsize)
  661. {
  662. int i, j, k;
  663. VECTOR N;
  664. HF_VAL **map = HField->Data->Map;
  665. /* First off, allocate all the memory needed to store the normal information */
  666. HField->Data->Normals_Height = zsize+1;
  667. HField->Data->Normals = (HF_Normals **)POV_MALLOC((zsize+1)*sizeof(HF_Normals *), "height field normals");
  668. for (i = 0; i <= zsize; i++)
  669. {
  670. HField->Data->Normals[i] = (HF_Normals *)POV_MALLOC((xsize+1)*sizeof(HF_Normals), "height field normals");
  671. }
  672. /*
  673. * For now we will do it the hard way - by generating the normals
  674. * individually for each elevation point.
  675. */
  676. for (i = 0; i < zsize; i++)
  677. {
  678. COOPERATE_0
  679. for (j = 0; j < xsize; j++)
  680. {
  681. Make_Vector(N, 0.0, 0.0, 0.0);
  682. k = 0;
  683. k += add_single_normal(map, xsize, zsize, j, i, j+1, i, j, i+1, N);
  684. k += add_single_normal(map, xsize, zsize, j, i, j, i+1, j-1, i, N);
  685. k += add_single_normal(map, xsize, zsize, j, i, j-1, i, j, i-1, N);
  686. k += add_single_normal(map, xsize, zsize, j, i, j, i-1, j+1, i, N);
  687. if (k == 0)
  688. {
  689. Error ("Failed to find any normals at: (%d, %d).\n", i, j);
  690. }
  691. normalize(N, N);
  692. HField->Data->Normals[i][j][0] = (short)(32767 * N[X]);
  693. HField->Data->Normals[i][j][1] = (short)(32767 * N[Y]);
  694. HField->Data->Normals[i][j][2] = (short)(32767 * N[Z]);
  695. }
  696. }
  697. }
  698. /*****************************************************************************
  699. *
  700. * FUNCTION
  701. *
  702. * Compute_HField
  703. *
  704. * INPUT
  705. *
  706. * OUTPUT
  707. *
  708. * RETURNS
  709. *
  710. * AUTHOR
  711. *
  712. * Doug Muir, David Buck, Drew Wells
  713. *
  714. * DESCRIPTION
  715. *
  716. * Copy image data into height field map. Create bounding blocks
  717. * for the block traversal. Calculate normals for smoothed height fields.
  718. *
  719. * CHANGES
  720. *
  721. * Feb 1995 : Modified to work with new intersection functions. [DB]
  722. *
  723. ******************************************************************************/
  724. void Compute_HField(HFIELD *HField, IMAGE *Image)
  725. {
  726. int x, z, max_x, max_z;
  727. int temp1 = 0, temp2 = 0;
  728. HF_VAL min_y, max_y, temp_y;
  729. /* Get height field map size. */
  730. max_x = Image->iwidth;
  731. if (Image->File_Type == POT_FILE)
  732. {
  733. max_x = max_x / 2;
  734. }
  735. max_z = Image->iheight;
  736. /* Allocate memory for map. */
  737. HField->Data->Map = (HF_VAL **)POV_MALLOC(max_z*sizeof(HF_VAL *), "height field");
  738. for (z = 0; z < max_z; z++)
  739. {
  740. HField->Data->Map[z] = (HF_VAL *)POV_MALLOC(max_x*sizeof(HF_VAL), "height field");
  741. }
  742. /* Copy map. */
  743. min_y = 65535L;
  744. max_y = 0;
  745. for (z = 0; z < max_z; z++)
  746. {
  747. COOPERATE_0
  748. for (x = 0; x < max_x; x++)
  749. {
  750. switch (Image->File_Type)
  751. {
  752. case GIF_FILE:
  753. temp1 = Image->data.map_lines[max_z - z - 1][x];
  754. temp2 = 0;
  755. break;
  756. case POT_FILE:
  757. temp1 = Image->data.map_lines[max_z - z - 1][x];
  758. temp2 = Image->data.map_lines[max_z - z - 1][x + max_x];
  759. break;
  760. case PPM_FILE:
  761. temp1 = Image->data.rgb_lines[max_z - z - 1].red[x];
  762. temp2 = Image->data.rgb_lines[max_z - z - 1].green[x];
  763. break;
  764. case PGM_FILE:
  765. case TGA_FILE:
  766. case PNG_FILE:
  767. if (Image->Colour_Map == NULL)
  768. {
  769. temp1 = Image->data.rgb_lines[max_z - z - 1].red[x];
  770. temp2 = Image->data.rgb_lines[max_z - z - 1].green[x];
  771. }
  772. else
  773. {
  774. temp1 = Image->data.map_lines[max_z - z - 1][x];
  775. temp2 = 0;
  776. }
  777. break;
  778. default:
  779. Error("Unknown image type in Compute_HField().\n");
  780. }
  781. temp_y = (HF_VAL)(256*temp1 + temp2);
  782. HField->Data->Map[z][x] = temp_y;
  783. min_y = min(min_y, temp_y);
  784. max_y = max(max_y, temp_y);
  785. }
  786. }
  787. /* Resize bounding box. */
  788. HField->Data->min_y = min_y;
  789. HField->Data->max_y = max_y;
  790. HField->bounding_box->bounds[0][Y] = max((DBL)min_y, HField->bounding_box->bounds[0][Y]) - HFIELD_OFFSET;
  791. HField->bounding_box->bounds[1][Y] = (DBL)max_y + HFIELD_OFFSET;
  792. /* Compute smoothed height field. */
  793. if (Test_Flag(HField, SMOOTHED_FLAG))
  794. {
  795. smooth_height_field(HField, max_x-1, max_z-1);
  796. }
  797. HField->Data->max_x = max_x-2;
  798. HField->Data->max_z = max_z-2;
  799. build_hfield_blocks(HField);
  800. }
  801. /*****************************************************************************
  802. *
  803. * FUNCTION
  804. *
  805. * build_hfield_blocks
  806. *
  807. * INPUT
  808. *
  809. * OUTPUT
  810. *
  811. * RETURNS
  812. *
  813. * AUTHOR
  814. *
  815. * Dieter Bayer
  816. *
  817. * DESCRIPTION
  818. *
  819. * Create the bounding block hierarchy used by the block traversal.
  820. *
  821. * CHANGES
  822. *
  823. * Feb 1995 : Creation.
  824. *
  825. ******************************************************************************/
  826. static void build_hfield_blocks(HFIELD *HField)
  827. {
  828. int x, z, nx, nz, wx, wz;
  829. int i, j;
  830. int xmin, xmax, zmin, zmax;
  831. DBL y, ymin, ymax, water;
  832. /* Get block size. */
  833. nx = max(1, (int)(sqrt((DBL)HField->Data->max_x)));
  834. nz = max(1, (int)(sqrt((DBL)HField->Data->max_z)));
  835. /* Get dimensions of sub-block. */
  836. wx = (int)ceil((DBL)(HField->Data->max_x + 2) / (DBL)nx);
  837. wz = (int)ceil((DBL)(HField->Data->max_z + 2) / (DBL)nz);
  838. /* Increase number of sub-blocks if necessary. */
  839. if (nx * wx < HField->Data->max_x + 2)
  840. {
  841. nx++;
  842. }
  843. if (nz * wz < HField->Data->max_z + 2)
  844. {
  845. nz++;
  846. }
  847. if (!Test_Flag(HField, HIERARCHY_FLAG) || ((nx == 1) && (nz == 1)))
  848. {
  849. /* We don't want a bounding hierarchy. Just use one block. */
  850. HField->Data->Block = (HFIELD_BLOCK **)POV_MALLOC(sizeof(HFIELD_BLOCK *), "height field blocks");
  851. HField->Data->Block[0] = (HFIELD_BLOCK *)POV_MALLOC(sizeof(HFIELD_BLOCK), "height field blocks");
  852. HField->Data->Block[0][0].xmin = 0;
  853. HField->Data->Block[0][0].xmax = HField->Data->max_x;
  854. HField->Data->Block[0][0].zmin = 0;
  855. HField->Data->Block[0][0].zmax = HField->Data->max_z;
  856. HField->Data->Block[0][0].ymin = HField->bounding_box->bounds[0][Y];
  857. HField->Data->Block[0][0].ymax = HField->bounding_box->bounds[1][Y];
  858. HField->Data->block_max_x = 1;
  859. HField->Data->block_max_z = 1;
  860. HField->Data->block_width_x = HField->Data->max_x + 2;
  861. HField->Data->block_width_z = HField->Data->max_y + 2;
  862. /*
  863. Debug_Info("\nHeight field: %d x %d (1 x 1 blocks)", HField->Data->max_x+2, HField->Data->max_z+2);
  864. */
  865. return;
  866. }
  867. /*
  868. Debug_Info("\nHeight field: %d x %d (%d x %d blocks)", HField->Data->max_x+2, HField->Data->max_z+2, nx, nz);
  869. */
  870. /* Allocate memory for blocks. */
  871. HField->Data->Block = (HFIELD_BLOCK **)POV_MALLOC(nz*sizeof(HFIELD_BLOCK *), "height field blocks");
  872. /* Store block information. */
  873. HField->Data->block_max_x = nx;
  874. HField->Data->block_max_z = nz;
  875. HField->Data->block_width_x = wx;
  876. HField->Data->block_width_z = wz;
  877. water = HField->bounding_box->bounds[0][Y];
  878. for (z = 0; z < nz; z++)
  879. {
  880. COOPERATE_1
  881. HField->Data->Block[z] = (HFIELD_BLOCK *)POV_MALLOC(nx*sizeof(HFIELD_BLOCK), "height field blocks");
  882. for (x = 0; x < nx; x++)
  883. {
  884. /* Get block's borders. */
  885. xmin = x * wx;
  886. zmin = z * wz;
  887. xmax = min((x + 1) * wx - 1, HField->Data->max_x);
  888. zmax = min((z + 1) * wz - 1, HField->Data->max_z);
  889. /* Find min. and max. height in current block. */
  890. ymin = BOUND_HUGE;
  891. ymax = -BOUND_HUGE;
  892. for (i = xmin; i <= xmax+1; i++)
  893. {
  894. for (j = zmin; j <= zmax+1; j++)
  895. {
  896. y = Get_Height(i, j, HField);
  897. ymin = min(ymin, y);
  898. ymax = max(ymax, y);
  899. }
  900. }
  901. /* Store block's borders. */
  902. HField->Data->Block[z][x].xmin = xmin;
  903. HField->Data->Block[z][x].xmax = xmax;
  904. HField->Data->Block[z][x].zmin = zmin;
  905. HField->Data->Block[z][x].zmax = zmax;
  906. HField->Data->Block[z][x].ymin = max(ymin, water) - HFIELD_OFFSET;
  907. HField->Data->Block[z][x].ymax = ymax + HFIELD_OFFSET;
  908. }
  909. }
  910. }
  911. /*****************************************************************************
  912. *
  913. * FUNCTION
  914. *
  915. * Translate_HField
  916. *
  917. * INPUT
  918. *
  919. * OUTPUT
  920. *
  921. * RETURNS
  922. *
  923. * AUTHOR
  924. *
  925. * Doug Muir, David Buck, Drew Wells
  926. *
  927. * DESCRIPTION
  928. *
  929. * -
  930. *
  931. * CHANGES
  932. *
  933. * -
  934. *
  935. ******************************************************************************/
  936. static void Translate_HField (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans)
  937. {
  938. Transform_HField(Object, Trans);
  939. }
  940. /*****************************************************************************
  941. *
  942. * FUNCTION
  943. *
  944. * Rotate_HField
  945. *
  946. * INPUT
  947. *
  948. * OUTPUT
  949. *
  950. * RETURNS
  951. *
  952. * AUTHOR
  953. *
  954. * Doug Muir, David Buck, Drew Wells
  955. *
  956. * DESCRIPTION
  957. *
  958. * -
  959. *
  960. * CHANGES
  961. *
  962. * -
  963. *
  964. ******************************************************************************/
  965. static void Rotate_HField (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans)
  966. {
  967. Transform_HField(Object, Trans);
  968. }
  969. /*****************************************************************************
  970. *
  971. * FUNCTION
  972. *
  973. * Scale_HField
  974. *
  975. * INPUT
  976. *
  977. * OUTPUT
  978. *
  979. * RETURNS
  980. *
  981. * AUTHOR
  982. *
  983. * Doug Muir, David Buck, Drew Wells
  984. *
  985. * DESCRIPTION
  986. *
  987. * -
  988. *
  989. * CHANGES
  990. *
  991. * -
  992. *
  993. ******************************************************************************/
  994. static void Scale_HField (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans)
  995. {
  996. Transform_HField(Object, Trans);
  997. }
  998. /*****************************************************************************
  999. *
  1000. * FUNCTION
  1001. *
  1002. * Invert_HField
  1003. *
  1004. * INPUT
  1005. *
  1006. * OUTPUT
  1007. *
  1008. * RETURNS
  1009. *
  1010. * AUTHOR
  1011. *
  1012. * Doug Muir, David Buck, Drew Wells
  1013. *
  1014. * DESCRIPTION
  1015. *
  1016. * -
  1017. *
  1018. * CHANGES
  1019. *
  1020. * -
  1021. *
  1022. ******************************************************************************/
  1023. static void Invert_HField (OBJECT *Object)
  1024. {
  1025. Invert_Flag(Object, INVERTED_FLAG);
  1026. }
  1027. /*****************************************************************************
  1028. *
  1029. * FUNCTION
  1030. *
  1031. * Transform_HField
  1032. *
  1033. * INPUT
  1034. *
  1035. * OUTPUT
  1036. *
  1037. * RETURNS
  1038. *
  1039. * AUTHOR
  1040. *
  1041. * Doug Muir, David Buck, Drew Wells
  1042. *
  1043. * DESCRIPTION
  1044. *
  1045. * -
  1046. *
  1047. * CHANGES
  1048. *
  1049. * -
  1050. *
  1051. ******************************************************************************/
  1052. static void Transform_HField (OBJECT *Object, TRANSFORM *Trans)
  1053. {
  1054. Compose_Transforms(((HFIELD *)Object)->Trans, Trans);
  1055. Compute_HField_BBox((HFIELD *)Object);
  1056. }
  1057. /*****************************************************************************
  1058. *
  1059. * FUNCTION
  1060. *
  1061. * Create_HField
  1062. *
  1063. * INPUT
  1064. *
  1065. * OUTPUT
  1066. *
  1067. * RETURNS
  1068. *
  1069. * AUTHOR
  1070. *
  1071. * Doug Muir, David Buck, Drew Wells
  1072. *
  1073. * DESCRIPTION
  1074. *
  1075. * Allocate and intialize a height field.
  1076. *
  1077. * CHANGES
  1078. *
  1079. * Feb 1995 : Modified to work with new intersection functions. [DB]
  1080. *
  1081. ******************************************************************************/
  1082. HFIELD *Create_HField()
  1083. {
  1084. HFIELD *New;
  1085. /* Allocate height field. */
  1086. New = (HFIELD *)POV_MALLOC(sizeof(HFIELD), "height field");
  1087. INIT_OBJECT_FIELDS(New, HFIELD_OBJECT, &HField_Methods)
  1088. /* Always uses Trans so always create one. */
  1089. New->Trans = Create_Transform();
  1090. New->bounding_box = Create_Box();
  1091. /* Allocate height field data. */
  1092. New->Data = (HFIELD_DATA *)POV_MALLOC(sizeof(HFIELD_DATA), "height field");
  1093. New->Data->References = 1;
  1094. New->Data->cache_pos = 0;
  1095. New->Data->Normals_Height = 0;
  1096. New->Data->Map = NULL;
  1097. New->Data->Normals = NULL;
  1098. New->Data->max_x = 0;
  1099. New->Data->max_z = 0;
  1100. New->Data->block_max_x = 0;
  1101. New->Data->block_max_z = 0;
  1102. New->Data->block_width_x = 0;
  1103. New->Data->block_width_z = 0;
  1104. Set_Flag(New, HIERARCHY_FLAG);
  1105. return(New);
  1106. }
  1107. /*****************************************************************************
  1108. *
  1109. * FUNCTION
  1110. *
  1111. * Copy_HField
  1112. *
  1113. * INPUT
  1114. *
  1115. * OUTPUT
  1116. *
  1117. * RETURNS
  1118. *
  1119. * AUTHOR
  1120. *
  1121. * Doug Muir, David Buck, Drew Wells
  1122. *
  1123. * DESCRIPTION
  1124. *
  1125. * NOTE: The height field data is not copied, only the number of references
  1126. * is counted, so that Destray_HField() knows if it can be destroyed.
  1127. *
  1128. * CHANGES
  1129. *
  1130. * -
  1131. *
  1132. ******************************************************************************/
  1133. static HFIELD *Copy_HField(OBJECT *Object)
  1134. {
  1135. HFIELD *New;
  1136. New = Create_HField();
  1137. /* Destroy obsolete things created in Create_HField(). */
  1138. Destroy_Transform(New->Trans);
  1139. Destroy_Box((OBJECT *)(New->bounding_box));
  1140. POV_FREE (New->Data);
  1141. /* Copy height field. */
  1142. *New = *((HFIELD *)Object);
  1143. New->Trans = Copy_Transform(((HFIELD *)Object)->Trans);
  1144. New->bounding_box = Copy_Box((OBJECT *)(((HFIELD *)Object)->bounding_box));
  1145. New->Data->References++;
  1146. return(New);
  1147. }
  1148. /*****************************************************************************
  1149. *
  1150. * FUNCTION
  1151. *
  1152. * Destroy_HField
  1153. *
  1154. * INPUT
  1155. *
  1156. * OUTPUT
  1157. *
  1158. * RETURNS
  1159. *
  1160. * AUTHOR
  1161. *
  1162. * Doug Muir, David Buck, Drew Wells
  1163. *
  1164. * DESCRIPTION
  1165. *
  1166. * NOTE: The height field data is destroyed if it's no longer
  1167. * used by any copy.
  1168. *
  1169. * CHANGES
  1170. *
  1171. * Feb 1995 : Modified to work with new intersection functions. [DB]
  1172. *
  1173. ******************************************************************************/
  1174. static void Destroy_HField (OBJECT *Object)
  1175. {
  1176. int i;
  1177. HFIELD *HField = (HFIELD *)Object;
  1178. Destroy_Transform(HField->Trans);
  1179. Destroy_Box((OBJECT *)(HField->bounding_box));
  1180. if (--(HField->Data->References) == 0)
  1181. {
  1182. if (HField->Data->Map != NULL)
  1183. {
  1184. for (i = 0; i < HField->Data->max_z+2; i++)
  1185. {
  1186. if (HField->Data->Map[i] != NULL)
  1187. {
  1188. POV_FREE (HField->Data->Map[i]);
  1189. }
  1190. }
  1191. POV_FREE (HField->Data->Map);
  1192. }
  1193. if (HField->Data->Normals != NULL)
  1194. {
  1195. for (i = 0; i < HField->Data->Normals_Height; i++)
  1196. {
  1197. POV_FREE (HField->Data->Normals[i]);
  1198. }
  1199. POV_FREE (HField->Data->Normals);
  1200. }
  1201. if (HField->Data->Block != NULL)
  1202. {
  1203. for (i = 0; i < HField->Data->block_max_z; i++)
  1204. {
  1205. POV_FREE(HField->Data->Block[i]);
  1206. }
  1207. POV_FREE(HField->Data->Block);
  1208. }
  1209. POV_FREE (HField->Data);
  1210. }
  1211. POV_FREE (Object);
  1212. }
  1213. /*****************************************************************************
  1214. *
  1215. * FUNCTION
  1216. *
  1217. * Compute_HField_BBox
  1218. *
  1219. * INPUT
  1220. *
  1221. * HField - Height field
  1222. *
  1223. * OUTPUT
  1224. *
  1225. * HField
  1226. *
  1227. * RETURNS
  1228. *
  1229. * AUTHOR
  1230. *
  1231. * Dieter Bayer
  1232. *
  1233. * DESCRIPTION
  1234. *
  1235. * Calculate the bounding box of a height field.
  1236. *
  1237. * CHANGES
  1238. *
  1239. * Aug 1994 : Creation.
  1240. *
  1241. ******************************************************************************/
  1242. void Compute_HField_BBox(HFIELD *HField)
  1243. {
  1244. Assign_BBox_Vect(HField->BBox.Lower_Left, HField->bounding_box->bounds[0]);
  1245. VSub (HField->BBox.Lengths, HField->bounding_box->bounds[1], HField->bounding_box->bounds[0]);
  1246. if (HField->Trans != NULL)
  1247. {
  1248. Recompute_BBox(&HField->BBox, HField->Trans);
  1249. }
  1250. }
  1251. /*****************************************************************************
  1252. *
  1253. * FUNCTION
  1254. *
  1255. * dda_traversal
  1256. *
  1257. * INPUT
  1258. *
  1259. * Ray - Current ray
  1260. * HField - Height field
  1261. * Start - Start point for the walk
  1262. * Block - Sub-block of the height field to traverse
  1263. *
  1264. * OUTPUT
  1265. *
  1266. * RETURNS
  1267. *
  1268. * int - TRUE if intersection was found
  1269. *
  1270. * AUTHOR
  1271. *
  1272. * Dieter Bayer
  1273. *
  1274. * DESCRIPTION
  1275. *
  1276. * Traverse the grid cell of the height field using a modified DDA.
  1277. *
  1278. * Based on the following article:
  1279. *
  1280. * Musgrave, F. Kenton, "Grid Tracing: Fast Ray Tracing for Height
  1281. * Fields", Research Report YALEU-DCS-RR-39, Yale University, July 1988
  1282. *
  1283. * You should note that there are (n-1) x (m-1) grid cells in a height
  1284. * field of (image) size n x m. A grid cell (i,j), 0 <= i <= n-1,
  1285. * 0 <= j <= m-1, extends from x = i to x = i + 1 - epsilon and
  1286. * y = j to y = j + 1 -epsilon.
  1287. *
  1288. * CHANGES
  1289. *
  1290. * Feb 1995 : Creation.
  1291. *
  1292. ******************************************************************************/
  1293. static int dda_traversal(RAY *Ray, HFIELD *HField, VECTOR Start, HFIELD_BLOCK *Block)
  1294. {
  1295. int found;
  1296. int xmin, xmax, zmin, zmax;
  1297. int x, z, signx, signz;
  1298. DBL ymin, ymax, y1, y2;
  1299. DBL px, pz, dx, dy, dz;
  1300. DBL delta, error, x0, z0;
  1301. DBL neary, fary, deltay;
  1302. /* Setup DDA. */
  1303. found = FALSE;
  1304. px = Start[X];
  1305. pz = Start[Z];
  1306. /* Get dimensions of current block. */
  1307. xmin = Block->xmin;
  1308. xmax = min(Block->xmax + 1, HField->Data->max_x);
  1309. zmin = Block->zmin;
  1310. zmax = min(Block->zmax + 1, HField->Data->max_z);
  1311. ymin = min(Start[Y], Block->ymin) - EPSILON;
  1312. ymax = max(Start[Y], Block->ymax) + EPSILON;
  1313. /* Check for illegal grid values (caused by numerical inaccuracies). */
  1314. if (px < (DBL)xmin)
  1315. {
  1316. if (px < (DBL)xmin - HFIELD_OFFSET)
  1317. {
  1318. Debug_Info("Illegal grid value in dda_traversal().\n");
  1319. return(FALSE);
  1320. }
  1321. else
  1322. {
  1323. px = (DBL)xmin;
  1324. }
  1325. }
  1326. else
  1327. {
  1328. if (px > (DBL)xmax + 1.0 - EPSILON)
  1329. {
  1330. if (px > (DBL)xmax + 1.0 + EPSILON)
  1331. {
  1332. Debug_Info("Illegal grid value in dda_traversal().\n");
  1333. return(FALSE);
  1334. }
  1335. else
  1336. {
  1337. px = (DBL)xmax + 1.0 - EPSILON;
  1338. }
  1339. }
  1340. }
  1341. if (pz < (DBL)zmin)
  1342. {
  1343. if (pz < (DBL)zmin - HFIELD_OFFSET)
  1344. {
  1345. Debug_Info("Illegal grid value in dda_traversal().\n");
  1346. return(FALSE);
  1347. }
  1348. else
  1349. {
  1350. pz = (DBL)zmin;
  1351. }
  1352. }
  1353. else
  1354. {
  1355. if (pz > (DBL)zmax + 1.0 - EPSILON)
  1356. {
  1357. if (pz > (DBL)zmax + 1.0 + EPSILON)
  1358. {
  1359. Debug_Info("Illegal grid value in dda_traversal().\n");
  1360. return(FALSE);
  1361. }
  1362. else
  1363. {
  1364. pz = (DBL)zmax + 1.0 - EPSILON;
  1365. }
  1366. }
  1367. }
  1368. dx = Ray->Direction[X];
  1369. dy = Ray->Direction[Y];
  1370. dz = Ray->Direction[Z];
  1371. /*
  1372. * Here comes the DDA algorithm.
  1373. */
  1374. /* Choose algorithm depending on the driving axis. */
  1375. if (fabs(dx) >= fabs(dz))
  1376. {
  1377. /*
  1378. * X-axis is driving axis.
  1379. */
  1380. delta = fabs(dz / dx);
  1381. x = (int)px;
  1382. z = (int)pz;
  1383. x0 = px - floor(px);
  1384. z0 = pz - floor(pz);
  1385. signx = sign(dx);
  1386. signz = sign(dz);
  1387. /* Get initial error. */
  1388. if (dx >= 0.0)
  1389. {
  1390. if (dz >= 0.0)
  1391. {
  1392. error = z0 + delta * (1.0 - x0) - 1.0;
  1393. }
  1394. else
  1395. {
  1396. error = -(z0 - delta * (1.0 - x0));
  1397. }
  1398. }
  1399. else
  1400. {
  1401. if (dz >= 0.0)
  1402. {
  1403. error = z0 + delta * x0 - 1.0;
  1404. }
  1405. else
  1406. {
  1407. error = -(z0 - delta * x0);
  1408. }
  1409. }
  1410. /* Get y differential. */
  1411. deltay = dy / fabs(dx);
  1412. if (dx >= 0.0)
  1413. {
  1414. neary = Start[Y] - x0 * deltay;
  1415. fary = neary + deltay;
  1416. }
  1417. else
  1418. {
  1419. neary = Start[Y] - (1.0 - x0) * deltay;
  1420. fary = neary + deltay;
  1421. }
  1422. /* Step through the cells. */
  1423. do
  1424. {
  1425. if (neary < fary)
  1426. {
  1427. y1 = neary;
  1428. y2 = fary;
  1429. }
  1430. else
  1431. {
  1432. y1 = fary;
  1433. y2 = neary;
  1434. }
  1435. if (intersect_pixel(x, z, Ray, HField, y1, y2))
  1436. {
  1437. if (HField->Type & IS_CHILD_OBJECT)
  1438. {
  1439. found = TRUE;
  1440. }
  1441. else
  1442. {
  1443. return(TRUE);
  1444. }
  1445. }
  1446. if (error > EPSILON)
  1447. {
  1448. z += signz;
  1449. if ((z < zmin) || (z > zmax))
  1450. {
  1451. break;
  1452. }
  1453. else
  1454. {
  1455. if (intersect_pixel(x, z, Ray, HField, y1, y2))
  1456. {
  1457. if (HField->Type & IS_CHILD_OBJECT)
  1458. {
  1459. found = TRUE;
  1460. }
  1461. else
  1462. {
  1463. return(TRUE);
  1464. }
  1465. }
  1466. }
  1467. error--;
  1468. }
  1469. else
  1470. {
  1471. if (error > -EPSILON)
  1472. {
  1473. z += signz;
  1474. error--;
  1475. }
  1476. }
  1477. x += signx;
  1478. error += delta;
  1479. neary = fary;
  1480. fary += deltay;
  1481. }
  1482. while ((neary >= ymin) && (neary <= ymax) && (x >= xmin) && (x <= xmax) && (z >= zmin) && (z <= zmax));
  1483. }
  1484. else
  1485. {
  1486. /*
  1487. * Z-axis is driving axis.
  1488. */
  1489. delta = fabs(dx / dz);
  1490. x = (int)px;
  1491. z = (int)pz;
  1492. x0 = px - floor(px);
  1493. z0 = pz - floor(pz);
  1494. signx = sign(dx);
  1495. signz = sign(dz);
  1496. /* Get initial error. */
  1497. if (dz >= 0.0)
  1498. {
  1499. if (dx >= 0.0)
  1500. {
  1501. error = x0 + delta * (1.0 - z0) - 1.0;
  1502. }
  1503. else
  1504. {
  1505. error = -(x0 - delta * (1.0 - z0));
  1506. }
  1507. }
  1508. else
  1509. {
  1510. if (dx >= 0.0)
  1511. {
  1512. error = x0 + delta * z0 - 1.0;
  1513. }
  1514. else
  1515. {
  1516. error = -(x0 - delta * z0);
  1517. }
  1518. }
  1519. /* Get y differential. */
  1520. deltay = dy / fabs(dz);
  1521. if (dz >= 0.0)
  1522. {
  1523. neary = Start[Y] - z0 * deltay;
  1524. fary = neary + deltay;
  1525. }
  1526. else
  1527. {
  1528. neary = Start[Y] - (1.0 - z0) * deltay;
  1529. fary = neary + deltay;
  1530. }
  1531. /* Step through the cells. */
  1532. do
  1533. {
  1534. if (neary < fary)
  1535. {
  1536. y1 = neary;
  1537. y2 = fary;
  1538. }
  1539. else
  1540. {
  1541. y1 = fary;
  1542. y2 = neary;
  1543. }
  1544. if (intersect_pixel(x, z, Ray, HField, y1, y2))
  1545. {
  1546. if (HField->Type & IS_CHILD_OBJECT)
  1547. {
  1548. found = TRUE;
  1549. }
  1550. else
  1551. {
  1552. return(TRUE);
  1553. }
  1554. }
  1555. if (error > EPSILON)
  1556. {
  1557. x += signx;
  1558. if ((x < xmin) || (x > xmax))
  1559. {
  1560. break;
  1561. }
  1562. else
  1563. {
  1564. if (intersect_pixel(x, z, Ray, HField, y1, y2))
  1565. {
  1566. if (HField->Type & IS_CHILD_OBJECT)
  1567. {
  1568. found = TRUE;
  1569. }
  1570. else
  1571. {
  1572. return(TRUE);
  1573. }
  1574. }
  1575. }
  1576. error--;
  1577. }
  1578. else
  1579. {
  1580. if (error > -EPSILON)
  1581. {
  1582. x += signx;
  1583. error--;
  1584. }
  1585. }
  1586. z += signz;
  1587. error += delta;
  1588. neary = fary;
  1589. fary += deltay;
  1590. }
  1591. while ((neary >= ymin-EPSILON) && (neary <= ymax+EPSILON) &&
  1592. (x >= xmin) && (x <= xmax) &&
  1593. (z >= zmin) && (z <= zmax));
  1594. }
  1595. return(found);
  1596. }
  1597. /*****************************************************************************
  1598. *
  1599. * FUNCTION
  1600. *
  1601. * block_traversal
  1602. *
  1603. * INPUT
  1604. *
  1605. * Ray - Current ray
  1606. * HField - Height field
  1607. * Start - Start point for the walk
  1608. *
  1609. * OUTPUT
  1610. *
  1611. * RETURNS
  1612. *
  1613. * int - TRUE if intersection was found
  1614. *
  1615. * AUTHOR
  1616. *
  1617. * Dieter Bayer
  1618. *
  1619. * DESCRIPTION
  1620. *
  1621. * Traverse the blocks of the height field.
  1622. *
  1623. * CHANGES
  1624. *
  1625. * Feb 1995 : Creation.
  1626. *
  1627. * Aug 1996 : Fixed bug as reported by Dean M. Phillips:
  1628. * "I found a bug in the height field code which resulted
  1629. * in "Illegal grid value in dda_traversal()." messages
  1630. * along with dark vertical lines in the height fields.
  1631. * This turned out to be caused by overlooking the units in
  1632. * which some boundary tests in two different places were
  1633. * made. It was easy to fix.
  1634. *
  1635. ******************************************************************************/
  1636. static int block_traversal(RAY *Ray, HFIELD *HField, VECTOR Start)
  1637. {
  1638. int xmax, zmax;
  1639. int x, z, nx, nz, signx, signz;
  1640. int found = FALSE;
  1641. int dx_zero, dz_zero;
  1642. DBL px, pz, dx, dy, dz;
  1643. DBL maxdv;
  1644. DBL ymin, ymax, y1, y2;
  1645. DBL neary, fary;
  1646. DBL k1, k2, dist;
  1647. VECTOR nearP, farP;
  1648. HFIELD_BLOCK *Block;
  1649. px = Start[X];
  1650. pz = Start[Z];
  1651. dx = Ray->Direction[X];
  1652. dy = Ray->Direction[Y];
  1653. dz = Ray->Direction[Z];
  1654. maxdv = (dx > dz) ? dx : dz;
  1655. /* First test for 'perpendicular' rays. */
  1656. if ((fabs(dx) < EPSILON) && (fabs(dz) < EPSILON))
  1657. {
  1658. x = (int)px;
  1659. z = (int)pz;
  1660. neary = Start[Y];
  1661. if (dy >= 0.0)
  1662. {
  1663. fary = 65536.0;
  1664. }
  1665. else
  1666. {
  1667. fary = 0.0;
  1668. }
  1669. return(intersect_pixel(x, z, Ray, HField, min(neary, fary), max(neary, fary)));
  1670. }
  1671. /* If we don't have blocks we just step through the grid. */
  1672. if ((HField->Data->block_max_x <= 1) && (HField->Data->block_max_z <= 1))
  1673. {
  1674. return(dda_traversal(Ray, HField, Start, &HField->Data->Block[0][0]));
  1675. }
  1676. /* Get dimensions of grid. */
  1677. xmax = HField->Data->block_max_x;
  1678. zmax = HField->Data->block_max_z;
  1679. ymin = (DBL)HField->Data->min_y - EPSILON;
  1680. ymax = (DBL)HField->Data->max_y + EPSILON;
  1681. dx_zero = (fabs(dx) < EPSILON);
  1682. dz_zero = (fabs(dz) < EPSILON);
  1683. signx = sign(dx);
  1684. signz = sign(dz);
  1685. /* Walk on the block grid. */
  1686. px /= HField->Data->block_width_x;
  1687. pz /= HField->Data->block_width_z;
  1688. x = (int)px;
  1689. z = (int)pz;
  1690. Assign_Vector(nearP, Start);
  1691. neary = Start[Y];
  1692. /*
  1693. * Here comes the block walk algorithm.
  1694. */
  1695. do
  1696. {
  1697. #ifdef HFIELD_EXTRA_STATS
  1698. Increase_Counter(stats[Ray_HField_Block_Tests]);
  1699. #endif
  1700. /* Get current block. */
  1701. Block = &HField->Data->Block[z][x];
  1702. /* Intersect ray with bounding planes. */
  1703. if (dx_zero)
  1704. {
  1705. k1 = BOUND_HUGE;
  1706. }
  1707. else
  1708. {
  1709. if (signx >= 0)
  1710. {
  1711. k1 = ((DBL)Block->xmax + 1.0 - Ray->Initial[X]) / dx;
  1712. }
  1713. else
  1714. {
  1715. k1 = ((DBL)Block->xmin - Ray->Initial[X]) / dx;
  1716. }
  1717. }
  1718. if (dz_zero)
  1719. {
  1720. k2 = BOUND_HUGE;
  1721. }
  1722. else
  1723. {
  1724. if (signz >= 0)
  1725. {
  1726. k2 = ((DBL)Block->zmax + 1.0 - Ray->Initial[Z]) / dz;
  1727. }
  1728. else
  1729. {
  1730. k2 = ((DBL)Block->zmin - Ray->Initial[Z]) / dz;
  1731. }
  1732. }
  1733. /* Figure out the indices of the next block. */
  1734. if (dz_zero || ((!dx_zero) && (k1<k2 - EPSILON / maxdv) && (k1>0.0)))
  1735. /* if ((k1 < k2 - EPSILON / maxdv) && (k1 > 0.0)) */
  1736. {
  1737. /* Step along the x-axis. */
  1738. dist = k1;
  1739. nx = x + signx;
  1740. nz = z;
  1741. }
  1742. else
  1743. {
  1744. if (dz_zero || ((!dx_zero) && (k1<k2 + EPSILON / maxdv) && (k1>0.0)))
  1745. /* if ((k1 < k2 + EPSILON / maxdv) && (k1 > 0.0)) */
  1746. {
  1747. /* Step along both axis (very rare case). */
  1748. dist = k1;
  1749. nx = x + signx;
  1750. nz = z + signz;
  1751. }
  1752. else
  1753. {
  1754. /* Step along the z-axis. */
  1755. dist = k2;
  1756. nx = x;
  1757. nz = z + signz;
  1758. }
  1759. }
  1760. /* Get point where ray leaves current block. */
  1761. VEvaluateRay(farP, Ray->Initial, dist, Ray->Direction);
  1762. fary = farP[Y];
  1763. if (neary < fary)
  1764. {
  1765. y1 = neary;
  1766. y2 = fary;
  1767. }
  1768. else
  1769. {
  1770. y1 = fary;
  1771. y2 = neary;
  1772. }
  1773. /* Can we hit current block at all? */
  1774. if ((y1 <= (DBL)Block->ymax + EPSILON) && (y2 >= (DBL)Block->ymin - EPSILON))
  1775. {
  1776. /* Test current block. */
  1777. #ifdef HFIELD_EXTRA_STATS
  1778. Increase_Counter(stats[Ray_HField_Block_Tests_Succeeded]);
  1779. #endif
  1780. if (dda_traversal(Ray, HField, nearP, &HField->Data->Block[z][x]))
  1781. {
  1782. if (HField->Type & IS_CHILD_OBJECT)
  1783. {
  1784. found = TRUE;
  1785. }
  1786. else
  1787. {
  1788. return(TRUE);
  1789. }
  1790. }
  1791. }
  1792. /* Step to next block. */
  1793. x = nx;
  1794. z = nz;
  1795. Assign_Vector(nearP, farP);
  1796. neary = fary;
  1797. }
  1798. while ((x >= 0) && (x < xmax) && (z >= 0) && (z < zmax) && (neary >= ymin) && (neary <= ymax));
  1799. return(found);
  1800. }