more hsr, opengl buffers & tests
DESCRIPTION
More HSR, OpenGL Buffers & Tests. Glenn G. Chappell [email protected] U. of Alaska Fairbanks CS 481/681 Lecture Notes Monday, February 2, 2004. Review: Advanced HSR Methods [1/2]. Most HSR methods fall into one of two categories: object-space methods and image-space methods. - PowerPoint PPT PresentationTRANSCRIPT
More HSR,OpenGL Buffers & Tests
Glenn G. [email protected]
U. of Alaska Fairbanks
CS 481/681 Lecture NotesMonday, February 2, 2004
2 Feb 2004 CS 481/681 2
Review:Advanced HSR Methods [1/2] Most HSR methods fall into one of two categories: object-space
methods and image-space methods. Object-Space Methods
Here, we do HSR before rasterization.• Often outside the pipeline entirely.
We deal with scene descriptions (polygons?). Image-Space Methods
Here, we deal with fragments or pixels.• Thus, if part of a pipeline, image-space methods generally come during or
after rasterization. General advantages of object-space methods:
Not limited by frame-buffer resolution. Most can be used as a pre-processing step.
General advantages of image-space methods: Fewer requirements; usable more often. Usually faster, for scenes with high polygon count.
2 Feb 2004 CS 481/681 3
Review:Advanced HSR Methods [2/2] Last time we looked at four image-space HSR methods:
Z-buffering.• A.K.A. “depth buffering”.• Very general.
A-buffering (not in text)• Store list of fragments in each pixel of “A buffer”.• Render final scene as post-processing step.• Very general, but not used much.
Ray Casting• Send a ray through each pixel. See what it hits first.• Requires a scene description.• Ray tracing is a fancier version of this.
Scan-Line Method• Proceed across scan line, keeping list of relevant polygons.
• With each new pixel, adjust list & depths of polygons.• Requires a scene description.• An important method, back before rendering-pipeline hardware.
2 Feb 2004 CS 481/681 4
More HSR:Intro. to Object-Space Methods Now we look at five object-space HSR methods.
Here, we deal with objects or polygons before rasterization.
Object-space methods require a scene description, so:• Their applicability is not as general as that of, say, z-
buffering.• They do not fit well within a rendering pipeline; however,
they can often be done before the pipeline. Object-space methods generally require comparisons
between polygons.• Thus, high polygon count can slow them down.• Many object-space methods do make good pre-processing
steps, however. First, an exception.
2 Feb 2004 CS 481/681 5
More HSR:5. Backface Culling If a polygon faces away from the viewer, cull it.
In other words, toss it out; do not rasterize it. If all objects are closed surfaces bounding convex regions, and no
object covers another, then backface culling does HSR for us. We do this after projection.
Why? In OpenGL:
glCullFace(GL_BACK);glEnable(GL_CULL_FACE);
Properties Object space. Works just fine in a pipeline. Not general at all, of course.
• Rarely is backface culling, by itself, enough to accomplish HSR for a scene.• But we can use it with some other method, to improve efficiency.
2 Feb 2004 CS 481/681 6
More HSR:6. Generic Object-Space HSR
For each polygon, clip it so that only its visible region remains. Tricky … No, I’m not going to tell you how.
Properties Object-space method. “Main loop” runs over polygons. Polygon-polygon comparisons required. Can be slow. Requires a scene description.
• So do it before the pipeline. Does not handle translucency.
2 Feb 2004 CS 481/681 7
More HSR:Note on Painter’s Algorithm A generally useful technique is the Painter’s Algorithm.
Method• Draw back-to-front.• New fragment colors replace old pixel colors.
This does HSR. But we need to figure out how to draw back-to-front. The P.A. also handles translucency nicely; we use blending.
Closely related is the Reverse Painter’s Algorithm. Method
• Draw front-to-back.• Only set a pixel’s color once.
We cannot do translucency with the R.P.A. Now we look at ways to draw in back-to-front order.
Front-to-back just reverses this, of course.
2 Feb 2004 CS 481/681 8
More HSR:7. Depth Sort Sort polygons by their depth. How can this be a problem?
Some collections of polygonscannot be sorted.
In such cases we split apolygon.
No details right now.• Maybe implement this as
part of a project? Properties
Object-space method. “Main loop” runs over polygons.
• Polygon-polygon comparisons required. Can be slow. Requires a scene description. Works with P.A. & R.P.A.
1
23
413
2
2 Feb 2004 CS 481/681 9
More HSR:8. BSP Trees We know how to get a back-to-front order with a BSP tree.
So we do it. This is really just a fancy (but relatively easy) way to
accomplish a depth sort.• However, we may split polygons more often than we need to.
Properties Object-space method. “Main loop” runs over polygons.
• Polygon-polygon comparisons required. Construction is slow.• BSP-tree construction works well as a preprocessing step, as long
as the scene is mostly static. Requires a scene description. Works with P.A. & R.P.A.
2 Feb 2004 CS 481/681 10
More HSR:Tossing Out Polygons
With a couple of small exceptions, all of the HSR methods we have discussed have a big problem: They require every polygon in the scene to be
rendered. For large, complex scenes, there are often huge
areas we cannot see. For example, wandering around in a building, you
cannot see most of the rooms. We could greatly improve efficiency if we could
throw out polygons in hidden regions. A recent method that addresses this problem is
“portal rendering”.
2 Feb 2004 CS 481/681 11
More HSR:9. Portal Rendering [1/3] A recently introduced method is portal rendering.
Introduced in 1995 by David P. Luebke and Chris Georges. Now implemented in many game engines. This is not in the text.
Method Divide the scene into cells (a.k.a. “sectors” or “rooms”).
• A cell is, roughly speaking, a room. Cells contain polygons. Some of these polygons are portals.
• A portal joins two cells.• Portal polygons are often completely transparent (invisible).
When rendering, adjoining cells are placed, using the proper transformations, behind the appropriate portals.
• Or do ray tracing: rays that hit a portal “go through” to another cell. Cell that do not (potentially) appear are not referenced at all. This is not a complete HSR method.
• Some other method must be used to do HSR within a cell.• However, portal rendering can dramatically improve efficiency of HSR.
2 Feb 2004 CS 481/681 12
More HSR:9. Portal Rendering [2/3] Consider the following cell-portal data structure.
Portals are in red.
This results in the following logical arrangement of cells.
Cell 1 Cell 2 Cell 3A
BC
D
To C
To A
To D
To B
Cell 1
Cell 2Cell 3
A
BC
D
2 Feb 2004 CS 481/681 13
More HSR:9. Portal Rendering [3/3] Portal rendering can easily be extended in interesting ways.
Suppose a portal leads from cell 1 into cell 1, at the same portal, with a transformation that reflects the cell about the plane of the portal.
• Result: Suppose a portal leads from cell 2 into the opposite end of cell 2. We
look into cell 1 from cell 3.• Result:
Generally, portals need not join cells in a reasonable fashion. Also, unlike structures like BSP trees, the cell-portal data
structure can easily be modified on the fly. Thus, portal rendering has no problem with:
Elevators. Rooms that are bigger than the buildings they lie inside. “Rips in the fabric of space”, or whatever …
Is there a project hidden in here somewhere?
2 Feb 2004 CS 481/681 14
OpenGL Buffers & Tests:Overview
We next look at various tools provided by OpenGL: Buffers Tests Blending
Part of the OpenGL philosophy is to provide tools without dictating their use. These tools can be used in many ways. Often the name (“depth buffer”) suggests the
most common use, but there is nothing “wrong” with using a tool quite differently.
2 Feb 2004 CS 481/681 15
OpenGL Buffers & Tests:Buffers
An OpenGL buffer is (essentially) an array that holds one piece of data for each pixel in the viewport.
OpenGL has 4 types of buffers: Color buffers Depth buffer Stencil buffer Accumulation buffer
Each buffer has an intended function; however, you may use the buffers in any way you wish.
In order to be used, a buffer must be allocated. Do this in your glutInitDisplayMode call.
2 Feb 2004 CS 481/681 16
OpenGL Buffers & Tests:Tests Related to the buffers are the OpenGL tests:
Scissor test Alpha test Depth test Stencil test
A test is an expression with a boolean value that OpenGL evaluates for every fragment. If the result is true, then the test passes, and the fragment
continues down the pipeline. Otherwise, the test fails, and fragment is discarded. All tests are done in the fragment-processing part of the
pipeline. In order to have any effect, a test must be enabled.
Do this with glEnable.
2 Feb 2004 CS 481/681 17
OpenGL Buffers & Tests:Buffers & Tests Together
Buffers and tests are associated. We have already seen this with the depth buffer & test.
Remember: Allocate buffers; enable tests!
Buffer Corresponding Test
-- Scissor Test
Color Buffers Alpha Test
Depth Buffer Depth Test
Stencil Buffer Stencil Test
Accumulation Buffer --
2 Feb 2004 CS 481/681 18
OpenGL Buffers & Tests:Clearing Every OpenGL buffer can be cleared.
In each case, the command to do this is the same: glClear. The buffers to clear are specified by bitwise-or’ing together
the appropriate GL_…_BUFFER_BIT constants. For example,
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_ACCUM_BUFFER_BIT);
The value to store in each pixel of a buffer is set by a separate command for each buffer:
glClearColorglClearDepthglClearStencilglClearAccum
2 Feb 2004 CS 481/681 19
OpenGL Buffers & Tests:Masking [1/2]
Most buffers have masks associated with them. The mask determines whether a buffer (or part of a
buffer) is ever written. For example, the color-buffer mask is controlled
by the glColorMask command. This command takes 4 parameters, all GLboolean’s,
representing R, G, B, and A, respectively. For example,glColorMask(false, true, true, true);means that the R portion of the color buffer will not be changed.
Note: The mask affects all commands that would change the buffer, even glClear.
2 Feb 2004 CS 481/681 20
OpenGL Buffers & Tests:Masking [2/2] In masking.cpp, I define five bool variables: redmask, greenmask,
bluemask, depthmask, clearall. Each defaults to true and is toggled by pressing the first letter in its
name. The interesting part of the code is at the start of function display:
if (clearall)
{
glColorMask(true, true, true, true);
glDepthMask(true);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
glColorMask(redmask, greenmask, bluemask, true);
glDepthMask(depthmask);
if (!clearall)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2 Feb 2004 CS 481/681 21
OpenGL Buffers & Tests:The Scissor Test The scissor test is by far the simplest of the
tests. It allows you to restrict drawing to a rectangular portion
of the viewport. To enable: glEnable(GL_SCISSOR_TEST); Then: glScissor(x, y, width, height);
Parameters are as in the glViewport command. (x,y) is the lower-left corner of the rectangle.
The scissor test passes if the pixel is within the rectangle; otherwise, it fails.
The scissor test is really just a quick, simple version of stenciling. We discuss stenciling later this week.