Advanced Graphics - Part 6
description
Transcript of Advanced Graphics - Part 6
COMP9018 - Advanced Graphics
Advanced Graphics - Part 6
• The big picture:– Still working on advanced polygonal
techniques with a special focus on OpenGL.
– Have almost finished some of the texture mapping techniques.
– Today: Blending, simple antialiasing, fog, polygon offset.
– Some simple applications: billboarding, lightmaps.
COMP9018 - Advanced Graphics
Specular highlights and texturing
• Problem: OpenGL pipeline does lighting calculations (including specular), then texturing.
COMP9018 - Advanced Graphics
VertexLighting
Geometry
Texturemodulation
Textures
Pixels
Important bit for us:
COMP9018 - Advanced Graphics
So what?• So what? If the texture is modulating the
the lighting, then this will break things.• Why? Because specular should be last
thing applied: should be after texture mapping.
• Idea: Specular is because of light sources, not because of texture.
• Solution: Postpone specular 'til after texture mapping.
COMP9018 - Advanced Graphics
VertexLighting
Geometry
Rasterisation(Gouraudw/ ModulatedTextures)
Textures
Pixels
Primary
Add specular(secondary)colour
Secondary
Fixing specular
COMP9018 - Advanced Graphics
How does this work?• Ambient/Diffuse component is
modulated by texture.• Specular component is added after
texture mapping.
COMP9018 - Advanced Graphics
Usefulness• Pro:
– Fixes the problem.• Con:
– Slow! Have to do a Gouraud shading again ... just for specular.
– Doesn't hide geometry.• Better solution?
– Exists -- will talk about later. Hint: If we are doing Gouraud, might as well use texture mapping ...
COMP9018 - Advanced Graphics
Using OpenGL for this• Ambient + diffuse = primary colour,
specular = secondary colour.• Easy to do:• glLightModeli(GL_LIGHT_MODEL_COLOR
_CONTROL, GL_SEPARATE_SPECULAR_COLOR)
• To switch back to normal: • glLightModeli(GL_LIGHT_MODEL_COLOR
_CONTROL, GL_SINGLE_COLOR).
COMP9018 - Advanced Graphics
Demo• Go through spectex.
COMP9018 - Advanced Graphics
Texture stack• Just like modelview stack.• Matrices are 4x4. • Remember, textures are (s,t,r,q).• If q!=1, then s' = s/q, t' = t/q, r' = r/q.• Will turn out to be useful for some
weird things like projective texturing.• But even now can be used for things
like animated textures.
COMP9018 - Advanced Graphics
Multitexturing• Won't go into details too much• But some graphics hardware supports
applying more than one texture in a single pass.
• Can always accomplish same effect in multiple passes
• OpenGL supports multitexturing. • Why > 1 texture? Envmapping, bump
mapping, light maps --wait a while.
COMP9018 - Advanced Graphics
Blending• Explains mysterious alpha channel. • Convention: alpha = 0 means
transparent alpha = 1 means completely opaque.
• Important OpenGL concept: Fragment. Basically same as a pixel.
• A very flexible system that can be used for lots of applications.
COMP9018 - Advanced Graphics
How blending works• You have the source(s): The fragment
that we're working on adding right now.
• You have the destination(d): The fragment that's already saved in the frame buffer and that we'll eventually overwrite.
• You have source and destination blending factors fs and fd
COMP9018 - Advanced Graphics
d = fssƒ fdd
Blending• Ignore multiple colours (and alpha)
for a moment. • Blending works by:
• So what's so hard about that? • Choice of fs and fd is tricky.
€
′ d = fss+ fd d
COMP9018 - Advanced Graphics
Question: What is "normal" rendering?
• What settings of fs and fd give normal rendering?
COMP9018 - Advanced Graphics
Answer• fs = 1, fd = 0• Why? • Because normally later rendered
polygons overwrite earlier written polygons
• We are ignoring depth tests for now, but that is an issue.
COMP9018 - Advanced Graphics
Almost transparent
Translucent Almost opaque
Transparency
€
α =0.2
€
α =0.6
€
α =0.9
COMP9018 - Advanced Graphics
· s= 0
· s= 1
Question: How to do transparency?
• Reminder: Alpha encodes transparency. • What settings of fs and fd allow us to
model transparency?• Assume source is in front of destination. • Depends on source's alpha. • If , then source is transparent so
output should be destination• If , source is opaque, so output
should be source€
α s = 0
€
α s =1
COMP9018 - Advanced Graphics
Question: Appropriate factors?
• So ... what are appropriate blending factors to implement transparency?
COMP9018 - Advanced Graphics
· s
· s
1 · s
Answer: Appropriate factors
• If source transparency is , then:
€
α s
€
fs =α s
fd =1−α s
COMP9018 - Advanced Graphics
Blending in OpenGL• Allows a fixed but flexible set of fs
and fd. • To switch on blending:
glEnable(GL_BLEND)• glBlendFunc(fs, fd) defines the
blending functions. • Default mode is like calling
glBlendFunc(GL_ONE, GL_ZERO)
COMP9018 - Advanced Graphics
Available blending functions
• Plenty to choose from!• Can affect different colours
differently. • Some can only be used for either
source or destination.• Reminder: the blending function is
multiplied by the incoming colour fragment (source), or the fragment in the frame buffer (destination)
COMP9018 - Advanced Graphics
Common blending modes• Expressed as a 4-tuple, for RGBA• Clamped between 0 and 1 • GL_ZERO f = (0,0,0,0)• GL_ONE f = (1,1,1,1)• GL_SRC_ALPHA f=(as, as, as, as)• GL_ONE_MINUS_SRC_ALPHA
f=(1-as, 1-as, 1-as, 1-as)
COMP9018 - Advanced Graphics
Less common blending modes
• GL_CONSTANT_ALPHA• GL_DST_ALPHA and
GL_ONE_MINUS_DST_ALPHA• GL_DST_COLOR (only for source)
and GL_SRC_COLOR (only for dest)
COMP9018 - Advanced Graphics
Demo• Blend two triangles - alpha.c
COMP9018 - Advanced Graphics
Nasty surprises• It doesn't commute!• Final colour observed depends on
order of polygons. Example: do calculations on OHP with left = (1,1,0, 0.75) right = (0,1,1,0.75)
• Why is this bad?• Come back to it in a moment
COMP9018 - Advanced Graphics
Transparency and 3D graphics
• Do we need to change anything in our approach when rendering a 3D scene?
• Are there problems?• Hint: Consider visible surface
determination.
COMP9018 - Advanced Graphics
Issues with 3D transparency
• Problem: If we draw a translucent object into the frame buffer that's close to us, then because of Z buffering, we won't draw what's behind it (because it will be further away).
• Z buffer means the destination may not be written
• Oh no! Does this mean that we have to resort to depth sorting again?
• What's the solution?
COMP9018 - Advanced Graphics
Solution• Problem is that close translucent
objects prevent rendering of far opaque objects, effectively making translucent objects opaque some of the time.
• What if we render opaque objects first? • Then translucent objects behind opaque
objects won't be rendered (good). • But what about translucent objects that
are close obscuring far translucent object due to the depth buffer?
COMP9018 - Advanced Graphics
The algorithm• Render opaque objects• Keep depth buffer test on, but
switch off writing to the depth buffer.
• Why? Prevent translucent-translucent occlusion
• Render translucent objects• Switch depth buffer writing back on
COMP9018 - Advanced Graphics
Problem solved?• That problem is solved.• But are there any other problems? • Hint: Order-dependence of
translucence.
COMP9018 - Advanced Graphics
More problems!• Strictly speaking, the translucent
polygons should be rendered back to front.
• But we can't guarantee this, now that we're not using the depth buffer.
• Is there a solution?• Not an easy one. We could:
– Use painter's algorithm– Use BSP trees.
COMP9018 - Advanced Graphics
How serious is this problem?
• In many cases, order of translucent objects not critical.
• In many cases, not likely to have too many translucent objects.
• If things are near translucent, it doesn’t make much difference.
• Example: Game levels
COMP9018 - Advanced Graphics
Demonstration• Have a look at (hacked) alpha3D.c
COMP9018 - Advanced Graphics
Other issues?• What happens to back face culling?• What about two-sided lighting?
COMP9018 - Advanced Graphics
Conclusions• Blending is good (we'll see why
soon).• But many of our assumptions when
rendering were built on the basis of opaque polygons.
• Blending breaks things if you're not careful.
COMP9018 - Advanced Graphics
Blending and textures• Can (of course) blend textures• Blending happens AFTER texturing. • Reminder: When using RGBA textures
– Replace does C=Ct, A=At– Modulate does C = CfCt, A = AfAt– Decal does C=Cf(1-At) + CtAt, A=Af– Blend does C=Cf(1-Ct) + CcCt, A=AfAt
• The final C and A are used for the blending calculations
COMP9018 - Advanced Graphics
Decal textures• Decal textures is like using blending. • Reminder: When texture is RGBA
(i.e. with Alpha), then Decal works like this:C = Cf(1-At) + CtAt; A=Af.
• This is like blending with glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
• Cheap way to blend.
COMP9018 - Advanced Graphics
Applications of blending• Lots of applications for blending. • Important infrastructure ... we'll use it
for almost everything, but in combination with other effects.
• Basically a mechanism for mixing different visual effects.
• We'll be looking at– Prefilter antialiasing– Billboarding– Lightmaps
COMP9018 - Advanced Graphics
Antialiasing via blending• Can use blending to accomplish line and
polygon edge blending.• How?• Two steps:
– Tell OpenGL to generate alpha values for partially covered pixels. glEnable(GL_LINE_SMOOTH)
– Switch on blending and set up the blending function appropriately (probably GL_SRC_ALPHA and GL_ONE_MINUS_SRC_ALPHA)
COMP9018 - Advanced Graphics
Demo• aargb.c
COMP9018 - Advanced Graphics
Aside: Hints• Hints?• Sometimes you can tell OpenGL how
important it is that it do things well or fast.
• glHint(target, mode) • mode = GL_FASTEST, GL_NICEST,
GL_DONT_CARE• Example target: GL_LINE_SMOOTH_HINT• But you never know what your
implementation will do
COMP9018 - Advanced Graphics
Antialiasing with blending• Can be used for lines, polygons,
etc.• To use for polygons,
glEnable(GL_POLYGON_SMOOTH)• But does not solve problem for
textures.• Still useful, especially with pure
geometry applications.
COMP9018 - Advanced Graphics
Billboarding• With hardware acceleration: textures
are cheap, geometry is expensive • So ... can we "fake" geometry using
textures? • Problem: Textures are rectangular. And
if you want to make them non-rectangular, then you need geometry. -> Catch 22!
• Solution: Use transparency.
COMP9018 - Advanced Graphics
Billboarding concepts• For objects in distance, use
rectangle with transparent texture on it.
• Example: Tree. • No need to make all geometry of a
tree, simply put up rectangle and paint tree on it.
COMP9018 - Advanced Graphics
What about close?• Problem: If user turns we can see
side of flat polygon!• Hack solution 1: Turn polygon so
that it always faces the eye. • How? Grab modelview matrix and
invert rotation.• Hack solution 2: Use two polygons
at right angles. Called a "fin" billboard.
COMP9018 - Advanced Graphics
Billboarding -fins
COMP9018 - Advanced Graphics
Demos• billboard-blend.c• Show texture using gimp
COMP9018 - Advanced Graphics
Billboards• Can use other shapes, e.g. cylinder
billboarding.• Generally, any technique where we
use a 2D texture to represent a 3D object.
• Hacky but workable. • Example: tuxracer
COMP9018 - Advanced Graphics
Alpha test• Blending is slow. Involves 8
multiplies and 4 adds per pixel. • But if all we want to do is simple
on-off transparency (note ... NOT partial transparency) can simply test alpha value. If alpha value meets certain criteria, can then render or not.
COMP9018 - Advanced Graphics
Alpha testing in OpenGL• Enabling: glEnable(GL_ALPHA_TEST)• Define function used with glAlphaFunc:• glAlphaFunc(function, reference)• function = GL_NEVER, GL_LESS,
GL_EQUAL, GL_LEQUAL, GL_GREATER, GL_GEQUAL, GL_NOTEQUAL, GL_ALWAYS.
• reference value to compare against.• Fun fact. You can do this with the depth
buffer too.
COMP9018 - Advanced Graphics
Demo• billboard-alpha.c