BSP Trees. Chapter Introduction. 8.2 Overview

Similar documents
Outline. Scanline conversion Incremental updates Edge table and active edge table. Gouraud shading Phong interpolation

Hidden Surface Elimination: BSP trees

9. Visible-Surface Detection Methods

Computer Graphics. Bing-Yu Chen National Taiwan University The University of Tokyo

CEng 477 Introduction to Computer Graphics Fall 2007

Computer Graphics. Bing-Yu Chen National Taiwan University

Geometric Primitives. Chapter 5

Ray Tracing Basics I. Computer Graphics as Virtual Photography. camera (captures light) real scene. photo. Photographic print. Photography: processing

Computing Visibility. Backface Culling for General Visibility. One More Trick with Planes. BSP Trees Ray Casting Depth Buffering Quiz

Rational Numbers: Graphing: The Coordinate Plane

Computer Graphics. Lecture 9 Hidden Surface Removal. Taku Komura

Midterm Exam Fundamentals of Computer Graphics (COMP 557) Thurs. Feb. 19, 2015 Professor Michael Langer

Basics of Computational Geometry

Binary Space Partition Trees. Overview. Binary Space Partitioning Trees (Fuchs, Kedem and Naylor `80)

Hidden surface removal. Computer Graphics

COMP30019 Graphics and Interaction Scan Converting Polygons and Lines

Let s write this out as an explicit equation. Suppose that the point P 0 = (x 0, y 0, z 0 ), P = (x, y, z) and n = (A, B, C).

Renderer Implementation: Basics and Clipping. Overview. Preliminaries. David Carr Virtual Environments, Fundamentals Spring 2005

Spatial Data Structures

CSE528 Computer Graphics: Theory, Algorithms, and Applications

CSE328 Fundamentals of Computer Graphics: Concepts, Theory, Algorithms, and Applications

1 EquationsofLinesandPlanesin 3-D

Overview. Pipeline implementation I. Overview. Required Tasks. Preliminaries Clipping. Hidden Surface removal

1.5 Equations of Lines and Planes in 3-D

FROM VERTICES TO FRAGMENTS. Lecture 5 Comp3080 Computer Graphics HKBU

Clipping. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

Spatial Data Structures

Computer Graphics: 8-Hidden Surface Removal

8. Hidden Surface Elimination

Spatial Data Structures

Hidden-Surface Removal.

Hidden Surface Removal

Spatial Data Structures

Spatial Data Structures

2 Geometry Solutions

Surfaces and Partial Derivatives

Spatial Data Structures and Speed-Up Techniques. Tomas Akenine-Möller Department of Computer Engineering Chalmers University of Technology

Lecture 25 of 41. Spatial Sorting: Binary Space Partitioning Quadtrees & Octrees

More Hidden Surface Removal

8. Hidden Surface Elimination

Announcements. Written Assignment2 is out, due March 8 Graded Programming Assignment2 next Tuesday

Course Guide (/8/teachers/teacher_course_guide.html) Print (/8/teachers/print_materials.html) LMS (/8

Geometric and Algebraic Connections

Exploring Fractals through Geometry and Algebra. Kelly Deckelman Ben Eggleston Laura Mckenzie Patricia Parker-Davis Deanna Voss

3D convex hulls. Computational Geometry [csci 3250] Laura Toma Bowdoin College

Clipping & Culling. Lecture 11 Spring Trivial Rejection Outcode Clipping Plane-at-a-time Clipping Backface Culling

Lines and Planes in 3D

Lecture 3: Convex sets

Computer Graphics II

The statement implies that any three intersection points of two distinct planes lie on a line.

Lecture 11: Ray tracing (cont.)

As a consequence of the operation, there are new incidences between edges and triangles that did not exist in K; see Figure II.9.

Mathematics (

1.5 Equations of Lines and Planes in 3-D

The Three Dimensional Coordinate System

The Distributive Property and Expressions Understand how to use the Distributive Property to Clear Parenthesis

Calculus I Review Handout 1.3 Introduction to Calculus - Limits. by Kevin M. Chevalier

CS559: Computer Graphics. Lecture 12: Antialiasing & Visibility Li Zhang Spring 2008

Math 3C Section 9.1 & 9.2

Lesson 1. Unit 2 Practice Problems. Problem 2. Problem 1. Solution 1, 4, 5. Solution. Problem 3

Three Dimensional Geometry. Linear Programming

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s.

About Finish Line Mathematics 5

Surfaces and Partial Derivatives

.(3, 2) Co-ordinate Geometry Co-ordinates. Every point has two co-ordinates. Plot the following points on the plane. A (4, 1) D (2, 5) G (6, 3)

Today. CS-184: Computer Graphics. Lecture #10: Clipping and Hidden Surfaces. Clipping. Hidden Surface Removal

CS 4204 Computer Graphics

Identifying those parts of a scene that are visible from a chosen viewing position, and only process (scan convert) those parts

Werner Purgathofer

(Refer Slide Time: 00:02:02)

Culling. Computer Graphics CSE 167 Lecture 12

The angle measure at for example the vertex A is denoted by m A, or m BAC.

Computer Graphics Prof. Sukhendu Das Dept. of Computer Science and Engineering Indian Institute of Technology, Madras Lecture - 14

Advanced 3D-Data Structures

2. Draw a non-isosceles triangle. Now make a template of this triangle out of cardstock or cardboard.

Chapter 5. Transforming Shapes

Homework #2 and #3 Due Friday, October 12 th and Friday, October 19 th

6.837 Introduction to Computer Graphics Final Exam Tuesday, December 20, :05-12pm Two hand-written sheet of notes (4 pages) allowed 1 SSD [ /17]

Lectures on Challenging Mathematics. Integrated Mathematics 3. Idea Math. Geometry (Part 2) Summer Internal Use

3D convex hulls. Computational Geometry [csci 3250] Laura Toma Bowdoin College

2-D Geometry for Programming Contests 1

Math 5320, 3/28/18 Worksheet 26: Ruler and compass constructions. 1. Use your ruler and compass to construct a line perpendicular to the line below:

Visible Surface Detection Methods

Math (Spring 2009): Lecture 5 Planes. Parametric equations of curves and lines

12.4 Rotations. Learning Objectives. Review Queue. Defining Rotations Rotations

Page 1. Area-Subdivision Algorithms z-buffer Algorithm List Priority Algorithms BSP (Binary Space Partitioning Tree) Scan-line Algorithms

7. The Gauss-Bonnet theorem

Math 6, Unit 8 Notes: Geometric Relationships

Section A1: Gradients of straight lines

11 1. Introductory part In order to follow the contents of this book with full understanding, certain prerequisites from high school mathematics will

CMSC427: Computer Graphics Lecture Notes Last update: November 21, 2014

Geometric Queries for Ray Tracing

Lesson 5: Definition of Rotation and Basic Properties

Line Arrangement. Chapter 6

Sec 4.1 Coordinates and Scatter Plots. Coordinate Plane: Formed by two real number lines that intersect at a right angle.

Section 4.1: Introduction to Trigonometry

Chapter 2: Transformations. Chapter 2 Transformations Page 1

Lecture 5. If, as shown in figure, we form a right triangle With P1 and P2 as vertices, then length of the horizontal

Midpoint and Distance Formulas

Notes on Spherical Geometry

Transcription:

Chapter 8 BSP Trees 8.1 Introduction In this document, we assume that the objects we are dealing with are represented by polygons. In fact, the algorithms we develop actually assume the polygons are triangles, though they can be generalized to other polygons. In previous lectures, we learned how to get a triangle from the 3D world in which our objects live onto the 2D screen on which the objects will be viewed using projection and other techniques. When one looks at a 3D object from a certain view point, it is never possible to see all the sides of this object. This means that when this object is projected onto a 2D surface, some of the triangles which make up the object should not be seen because they are hidden from the viewer. In other words, they should be eliminated. This is known as hidden surface elimination. There are many techniques to achieve this. In this document, we develop a technique to achieve this which uses BSP trees. BSP stands for Binary Space Partitioning. The reason for this name will become obvious as we describe the process. The technique we are about to describe works well when we are making many images from different view points of the same geometry. 8.2 Overview The BSP tree algorithm is an example of a painter s algorithm. Such an algorithm draws objects from back to front. Polygons which are behind other polygons and should not be seen will be drawn over and will not be seen. Thus, this will perform hidden surface elimination automatically. This is illustrated on figure 8.1. The algorithm is very simple. It can be implemented as follows: Algorithm 93 (Painter s Algorithm) Follow these steps: 1. Sort objects from back to front. 2. Foreachobjectdo 107

108 CHAPTER 8. BSP TREES Figure 8.1: An illustration of the painter s algorithm Figure 8.2: A cycle Draw object on the screen In reality, it is not as simple. There is one major difficulty, the relative order of multiple objects is not always well defined. This makes the first step of our algorithm not easy to implement. Figure 8.2 shows an example of what is called a cycle. The relative order of the three triangles is not clear. To avoid a situation like the one of figure 8.2, we must make sure that no triangle crosses the plane defined by any other triangle. This is a very restrictive assumption. First, we will develop an algorithm with this assumption. Then, we will see how to relax this condition and still have a painter s algorithm. Before we do this, let us review some mathematical concepts which will be very useful here.

8.3. QUICK MATH REVIEW 109 8.3 Quick Math Review Let a =(x a,y a,z a ) be a point and n =(A, B, C) beavector. Youwillrecall that the equation of the plane containing a with normal n is given by (p a) n =0 (8.1) In this equation, p =(x, y, z) represents a point and equation 8.1 is simply the condition the point must satisfy to be on the plane. If we plug in the coordinates of the points and vector involved, equation 8.1 becomes (x x a ) A +(y y a ) B +(z z a ) C =0 which can be written as where Ax + By + Cz + D =0 (8.2) D = Ax a By a Cz a = n a Thiscanalsobewrittenas n p + D =0 (8.3) Given a point p, we can define a function f by Thus, a point p is on the plane if f (p) =(p a) n (8.4) f (p) =0 Whataboutifthepointisnotontheplane? Eitherwewillhavef (p) > 0 or f (p) < 0. Remembering that (p a) n = (p a) n cos θ where θ is the smallest angle between (p a) and n taken between 0 and π. We see that if p is on the side of the plane where n is pointing, the angle between (p a) and n will be between 0 and π 2 and will therefore be positive. Hence, we will have f (p) > 0. Similarly, if p is on the side of the plane opposite the direction where n is pointing, we will have f (p) < 0. Thus, the function f which defines the plane gives us very important information. It tell us the following: Proposition 94 Let f be the function defining the plane containing a with normal n (as in equation 8.2, and let p be a point. Then, the following is true: 1. If f (p) =0,thenp is on the plane.

110 CHAPTER 8. BSP TREES 2. If f (p) > 0, thenp is on the side of the plane where n is pointing. 3. If f (p) < 0, thenp is on the side of the plane opposite the direction where n is pointing. Given a triangle T with vertices a =(x a,y a,z a ), b =(x b,y b,z b ) and c = (x c,y c,z c ), we can write the equation of the plane containing this triangle. We can select any of the vertices as the point the plane will contain (since it will contain all the vertices). Let us select a. The normal is given by (b a) (c a). Thus, the function f defining the plane which contains T is f (p) =(p a) ((b a) (c a)) (8.5) This is called the scalar triple product. Geometrically, f (p) represents the volume of the parallelepiped determined by the vectors p a, b a and c a. It can be proven (see a multivariable calculus book) that x x a y y a z z a f (p) = x b x a y b y a z b z a (8.6) x c x a y c y a z c z a Expanding this determinant gives f (p) = (z c (y b y a ) y c (z b z a )) x (8.7) +(x c (z b z a ) z c (x b x a )) y +(y c (x b x a ) x c (y b y a )) z +(x c (y a z b y b z a )+y c (x a z b x b z a )+z c (x b y a x a y b )) Apointp willbeonthetrianglewithverticesa, b, c if and only if f (p) =0 where f is as in equation 8.7. It is important that we be consistent when we form the cross product to find the normal of a triangle so that all the normals are oriented the same way. 8.4 The Simple Case of Two Triangles We begin by illustrating our algorithm with two triangles. Let T 1 and T 2 be two triangles. Let the plane containing T 1 be defined by the function f 1. Though this is not correct, we will use the notation f (T ) to mean f (p) for every vertex of the triangle T. Let e denote the view point. Our goal is to sort these two triangles from the furthest to the closest of e. We know that f 1 (T 1 )=0. If f 1 (e) and f 1 (T 2 ) havethesamesign,thene and T 2 are on the same side of T 1. Thus T 1 is the furthest. If f 1 (e) and f 1 (T 2 ) have opposite signs, then e and T 2 are on opposite sides of T 1. Thus T 2 is the furthest. Thus, we have the following algorithm: Algorithm 95 To sort triangles T 1 and T 2 with respect to the viewpoint e, follow the steps below:

8.5. A FIRST ALGORITHM 111 If (f 1 (e) and f 1 (T 2 ) have the same sign) then Draw T 1 Draw T 2 Draw T 2 Draw T 1 8.5 A First Algorithm We now generalize the algorithm described above to the case of many objects. We still have our restriction that no triangle crosses the plane defined by any other triangle. To derive our algorithm, we will create a binary tree data structure. This means that at each node, there can only be at most two edges (or branches). In our case, the two branches will be the positive branch and the negative branch. The positive branch will contain those triangles for which f (p) > 0 where f is the function defining the plane containing the triangle at the root of the tree.. We will present two procedures. One which creates the tree, the other one which traverses the tree to draw the triangles in the correct order. Suppose that we have triangles labeled T 1,T 2,..., T n. The procedure to create the tree will take the triangles one at a time and figure out where to put it in the tree. Since T 1 is the first triangle in the list, we will make it the root of the tree. As above, let e denote the viewpoint. Let f i be the function which defines the planes containing T i. We will use the subscripts when we mean to talk about a specific triangle. Otherwise, we will use T to denote a triangle, and f to denote the function defining the plane containing it. The algorithms we are about to write are recursive. It means that they can call themselves. So, to implement them the chosen programming language has to support recursion, which is the case for the most commonly used programming languages such as C, C++, java, pascal,... Let us assume further that our tree is implemented by a data structure called bsptree. The function (method) to draw the tree is shown below. It assumes that at each node, the branch on one side should be drawn first, then the node should be drawn, then the branch on the other side should be drawn last. Which side is drawn first depends on where the view point is with respect to the current node. function draw(bsptree tree, point e) If (tree.empty) then return if (f tree.root (e) < 0) then

112 CHAPTER 8. BSP TREES draw(tree.plus,e) plot tree.triangle draw(tree.minus,e) draw(tree.minus,e) plot tree.triangle draw(tree.plus,e) The code which builds the tree which can be traversed by the code above is: tree-root = node(t 1 ) for (i =2to n) do tree-root.add(t i ) The add function is defined as follows: function add(triangle T ) if (f (a) < 0 and f (b) < 0 and f (c) < 0) then if(negative-subtree is empty) then negative-subtree = node(t ) else negative-subtree.add(t ) if (f (a) > 0 and f (b) > 0 and f (c) > 0) then if(positive-subtree is empty) then positive-subtree = node(t ) else positive-subtree.add(t ) for now, we assumed this was not possible Practice building a tree and then drawing the rectangles from it assuming we have the following rectangles: T 1,T 2,..., T 10 arranged in the following order: T 5 T 8 T 4 T 3 T 1 T 2 T 6 T 10 T 7 T 9 e

8.6. TRIANGLE CUTTING 113 Figure 8.3: Triangle crossing the plane of another triangle 8.6 Triangle Cutting The only case left is when a triangle crosses the dividing plane as shown in figure 8.3When it happens, two vertices of the triangle are on one side, the third is on the other. We are showing the case when c is on the other side. There are two other cases. We will write an algorithm for this case. If it is not c but one of the other vertices, we will do some swapping first. Remember, we always keep the vertices in the same order (counterclockwise) so that the normal point in the same direction. When we are in this case, we create new triangles. We create T 1 =(a, b, A), T 2 =(b, B, A) and T 3 =(A, B, c). Wealsoneedtoworryabout another issue. If the vertex which is by itself happens to be very close to the plane, then T 2 would be almost flat, and T 3 wouldhaveanareaalmostequalto 0. Furthermore, because of errors introduced in every computations, 0 is never 0 in computer science. It is a very bad habit to check if something is equal to 0. We should never have a statement like if x == 0. Instead, we should check if abs (x) <ε, where ε can be considered to be 0. With this in mind, we can rewrite the above algorithm as follows: function add(triangle T ) fa = f (a) fb = f (b) fc = f (c) if (abs (fa) <ε) then fa =0 if (abs (fb) <ε) then

114 CHAPTER 8. BSP TREES fb =0 if (abs (fc) <ε) then fc =0 if (fa 0 and fb 0 and fc 0) then if(negative-subtree is empty) then negative-subtree = node(t ) else negative-subtree.add(t ) if (fa 0 and fb 0 and fc 0) then if(positive-subtree is empty) then positive-subtree = node(t ) else positive-subtree.add(t ) cut triangle into three triangles and insert the new triangles into the tree. The algorithm to cut the triangles is: If (fa fc 0) then swap(fa,fc) swap(a, c) swap(fb,fc) swap(b, c) if (fb fc 0) then swap(fb,fc) swap(b, c) swap(fa,fc) swap(a, c) Compute A Compute B

8.7. ASSIGNMENT 115 if (fc 0) then negative-subtree.add(t 1 ) negative-subtree.add(t 2 ) positive-subtree.add(t 3 ) positive-subtree.add(t 1 ) positive-subtree.add(t 2 ) negative-subtree.add(t 3 ) We can find A as follows. A is at the intersection of the dividing plane and the line through a and c. The line through a and c has equation p (t) =a + t (c a) We recall from equation 8.3 that the dividing plane is given by n p + D =0. Combining the two gives n a + t n c t n a + D = 0 t = n a + D n (c a) Therefore, A = a n a + D (c a) n (c a) We obtain a similar expression for B. 8.7 Assignment 1. Prove equation 8.7 2. Derive the expression for B at the end of the last section (just above). 3. Given N triangles, what is the minimum number of triangles that could be added to a resulting BSP tree? What is the maximum? 4. Think about and be ready to discuss in class the implementation of a simple 3D graphics pipeline which would only involve projection. It would take as input a set of triangles representing the various objects in the scene. The output would be pixel coordinates. Be very specific as to how you would implement this.