580 likes | 771 Views
Week 15 - Friday. CS361. Last time. What did we talk about last time? Review up to Exam 2. Questions?. Project 4. Review. Polygonal Techniques. Tessellation. Surfaces often need to be tessellated , broken down into polygons The surfaces can be Convex polygons
E N D
Week 15 - Friday CS361
Last time • What did we talk about last time? • Review up to Exam 2
Tessellation • Surfaces often need to be tessellated, broken down into polygons • The surfaces can be • Convex polygons • More complicated polygons • 3D surfaces made out of complicated polygons
Triangulation • For most graphics hardware, polygons must be triangulated into component triangles • "Bowties" have to be converted into triangles, perhaps making a guess about what the modeler meant • The literature contains many triangulation algorithms including the O(n2) ear clipping algorithm
Edge cracking and T-vertices • Splines are often used to generate curved surfaces • Turning the splines into polygons can cause two surfaces to have cracks between them, called edge cracking • Edge stitching makes sure all vertices on a shared edge between surfaces are shared as well • Shading is also an issue for T-vertices • Triangles that share edges should share all the same vertices too
Triangle fans, strips, and meshes • In order to reuse data, we can break groups of triangles into fans, strips, and meshes • A triangle fan has a center vertex shared by all triangles • A triangle strip is similar to a triangle fan except that the triangles do not all share a common vertex • For a closed surface, a triangle mesh can be more efficient than either fans or strips
Vertex buffers • Meshes are generally sent as a vertex buffer and an index buffer • They are a generic way to store model data in a contiguous chunk of memory • The memory could be: • A list of points • A list of line segments • A polyline • A triangle list • A triangle fan • A triangle strip • The size of a vertex is called the stride • If you do not want to repeat information in the vertex buffer, you can use an index buffer to specify which vertices from the vertex buffer to use
Simplification • Mesh simplification is a way of reducing polygon count while preserving appearance • Three common kinds: • Static – make several models of different complexity and choose the right one • Dynamic – generate models on the fly, allowing for a continuous spectrum of level of detail (LOD) • View-dependent – change based on the particular view
Dynamic simplification • Use edge collapses • Merge two vertices into one • There are different strategies for determining which edge to collapse, but we usually look for a "low cost" edge • There are different strategies for determining cost… • Some edges should not be collapsed • If it causes a surface's normal to flip • If it causes edges to cross
Ray • A ray r(t) is defined by an origin point o and a direction vector d • d is usually normalized • Negative t values are behind the starting point of the ray and don't count • Since d is normalized, positive t values give the distance of the point from o • It is also common to store l, the maximum distance along the ray we want to look
Surface • An implicit surface is one described by a vector equation where any point on the surface has a value of 0 • f(p) = f(px, py, pz) = 0 • Implicit sphere: f(p) = px2 + py2 + pz2 - r2 = 0 • An explicit surface is one parameterized by two parameters • Explicit sphere:
Axis-aligned bounding box • An axis-aligned bounding box (AABB) (also known as a rectangular box) is a box whose faces have normals pointing the same way as the x, y, and z axes • It's just a non-rotated box in 3 space • It can be defined with two points (lower corner and upper corner)
Oriented bounding box • An oriented bounding box (OBB) is an AABB that has been arbitrarily rotated • It can be described by • A center point bc • Three normalized vectors bu, bv, and bw giving the side directions of the box • And half-lengths (from center to wall) huB, hvB, and hwB
k-DOP • A k discrete oriented polytope (k-DOP) has k/2 normalized normalsni • For each ni, there are two values dimin and dimax which defines a slab Si that is the volume between the two planes • The k-DOP is the intersections of all the slabs
Separating axis test • For two arbitary, convex, disjoint polyhedra A and B, there exists a separating axis where the projections of the polyhedra are also disjoint • Furthermore, there is an axis that is orthogonal to (making the separating plane parallel to) • A face of A or • A face of B or • An edge from each polyhedron (take the cross product) • This definition of polyhedra is general enough to include triangles and line segments
AABB and k-DOPs • An AABB is the easiest • Take the minimum and maximum points in each axis and, BOOM, you've got an AABB • A k-DOP is not much harder • Take the minimum and maximum values in each of the k/2 axes and use those to define the slabs • You have to have axes in mind ahead of time
Bounding spheres • Not as simple as you might think • One approach: • Make an AABB and use the center and diagonal of the corners to make your sphere • Or: • Make the AABB and do another pass through the vertices, taking the one furthest from the center as the radius • There are other more complicated ideas
Geometric probability • The relative probability that a random point is inside an object is proportional to the object's volume • However, the relative probability that a random ray intersects and object is proportional to its surface area
Ray/sphere intersection • We can write the implicit sphere equation as f(p) = ||p – c|| – r = 0 • p is any point on the surface • c is the center • r is the radius • By substituting in r(t) for p, we can eventually get the equation t2 + 2tb + c = 0, where b = d • (o – c) and c = (o – c) •(o – c) – r2 • If the discriminant is negative, the ray does not hit the sphere, otherwise, we can compute the location(s) where it does
Optimized ray/sphere • Looking at it geometrically, we can optimize the test • Find the vector from the ray origin to the center of the sphere l = c – 0 • Find the squared length l2 = l • l • If l2 < r2, then o is in the sphere, intersect! • If not, project l onto d: s = l • d • If s < 0, then the ray points away from the sphere, reject • Otherwise, use the Pythagorean theorem to find the squared distance from the sphere center to the projection: m2 = l2 – s2 • If m2 > r2, the ray will miss, otherwise it hits
Slabs method for ray/box • First find the t value where the ray intersects each plane • The box is made up of 3 slabs • Find the min t and max t for each slab • The final tmin is the max of all the tmin values • The final tmax is the min of all the tmax values • If tmin ≤ tmax, the ray intersects the box, otherwise it does not • The idea can be extended to frustums and k-DOPs
Line segment/box overlap test • This method uses the separating axis test and only works for AABBs and line segments • The AABB has its center at (0,0,0) and size half vector h • The line segment is defined by center c and half vector w • If |ci| > wi + hi for any ix,y,zthen disjoint • There is another test for each axis that is the cross product of the x, y, and z axis and w • If any test passes, then disjoint • Only if all tests fail, then overlap
Triangle representation • One way to represent a triangle is with barycentric coordinates • For triangles, barycentric coordinates are weights that describe where in the triangle you are, relative to the three vertices • These weights are commonly labeled u, v, and w and have the following properties • u ≥ 0, v ≥ 0, w ≥ 0 and u + v + w ≤ 1
Ray triangle intersection • We represent a point f(u,v) on a triangle with the following explicit formula • f(u,v) = (1 – u – v)p0 + up1 + vp2 • Then, setting the ray equal to this equation gives • o + td = (1 – u – v)p0 + up1 + vp2 • This is simply a vector representation of three equations with three unknowns • If the solution has a positive t, and u and v between 0 and 1, it's an intersection
Plane/box intersection • Simplest idea: Plug all the vertices of the box into the plane equation n • x + d = 0 • If you get positive and negative values, then the box is above and below the plane, intersection! • There are more efficient ways that can be done by projecting the box onto the plane
Sphere/sphere intersection • Sphere/sphere is the easiest • Is the distance between their centers bigger than the sum of their radii? • If yes, they are disjoint • If no, they overlap r2 r1 c2 c1
AABB/AABB intersection • We test each dimension to see if the min of one box is greater than the max of other or vice versa • If that's ever true, they're disjoint • If it's never true, they overlap intersect(A, B ) { for i in x,y,z • if(aimin > bimax or bimin > aimax ) return DISJOINT return OVERLAP }
Line/line intersection • We will only look at the 2D problem, but the book has discussion of 3D lines as well • For a 2D vector (x, y), we define its perp dot product (x, y) = (-y, x) • Thus, we can work through the equations of a line (only the path to the value of s is shown) • r1(s) = r2(t) • o1 + sd1 = o2 + td2 • sd1 • d2 = (o2 – o1) • d2 • s = ((o2 – o1) • d2) / d1 • d2
Collision detection • There are three important pieces to collision handling: • Collision detection • Did these objects collide? • Collision determination • When and where did these objects collide exactly? • Collision response • What happens as a result of the collision?
Collision detection with rays • Rather than try to test complex models for collision with an environment, we can use representative rays instead • Perhaps one ray for each wheel of a car • A positive ray distance means space between the objects • A negative ray distance means collision • A zero ray distance means the objects are merely touching • It's essentially a ray tracing problem
BSP trees • Binary space partitioning trees (BSP trees) are a common way of dividing space hierarchically • For axis-aligned BSPs, one axis is chosen and a perpendicular plane is generated to divide the box • This process is repeatedly recursively until some criteria (like 3 or fewer objects per division) is reached • BSPs can also be split by choosing polygons to divide the world (usually done so to make a perfectly balanced tree) • BSPs are good for static scenes (moving objects can cause huge portions of the tree to be recreated)
Multiple objects CD • Hierarchies are generally made for static scenes • Then we test against them for collisions with dynamic objects • What about when there are multiple moving objects that might interact with each other? • We work in two phases • Broad phase collision detection • Exact collision detection among candidates
Sweep-and-prune • Assume everything has an AABB or a bounding sphere • Assume temporal coherence (stuff doesn't move that much over a small amount of time) • On one dimension, we can sort the endpoints of the AABBs • We can quickly throw out objects that cannot possibly intersect • Bubble sort to the rescue! • We could sort everything in O(n log n) every time • Because of temporal coherence, not many end points change order and adaptive sorts work in around O(n)
Grids • Another possibility is keeping large grid cells that keep track of which objects or BVs are inside them • Objects that do not share grid cells do not need to be checked for collision • Finding the right grid cell size can be difficult • Spatial hashing can be used as well (mapping to a hash table based on location)
Putting it together • Here is an outline of a frame in a typical two-phase CD system
Non-photorealistic rendering • Most of the work we've focused on all semester is doing rendering that in some way mirrors the natural world • However, a wide area of rendering is non-photorealistic rendering (NPR) • Goals: • Simplified technical drawings • Simulating artistic styles
Toon shading • The most common form of NPR in video games is toon shading • Also called cel shading • The goal is to render 3D models as if they were cartoons • Shading is often done with either a single color or a two tone (color and shading) approach • Then a thick black silhouette is added around edges
Coloring • The color is often determined by the dot product n ·l (surface normal dot light vector) • If negative, the surface should be darkened • Otherwise, it's some flat color • Or a threshold other than 0 can be used • A more complex system uses a one dimensional texture indexed into with the dot product Highlight Normal Shadow
Silhouette edge rendering • The more complicated problem is properly rendering edges with a thick dark line • A number of different edges are of interest • Boundary or border edges are edges where one polygon is not adjacent to any other • Not found in 3D solid objects • A crease, hard, or feature edge is an edge between two polygons that is sharper than some threshold angle • A material edge is an edge between two polygons with different materials • A silhouette edge is when two neighboring polygons face different directions, relative to the eye
Procedural geometry silhouetting • Cel shading focuses on rendering silhouette edges • Many of these techniques rely on manipulating back facing polygons • The crossover between front facing and back facing polygons is the silhouette • It's easy to determine which is which • After some manipulation of the backfaces, they are rendered in black
z-biased backfaces • If the backfaces are rendered in black without any change, they will be hidden • Before rendering, all backfaces can be translated to be closer to the viewer • Translation can be by • A fixed amount • An amount that takes into account non-linear z-depths • An amount based on the angle of the polygon normal • None of these techniques give uniform thickness lines
Triangle fattening • Another approach is to fatten each backface triangle • The slope of the triangle and the distance from the viewer determine the expansion of each edge • It doesn't work well for thin traingles