Anda di halaman 1dari 11

BSP Tutorial by Steven Cento

Background Info: BSP stands for Binary Space Partitioning Tree. They where first brought into the light when John Carmack of ID Software used them in the game D ! "tm#. Since then BSP has played some part in ! ST $d %pplications. I apologi&e a head of time for my use of the words wall and polygon they mean the same thing. Ok So What Can This BSP Thing Do For e:

BSP is one of the fastest ways to do '(Sorting) Collision Testing) *endering) +ode Clipping) ,ighting calculations and Possible -isible Set. Fine !o" Tell e #o":

The first thing I am going to show you is how to create a BSP tree and do '(Sorting. Take this room for e.ample.

/igure 0

This is a o1erhead 1iew of a s2uare room. +ote the line denoting which side of the wall is the front and which are the back. '(Sorting "Telling which walls are in front of which is not a problem here. +o matter where you are you can only see 3 walls.

/igure 4

+ow this is a problem5 If you 1iewpoint is at camera point % you can see some walls but not others. 6ow can we tell which walls are in front of which7 If we can8t '(Sort a room then it won8t look right when we render it. pen9, and Direct $D ha1e a limited '(Sorting ability) plus it8s -:*; slow. So what can we do to '(Sort the room7 That8s where BSP comes in. Bsp trees come in many types) +ode) ,eaf and Solid. I will discuss all but the last. The first one I am going to talk about is the leaf tree. % leaf is a group of polygons "in our case walls# that ha1e no other nodes "% node is a split in the BSP tree that in the case of a leaf tree contains no polygon data#. /or e.ample<

/igure $

+odes may connect to other nodes but leafs may !$%$& connect other leafs. +ow we will create our first tree.

/igure 3

%s you can see in figure 3) I labeled the walls and added the red line %. The red line is a node line. It splits the room in half. =e can now define e1erything in that picture in terms of in front of or behind line %. If you notice the line passes through wall8s 0 and >. =e are going to say the front of % is the same as the front of 0 and >. ;ou can do it the opposite way but it8s ?ust easier to remember it this way.

/igure >

This is what our BSP tree looks like now. %s you can see) e1ery polygon in front of node % is on the front list. The front of a node is always noted on the left of the node and back is indicated to the back of the node. The front side of the node is a con1e. "con1e. means you can see all the walls no matter where you are) like figure 0#. so it does not need to be depth sorted any more.

/igure @

6owe1er the back of the node is not a con1e. so we split it again to make it two con1e. parts. =e do this by adding node line B. B is behind % so we add it to the back of %. Then we determine which side of B all the polygons on the back of %.

/igure A

+otice how walls @ B 04 is behind % and behind B so it8s placed in a leaf in the back branch of % and in the back branch of B. 0C) 00) D) B E are in back of % and in front of B so it8s placed in a node behind % and in front of B. Since we now ha1e all con1e. parts we do not need to create a new node. That8s it5 =e now ha1e a leaf based BSP tree. I will discuss how to render it in a second. +e.t I want to discuss a node based BSP tree. Ok so "hat's diffrent "ith node based BSP trees: There are three things that set node based trees apart from leaf based trees. The first thing that is different with node based trees is the fact that the nodes are walls. =e can8t ?ust pick any line to split the le1el like we could with leaf trees. The other thing that is different is that leafs only ha1e 0 polygon. 6ere is a e.ample<

/igure D

%s you can see we ha1e > leafs with > polygons. %ll the other polygons are part of the tree.

/igure E

Instead of using % to split our le1el "the plane of walls 0 B >#. I ha1e split it along wall 04. =hy7 =ith basic node trees we can8t "copolar polygons are a whole other topic#. ;ou may ha1e noticed that part of wall $ is in front of 04 and part is behind. =ell. =e can8t ha1e this.

So what can we do7 Simple we split wall $ into wall $a and wall $b. I will now slice up the rest of the le1el using it8s walls as nodes.

/igure 0C B 00

=e continue to build more nodes with 04 until we reach a leaf of 0 polygon "in this case the leaf is Ea#.

/igure 04 B 0$

=e continue by partitioning the back 00 until we get a leaf of $a.

/igure 03)0> B 0@

=e then pick @) split Eb to form Eb B EC) split $b to form $b B $c) partitioning the back of si.. =e then partition A8s front till we get leaf Eb..

/igure 0A B0D

In figure 0A B 0D I partition the back side of A till we get leaf $b.

/igure 0E

k we partition the front of @ to get the leaf $c. If you did this correctly your BSP tree should look like this..

/igure D

Fine( I understand) !o"( ho" does this hel* +e ,-Sort:

/igure 4

=e are back to our original problem. 6ow can we tell what walls are in front of which. This is important cause if we draw from back to front the polygons will o1erlap correctly. /irst I am going to show you how to render a leaf tree back to front.

/igure @ B A

6ere is our leaf tree and room from before. If the camera was in position % "as seen in /igure 4#. +ow we will '(Sort5 Is the camera in front of node %7 ;es. So we check the B.C/ of the tree. Is the back of % a leaf "meaning is it a list of polygons#7 +o so we check to see if we are on the front of B. =e are on the back so) we go to the front. Is the /ront of B a leaf7 ;es5 So we draw the front of B "0C) 00) A) D) E#. +ow we mo1e back to B and check the back. =hy7

Cause once you hit a leaf you go back to the last node and go down that side. So is the back of B a leaf7 ;es) so we render the back of B "04) @#. Since we ha1e now rendered all of the back of the tree we then test the front. Is the front of % a leaf. ;es so we render the leaf "0) 4) $) 3) >#. =ant to try it yourself7 Pick a spot on the map. Then ask) /igure out what the root node is "the root node is the node at the -:*; top of the tree) in our e.ample %#. Take the root node and follow these instructions. Start< If the node is not a leaf ask) %re you in front of the node7 +o) go down the front of the tree. ;es) go down the back of the tree. 9et that node ID and go back to the start "if your in front of % you go down the back of % to hit B. 9o back to Start and *epeat with node B#. If it is a leaf ask) 6a1e we already rendered these polygons7 +o) *ender the polygons and go back to the last node and go the down the opposite branch of the tree "So if you where on the front of B in our e.ample you would go to the front of B#. ;es) then go back to the last node and then go back to the node before that one "so if we where on a leaf in B in our e.ample we would hop back to %#. :nd< If you did it right. ;ou should ha1e a perfectly '(Sorted world. %ll The +odes should render from far away to close.

/igure D

*endering a +ode Based Tree is the same thing only when you hit a empty branch you go down the opposite way. /or e.ample in figure D) node 0C has no back. If we are called to render 0C back we render 0C front "Ea#. Then come back and render 0C. I similar thing happens when you hit a leaf. ,ets say I hit leaf $a. I render polygons in this order. $a) 4) 0) Ea) 0C) 00. Then I would render the front part of the tree. ,et8s say I hit leaf Eb. I would render in this order) Eb D) $b) 3) >) A) $c) Ec) @) 04. That simple. Ok( ,-Sorting is nice but ho" do I figure out "hat "alls I can see useing BSP0 ,et8s face it. If you ha1e ECCC polygons on a single le1el e1en BSP won8t be enough to '(sort fast enough for real time real(time applications. So we need to figure out how many of those

ECCC polygons we can see from any one point. That way we can ?ust render those. So if anyway you look from point % you can only see 0CC polygons ?ust render those 0CC not the ECCC. k) So how do we figure out what polygons you can see7 This is a multi step process) the first step is to create portals. Well( "hat's a *ortal0 % portal is a con1e. area in between leafs. 6ere is a more comple. le1el. +ote the node splits and attached BSP tree.

+ow I am going to show you the set of portals that go along with this le1el. + T:< %ll portals will be drawn in green.

+otice how they go together7 +o7 Does this help7

=ell) If you still can8t guess. Portals are e1erywhere a node split does not touch a wall. =hich brings us to our first rule about portals. They are always on node splits. The second rule is portals must always be between two leafs. D8s leafs are "0Ab) 0Da) 40b# and "0Db) 0E) 4C#. So back to the topic of this section how do we figure out what we can see and what we can8t. To figure out what we can see we cast rays "draw lines# from e1ery point "e1ery tenth of a point if you want. The smaller the number the less chance of a error but the smaller you make the number) the longer it will take your program to compute.# in portal % to e1ery point in e1ery other portal. In order to say FPortal G and see portal ;F. %t least 0 ray has to make it from portal G to portal ; without being obstructed. So lets do so 1isibility calculations. Portal % B C D : / 9 6 I J What They Can See /) 9) 6 C) D B) D B) C / %) :) 9 %) /) 6 %) 9) I 6 + T6I+9

k) +ow that we know what can see what. There is only one more thing we need to put this data to good use. =e need to create bounding bo.es. =ell) what is a bounding bo.7 %

bounding bo. is a con1e. area "s2uare is better but con1e. is ok# between portals. /or e.ample<

:ach portal has 4 bounding bo.es.

I ha1e highlighted portal C8s bounding bo.es in yellow. +ow let me demonstrate how all this helps us.

Portal What They Can See What are their 1 leafs +ade of0 % B C D : / 9 6 I J /) 9) 6 C) D B) D B) C / %) :) 9 %) /) 6 %) 9) I 6 + T6I+9 0a) 0>) 0@) 40c) 44) 0c 0c) 0@) 0Aa) 40a) 40c) 44 0Aa) 0Ab) 0Da) 40b) 40a 0Ab) 0Da) 0Db) 0E) 4C) 40b 0C) 00) 04) 0$b) 0$a) 03 0a) 0C) 0$a) 03) 0> 0a) 0b) 4a) E) 0> 0b) 4a) 4b) Da) E 4b) $) @b) A) Db) Da 3) >) @a) @b) A) Db) $

%bo1e I ha1e the node diagram) the portal diagram) the BSP tree and the list of Portals with 1isibility set. /or the sake of this e.ercise lets say you where standing in the bounding bo. between portals 6 and I. +ow we will create a list of polygons we ha1e to draw. /irst *ule. If you in it8s bounding bo.) you must draw it8s leafs. Since we are in 6 and I8s bounding bo. we must draw their polygons "0b) 4a) 4b) E) 4b) $) @b) A) Db) Da#. Second *ule. ;ou must draw all the polygons for the portals that the portals can see. 6 can see Portals %) 9 and I. I can see 6. So our list is "%) 9) I) 6#. Since we ha1e already included 6 and I8s polygons on the list we can remo1e them from the list of portals. So we now ha1e % and 9. =e now add % and 98s polygons to our list "0a) 0@) 40c) 44) 0c) 0b) 4a) E) 0>#. That list plus our first list from our first rule makes our FPolygon ,istF as I ha1e dubbed it. ur FPolygon ,istF is "0b) 4a) 4b) E) 4b) $) @b) A) Db) Da 0a) 0@) 40c) 44) 0c) 0>#. +ow we '(Sort ?ust like we did in part one) only difference is that we will not render any polygon that is not on that list. ut of $C polygons we only rendered 0@. That will speed up the whole rendering process.