hidden surfaces iii week 9, wed mar 17tmm/courses/314/vjan2010/... · 3 review: bsp trees...
TRANSCRIPT
![Page 1: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/1.jpg)
University of British ColumbiaCPSC 314 Computer Graphics
Jan-Apr 2010
Tamara Munzner
http://www.ugrad.cs.ubc.ca/~cs314/Vjan2010
Hidden Surfaces III
Week 9, Wed Mar 17
![Page 2: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/2.jpg)
2
Review: BSP Trees
• preprocess: create binary tree• recursive spatial partition• viewpoint independent
![Page 3: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/3.jpg)
3
Review: BSP Trees• runtime: correctly traversing this tree enumerates
objects from back to front• viewpoint dependent: check which side of plane
viewpoint is on at each node• draw far, draw object in question,
draw near
1
2
3
4 5
6
78
9
F N
FN
FN
N F
NF
1 2
34
5
6
78
9
FN
FN
FN
![Page 4: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/4.jpg)
4
Review: The Z-Buffer Algorithm• augment color framebuffer with Z-buffer or
depth buffer which stores Z value at eachpixel• at frame beginning, initialize all pixel depths
to ∞• when rasterizing, interpolate depth (Z)
across polygon• check Z-buffer before storing pixel color in
framebuffer and storing depth in Z-buffer• don’t write pixel if its Z value is more distant
than the Z value already stored there
![Page 5: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/5.jpg)
5
More: Integer Depth Buffer• reminder from picking discussion
• depth lies in the NDC z range [0,1]• format: multiply by 2^n -1 then round to nearest int
• where n = number of bits in depth buffer• 24 bit depth buffer = 2^24 = 16,777,216 possible
values• small numbers near, large numbers far
• consider depth from VCS: (1<<N) * ( a + b / z )• N = number of bits of Z precision• a = zFar / ( zFar - zNear )• b = zFar * zNear / ( zNear - zFar )• z = distance from the eye to the object
![Page 6: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/6.jpg)
6
Review: Depth Test Precision
• reminder: perspective transformation mapseye-space (view) z to NDC z
• thus:
![Page 7: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/7.jpg)
7
Review: Depth Test Precision
• therefore, depth-buffer essentially stores 1/z,rather than z!
• issue with integer depth buffers• high precision for near objects• low precision for far objects
-z-zeyeeye
zzNDCNDC
-n-n -f-f
![Page 8: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/8.jpg)
8
Review: Depth Test Precision• low precision can lead to depth fighting for far objects
• two different depths in eye space get mapped to samedepth in framebuffer
• which object “wins” depends on drawing order and scan-conversion
• gets worse for larger ratios f:n• rule of thumb: f:n < 1000 for 24 bit depth buffer
• with 16 bits cannot discern millimeter differences inobjects at 1 km distance
• demo:sjbaker.org/steve/omniv/love_your_z_buffer.html
![Page 9: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/9.jpg)
9
Correction: Ortho Camera Projection• camera’s back plane
parallel to lens• infinite focal length• no perspective
convergence• just throw away z values• x and y coordinates do
not change with respectto z in this projection
!!!
"
#
$$$
%
&
=
!!!
"
#
$$$
%
&
0
y
x
z
y
x
p
p
p
!!!!
"
#
$$$$
%
&
!!!!
"
#
$$$$
%
&
=
!!!!
"
#
$$$$
%
&
11000
0000
0010
0001
1
z
y
x
z
y
x
p
p
p
P
nearfar
nearfar
nearfar
bottop
bottop
bottop
leftright
leftright
leftright
P
!!!!!!!!!!
"
#
$$$$$$$$$$
%
&
'
+'
'
'
'
+'
'
'
+'
'
=
1000
200
02
0
002
'
week4.day2, slide 18
![Page 10: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/10.jpg)
10
Z-Buffer Algorithm Questions
• how much memory does the Z-buffer use?• does the image rendered depend on the
drawing order?• does the time to render the image depend on
the drawing order?• how does Z-buffer load scale with visible
polygons? with framebuffer resolution?
![Page 11: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/11.jpg)
11
Z-Buffer Pros
• simple!!!• easy to implement in hardware
• hardware support in all graphics cards today• polygons can be processed in arbitrary order• easily handles polygon interpenetration• enables deferred shading
• rasterize shading parameters (e.g., surfacenormal) and only shade final visible fragments
![Page 12: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/12.jpg)
12
Z-Buffer Cons• poor for scenes with high depth complexity
• need to render all polygons, even ifmost are invisible
• shared edges are handled inconsistently• ordering dependent
eyeeye
![Page 13: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/13.jpg)
13
Z-Buffer Cons• requires lots of memory
• (e.g. 1280x1024x32 bits)• requires fast memory
• Read-Modify-Write in inner loop• hard to simulate translucent polygons
• we throw away color of polygons behindclosest one
• works if polygons ordered back-to-front• extra work throws away much of the speed
advantage
![Page 14: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/14.jpg)
14
Hidden Surface Removal
• two kinds of visibility algorithms• object space methods• image space methods
![Page 15: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/15.jpg)
15
Object Space Algorithms• determine visibility on object or polygon level
• using camera coordinates• resolution independent
• explicitly compute visible portions of polygons• early in pipeline
• after clipping• requires depth-sorting
• painter’s algorithm• BSP trees
![Page 16: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/16.jpg)
16
Image Space Algorithms
• perform visibility test for in screen coordinates• limited to resolution of display• Z-buffer: check every pixel independently
• performed late in rendering pipeline
![Page 17: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/17.jpg)
17
Projective Rendering Pipeline
OCS - object coordinate system
WCS - world coordinate system
VCS - viewing coordinate system
CCS - clipping coordinate system
NDCS - normalized device coordinate system
DCS - device coordinate system
OCSOCS WCSWCS VCSVCS
CCSCCS
NDCSNDCS
DCSDCS
modelingmodelingtransformationtransformation
viewingviewingtransformationtransformation
projectionprojectiontransformationtransformation
viewportviewporttransformationtransformation
alter walter w
/ w/ w
object world viewing
device
normalizeddevice
clipping
perspectiveperspectivedivisiondivision
glVertex3f(x,y,z)glVertex3f(x,y,z)
glTranslatefglTranslatef(x,y,z)(x,y,z)glRotatefglRotatef((thth,x,y,z),x,y,z)........
gluLookAtgluLookAt(...)(...)
glFrustumglFrustum(...)(...)
glutInitWindowSizeglutInitWindowSize(w,h)(w,h)glViewportglViewport(x,y,a,b)(x,y,a,b)
![Page 18: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/18.jpg)
18
Rendering Pipeline
GeometryDatabase
Model/ViewTransform. Lighting Perspective
Transform. Clipping
ScanConversion
DepthTestTexturing Blending
Frame-buffer
OCSOCSobject
WCSWCSworld
VCSVCSviewing
CCSCCSclipping
NDCSNDCS
normalizeddevice
SCSSCSscreen
(2D)DCSDCSdevice
(3D)
(4D)
/w/w
![Page 19: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/19.jpg)
19
Backface Culling
![Page 20: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/20.jpg)
20
Back-Face Culling
• on the surface of a closed orientablemanifold, polygons whose normals pointaway from the camera are alwaysoccluded:
note: backface cullingalone doesn’t solve the
hidden-surface problem!
![Page 21: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/21.jpg)
21
Back-Face Culling
• not rendering backfacing polygons improvesperformance• by how much?
• reduces by about half the number of polygonsto be considered for each pixel
• optimization when appropriate
![Page 22: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/22.jpg)
22
Back-Face Culling• most objects in scene are typically “solid”• rigorously: orientable closed manifolds
• orientable: must have two distinct sides• cannot self-intersect• a sphere is orientable since has
two sides, 'inside' and 'outside'.• a Mobius strip or a Klein bottle is
not orientable• closed: cannot “walk” from one
side to the other• sphere is closed manifold• plane is not
![Page 23: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/23.jpg)
23
Back-Face Culling
• examples of non-manifold objects:• a single polygon• a terrain or height field• polyhedron w/ missing face• anything with cracks or holes in boundary• one-polygon thick lampshade
![Page 24: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/24.jpg)
24
Back-face Culling: VCS
yy
zz
first idea:first idea:cull if cull if 0<
ZN
sometimessometimesmisses polygons thatmisses polygons thatshould be culledshould be culledeyeeye
![Page 25: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/25.jpg)
25
Back-face Culling: NDCS
yy
zz eyeeye
VCSVCS
NDCSNDCS
eyeeye works to cull ifworks to cull if 0>ZN
yyzz
![Page 26: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/26.jpg)
26
Invisible Primitives• why might a polygon be invisible?
• polygon outside the field of view / frustum• solved by clipping
• polygon is backfacing• solved by backface culling
• polygon is occluded by object(s) nearer the viewpoint• solved by hidden surface removal
![Page 27: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/27.jpg)
27
![Page 28: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/28.jpg)
28
Blending
![Page 29: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/29.jpg)
29
Rendering Pipeline
GeometryDatabase
Model/ViewTransform. Lighting Perspective
Transform. Clipping
ScanConversion
DepthTestTexturing Blending
Frame-buffer
![Page 30: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/30.jpg)
30
Blending/Compositing• how might you combine multiple elements?• foreground color A, background color B
![Page 31: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/31.jpg)
31
Premultiplying Colors• specify opacity with alpha channel: (r,g,b,α)
• α=1: opaque, α=.5: translucent, α=0: transparent
• A over B• C = αA + (1-α)B
• but what if B is also partially transparent?• C = αA + (1-α) βB = βB + αA + βB - α βB• γ = β + (1-β)α = β + α – αβ
• 3 multiplies, different equations for alpha vs. RGB
• premultiplying by alpha• C’ = γ C, B’ = βB, A’ = αA
• C’ = B’ + A’ - αB’• γ = β + α – αβ
• 1 multiply to find C, same equations for alpha and RGB
![Page 32: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/32.jpg)
32
Texturing
![Page 33: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/33.jpg)
33
Rendering Pipeline
GeometryDatabase
Model/ViewTransform. Lighting Perspective
Transform. Clipping
ScanConversion
DepthTestTexturing Blending
Frame-buffer
Geometry ProcessingGeometry Processing
RasterizationRasterization Fragment ProcessingFragment Processing
![Page 34: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/34.jpg)
34
Texture Mapping• real life objects have
nonuniform colors,normals
• to generate realisticobjects, reproducecoloring & normalvariations = texture
• can often replacecomplex geometricdetails
![Page 35: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/35.jpg)
35
Texture Mapping• introduced to increase realism
• lighting/shading models not enough• hide geometric simplicity
• images convey illusion of geometry• map a brick wall texture on a flat polygon• create bumpy effect on surface
• associate 2D information with 3D surface• point on surface corresponds to a point in
texture• “paint” image onto polygon
![Page 36: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/36.jpg)
36
Color Texture Mapping
• define color (RGB) for each point on objectsurface
• two approaches• surface texture map• volumetric texture
![Page 37: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/37.jpg)
37
Texture Coordinates• texture image: 2D array of color values (texels)• assigning texture coordinates (s,t) at vertex with
object coordinates (x,y,z,w)• use interpolated (s,t) for texel lookup at each pixel• use value to modify a polygon’s color
• or other surface property• specified by programmer or artist glTexCoord2f(s,t)
glVertexf(x,y,z,w)
![Page 38: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/38.jpg)
38
Texture Mapping Example
+ =
![Page 39: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/39.jpg)
39
Example Texture Map
glTexCoord2d(0,0);glVertex3d (0, -2, -2);
glTexCoord2d(1,1);glVertex3d (0, 2, 2);
![Page 40: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/40.jpg)
40
Fractional Texture Coordinates
(0,0) (1,0)
(0,1) (1,1)
(0,0) (.25,0)
(0,.5) (.25,.5)
textureimage
![Page 41: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/41.jpg)
41
Texture Lookup: Tiling and Clamping
• what if s or t is outside the interval [0…1]?• multiple choices
• use fractional part of texture coordinates• cyclic repetition of texture to tile whole surface
glTexParameteri( …, GL_TEXTURE_WRAP_S, GL_REPEAT,GL_TEXTURE_WRAP_T, GL_REPEAT, ... )
• clamp every component to range [0…1]• re-use color values from texture image border
glTexParameteri( …, GL_TEXTURE_WRAP_S, GL_CLAMP,GL_TEXTURE_WRAP_T, GL_CLAMP, ... )
![Page 42: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/42.jpg)
42
glTexCoord2d(1, 1);glVertex3d (x, y, z);
(1,0)
(0,0) (0,1)
(1,1)
Tiled Texture Map
glTexCoord2d(4, 4);glVertex3d (x, y, z);
(4,4)
(0,4)
(4,0)
(0,0)
![Page 43: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/43.jpg)
43
Demo
• Nate Robbins tutors• texture
![Page 44: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/44.jpg)
44
Texture Coordinate Transformation• motivation
• change scale, orientation of texture on an object• approach
• texture matrix stack• transforms specified (or generated) tex coords
glMatrixMode( GL_TEXTURE );glLoadIdentity();glRotate(); …
• more flexible than changing (s,t) coordinates• [demo]
![Page 45: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/45.jpg)
45
Texture Functions• once have value from the texture map, can:
• directly use as surface color: GL_REPLACE• throw away old color, lose lighting effects
• modulate surface color: GL_MODULATE• multiply old color by new value, keep lighting info• texturing happens after lighting, not relit
• use as surface color, modulate alpha: GL_DECAL• like replace, but supports texture transparency
• blend surface color with another: GL_BLEND• new value controls which of 2 colors to use• indirection, new value not used directly for coloring
• specify with glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE, <mode>)
• [demo]
![Page 46: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/46.jpg)
46
Texture Pipeline
Texel color
(0.9,0.8,0.7)
(x, y, z)
Object position
(-2.3, 7.1, 17.7)
(s, t)
Parameter space
(0.32, 0.29)
Texel space
(81, 74)
(s’, t’)
Transformedparameter space
(0.52, 0.49)
Final color
(0.45,0.4,0.35)
Object color
(0.5,0.5,0.5)
![Page 47: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/47.jpg)
47
Texture Objects and Binding• texture object
• an OpenGL data type that keeps textures resident in memory andprovides identifiers to easily access them
• provides efficiency gains over having to repeatedly load and reload atexture
• you can prioritize textures to keep in memory• OpenGL uses least recently used (LRU) if no priority is assigned
• texture binding• which texture to use right now• switch between preloaded textures
![Page 48: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/48.jpg)
48
Basic OpenGL Texturing• create a texture object and fill it with texture data:
• glGenTextures(num, &indices) to get identifiers for the objects• glBindTexture(GL_TEXTURE_2D, identifier) to bind
• following texture commands refer to the bound texture• glTexParameteri(GL_TEXTURE_2D, …, …) to specify
parameters for use when applying the texture• glTexImage2D(GL_TEXTURE_2D, ….) to specify the texture data
(the image itself)• enable texturing: glEnable(GL_TEXTURE_2D)• state how the texture will be used:
• glTexEnvf(…)• specify texture coordinates for the polygon:
• use glTexCoord2f(s,t) before each vertex:• glTexCoord2f(0,0); glVertex3f(x,y,z);
![Page 49: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/49.jpg)
49
Low-Level Details• large range of functions for controlling layout of texture data
• state how the data in your image is arranged• e.g.: glPixelStorei(GL_UNPACK_ALIGNMENT, 1) tells
OpenGL not to skip bytes at the end of a row• you must state how you want the texture to be put in memory:
how many bits per “pixel”, which channels,…• textures must be square and size a power of 2
• common sizes are 32x32, 64x64, 256x256• smaller uses less memory, and there is a finite amount of
texture memory on graphics cards• ok to use texture template sample code for project 4
• http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=09
![Page 50: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/50.jpg)
50
Texture Mapping
• texture coordinates• specified at vertices
glTexCoord2f(s,t);glVertexf(x,y,z);
• interpolated across triangle (like R,G,B,Z)• …well not quite!
![Page 51: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/51.jpg)
51
Texture Mapping
• texture coordinate interpolation• perspective foreshortening problem
![Page 52: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/52.jpg)
52
Interpolation: Screen vs. World Space
• screen space interpolation incorrect• problem ignored with shading, but artifacts
more visible with texturing
P1(x,y,z)
V0(x’,y’)
V1(x’,y’)
P0(x,y,z)
![Page 53: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/53.jpg)
53
Texture Coordinate Interpolation• perspective correct interpolation
• α, β, γ :• barycentric coordinates of a point P in a triangle
• s0, s1, s2 :• texture coordinates of vertices
• w0, w1,w2 :• homogeneous coordinates of vertices
(s1,t1)
(s0,t0)
(s2,t2)
(x1,y1,z1,w1)
(x0,y0,z0,w0)
(x2,y2,z2,w2)
(α,β,γ)(s,t)?
![Page 54: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/54.jpg)
54
Reconstruction
(image courtesy of (image courtesy of Kiriakos KutulakosKiriakos Kutulakos, U Rochester), U Rochester)
![Page 55: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/55.jpg)
55
Reconstruction
• how to deal with:• pixels that are much larger than texels?
• apply filtering, “averaging”
• pixels that are much smaller than texels ?• interpolate
![Page 56: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/56.jpg)
56
MIPmapping
Without MIP-mappingWithout MIP-mapping
With MIP-mappingWith MIP-mapping
use use ““image pyramidimage pyramid”” to to precomputeprecomputeaveraged versions of the textureaveraged versions of the texture
store whole pyramid instore whole pyramid insingle block of memorysingle block of memory
![Page 57: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/57.jpg)
57
MIPmaps• multum in parvo -- many things in a small place
• prespecify a series of prefiltered texture maps of decreasingresolutions
• requires more texture storage• avoid shimmering and flashing as objects move
• gluBuild2DMipmaps
• automatically constructs a family of textures from originaltexture size down to 1x1
without with
![Page 58: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/58.jpg)
58
MIPmap storage
• only 1/3 more space required
![Page 59: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/59.jpg)
59
Texture Parameters
• in addition to color can control othermaterial/object properties• surface normal (bump mapping)• reflected color (environment mapping)
![Page 60: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/60.jpg)
60
Bump Mapping: Normals As Texture• object surface often not smooth – to recreate correctly
need complex geometry model• can control shape “effect” by locally perturbing surface
normal• random perturbation• directional change over region
![Page 61: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/61.jpg)
61
Bump Mapping
![Page 62: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/62.jpg)
62
Bump Mapping
![Page 63: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/63.jpg)
63
Embossing
• at transitions• rotate point’s surface normal by θ or - θ
![Page 64: Hidden Surfaces III Week 9, Wed Mar 17tmm/courses/314/Vjan2010/... · 3 Review: BSP Trees •runtime: correctly traversing this tree enumerates objects from back to front •viewpoint](https://reader035.vdocument.in/reader035/viewer/2022071004/5fc15b9f1694355f4252710f/html5/thumbnails/64.jpg)
64
Displacement Mapping• bump mapping gets
silhouettes wrong• shadows wrong too
• change surfacegeometry instead• only recently
available withrealtime graphics
• need to subdividesurface