home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / PCGPEV10.ZIP / BSP.TXT < prev    next >
Text File  |  1994-05-10  |  7KB  |  150 lines

  1.                  ┌───────────────────────────────────┐
  2.                  │ A Simple Explanation of BSP Trees │
  3.                  └───────────────────────────────────┘
  4.  
  5.             Written for the PC-GPE by Mark Feldman
  6.             e-mail address : u914097@student.canberra.edu.au
  7.                              myndale@cairo.anu.edu.au
  8.  
  9.              ┌───────────────────────────────────────────┐
  10.              │      THIS FILE MAY NOT BE DISTRIBUTED     │
  11.              │ SEPARATE TO THE ENTIRE PC-GPE COLLECTION. │
  12.              └───────────────────────────────────────────┘
  13.  
  14.  
  15. ┌────────────┬───────────────────────────────────────────────────────────────
  16. │ Disclaimer │
  17. └────────────┘
  18.  
  19. I assume no responsibility whatsoever for any effect that this file, the
  20. information contained therein or the use thereof has on you, your sanity,
  21. computer, spouse, children, pets or anything else related to you or your
  22. existance. No warranty is provided nor implied with this information.
  23.  
  24. ┌───────────┬────────────────────────────────────────────────────────────────
  25. │ BSP Trees │
  26. └───────────┘
  27.  
  28. Binary Space Partition trees are handy for drawing 3D scenes where the
  29. positions of objects are fixed and the user's viewing coordinate changes
  30. (flight simulators being a classic example).
  31.  
  32. BSP's are an extention of the "Painter's Algorithm". The painter's algorithm
  33. works by drawing all the polygons (or texture maps) in a scene in back-to-
  34. front order, so that polygon's in the background are drawn first, and
  35. polygons in the foreground are drawn over them. The "classic" painter's
  36. algorithm does have a few problems however:
  37. 1) polygon's will not be drawn correctly if they pass through any other
  38. polygon
  39. 2) it's difficult and computationally expensive calculating the order that
  40. the polygons should be drawn in for each frame
  41. 3) the algorithm cannot handle cases of cyclic overlap such as the
  42. following :
  43.                            ___       ___
  44.                           |   |     |   |
  45.                         __|   |_____|___|___
  46.                        |  |   |             |
  47.                        |__|   |_____________|
  48.                           |   |     |   |
  49.                         __|___|_____|   |___
  50.                        |            |   |   |
  51.                        |____________|   |___|
  52.                           |   |     |   |
  53.                           |___|     |___|
  54.  
  55. In this case it doesn't matter which order you draw the polygon's it still
  56. won't look right!
  57.  
  58. BSP's help solve all these problems.
  59.  
  60. Ok, so let's get down to business. BSP's work by building an ordered tree of
  61. all the objects in a scene. Let's imagine we live in a 2D world and we have
  62. a scene like this:
  63.  
  64.              ┌────────────────────────────────────┐
  65.              │                                    │
  66.              │                                    │
  67.              │              ────────────────────  │
  68.              │                    line 1          │
  69.              │           \                        │
  70.              │             \                      │
  71.              │               \ line 2             │
  72.              │                 \                  │
  73.              │                   \                │
  74.              │   ────────          \              │
  75.              │   line 3              \            │
  76.              │                                    │
  77.              └────────────────────────────────────┘
  78.  
  79.                               ^
  80.                               viewpoint (assume the viewpoint is the
  81.                                          origin for this example)
  82.  
  83.  
  84. Now if we were to draw this scene using the painters algorithm we would
  85. draw line 1 first, then line 2, finally line 3. Using BSP's we can figure
  86. out the order beforehand and create a tree. First we note that any
  87. arbitrary point <x,y> can be on either of it's 2 sides or on the line (which
  88. can be regarded as being on either of the sides). When we build our tree, we
  89. take a line and put all the lines on one side of it to the left and all the
  90. nodes on the other side on the right. So for the above example could wind up
  91. with the following tree:
  92.  
  93.           1
  94.          /
  95.         2
  96.        /
  97.       3
  98.  
  99. Alternatively, we could also wind up with this tree:
  100.  
  101.         2
  102.        / \
  103.       3   1
  104.  
  105. Notice that line 2 is the head node, line 3 is on the same side of line 2
  106. as the origin is and line 1 is on the opposite side.
  107.  
  108. Now, I hear you say "but hang on a tic, what if line 3 is the head node? What
  109. side of it is line 2 on?". Yes boys and girls, there had to be a catch
  110. somewhere and this is it. What you have to do here is split line 2 into
  111. *TWO* lines so that each portion falls nice and neatly onto either side of
  112. line 3, so you get a tree like this:
  113.  
  114.        3
  115.       / \
  116.     2a   2b
  117.           \
  118.            1
  119.  
  120. The lines 2a and 2b are portions of the original line 2. If you draw *BOTH*
  121. of them on the screen it will look as though you've drawn the entire original
  122. line.
  123.  
  124. You don't have to worry about balancing a BSP tree, since you have to
  125. traverse every node in it every time you draw the scene anyway. The trick
  126. is to figure out how to organise the tree so that you get the *least* number
  127. of polygon splits. I tackled this by looking at each polygon yet to be
  128. inserted into the tree, calculating how many splits it will cause if it
  129. is inserted next and selecting the one which will cause the fewest. This
  130. is a very slow way of going about things, O(N^2) I think, but for most games
  131. you only need to sort the tree once when you are developping the game and not
  132. during the game itself.
  133.  
  134. Extending these concepts to 3D is pretty straight-forward. Let's say that
  135. polygon 1 is at the top of the BSP tree and we want to insert polygon 2. If
  136. all the points in polygon 2 fall on one side or the other of polygon 1 then
  137. you insert it into polygon 2's left or right node. If some points fall on
  138. one side and the rest fall on the other, then you have to figure out the line
  139. of intersection formed by the planes that each polygon lies in and split
  140. polygon 2 along this line. Each of these 2 new polygons will then fall on
  141. either side of polygon 1.
  142.  
  143.  
  144. To draw the objects in a BSP tree you start at the top node and figure out
  145. which side of the object your view coordinate is on. You then traverse the
  146. node for the *other* side, draw the current object, then traverse the node
  147. for the side the view coordinate is on.
  148.  
  149.  
  150.