VBUFFER.C 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951
  1. /****************************************************************************
  2. * vbuffer.c
  3. *
  4. * This module implements functions that implement the vista buffer.
  5. *
  6. * This module was written by Dieter Bayer [DB].
  7. *
  8. * from Persistence of Vision(tm) Ray Tracer
  9. * Copyright 1996,1999 Persistence of Vision Team
  10. *---------------------------------------------------------------------------
  11. * NOTICE: This source code file is provided so that users may experiment
  12. * with enhancements to POV-Ray and to port the software to platforms other
  13. * than those supported by the POV-Ray Team. There are strict rules under
  14. * which you are permitted to use this file. The rules are in the file
  15. * named POVLEGAL.DOC which should be distributed with this file.
  16. * If POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  17. * Team Coordinator by email to team-coord@povray.org or visit us on the web at
  18. * http://www.povray.org. The latest version of POV-Ray may be found at this site.
  19. *
  20. * This program is based on the popular DKB raytracer version 2.12.
  21. * DKBTrace was originally written by David K. Buck.
  22. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  23. *
  24. * Modifications by Thomas Willhalm, March 1999, used with permission
  25. *
  26. *****************************************************************************/
  27. /****************************************************************************
  28. *
  29. * Explanation:
  30. *
  31. * -
  32. *
  33. * ---
  34. *
  35. * Mar 1994 : Creation.
  36. *
  37. *****************************************************************************/
  38. #include "frame.h"
  39. #include "vector.h"
  40. #include "povproto.h"
  41. #include "bbox.h"
  42. #include "boxes.h"
  43. #include "hfield.h"
  44. #include "lighting.h"
  45. #include "matrices.h"
  46. #include "objects.h"
  47. #include "povray.h"
  48. #include "render.h"
  49. #include "triangle.h"
  50. #include "vbuffer.h"
  51. #include "vlbuffer.h"
  52. #include "userio.h"
  53. /*****************************************************************************
  54. * Local preprocessor defines
  55. ******************************************************************************/
  56. /*****************************************************************************
  57. * Local typedefs
  58. ******************************************************************************/
  59. /*****************************************************************************
  60. * Local variables
  61. ******************************************************************************/
  62. static DBL Distance;
  63. static MATRIX WC2VC, WC2VCinv;
  64. static VECTOR gO, gU, gV, gW;
  65. /* Planes for 3d-clipping. */
  66. static VECTOR VIEW_VX1 = {-0.8944271910, 0.0, -0.4472135955};
  67. static VECTOR VIEW_VX2 = { 0.8944271910, 0.0, -0.4472135955};
  68. static VECTOR VIEW_VY1 = {0.0, -0.8944271910, -0.4472135955};
  69. static VECTOR VIEW_VY2 = {0.0, 0.8944271910, -0.4472135955};
  70. static DBL VIEW_DX1 = 0.4472135955;
  71. static DBL VIEW_DX2 = 0.4472135955;
  72. static DBL VIEW_DY1 = 0.4472135955;
  73. static DBL VIEW_DY2 = 0.4472135955;
  74. static PROJECT_TREE_NODE *Root_Vista;
  75. /*****************************************************************************
  76. * Static functions
  77. ******************************************************************************/
  78. static void init_view_coordinates (void);
  79. static void project_raw_rectangle (PROJECT *Project, VECTOR P1, VECTOR P2, VECTOR P3, VECTOR P4, int *visible);
  80. static void project_raw_triangle (PROJECT *Project, VECTOR P1, VECTOR P2, VECTOR P3, int *visible);
  81. static void project_bbox (PROJECT *Project, VECTOR *P, int *visible);
  82. static void project_bounds (PROJECT *Project, BBOX *BBox, int *visible);
  83. static void get_perspective_projection (OBJECT *Object, PROJECT *Project, int infinite);
  84. static void get_orthographic_projection (OBJECT *Object, PROJECT *Project, int infinite);
  85. static void project_object (OBJECT *Object, PROJECT *Project);
  86. static void project_box (PROJECT *Project, OBJECT *Object, int *visible);
  87. static void project_hfield (PROJECT *Project, OBJECT *Object, int *visible);
  88. static void project_triangle (PROJECT *Project, OBJECT *Object, int *visible);
  89. static void project_smooth_triangle (PROJECT *Project, OBJECT *Object, int *visible);
  90. static void transform_point (VECTOR P);
  91. static void project_bounding_slab (PROJECT *Project, PROJECT_TREE_NODE **Tree, BBOX_TREE *Node);
  92. static int intersect_vista_tree (RAY *Ray, PROJECT_TREE_NODE *Tree, int x, INTERSECTION *Best_Intersection);
  93. static void draw_projection (PROJECT *Project, int color, int *BigRed, int *BigBlue);
  94. static void draw_vista (PROJECT_TREE_NODE *Tree, int *BigRed, int *BigBlue);
  95. /*****************************************************************************
  96. *
  97. * FUNCTION
  98. *
  99. * Prune_Vista_Tree
  100. *
  101. * INPUT
  102. *
  103. * y - Current scanline number
  104. *
  105. * OUTPUT
  106. *
  107. * RETURNS
  108. *
  109. * AUTHOR
  110. *
  111. * Dieter Bayer
  112. *
  113. * DESCRIPTION
  114. *
  115. * Prune vista tree, i.e. mark all nodes not on the current line inactive.
  116. *
  117. * CHANGES
  118. *
  119. * May 1994 : Creation.
  120. *
  121. ******************************************************************************/
  122. void Prune_Vista_Tree(int y)
  123. {
  124. unsigned short i;
  125. PROJECT_TREE_NODE *Node, *Sib;
  126. /* If there's no vista tree then return. */
  127. if (Root_Vista == NULL)
  128. {
  129. return;
  130. }
  131. Node_Queue->QSize = 0;
  132. Increase_Counter(stats[VBuffer_Tests]);
  133. if ((y < Root_Vista->Project.y1) || (y > Root_Vista->Project.y2))
  134. {
  135. /* Root doesn't lie on current line --> prune root */
  136. Root_Vista->is_leaf |= PRUNE_TEMPORARY;
  137. }
  138. else
  139. {
  140. /* Root lies on current line --> unprune root */
  141. Increase_Counter(stats[VBuffer_Tests_Succeeded]);
  142. Root_Vista->is_leaf &= ~PRUNE_TEMPORARY;
  143. Node_Queue->Queue[(Node_Queue->QSize)++] = Root_Vista;
  144. }
  145. while (Node_Queue->QSize > 0)
  146. {
  147. Node = Node_Queue->Queue[--(Node_Queue->QSize)];
  148. if (Node->is_leaf & TRUE)
  149. {
  150. Increase_Counter(stats[VBuffer_Tests]);
  151. if ((y < Node->Project.y1) || (y > Node->Project.y2))
  152. {
  153. /* Leaf doesn't lie on current line --> prune leaf */
  154. Node->is_leaf |= PRUNE_TEMPORARY;
  155. }
  156. else
  157. {
  158. /* Leaf lies on current line --> unprune leaf */
  159. Increase_Counter(stats[VBuffer_Tests_Succeeded]);
  160. Node->is_leaf &= ~PRUNE_TEMPORARY;
  161. }
  162. }
  163. else
  164. {
  165. /* Check siblings of the node */
  166. for (i = 0; i < Node->Entries; i++)
  167. {
  168. Sib = Node->Entry[i];
  169. Increase_Counter(stats[VBuffer_Tests]);
  170. if ((y < Sib->Project.y1) || (y > Sib->Project.y2))
  171. {
  172. /* Sibling doesn't lie on current line --> prune sibling */
  173. Sib->is_leaf |= PRUNE_TEMPORARY;
  174. }
  175. else
  176. {
  177. /* Sibling lies on current line --> unprune sibling */
  178. Increase_Counter(stats[VBuffer_Tests_Succeeded]);
  179. Sib->is_leaf &= ~PRUNE_TEMPORARY;
  180. /* Add sibling to list */
  181. /* Reallocate queue if it's too small. */
  182. Reinitialize_VLBuffer_Code();
  183. Node_Queue->Queue[(Node_Queue->QSize)++] = Sib;
  184. }
  185. }
  186. }
  187. }
  188. }
  189. /*****************************************************************************
  190. *
  191. * FUNCTION
  192. *
  193. * Trace_Primary_Ray
  194. *
  195. * INPUT
  196. *
  197. * Ray - Current ray
  198. * Colour - Ray's colour
  199. * x - Current x-coordinate
  200. *
  201. * OUTPUT
  202. *
  203. * colour
  204. *
  205. * RETURNS
  206. *
  207. * AUTHOR
  208. *
  209. * Dieter Bayer
  210. *
  211. * DESCRIPTION
  212. *
  213. * Trace a primary ray using the vista tree.
  214. *
  215. * CHANGES
  216. *
  217. * May 1994 : Creation.
  218. *
  219. * Nov 1994 : Rearranged calls to Fog, Ranibow and Skyblend.
  220. * Added call to Atmosphere for atmospheric effects. [DB]
  221. *
  222. * Jan 1995 : Set intersection depth to Max_Distance for infinte rays. [DB]
  223. * Jul 1995 : Added code to support alpha channel. [DB]
  224. *
  225. ******************************************************************************/
  226. void Trace_Primary_Ray (RAY *Ray, COLOUR Colour, DBL Weight, int x)
  227. {
  228. int i, Intersection_Found, all_hollow;
  229. INTERSECTION Best_Intersection;
  230. COOPERATE_0
  231. Increase_Counter(stats[Number_Of_Rays]);
  232. /* Transmittance has to be 1 to make alpha channel output to work. [DB] */
  233. Make_ColourA(Colour, 0.0, 0.0, 0.0, 0.0, 1.0);
  234. if ((Trace_Level > Max_Trace_Level) || (Weight < ADC_Bailout))
  235. {
  236. if (Weight < ADC_Bailout)
  237. {
  238. Increase_Counter(stats[ADC_Saves]);
  239. }
  240. return;
  241. }
  242. if (Trace_Level > Highest_Trace_Level)
  243. {
  244. Highest_Trace_Level = Trace_Level;
  245. }
  246. Best_Intersection.Depth = BOUND_HUGE;
  247. /* What objects does this ray intersect? */
  248. Intersection_Found = intersect_vista_tree(Ray, Root_Vista, x, &Best_Intersection);
  249. if (Intersection_Found)
  250. {
  251. Determine_Apparent_Colour(&Best_Intersection, Colour, Ray, 1.0);
  252. }
  253. else
  254. {
  255. /* Infinite ray, set intersection distance. */
  256. Best_Intersection.Depth = Max_Distance;
  257. Do_Infinite_Atmosphere(Ray, Colour);
  258. }
  259. /* Test if all contained objects are hollow. */
  260. all_hollow = TRUE;
  261. if (Ray->Index > -1)
  262. {
  263. for (i = 0; i <= Ray->Index; i++)
  264. {
  265. if (!Ray->Interiors[i]->hollow)
  266. {
  267. all_hollow = FALSE;
  268. break;
  269. }
  270. }
  271. }
  272. /* Apply finite atmospheric effects. */
  273. if (all_hollow && (opts.Quality_Flags & Q_VOLUME))
  274. {
  275. Do_Finite_Atmosphere(Ray, &Best_Intersection, Colour, FALSE);
  276. }
  277. }
  278. /*****************************************************************************
  279. *
  280. * FUNCTION
  281. *
  282. * intersect_vista_tree
  283. *
  284. * INPUT
  285. *
  286. * Ray - Primary ray
  287. * Tree - Vista tree's top-node
  288. * x - Current x-coordinate
  289. * Best_Intersection - Intersection found
  290. *
  291. * OUTPUT
  292. *
  293. * Best_Intersection
  294. *
  295. * RETURNS
  296. *
  297. * AUTHOR
  298. *
  299. * Dieter Bayer
  300. *
  301. * DESCRIPTION
  302. *
  303. * Intersect a PRIMARY ray with the vista tree
  304. * (tree pruning is used can be primary ray!!!).
  305. *
  306. * CHANGES
  307. *
  308. * May 1994 : Creation.
  309. *
  310. ******************************************************************************/
  311. static int intersect_vista_tree(RAY *Ray, PROJECT_TREE_NODE *Tree, int x, INTERSECTION *Best_Intersection)
  312. {
  313. INTERSECTION New_Intersection;
  314. unsigned short i;
  315. /* unsigned size; */ /* tw, mtg */
  316. int Found;
  317. RAYINFO rayinfo;
  318. DBL key;
  319. BBOX_TREE *BBox_Node;
  320. PROJECT_TREE_NODE *Node;
  321. /* If there's no vista tree then return. */
  322. if (Tree == NULL)
  323. {
  324. return(FALSE);
  325. }
  326. /* Start with an empty priority queue */
  327. VLBuffer_Queue->QSize = 0;
  328. Found = FALSE;
  329. #ifdef BBOX_EXTRA_STATS
  330. Increase_Counter(stats[totalQueueResets]);
  331. #endif
  332. /* Descend tree. */
  333. Node_Queue->QSize = 0;
  334. /* Create the direction vectors for this ray */
  335. Create_Rayinfo(Ray, &rayinfo);
  336. /* Fill the priority queue with all possible candidates */
  337. /* Check root */
  338. Increase_Counter(stats[VBuffer_Tests]);
  339. if ((x >= Tree->Project.x1) && (x <= Tree->Project.x2))
  340. {
  341. Increase_Counter(stats[VBuffer_Tests_Succeeded]);
  342. Node_Queue->Queue[(Node_Queue->QSize)++] = Tree;
  343. }
  344. while (Node_Queue->QSize > 0)
  345. {
  346. Tree = Node_Queue->Queue[--(Node_Queue->QSize)];
  347. switch (Tree->is_leaf)
  348. {
  349. case FALSE:
  350. /* Check siblings of the unpruned node in 2d */
  351. for (i = 0; i < Tree->Entries; i++)
  352. {
  353. Node = Tree->Entry[i];
  354. /* Check unpruned siblings only */
  355. if (Node->is_leaf < PRUNE_CHECK)
  356. {
  357. Increase_Counter(stats[VBuffer_Tests]);
  358. if ((x >= Node->Project.x1) && (x <= Node->Project.x2))
  359. {
  360. /* Add node to node queue. */
  361. Increase_Counter(stats[VBuffer_Tests_Succeeded]);
  362. /* Reallocate queue if it's too small. */
  363. Reinitialize_VLBuffer_Code();
  364. Node_Queue->Queue[(Node_Queue->QSize)++] = Node;
  365. }
  366. }
  367. }
  368. break;
  369. case TRUE:
  370. /* Unpruned leaf --> test object's bounding box in 3d */
  371. Check_And_Enqueue(VLBuffer_Queue,
  372. ((PROJECT_TREE_LEAF *)Tree)->Node,
  373. &(((PROJECT_TREE_LEAF *)Tree)->Node->BBox),
  374. &rayinfo);
  375. break;
  376. /* default:
  377. The node/leaf is pruned and needn't be checked */
  378. }
  379. }
  380. /* Now test the candidates in the priority queue */
  381. while (VLBuffer_Queue->QSize > 0)
  382. {
  383. Priority_Queue_Delete(VLBuffer_Queue, &key, &BBox_Node);
  384. if (key > Best_Intersection->Depth)
  385. break;
  386. if (Intersection(&New_Intersection, (OBJECT *)BBox_Node->Node, Ray))
  387. {
  388. if (New_Intersection.Depth < Best_Intersection->Depth)
  389. {
  390. *Best_Intersection = New_Intersection;
  391. Found = TRUE;
  392. }
  393. }
  394. }
  395. return(Found);
  396. }
  397. /*****************************************************************************
  398. *
  399. * FUNCTION
  400. *
  401. * project_raw_triangle
  402. *
  403. * INPUT
  404. *
  405. * Project - Triangle's projection
  406. * P1, P2, P3 - Triangle's edges
  407. * visible - Flag if triangle is visible
  408. *
  409. * OUTPUT
  410. *
  411. * Project, visible
  412. *
  413. * RETURNS
  414. *
  415. * AUTHOR
  416. *
  417. * Dieter Bayer
  418. *
  419. * DESCRIPTION
  420. *
  421. * Project a triangle onto the screen.
  422. *
  423. * CHANGES
  424. *
  425. * May 1994 : Creation.
  426. *
  427. ******************************************************************************/
  428. static void project_raw_triangle (PROJECT *Project, VECTOR P1, VECTOR P2, VECTOR P3, int *visible)
  429. {
  430. VECTOR Points[MAX_CLIP_POINTS];
  431. int i, number;
  432. int x, y;
  433. Assign_Vector(Points[0], P1);
  434. Assign_Vector(Points[1], P2);
  435. Assign_Vector(Points[2], P3);
  436. number = 3;
  437. /* Clip triangle only if some quick tests say it's necessary.
  438. Assuming that only a few triangles need clipping this saves some time.
  439. (I don't need to write fabs(1+P?[Z]) since the tests succeed anyway if
  440. P?[Z] < -1. Hope the compiler doesn't change the tests' order!) */
  441. if ((P1[Z] < -1.0) || (P2[Z] < -1.0) || (P3[Z] < -1.0) ||
  442. (fabs(P1[X]) > 0.5*(1.0+P1[Z])) || (fabs(P1[Y]) > 0.5*(1.0+P1[Z])) ||
  443. (fabs(P2[X]) > 0.5*(1.0+P2[Z])) || (fabs(P2[Y]) > 0.5*(1.0+P2[Z])) ||
  444. (fabs(P3[X]) > 0.5*(1.0+P3[Z])) || (fabs(P3[Y]) > 0.5*(1.0+P3[Z])))
  445. {
  446. Clip_Polygon(Points, &number, VIEW_VX1, VIEW_VX2, VIEW_VY1, VIEW_VY2,
  447. VIEW_DX1, VIEW_DX2, VIEW_DY1, VIEW_DY2);
  448. }
  449. if (number)
  450. {
  451. for (i = 0; i < number; i++)
  452. {
  453. if (Points[i][Z] < -1.0 + EPSILON)
  454. {
  455. Points[i][X] = Points[i][Y] = 0.0;
  456. }
  457. else
  458. {
  459. Points[i][X] /= 1.0 + Points[i][Z];
  460. Points[i][Y] /= 1.0 + Points[i][Z];
  461. }
  462. x = Frame.Screen_Width/2 + (int)(Frame.Screen_Width * Points[i][X]);
  463. y = Frame.Screen_Height/2 - (int)(Frame.Screen_Height * Points[i][Y]);
  464. if (x < Project->x1) Project->x1 = x;
  465. if (x > Project->x2) Project->x2 = x;
  466. if (y < Project->y1) Project->y1 = y;
  467. if (y > Project->y2) Project->y2 = y;
  468. }
  469. *visible = TRUE;
  470. }
  471. }
  472. /*****************************************************************************
  473. *
  474. * FUNCTION
  475. *
  476. * project_raw_rectangle
  477. *
  478. * INPUT
  479. *
  480. * Project - Rectangle's projection
  481. * P1, P2, P3, P4 - Rectangle's edges
  482. * visible - Flag if rectangle is visible
  483. *
  484. * OUTPUT
  485. *
  486. * Project, visible
  487. *
  488. * RETURNS
  489. *
  490. * AUTHOR
  491. *
  492. * Dieter Bayer
  493. *
  494. * DESCRIPTION
  495. *
  496. * Project a rectangle onto the screen.
  497. *
  498. * CHANGES
  499. *
  500. * May 1994 : Creation.
  501. *
  502. ******************************************************************************/
  503. static void project_raw_rectangle(PROJECT *Project, VECTOR P1, VECTOR P2, VECTOR P3, VECTOR P4, int *visible)
  504. {
  505. VECTOR Points[MAX_CLIP_POINTS];
  506. int i, number;
  507. int x, y;
  508. Assign_Vector(Points[0], P1);
  509. Assign_Vector(Points[1], P2);
  510. Assign_Vector(Points[2], P3);
  511. Assign_Vector(Points[3], P4);
  512. number = 4;
  513. Clip_Polygon(Points, &number, VIEW_VX1, VIEW_VX2, VIEW_VY1, VIEW_VY2,
  514. VIEW_DX1, VIEW_DX2, VIEW_DY1, VIEW_DY2);
  515. if (number)
  516. {
  517. for (i = 0; i < number; i++)
  518. {
  519. if (Points[i][Z] < -1.0 + EPSILON)
  520. {
  521. Points[i][X] = Points[i][Y] = 0.0;
  522. }
  523. else
  524. {
  525. Points[i][X] /= 1.0 + Points[i][Z];
  526. Points[i][Y] /= 1.0 + Points[i][Z];
  527. }
  528. x = Frame.Screen_Width/2 + (int)(Frame.Screen_Width * Points[i][X]);
  529. y = Frame.Screen_Height/2 - (int)(Frame.Screen_Height * Points[i][Y]);
  530. if (x < Project->x1) Project->x1 = x;
  531. if (x > Project->x2) Project->x2 = x;
  532. if (y < Project->y1) Project->y1 = y;
  533. if (y > Project->y2) Project->y2 = y;
  534. }
  535. *visible = TRUE;
  536. }
  537. }
  538. /*****************************************************************************
  539. *
  540. * FUNCTION
  541. *
  542. * project_bbox
  543. *
  544. * INPUT
  545. *
  546. * Project - Box's projection
  547. * P - Box's edges
  548. * visible - Flag if box is visible
  549. *
  550. * OUTPUT
  551. *
  552. * Project, visible
  553. *
  554. * RETURNS
  555. *
  556. * AUTHOR
  557. *
  558. * Dieter Bayer
  559. *
  560. * DESCRIPTION
  561. *
  562. * Project a box onto the screen.
  563. *
  564. * CHANGES
  565. *
  566. * May 1994 : Creation.
  567. *
  568. ******************************************************************************/
  569. static void project_bbox(PROJECT *Project, VECTOR *P, int *visible)
  570. {
  571. int vis, i, x, y;
  572. PROJECT New;
  573. New.x1 = MAX_BUFFER_ENTRY;
  574. New.x2 = MIN_BUFFER_ENTRY;
  575. New.y1 = MAX_BUFFER_ENTRY;
  576. New.y2 = MIN_BUFFER_ENTRY;
  577. vis = FALSE;
  578. /* Check if all points lie "in front" of the viewer. */
  579. if ((P[0][Z] > -1.0) && (P[1][Z] > -1.0) && (P[2][Z] > -1.0) && (P[3][Z] > -1.0) &&
  580. (P[4][Z] > -1.0) && (P[5][Z] > -1.0) && (P[6][Z] > -1.0) && (P[7][Z] > -1.0))
  581. {
  582. /* Check if all points lie inside the "viewing pyramid". */
  583. if ((fabs(P[0][X]) <= 0.5*(1.0+P[0][Z])) && (fabs(P[1][X]) <= 0.5*(1.0+P[1][Z])) &&
  584. (fabs(P[2][X]) <= 0.5*(1.0+P[2][Z])) && (fabs(P[3][X]) <= 0.5*(1.0+P[3][Z])) &&
  585. (fabs(P[4][X]) <= 0.5*(1.0+P[4][Z])) && (fabs(P[5][X]) <= 0.5*(1.0+P[5][Z])) &&
  586. (fabs(P[6][X]) <= 0.5*(1.0+P[6][Z])) && (fabs(P[7][X]) <= 0.5*(1.0+P[7][Z])) &&
  587. (fabs(P[0][Y]) <= 0.5*(1.0+P[0][Z])) && (fabs(P[1][Y]) <= 0.5*(1.0+P[1][Z])) &&
  588. (fabs(P[2][Y]) <= 0.5*(1.0+P[2][Z])) && (fabs(P[3][Y]) <= 0.5*(1.0+P[3][Z])) &&
  589. (fabs(P[4][Y]) <= 0.5*(1.0+P[4][Z])) && (fabs(P[5][Y]) <= 0.5*(1.0+P[5][Z])) &&
  590. (fabs(P[6][Y]) <= 0.5*(1.0+P[6][Z])) && (fabs(P[7][Y]) <= 0.5*(1.0+P[7][Z])))
  591. {
  592. /* No clipping is needed. Just project the points. */
  593. vis = TRUE;
  594. for (i = 0; i < 8; i++)
  595. {
  596. if (P[i][Z] < -1.0 + EPSILON)
  597. {
  598. P[i][X] = P[i][Y] = 0.0;
  599. }
  600. else
  601. {
  602. P[i][X] /= 1.0 + P[i][Z];
  603. P[i][Y] /= 1.0 + P[i][Z];
  604. }
  605. x = Frame.Screen_Width/2 + (int)(Frame.Screen_Width * P[i][X]);
  606. y = Frame.Screen_Height/2 - (int)(Frame.Screen_Height * P[i][Y]);
  607. if (x < New.x1) New.x1 = x;
  608. if (x > New.x2) New.x2 = x;
  609. if (y < New.y1) New.y1 = y;
  610. if (y > New.y2) New.y2 = y;
  611. }
  612. }
  613. }
  614. if (!vis)
  615. {
  616. project_raw_rectangle(&New, P[0], P[1], P[3], P[2], &vis);
  617. project_raw_rectangle(&New, P[4], P[5], P[7], P[6], &vis);
  618. project_raw_rectangle(&New, P[0], P[1], P[5], P[4], &vis);
  619. project_raw_rectangle(&New, P[2], P[3], P[7], P[6], &vis);
  620. project_raw_rectangle(&New, P[1], P[3], P[7], P[5], &vis);
  621. project_raw_rectangle(&New, P[0], P[2], P[6], P[4], &vis);
  622. }
  623. if (vis)
  624. {
  625. if (New.x1 > Project->x1) Project->x1 = New.x1;
  626. if (New.x2 < Project->x2) Project->x2 = New.x2;
  627. if (New.y1 > Project->y1) Project->y1 = New.y1;
  628. if (New.y2 < Project->y2) Project->y2 = New.y2;
  629. *visible = TRUE;
  630. }
  631. }
  632. /*****************************************************************************
  633. *
  634. * FUNCTION
  635. *
  636. * project_bounds
  637. *
  638. * INPUT
  639. *
  640. * Project - Bounding box's projection
  641. * BBox - Bounding box
  642. * visible - Flag if bounding box is visible
  643. *
  644. * OUTPUT
  645. *
  646. * Project, visible
  647. *
  648. * RETURNS
  649. *
  650. * AUTHOR
  651. *
  652. * Dieter Bayer
  653. *
  654. * DESCRIPTION
  655. *
  656. * Project a bounding box onto the screen.
  657. *
  658. * CHANGES
  659. *
  660. * May 1994 : Creation.
  661. *
  662. ******************************************************************************/
  663. static void project_bounds(PROJECT *Project, BBOX *BBox, int *visible)
  664. {
  665. int i;
  666. VECTOR P[8];
  667. for (i = 0; i<8; i++)
  668. {
  669. P[i][X] = ((i & 1) ? BBox->Lengths[X] : 0.0) + BBox->Lower_Left[X];
  670. P[i][Y] = ((i & 2) ? BBox->Lengths[Y] : 0.0) + BBox->Lower_Left[Y];
  671. P[i][Z] = ((i & 4) ? BBox->Lengths[Z] : 0.0) + BBox->Lower_Left[Z];
  672. transform_point(P[i]);
  673. }
  674. project_bbox(Project, P, visible);
  675. }
  676. /*****************************************************************************
  677. *
  678. * FUNCTION
  679. *
  680. * project_box
  681. *
  682. * INPUT
  683. *
  684. * Project - Projection
  685. * Object - Object
  686. * visible - Flag if object is visible
  687. *
  688. * OUTPUT
  689. *
  690. * Project, visible
  691. *
  692. * RETURNS
  693. *
  694. * AUTHOR
  695. *
  696. * Dieter Bayer
  697. *
  698. * DESCRIPTION
  699. *
  700. * Project a box onto the screen.
  701. *
  702. * CHANGES
  703. *
  704. * May 1994 : Creation.
  705. *
  706. ******************************************************************************/
  707. static void project_box(PROJECT *Project, OBJECT *Object, int *visible)
  708. {
  709. int i;
  710. VECTOR P[8];
  711. BOX *box;
  712. box = (BOX *)Object;
  713. for (i = 0; i<8; i++)
  714. {
  715. P[i][X] = (i & 1) ? box->bounds[1][X] : box->bounds[0][X];
  716. P[i][Y] = (i & 2) ? box->bounds[1][Y] : box->bounds[0][Y];
  717. P[i][Z] = (i & 4) ? box->bounds[1][Z] : box->bounds[0][Z];
  718. if (box->Trans != NULL)
  719. {
  720. MTransPoint(P[i], P[i], box->Trans);
  721. }
  722. transform_point(P[i]);
  723. }
  724. project_bbox(Project, P, visible);
  725. }
  726. /*****************************************************************************
  727. *
  728. * FUNCTION
  729. *
  730. * project_hfield
  731. *
  732. * INPUT
  733. *
  734. * Project - Projection
  735. * Object - Object
  736. * visible - Flag if object is visible
  737. *
  738. * OUTPUT
  739. *
  740. * Project, visible
  741. *
  742. * RETURNS
  743. *
  744. * AUTHOR
  745. *
  746. * Dieter Bayer
  747. *
  748. * DESCRIPTION
  749. *
  750. * Project the bounding box of a height field onto the screen.
  751. *
  752. * CHANGES
  753. *
  754. * May 1994 : Creation.
  755. *
  756. ******************************************************************************/
  757. static void project_hfield(PROJECT *Project, OBJECT *Object, int *visible)
  758. {
  759. int i;
  760. VECTOR P[8];
  761. HFIELD *hfield;
  762. hfield = (HFIELD *)Object;
  763. for (i = 0; i<8; i++)
  764. {
  765. Assign_Vector(P[i], hfield->bounding_box->bounds[0]);
  766. P[i][X] = (i & 1) ? hfield->bounding_box->bounds[1][X] : hfield->bounding_box->bounds[0][X];
  767. P[i][Y] = (i & 2) ? hfield->bounding_box->bounds[1][Y] : hfield->bounding_box->bounds[0][Y];
  768. P[i][Z] = (i & 4) ? hfield->bounding_box->bounds[1][Z] : hfield->bounding_box->bounds[0][Z];
  769. if (hfield->Trans != NULL)
  770. {
  771. MTransPoint(P[i], P[i], hfield->Trans);
  772. }
  773. transform_point(P[i]);
  774. }
  775. project_bbox(Project, P, visible);
  776. }
  777. /*****************************************************************************
  778. *
  779. * FUNCTION
  780. *
  781. * project_triangle
  782. *
  783. * INPUT
  784. *
  785. * Project - Projection
  786. * Object - Object
  787. * visible - Flag if object is visible
  788. *
  789. * OUTPUT
  790. *
  791. * Project, visible
  792. *
  793. * RETURNS
  794. *
  795. * AUTHOR
  796. *
  797. * Dieter Bayer
  798. *
  799. * DESCRIPTION
  800. *
  801. * Project a triangle onto the screen.
  802. *
  803. * CHANGES
  804. *
  805. * May 1994 : Creation.
  806. *
  807. ******************************************************************************/
  808. static void project_triangle(PROJECT *Project, OBJECT *Object, int *visible)
  809. {
  810. int i, vis;
  811. VECTOR P[3];
  812. PROJECT New;
  813. New.x1 = MAX_BUFFER_ENTRY;
  814. New.x2 = MIN_BUFFER_ENTRY;
  815. New.y1 = MAX_BUFFER_ENTRY;
  816. New.y2 = MIN_BUFFER_ENTRY;
  817. Assign_Vector(P[0], ((TRIANGLE *)Object)->P1);
  818. Assign_Vector(P[1], ((TRIANGLE *)Object)->P2);
  819. Assign_Vector(P[2], ((TRIANGLE *)Object)->P3);
  820. for (i = 0; i < 3; i++)
  821. {
  822. transform_point(P[i]);
  823. }
  824. vis = FALSE;
  825. project_raw_triangle(&New, P[0], P[1], P[2], &vis);
  826. if (vis)
  827. {
  828. if (New.x1 > Project->x1) Project->x1 = New.x1;
  829. if (New.x2 < Project->x2) Project->x2 = New.x2;
  830. if (New.y1 > Project->y1) Project->y1 = New.y1;
  831. if (New.y2 < Project->y2) Project->y2 = New.y2;
  832. *visible = TRUE;
  833. }
  834. }
  835. /*****************************************************************************
  836. *
  837. * FUNCTION
  838. *
  839. * project_smooth_triangle
  840. *
  841. * INPUT
  842. *
  843. * Project - Projection
  844. * Object - Object
  845. * visible - Flag if object is visible
  846. *
  847. * OUTPUT
  848. *
  849. * Project, visible
  850. *
  851. * RETURNS
  852. *
  853. * AUTHOR
  854. *
  855. * Dieter Bayer
  856. *
  857. * DESCRIPTION
  858. *
  859. * Project a smooth triangle onto the screen.
  860. *
  861. * CHANGES
  862. *
  863. * May 1994 : Creation.
  864. *
  865. ******************************************************************************/
  866. static void project_smooth_triangle(PROJECT *Project, OBJECT *Object, int *visible)
  867. {
  868. int i, vis;
  869. VECTOR P[3];
  870. PROJECT New;
  871. New.x1 = MAX_BUFFER_ENTRY;
  872. New.x2 = MIN_BUFFER_ENTRY;
  873. New.y1 = MAX_BUFFER_ENTRY;
  874. New.y2 = MIN_BUFFER_ENTRY;
  875. Assign_Vector(P[0], ((SMOOTH_TRIANGLE *)Object)->P1);
  876. Assign_Vector(P[1], ((SMOOTH_TRIANGLE *)Object)->P2);
  877. Assign_Vector(P[2], ((SMOOTH_TRIANGLE *)Object)->P3);
  878. for (i = 0; i < 3; i++)
  879. {
  880. transform_point(P[i]);
  881. }
  882. vis = FALSE;
  883. project_raw_triangle(&New, P[0], P[1], P[2], &vis);
  884. if (vis)
  885. {
  886. if (New.x1 > Project->x1) Project->x1 = New.x1;
  887. if (New.x2 < Project->x2) Project->x2 = New.x2;
  888. if (New.y1 > Project->y1) Project->y1 = New.y1;
  889. if (New.y2 < Project->y2) Project->y2 = New.y2;
  890. *visible = TRUE;
  891. }
  892. }
  893. /*****************************************************************************
  894. *
  895. * FUNCTION
  896. *
  897. * transform_point
  898. *
  899. * INPUT
  900. *
  901. * P - Point to transform
  902. *
  903. * OUTPUT
  904. *
  905. * P
  906. *
  907. * RETURNS
  908. *
  909. * AUTHOR
  910. *
  911. * Dieter Bayer
  912. *
  913. * DESCRIPTION
  914. *
  915. * Transform a point from the world coordinate system to the viewer's
  916. * coordinate system.
  917. *
  918. * CHANGES
  919. *
  920. * May 1994 : Creation.
  921. *
  922. ******************************************************************************/
  923. static void transform_point(VECTOR P)
  924. {
  925. DBL x,y,z;
  926. x = P[X] - gO[X];
  927. y = P[Y] - gO[Y];
  928. z = P[Z] - gO[Z];
  929. P[X] = gU[X] * x + gU[Y] * y + gU[Z] * z;
  930. P[Y] = gV[X] * x + gV[Y] * y + gV[Z] * z;
  931. P[Z] = gW[X] * x + gW[Y] * y + gW[Z] * z;
  932. }
  933. /*****************************************************************************
  934. *
  935. * FUNCTION
  936. *
  937. * Init_View_Coordinates
  938. *
  939. * INPUT
  940. *
  941. * OUTPUT
  942. *
  943. * RETURNS
  944. *
  945. * AUTHOR
  946. *
  947. * Dieter Bayer
  948. *
  949. * DESCRIPTION
  950. *
  951. * Init the matrices and vectors used to transform a point from
  952. * the world coordinate system to the viewer's coordinate system.
  953. *
  954. * CHANGES
  955. *
  956. * May 1994 : Creation.
  957. *
  958. ******************************************************************************/
  959. static void init_view_coordinates()
  960. {
  961. DBL k1, k2, k3, up_length, right_length;
  962. MATRIX A, B;
  963. Assign_Vector(gU, Frame.Camera->Right);
  964. Assign_Vector(gV, Frame.Camera->Up);
  965. Assign_Vector(gW, Frame.Camera->Direction);
  966. VAdd (gO, Frame.Camera->Location, Frame.Camera->Direction);
  967. VNormalize(gU,gU);
  968. VNormalize(gV,gV);
  969. VNormalize(gW,gW);
  970. VDot(k1, gU, gV);
  971. VDot(k2, gU, gW);
  972. VDot(k3, gV, gW);
  973. if ((fabs(k1) > EPSILON) || (fabs(k2) > EPSILON) || (fabs(k3) > EPSILON))
  974. {
  975. Error("Cannot use non-perpendicular camera vectors with vista buffer.\n");
  976. }
  977. VLength (Distance, Frame.Camera->Direction);
  978. VLength (up_length, Frame.Camera->Up);
  979. VLength (right_length, Frame.Camera->Right);
  980. VScaleEq (gU, 1.0/right_length);
  981. VScaleEq (gV, 1.0/up_length);
  982. VScaleEq (gW, 1.0/Distance);
  983. A[0][0] = gU[X]; A[0][1] = gU[Y]; A[0][2] = gU[Z]; A[0][3] = 0.0;
  984. A[1][0] = gV[X]; A[1][1] = gV[Y]; A[1][2] = gV[Z]; A[1][3] = 0.0;
  985. A[2][0] = gW[X]; A[2][1] = gW[Y]; A[2][2] = gW[Z]; A[2][3] = 0.0;
  986. A[3][0] = 0.0; A[3][1] = 0.0; A[3][2] = 0.0; A[3][3] = 1.0;
  987. B[0][0] = 1.0; B[0][1] = 0.0; B[0][2] = 0.0; B[0][3] = -gO[X];
  988. B[1][0] = 0.0; B[1][1] = 1.0; B[1][2] = 0.0; B[1][3] = -gO[Y];
  989. B[2][0] = 0.0; B[2][1] = 0.0; B[2][2] = 1.0; B[2][3] = -gO[Z];
  990. B[3][0] = 0.0; B[3][1] = 0.0; B[3][2] = 0.0; B[3][3] = 1.0;
  991. MTimes(WC2VC, A, B);
  992. MInvers(WC2VCinv, WC2VC);
  993. }
  994. /*****************************************************************************
  995. *
  996. * FUNCTION
  997. *
  998. * get_perspective_projection
  999. *
  1000. * INPUT
  1001. *
  1002. * Object - Object to project
  1003. * Project - Projection
  1004. * infinite - Flag if object is infinite
  1005. *
  1006. * OUTPUT
  1007. *
  1008. * Project
  1009. *
  1010. * RETURNS
  1011. *
  1012. * AUTHOR
  1013. *
  1014. * Dieter Bayer
  1015. *
  1016. * DESCRIPTION
  1017. *
  1018. * Get the perspective projection of a single object, i.e.
  1019. * the smallest rectangle enclosing the object's image on the screen.
  1020. *
  1021. * CHANGES
  1022. *
  1023. * May 1994 : Creation.
  1024. *
  1025. ******************************************************************************/
  1026. static void get_perspective_projection(OBJECT *Object, PROJECT *Project, int infinite)
  1027. {
  1028. int visible;
  1029. METHODS *Methods;
  1030. visible = FALSE;
  1031. Methods = Object->Methods;
  1032. /* If the object is infinite, there's no sense of projecting */
  1033. if (!infinite)
  1034. {
  1035. if ((Methods == &Box_Methods) ||
  1036. (Methods == &Smooth_Triangle_Methods) ||
  1037. (Methods == &Triangle_Methods) ||
  1038. (Methods == &HField_Methods))
  1039. {
  1040. if (Methods == &Box_Methods)
  1041. project_box(Project, Object, &visible);
  1042. if (Methods == &HField_Methods)
  1043. project_hfield(Project, Object, &visible);
  1044. if (Methods == &Smooth_Triangle_Methods)
  1045. project_smooth_triangle(Project, Object, &visible);
  1046. if (Methods == &Triangle_Methods)
  1047. project_triangle(Project, Object, &visible);
  1048. }
  1049. else
  1050. {
  1051. project_bounds(Project, &Object->BBox, &visible);
  1052. }
  1053. }
  1054. if (visible)
  1055. {
  1056. if (opts.Options & ANTIALIAS)
  1057. {
  1058. /* Increase the rectangle to make sure that nothing will be missed.
  1059. For anti-aliased images increase by a larger amount. */
  1060. Project->x1 = max (0, Project->x1 - 2);
  1061. Project->x2 = min (Frame.Screen_Width-1, Project->x2 + 2);
  1062. Project->y1 = max (-1, Project->y1 - 2);
  1063. Project->y2 = min (Frame.Screen_Height-1, Project->y2 + 2);
  1064. }
  1065. else
  1066. {
  1067. /* Increase the rectangle to make sure that nothing will be missed. */
  1068. Project->x1 = max (0, Project->x1 - 1);
  1069. Project->x2 = min (Frame.Screen_Width-1, Project->x2 + 1);
  1070. Project->y1 = max (0, Project->y1 - 1);
  1071. Project->y2 = min (Frame.Screen_Height-1, Project->y2 + 1);
  1072. }
  1073. }
  1074. else
  1075. {
  1076. if (!infinite)
  1077. {
  1078. /* Object is invisible (the camera can't see it) */
  1079. Project->x1 = Project->y1 = MAX_BUFFER_ENTRY;
  1080. Project->x2 = Project->y2 = MIN_BUFFER_ENTRY;
  1081. }
  1082. }
  1083. }
  1084. /*****************************************************************************
  1085. *
  1086. * FUNCTION
  1087. *
  1088. * get_orthographic_projection
  1089. *
  1090. * INPUT
  1091. *
  1092. * Object - Object to project
  1093. * Project - Projection
  1094. * infinite - Flag if object is infinite
  1095. *
  1096. * OUTPUT
  1097. *
  1098. * Project
  1099. *
  1100. * RETURNS
  1101. *
  1102. * AUTHOR
  1103. *
  1104. * Dieter Bayer
  1105. *
  1106. * DESCRIPTION
  1107. *
  1108. * Get the orthographic projection of a single object, i.e.
  1109. * the smallest rectangle enclosing the object's image on the screen.
  1110. *
  1111. * CHANGES
  1112. *
  1113. * May 1994 : Creation.
  1114. *
  1115. ******************************************************************************/
  1116. static void get_orthographic_projection(OBJECT *Object, PROJECT *Project, int infinite)
  1117. {
  1118. int visible, i, x, y;
  1119. VECTOR P[8];
  1120. visible = FALSE;
  1121. /* If the object is infinite, there's no sense of projecting */
  1122. if (!infinite)
  1123. {
  1124. /* The following could be done better but since only a minority of all
  1125. objects in a scene are partially visible I don't think it's worth it. */
  1126. for (i = 0; i < 8; i++)
  1127. {
  1128. P[i][X] = ((i & 1) ? Object->BBox.Lengths[X] : 0.0) + Object->BBox.Lower_Left[X];
  1129. P[i][Y] = ((i & 2) ? Object->BBox.Lengths[Y] : 0.0) + Object->BBox.Lower_Left[Y];
  1130. P[i][Z] = ((i & 4) ? Object->BBox.Lengths[Z] : 0.0) + Object->BBox.Lower_Left[Z];
  1131. transform_point(P[i]);
  1132. /* Check if bounding box is visible */
  1133. if (P[i][Z] >= 0.0) visible = TRUE;
  1134. }
  1135. /* Now get the projection */
  1136. if (visible)
  1137. {
  1138. Project->x1 = Project->y1 = MAX_BUFFER_ENTRY;
  1139. Project->x2 = Project->y2 = MIN_BUFFER_ENTRY;
  1140. for (i = 0; i < 8; i++)
  1141. {
  1142. /* The visible area is -0.5...+0.5/-0.5...+0.5 */
  1143. if (P[i][X] < -0.5) P[i][X] = -0.5;
  1144. if (P[i][X] > 0.5) P[i][X] = 0.5;
  1145. if (P[i][Y] < -0.5) P[i][Y] = -0.5;
  1146. if (P[i][Y] > 0.5) P[i][Y] = 0.5;
  1147. x = Frame.Screen_Width/2 + (int)(Frame.Screen_Width * P[i][X]);
  1148. y = Frame.Screen_Height/2 - (int)(Frame.Screen_Height * P[i][Y]);
  1149. if (x < Project->x1) Project->x1 = x;
  1150. if (x > Project->x2) Project->x2 = x;
  1151. if (y < Project->y1) Project->y1 = y;
  1152. if (y > Project->y2) Project->y2 = y;
  1153. }
  1154. }
  1155. }
  1156. if (visible)
  1157. {
  1158. if (opts.Options & ANTIALIAS)
  1159. {
  1160. /* Increase the rectangle to make sure that nothing will be missed.
  1161. For anti-aliased images decrease the lower borders. */
  1162. Project->x1 = max (0, Project->x1 - 2);
  1163. Project->x2 = min (Frame.Screen_Width-1, Project->x2 + 1);
  1164. Project->y1 = max (-1, Project->y1 - 2);
  1165. Project->y2 = min (Frame.Screen_Height-1, Project->y2 + 1);
  1166. }
  1167. else
  1168. {
  1169. /* Increase the rectangle to make sure that nothing will be missed. */
  1170. Project->x1 = max (0, Project->x1 - 1);
  1171. Project->x2 = min (Frame.Screen_Width-1, Project->x2 + 1);
  1172. Project->y1 = max (0, Project->y1 - 1);
  1173. Project->y2 = min (Frame.Screen_Height-1, Project->y2 + 1);
  1174. }
  1175. }
  1176. else
  1177. {
  1178. if (!infinite)
  1179. {
  1180. /* Object is invisible (the camera can't see it) */
  1181. Project->x1 = Project->y1 = MAX_BUFFER_ENTRY;
  1182. Project->x2 = Project->y2 = MIN_BUFFER_ENTRY;
  1183. }
  1184. }
  1185. }
  1186. /*****************************************************************************
  1187. *
  1188. * FUNCTION
  1189. *
  1190. * project_object
  1191. *
  1192. * INPUT
  1193. *
  1194. * Object - Object to project
  1195. * Project - Projection
  1196. *
  1197. * OUTPUT
  1198. *
  1199. * Project
  1200. *
  1201. * RETURNS
  1202. *
  1203. * AUTHOR
  1204. *
  1205. * Dieter Bayer
  1206. *
  1207. * DESCRIPTION
  1208. *
  1209. * Get the projection of a single object depending on the camera
  1210. * used (perspective/orthographic).
  1211. *
  1212. * CHANGES
  1213. *
  1214. * May 1994 : Creation.
  1215. *
  1216. ******************************************************************************/
  1217. static void project_object(OBJECT *Object, PROJECT *Project)
  1218. {
  1219. int infinite;
  1220. /* Init project fields, assuming the object is visible! */
  1221. Project->x1 = Project->y1 = MIN_BUFFER_ENTRY;
  1222. Project->x2 = Project->y2 = MAX_BUFFER_ENTRY;
  1223. infinite = Test_Flag(Object, INFINITE_FLAG);
  1224. switch (Frame.Camera->Type)
  1225. {
  1226. case PERSPECTIVE_CAMERA:
  1227. get_perspective_projection(Object, Project, infinite);
  1228. break;
  1229. case ORTHOGRAPHIC_CAMERA:
  1230. get_orthographic_projection(Object, Project, infinite);
  1231. break;
  1232. default:
  1233. Error("Wrong camera type in project_object().\n");
  1234. break;
  1235. }
  1236. }
  1237. /*****************************************************************************
  1238. *
  1239. * FUNCTION
  1240. *
  1241. * project_bounding_slab
  1242. *
  1243. * INPUT
  1244. *
  1245. * Project - Projection
  1246. * Tree - Current node/leaf
  1247. * Object - Node/leaf in bounding slab hierarchy
  1248. *
  1249. * OUTPUT
  1250. *
  1251. * Project, Tree
  1252. *
  1253. * RETURNS
  1254. *
  1255. * AUTHOR
  1256. *
  1257. * Dieter Bayer
  1258. *
  1259. * DESCRIPTION
  1260. *
  1261. * Project the bounding slab hierarchy onto the screen and thus create
  1262. * the vista buffer hierarchy.
  1263. *
  1264. * CHANGES
  1265. *
  1266. * May 1994 : Creation.
  1267. *
  1268. ******************************************************************************/
  1269. static void project_bounding_slab(PROJECT *Project, PROJECT_TREE_NODE **Tree, BBOX_TREE *Node)
  1270. {
  1271. short int i;
  1272. PROJECT Temp;
  1273. PROJECT_TREE_LEAF *Leaf;
  1274. PROJECT_TREE_NODE New;
  1275. if (Node->Entries)
  1276. {
  1277. /* Current object is a bounding object, i.e. a node in the slab tree. */
  1278. /* First, init new entry. */
  1279. New.Entries = 0;
  1280. New.Node = Node;
  1281. New.Project.x1 = New.Project.y1 = MAX_BUFFER_ENTRY;
  1282. New.Project.x2 = New.Project.y2 = MIN_BUFFER_ENTRY;
  1283. /* Allocate temporary memory for node/leaf entries. */
  1284. New.Entry = (PROJECT_TREE_NODE **)POV_MALLOC(Node->Entries*sizeof(PROJECT_TREE_NODE *), "temporary tree entry");
  1285. /* This is no leaf, it's a node. */
  1286. New.is_leaf = FALSE;
  1287. /* Second, get new entry, i.e. project node's entries. */
  1288. for (i = 0; i < Node->Entries; i++)
  1289. {
  1290. New.Entry[i] = NULL;
  1291. project_bounding_slab(&Temp, &New.Entry[New.Entries], Node->Node[i]);
  1292. /* Use only visible entries. */
  1293. if (New.Entry[New.Entries] != NULL)
  1294. {
  1295. New.Project.x1 = min(New.Project.x1, Temp.x1);
  1296. New.Project.x2 = max(New.Project.x2, Temp.x2);
  1297. New.Project.y1 = min(New.Project.y1, Temp.y1);
  1298. New.Project.y2 = max(New.Project.y2, Temp.y2);
  1299. New.Entries++;
  1300. }
  1301. }
  1302. /* If there are any visible entries, we'll use them. */
  1303. if (New.Entries > 0)
  1304. {
  1305. /* If there's only one entry, we won't need a new node. */
  1306. if (New.Entries == 1)
  1307. {
  1308. *Tree = New.Entry[0];
  1309. *Project = New.Project;
  1310. }
  1311. else
  1312. {
  1313. /* Allocate memory for new node in the vista tree. */
  1314. *Tree = (PROJECT_TREE_NODE *)POV_MALLOC(sizeof(PROJECT_TREE_NODE), "vista tree node");
  1315. **Tree = New;
  1316. /* Allocate memory for node/leaf entries. */
  1317. (*Tree)->Entry = (PROJECT_TREE_NODE **)POV_MALLOC(New.Entries*sizeof(PROJECT_TREE_NODE *), "vista tree node");
  1318. memcpy((*Tree)->Entry, New.Entry, New.Entries*sizeof(PROJECT_TREE_NODE *));
  1319. *Project = New.Project;
  1320. }
  1321. }
  1322. /* Get rid of temporary node/leaf entries. */
  1323. POV_FREE(New.Entry);
  1324. }
  1325. else
  1326. {
  1327. COOPERATE_0
  1328. /* Current object is a normal object, i.e. a leaf in the slab tree. */
  1329. /* Get object's projection. */
  1330. project_object((OBJECT *)Node->Node, Project);
  1331. /* Is the object visible? */
  1332. if ((Project->x1 <= Project->x2) && (Project->y1 <= Project->y2))
  1333. {
  1334. /* Allocate memory for new leaf in the vista tree. */
  1335. *Tree = (PROJECT_TREE_NODE *)POV_MALLOC(sizeof(PROJECT_TREE_LEAF), "vista tree leaf");
  1336. /* Init new leaf. */
  1337. Leaf = (PROJECT_TREE_LEAF *)(*Tree);
  1338. Leaf->Node = Node;
  1339. Leaf->Project = *Project;
  1340. /* Yes, this is a leaf. */
  1341. Leaf->is_leaf = TRUE;
  1342. }
  1343. }
  1344. }
  1345. /*****************************************************************************
  1346. *
  1347. * FUNCTION
  1348. *
  1349. * Build_Vista_Buffer
  1350. *
  1351. * INPUT
  1352. *
  1353. * OUTPUT
  1354. *
  1355. * RETURNS
  1356. *
  1357. * AUTHOR
  1358. *
  1359. * Dieter Bayer
  1360. *
  1361. * DESCRIPTION
  1362. *
  1363. * Build the vista tree, i.e. the 2d representation of the bounding slab
  1364. * hierarchy in image space.
  1365. *
  1366. * This only works for perspective and orthographic cameras.
  1367. *
  1368. * CHANGES
  1369. *
  1370. * May 1994 : Creation.
  1371. *
  1372. ******************************************************************************/
  1373. void Build_Vista_Buffer()
  1374. {
  1375. PROJECT Project;
  1376. Root_Vista = NULL;
  1377. /* Check if vista buffer can be used. */
  1378. if ((!opts.Use_Slabs) ||
  1379. (Frame.Camera->Tnormal != NULL) ||
  1380. ((Frame.Camera->Type != PERSPECTIVE_CAMERA) && (Frame.Camera->Type != ORTHOGRAPHIC_CAMERA)) ||
  1381. ((Frame.Camera->Aperture != 0.0) && (Frame.Camera->Blur_Samples > 0)))
  1382. {
  1383. opts.Options &= ~USE_VISTA_BUFFER;
  1384. }
  1385. if (opts.Options & USE_VISTA_BUFFER)
  1386. {
  1387. Status_Info("\nCreating vista buffer.");
  1388. init_view_coordinates();
  1389. project_bounding_slab(&Project, &Root_Vista, Root_Object);
  1390. }
  1391. }
  1392. /*****************************************************************************
  1393. *
  1394. * FUNCTION
  1395. *
  1396. * Destroy_Vista_Buffer
  1397. *
  1398. * INPUT
  1399. *
  1400. * OUTPUT
  1401. *
  1402. * RETURNS
  1403. *
  1404. * AUTHOR
  1405. *
  1406. * Dieter Bayer
  1407. *
  1408. * DESCRIPTION
  1409. *
  1410. * Destroy the vista tree.
  1411. *
  1412. * CHANGES
  1413. *
  1414. * Sep 1994 : Creation.
  1415. *
  1416. ******************************************************************************/
  1417. void Destroy_Vista_Buffer()
  1418. {
  1419. if ((opts.Options & USE_VISTA_BUFFER) && (Root_Vista != NULL))
  1420. {
  1421. Destroy_Project_Tree(Root_Vista);
  1422. Root_Vista = NULL;
  1423. }
  1424. }
  1425. /*****************************************************************************
  1426. *
  1427. * FUNCTION
  1428. *
  1429. * draw_projection
  1430. *
  1431. * INPUT
  1432. *
  1433. * Project - projection to draw
  1434. * color - Color to be used
  1435. *
  1436. * OUTPUT
  1437. *
  1438. * RETURNS
  1439. *
  1440. * AUTHOR
  1441. *
  1442. * Dieter Bayer
  1443. *
  1444. * DESCRIPTION
  1445. *
  1446. * Draws a projection in the specified color.
  1447. *
  1448. * CHANGES
  1449. *
  1450. * May 1994 : Creation.
  1451. * Jul 1996 : Draw boxes in white when doing grayscale preview
  1452. *
  1453. ******************************************************************************/
  1454. static void draw_projection(PROJECT *Project, int color, int *BigRed, int *BigBlue)
  1455. {
  1456. int x1, x2, y1, y2, draw_it;
  1457. unsigned char r, g, b, gray;
  1458. unsigned char a=0;
  1459. gray = (opts.PaletteOption == GREY) ? 255 : 0;
  1460. switch (color)
  1461. {
  1462. case RED : r = 255; g = b = gray; break;
  1463. case GREEN : g = 255; r = b = gray; break;
  1464. case BLUE : b = 255; r = g = gray; break;
  1465. default : r = g = b = 255;
  1466. }
  1467. x1 = Project->x1;
  1468. x2 = Project->x2;
  1469. y1 = Project->y1;
  1470. y2 = Project->y2;
  1471. if ((x1 <= x2) && (y1 <= y2))
  1472. {
  1473. if (x1 < 0) x1 = 0;
  1474. if (x2 < 0) x2 = 0;
  1475. if (y1 < 0) y1 = 0;
  1476. if (y2 < 0) y2 = 0;
  1477. if (x1 >= Frame.Screen_Width) x1 = Frame.Screen_Width - 1;
  1478. if (x2 >= Frame.Screen_Width) x2 = Frame.Screen_Width - 1;
  1479. if (y1 >= Frame.Screen_Height) y1 = Frame.Screen_Height - 1;
  1480. if (y2 >= Frame.Screen_Height) y2 = Frame.Screen_Height - 1;
  1481. /* Check for full-screen rectangle. */
  1482. draw_it = TRUE;
  1483. if ((x1 == 0) && (x2 == Frame.Screen_Width - 1) &&
  1484. (y1 == 0) && (y2 == Frame.Screen_Height - 1))
  1485. {
  1486. draw_it = FALSE;
  1487. switch (color)
  1488. {
  1489. case RED : if (!(*BigRed)) { *BigRed = draw_it = TRUE; } break;
  1490. case BLUE : if (!(*BigBlue)) { *BigBlue = draw_it = TRUE; } break;
  1491. }
  1492. }
  1493. if (draw_it)
  1494. {
  1495. POV_DISPLAY_PLOT_BOX(x1,y1,x2,y2,r,g,b,a);
  1496. }
  1497. }
  1498. }
  1499. /*****************************************************************************
  1500. *
  1501. * FUNCTION
  1502. *
  1503. * draw_vista
  1504. *
  1505. * INPUT
  1506. *
  1507. * Tree - current node/leaf in the vista tree
  1508. *
  1509. * OUTPUT
  1510. *
  1511. * RETURNS
  1512. *
  1513. * AUTHOR
  1514. *
  1515. * Dieter Bayer
  1516. *
  1517. * DESCRIPTION
  1518. *
  1519. * Draws recursively all projections of subnodes in the current node.
  1520. *
  1521. * CHANGES
  1522. *
  1523. * May 1994 : Creation.
  1524. *
  1525. ******************************************************************************/
  1526. static void draw_vista(PROJECT_TREE_NODE *Tree, int *BigRed, int *BigBlue)
  1527. {
  1528. unsigned short i;
  1529. PROJECT_TREE_LEAF *Leaf;
  1530. if (Tree->is_leaf)
  1531. {
  1532. Leaf = (PROJECT_TREE_LEAF *)Tree;
  1533. COOPERATE_1
  1534. if (((OBJECT *)Leaf->Node->Node)->Type & COMPOUND_OBJECT)
  1535. {
  1536. draw_projection(&Leaf->Project, BLUE, BigRed, BigBlue);
  1537. }
  1538. else
  1539. {
  1540. draw_projection(&Leaf->Project, RED, BigRed, BigBlue);
  1541. }
  1542. }
  1543. else
  1544. {
  1545. for (i = 0; i < Tree->Entries; i++)
  1546. {
  1547. draw_vista(Tree->Entry[i], BigRed, BigBlue);
  1548. }
  1549. }
  1550. /* draw bounding object's vista */
  1551. /*
  1552. draw_projection(&Tree->Project, GREEN);
  1553. */
  1554. }
  1555. /*****************************************************************************
  1556. *
  1557. * FUNCTION
  1558. *
  1559. * Draw_Vista_Buffer
  1560. *
  1561. * INPUT
  1562. *
  1563. * OUTPUT
  1564. *
  1565. * RETURNS
  1566. *
  1567. * AUTHOR
  1568. *
  1569. * Dieter Bayer
  1570. *
  1571. * DESCRIPTION
  1572. *
  1573. * Draw the vista tree.
  1574. *
  1575. * CHANGES
  1576. *
  1577. * May 1994 : Creation.
  1578. *
  1579. ******************************************************************************/
  1580. void Draw_Vista_Buffer()
  1581. {
  1582. int BigRed, BigBlue;
  1583. BigRed = BigBlue = FALSE;
  1584. if ((Root_Vista != NULL) && (opts.Options & USE_VISTA_DRAW))
  1585. {
  1586. draw_vista(Root_Vista, &BigRed, &BigBlue);
  1587. }
  1588. }