




                           ͻ
                                                   
                                Merc Software     
                                                   
                                  Le Poulindu      
                             F   56610   Arradon   
                                                   
                               CIS [100330,3606]   
                                                   
                           ͼ









                                  B S P  Algorithm

                     A real time capable hidden line algorithm.



        The following text is an abstract of an article that was published in
        the c't magazine 2/94 ( Heise Verlag D Hannover ).


        Real-time 3D graphics applications usually need a lot of computing power
        or/and a lot of memory. Most animations solve the problem saving a
        sequence of precalculated pictures in a compressed format to the disc,
        retreiving them later as an animated movie. Others stress your budget
        by requiring high end workstation outfit with array prozessors and
        Z-buffers.

        The state of performance offered by modern PC's and intelligent graphic
        boards sheds new light on an old algorithm invented by Henry Fuchs et
        al. / Chapel Hill N.C.


        If the application is interactive and needs to display true motion in
        space ( for ex. in flight simulators, virtual reality applications or
        true 3D games) the movie solution is no more possible. Another way
        consists of transfering as much of the computation as possible out of
        the online part. The BSP ( binary space partitioning ) algorithm which
        is delineated here allows to solve a major part of the hidden
        line/surface problem off-line without an expensive Z-buffer.

        The BSP hidden line algorithm applies only to polygonal data bases. 
        The polygons must be oriented and we will use the convention
        that a polygon is visible if its vertices turn clockwise. The BSP-tree
        is a binary tree which may include occulting information in a 3D scene
        to a polygon list of an object. Every polygon in a BSP is represented
        by a node which contains two pointers to front and back subtrees.

        The basic idea is the following: a plane divides space in two parts, if
        the plane is oriented we get a front "half space" and a back one. For
        example, the faces of a convex object are all chained over the back
        pointers according to the definition of convexity ( an object is convex
        if all his faces are behind the plane defined by any other face).

        We can precalculate this BSP tree off-line for objects independent of
        any viewers location and orientation. Only few calculations must then
        be done in realtime to solve the right occulting relative to a viewer
        moving in a 3D scenario.


        Now for the rules:

        ***********
        RULE 1
        Faces of convex objects are all chained by back pointers.
        ***********

        Let us start an example with the following house:

        face number
          1         garden                                             |
          2         back north side ( not visible )                   | |
          3         east side                           ___---        | |
          4         west side ( not vis.)         ___---       \     |   |
          5         front south side            /\               \   |   |
          6         roof west ( not vis.)     /   \                \|  13 |
          7         roof east               /       \       7        \    |
          8,9,10    south tree            /           \                \   |
          11,12,13  north tree          /               \         ___---   |
          14        door              /                   \ ___---      |   |
          15,16     windows          |          5          |            |___|
                                     |   ___         ___   |   |        |
                                     |  |___|       |___|  |   ||  3    |
                                     |   15           16   |  | ||      |
                                     |         ___         |  | ||      |
                                     |        |   |        | |  | |     |
                                     |        |14 |        | |  | |__---|
                                     |________|___|________||   |9 |
                                                            | 8  | |
                                                            |     |  |
                                                            |__   | _|
                                                               -----

        To see more about the house start the program "BSP house.dat".
        The walls and the roof form a convex object.

        Here is its BSP-tree:

         <-- back                     front -->
                     (5) south side               /  means back pointer
                          /                       \  means front pointer
                   (4) west side
                       /
                (3) east side
                    /
             (2) north side
                 /
           (6) roof west
              /
        (7) roof east

        The house is on the front side of the garden

                    (1) garden
                          \
                     (5) front side
                          /
                   (4) west side
                       /
                (3) east side
                    /
             (2) north side
                 /
           (6) roof west
              /
       (7) roof east

        **********
        RULE 2
        Coplanar faces may be chained any way if they do'nt overlap. If they 
        do, the overlaping face must be chained to the overlapping one with a
        front pointer.
        **********

        Windows and door are in the same plane as the front side, but we must 
        draw them after the wall. So we chain them to the south side with a
        back pointer. The order they are chained together is not important
        since door and windows do not overlap.
        We may chain them in several ways:

                 door          or       window1       or        door
                /   \                      /                      \
            win.1   win.2                door                    win.1
                                           \                        \
                                          win.2                    win.2
        or any other combination.


        Our BSP-tree is now :

                            (1) garden
                                  \
                             (5) front side
                                /       \
                        (4) west side  (14) door
                             /            \
                      (3) east side     (15) win.1
                          /               /
                   (2) north side    (16) win.2
                       /
                (6) roof west
                    /
             (7) roof east


        Connecting the two ( green ) trees should now be easy. They are convex
        and will form two back chains. The tree in front of the south side of
        the house may be chained to the front pointer of (16) window 2. The
        only possibility for the tree behind the house is offered by the front
        pointer of the (2) north side.




        This is now our complete BSP tree :

                            (1) garden
                                  \
                             (5) front side
                                /       \
                        (4) west side  (14) door
                             /            \
                      (3) east side     (15) win.1
                          /                 /
                   (2) north side      (16) win.2
                     /       \               \
            (6) roof west   (8) fir         (17) fir
                  /           /                /
          (7) roof east    (9) fir        (18) fir
                            /               /
                         (10) fir       (19) fir


        It is not always possible to get a tree without additional preparation.
        Start now the program with "BSP boards.dat". The three boards you see
        constitute a closed chain of overlaping faces. The only way to make a
        BSP-tree is to split two of these boards. Running the program in 
        steping mode will show how it works.

        The BSP-tree of the boards is:

                                         /\   ___
                                       /    \|   |
                 (1)                    \  5 |   |                /\
                /   \                     \  |   |              /    \
              (5)   (4)                     \|   |\           /     /
             /         \                     |   |  \       /     /
           (2)         (3)                   |   |   /\   /  2  /
                                             |   |\/    \     /
                                             | 1 |  \     \ /
                                             |   |  / \     \
                                             |   |/\    \  3  \
                                             |  /    \/   \     \
                                             |/     /       \     \
                                            /  4  /           \    /
                                          /     /|              \/
                                        /     /  |
                                       \    /|   |
                                         \/  |___|


        *************
        RULE 3
        If the plane defined by a face F1 cuts another face F2, the only way to
        insert F2 below F1 in the tree is to split F2. The splitting line is the
        intersection of F2 with the plane defined by F1.
        *************

        Also concavities may be a source of problems. Because BSP-trees should
        minimize calculations in real-time 3D applications, avoiding splitting
        will be a priority for BSP-trees designers.

        Start "BSP tree1.dat" and switch to stepping mode ( with 's'). The tree
        was designed with 12 faces : 3 for the trunc and 6 for the top.
        Because of the concavity at the jonction trunc-crown, some splitting 
        occured resulting in 12 faces. An easy way to avoid this expansion 
        consist of inserting an additional face at the place of the concavity,
        at the top of the trunc. This face, which will never be visible because
        it is inside of the tree, is called a separation plane. It cuts the
        tree in two convex objects, trunc and top. If we use this face as the
        first node in the BSP, no splitting will be necessary. File 'tree2.dat'
        demonstrates such a tree with a separation plane. The inserted face was
        colored red to make it more visible. This BSP-tree contains only 10
        faces.

        *************
        RULE 4
        Inserting well oriented separation planes at concave locationss can
        avoid wild splitting and a possible explosion of the data base.
        *************




        Performing BSP-tree traversal is quite easy and short, here the
        recoursive algorithm in pseudo code :

            bsprun ( root )
               if ( face visible )
                  bsprun( back )
                  drawpoly( root)
                  bsprun( front)
               else
                  bsprun( front )
                  bsprun( back )
               endif
	
        (face visible) is a function testing the orientation of a faces
        normal with respect to the viewer. It results in TRUE if this vector
        points to the viewer.


        An interesting feature of this algorithm is that it produces output
        while it runs. So if we have a graphics board with a capability to
        draw polygons with a hardware accelerator, a smooth pipeline will
        result improving realtime performance dramatically.

        Files 'cube.dat', 'farm.dat' and 'star.dat' demonstrate how powerfull
        the BSP-tree algorithm is and that it is able to solve the hidden line
        problem even for highly complex and concave objects.

        The torus file was extracted out PRIMS.ZIP Lib2. Additional faces were
        created between the cylinders which build the torus. Without this 20
        separation planes the object would have explosed from 400 to more than
        700 faces!

