320 likes | 449 Views
3D Graphics - Further Topics (Brief Overviews). Tips for speeding up OpenGL Z-Buffering Blobby Modelling Collision Detection Ray Tracing. A few tips in OpenGL. Polygons are slow . The more polygons in your scene, the more processing your scene demands. Less polygons = faster rendering.
E N D
3D Graphics - Further Topics(Brief Overviews) Tips for speeding up OpenGL Z-Buffering Blobby Modelling Collision Detection Ray Tracing
A few tips in OpenGL • Polygons are slow. • The more polygons in your scene, the more processing your scene demands. Less polygons = faster rendering. • Depth of field is expensive. • gluPerspective(45, 1, 0.01, 1000) • gluPerspective(45, 1, 1, 100) • The second will look much nicer than the first, because from 0.01 to 1000 is 10-2 to 103; that’s five orders of magnitude. Whereas from 1 to 100 is from 100 to 102; much less range, so GL can store more z-buffer detail about the smaller range. • Matrix operations are slow. • Matrix operations--especially inversions--are slow and expensive. Avoid them when possible.
A few tips in OpenGL • Use display lists whenever possible. • A display list is a pre-saved, pre-processed copy of all your graphics operations. • If you pass a bunch of operations (like glPushMatrix(), glVertex3f(), etc) to OpenGL, it processes each as it arrives. • A display list optimises this process by recording the operations requested and storing them for future recall. • You can then call a display list later on with minimal effort and a much higher frame rate.
A few tips in OpenGL • Display lists: void render(void) { static GLuint n = 0; if (!n) { n = glGenLists(1); glNewList(n, GL_COMPILE_AND_EXECUTE); glutWireSphere(1,10,10); ... glEndList(); } else glCallList(n); }
A few tips in OpenGL • Z-Buffering • When you render, you draw pixels onto a big 2D array of color values (the screen). • Each pixel comes from a polygon on an object somewhere. • That spot on that object is a certain distance from the camera. • A Z-buffer is a 2D array, the same size as the screen, of distances from the camera. For each pixel on the screen, a z-buffer stores how far away the spot that generated that pixel was. • When a new polygon is drawn over an older one, the z-buffer values are compared for each pixel. Only those pixels where the new color comes from a closer distance are drawn. • That’s how objects occlude one another.
Blobby Modelling • “Metaball, or ‘Blobby’, Modelling is a technique which uses implicit surfaces to produce models which seem more ‘organic’ or ‘blobby’ than conventional models built from flat planes and rigid angles”. --me • Uses of blobby modelling: • Organic forms and nonlinear shapes • Scientific modelling (electron orbitals, some medical imaging) • Muscles and joints with skin • Rapid prototyping • CAD/CAM solid geometry
Blobby Modelling Examples-- Paul Bourke (1997)
Blobby Modelling Examples-- “New Train” - Wyvill
Blobby Modelling Examples-- “Cabrit Model” - Wyvill
How does it work? • The user controls a set of control points instead of working directly with the surface. The control points in turn influence the shape of the model. • Each point in space generates a field of force, which drops off as a function of distance from the point; like gravity weakening with distance. • A blobby model is formed from the shells of these force fields, the implicit surface which they define in space. • An implicit surface is a surface consisting of all the points in space where a mathematical function (in this case, the value of the force field) has a particular key value (such as 0.5). Force = 2 1 0.5 0.25 ...
How does it REALLY work? • Introducing : Octrees. • An Octree is a recursive subdivision of space which “homes in” on the surface, from larger to finer detail. As the Octree subdivides and splits into smaller Octrees, only the Octrees which contain some of the surface are processed; empty Octrees are discarded. • An octree encloses a cube in space. You evaluate the force function F(r) at each vertex of the cube.
How does it REALLY work? • To display a set of Octrees, you need to convert the Octrees into polygons. • Each corner of each Octree has a value for the force function. • If some corners are “hot” (above the force limit) and others are “cold” (below the force limit) then the implicit surface must cross the cube edges in between. • You can draw triangles and polygons from the edges that the surfaces crosses. This turns an Octree into a polygon which can be rendered. • To refine the polygonalization, you subdivide the cube into eight subcubes, discarding any child whose vertices are all hot or all cold.
Refining the surface • Recursive subdivision :
Smoothing the surface • Improved edge vertices • The naive implementation is to form polygons whose vertices are the midpoints of the edges which lie between hot and cold vertices. • A dramatic smoothing effect can be achieved by using the relative strength of the hot and cold corners to determine whether the intersection point should really fall closer to hot or to cold. The vertices of the polygon can be more closely aproximated by points linearly interpolated along the edges of the cube. • t = (0.5 - F(P1)) / (F(P2) - F(P1)) • P = P1 + t * (P2 - P1)
Blobby Modelling • Some sources for further inspiration: • http://astronomy.swin.edu.au/~pbourke/modelling/implicitsurf/ • http://www.cs.berkeley.edu/~job/Papers/turk-2002-MIS.pdf • http://www.unchainedgeometry.com/jbloom/papers/interactive.pdf • http://www-courses.cs.uiuc.edu/~cs319/polygonization.pdf • http://bentonian.com/YAMM
Collision Detection (redux) • There are a number of different approaches to collision detection. Which one you use will depend on how much accuracy you need. • Sample cases: • Lasers shooting at a spaceship • Character walking through a maze • Billiard balls colliding • Fitting pieces together in CAD software
Collision Detection • Simplest form: sphere-to-sphere • To test for a collision between two spheres, test to see if the distance between their two centers is less than the sum of their radii. • dist = sqrt((A.x-B.x)2 + (A.y-B.y)2 + (A.z-B.z)2) • if (dist < A.radius + B.radius) then collision. A B
Collision Detection • Angled intesections • Finding the angle and point of intersection between moving spheres: • Solve for the point on the line of travel which is precisely the sum of the two spheres’ radii away from the center of the stationary sphere. • Subtracting the stationary sphere’s center from the point of intersection will also give you the vector that is each sphere’s normal at the impact point. Radius1 + Radius2
Collision Detection • Being shot with a laser: • If your game character is being shot at, you may not care whether their head or foot is hit. So don’t test them. • You can often reduce a player character to a cylinder. Testing for the intersection between a ray (the line of fire of a gun) and a cylinder (the player) is really just a test of the distance between two lines. Test the distance between the line and the axis of the cylinder. • If all fire is known to be horizontal, you can ignore the Y value completely and reduce the problem to a line/circle test.
Collision Detection • Navigating a maze • A maze is an extremely optimizable collision-detection problem. You can store your maze as a 2D array, • char maze[][] = { “##################”, • “# #”, • “#### ######## ##”, • “# #### ##”, • “# ## #”, • “# ## #”, • “##################” }; • ...or as a dynamically-allocated two-dimensional array drawn from a bitmap, or what have you. Each cell in your array will represent one “square” of the maze, and while the user might move around within a square, they cannot enter squares with wall in them.
Collision Detection • Navigating a maze • Don’t try to collide against the polygons of the maze model at all; instead, check whether your user can move or not by inspecting the contents of your array. This requires almost no math and is much faster than poly-to-poly collision detection. • Each square of the maze • may be larger than the player, • but it’s either passable or it’s • not; test the array of data to • see if the player’s position is • legal. Wall Space Wall Wall Wall Space Space Space Wall Space = 0 Space Wall Wall Wall = 1
Collision Detection • Sometimes, you’ll need to collide against some moving objects and some stationary objects. • Many forms of collision-detection involve square roots or other computationally expensive operations. If you can avoid making the actual test in the first place you can save time. • A BSP Tree (or Binary Space Partitioning Tree) is a data structure, like an Octree, which breaks space up into cubical or rectangular volumes. • The idea behind a BSP tree is that within any large space, there will be smaller things which are on one side or the other of that space. You can recursively divide and shrink the rectangular volumes to form a tree of volumes, the smallest of which contain only one or two collideable objects. • Testing whether you are inside a rectangular volume is sure to be much cheaper than doing polygonal collision tests.
Collision Detection • Line-to-poly intersection • To interesect a polygon with a line, find the point where the line intersects the polygon’s plane. • Test to see if the point is inside the polygon. • - One way to do so: dot the vector from the point to any vertex with the vector from the point to each of the other vertices in turn. If no dot product is negative, then all the vectors point in the same direction, implying that the point is outside the polygon. • The general case • The general case of collision detection is to intersect two polygons together. • Poly-to-poly intersection is mathematically traumatic and will not be addressed here.
Collision Detection • Some sources for further inspiration: • http://www.stanford.edu/~jgao/collision-detection.html • http://www.cosy.sbg.ac.at/~held/projects/collision/collision.html • http://www.cs.brown.edu/courses/gs007/lect/sim/web/murat.html
Ray Tracing • Ray Tracing is a different way of rendering images. • Instead of rendering each of the polygonal faces of the surface, or going through complex hoops to convert implicit surfaces to polygon meshes, render the scene based on what the eye can see instead of where the data lies.
Ray Tracing • How it works: • Logically, in order to reach the camera, light has to pass through the rectangle floating in the virtual world which is the rectangle matching the screen in the real world. We’ve sometimes called this rectangle the virtual canvas. OpenGL goes to a great deal of effort to paint every object in the scene onto the canvas, pixel by pixel. • In the end, the virtual canvas becomes the real-world screen. • Instead of painting objects one-by-one, ray-tracing reverses the question to ask, “who colored this dot on the canvas?” • In ray-tracing a beam is fired from the eye (camera) through each pixel of the virtual canvas, then traced onwards to see what it hits. Jamis Buck (1999)
Ray Tracing • Ray-casting • Ray-casting is the act of throwing a ray of “light” out from the camera, through each of the pixels in the scene, until it hits an object. Whatever color the objectis, that’s the color of that pixel. • We can calculate the exact shadeand color of the object by gauginghow much light reaches it fromthe light sources in the scene. • The angle of incidence of the light,combined with the location of the camera and the surface normal at the impact point, are used to calculate the proper lighting equations. Jamis Buck (1999)
Ray Tracing • Shadows • To find which lights shine on an object, we use a technique called “shadow casting”. A ray is fired from the point on the object towards each light in the scene. Where the ray reaches the light, the light shines on the object and the object’s lighting is calculated; where the ray doesn’t reach the objectbecause of an obstruction, that point on the object is out of the path of the light, and therefor in shadow. Jamis Buck (1999)
Ray Tracing • Some sources for further inspiration: • http://www.geocities.com/jamisbuck/raytracing.html • Scenes from Bunny, anOscar-winning short film fromBlue Sky Studios (http://bunny.blueskystudios.com/)