introduction to 3d graphics lecture 6: real-time rendering anthony steed university college london

40
Introduction to 3D Introduction to 3D Graphics Graphics Lecture 6: Real-Time Lecture 6: Real-Time Rendering Rendering Anthony Steed Anthony Steed University College London University College London

Upload: prosper-boone

Post on 24-Dec-2015

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

Introduction to 3D GraphicsIntroduction to 3D Graphics

Lecture 6: Real-Time Lecture 6: Real-Time RenderingRendering

Anthony SteedAnthony Steed

University College LondonUniversity College London

Page 2: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

OverviewOverview

Scanning PolygonsScanning Polygons– Filling PixelsFilling Pixels

Gouraud and Phong ShadingGouraud and Phong Shading Visibility Culling Re-VisitedVisibility Culling Re-Visited

– Z-bufferZ-buffer Texture MappingTexture Mapping

Page 3: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

Recall Point in Polygon Recall Point in Polygon TestTest

Ray shooting is fast, but why not Ray shooting is fast, but why not find the actual span for each linefind the actual span for each line– I.E. use the intersection points we I.E. use the intersection points we

would have found and directly draw would have found and directly draw them (point coherency)them (point coherency)

+1

-1

Page 4: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

Scan-Line CoherencyScan-Line Coherency

Intersection points of polygon edges Intersection points of polygon edges with scan lines change little on a line by with scan lines change little on a line by line basisline basis

bxx

bxay

bxay

xi

ii

ii

11

11

y = i-1

y = ixi

xi-1

Page 5: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

Overview of Active Edge Overview of Active Edge TableTable

For each scan-line in a polygon For each scan-line in a polygon only certain edges need only certain edges need consideringconsidering

Keep an Keep an ACTIVE ACTIVE edge tableedge table– Update this edge table based upon Update this edge table based upon

the vertical extent of the edgesthe vertical extent of the edges From the AET extract the required From the AET extract the required

spansspans

Page 6: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

Setting UpSetting Up

““fix” edgesfix” edges– make sure y1<y2 for each (x1,y1) make sure y1<y2 for each (x1,y1)

(x2,y2)(x2,y2) Form an ETForm an ET

– Bucket sort all edges on minimum y Bucket sort all edges on minimum y valuevalue

– 1 bucket might contain several edges1 bucket might contain several edges– Each edge element contains Each edge element contains

(max Y, start X, X increment)(max Y, start X, X increment)

Page 7: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

Maintaining the AETMaintaining the AET

For each scan lineFor each scan line– Remove all edges whose y2 is equal Remove all edges whose y2 is equal

to current lineto current line– Update the x value for each Update the x value for each

remaining edgeremaining edge– Add all edges whose y1 is equal to Add all edges whose y1 is equal to

current linecurrent line

Page 8: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

Drawing the AETDrawing the AET

Sort the active edges on x intersectionSort the active edges on x intersection Pairs of edges are the spans we Pairs of edges are the spans we

requirerequire

Caveats (discussed in the notes)Caveats (discussed in the notes)– Don’t consider horizontal linesDon’t consider horizontal lines– Maximum vertices are not drawnMaximum vertices are not drawn– Plenty of special cases when polygons Plenty of special cases when polygons

share edgesshare edges

Page 9: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

ExampleExample

0 1 2 3 4 5 6 7 8

87

6

5

4

3

2

1

0

a b

cd

Page 10: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

SetupSetup

Edges are Edges are

Edge Table ContainsEdge Table Contains

Edge Label Coordinates y1 Structure a (1,1) to (4,7) 1 (7,1,0.5) b (7,2) to (4,7) 2 (7,7,-0.6) c (7,2) to (4,4) 2 (4,7,-1.5) d (1,1) to (4,4) 1 (4,1,1)

y1 Sequence of Edges1 (7,1,0.5), (4, 1, 1)2 (7,7,-0.6), (4, 7,-1.5)

Page 11: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

On Each LineOn Each Line

Line Active Edge Table Spans0 empty 1 (7,1,0.5), (4,1,1) 1 to 12 (7,1.5,0.5), (4,2,1), (7,7,-0.6), (4,7,-1.5) 1.5 to 2, 7 to 73 (7,2.0,0.5), (4,3,1), (4,5.5,-1.5), (7,6.4,-0.6) 2.0 to 3, 5.5 to 6.44 (7,2.5,0.5), (7,5.8,-0.6) 2.5 to 5.85 (7,3.0,0.5), (7,5.2,-0.6) 3.0 to 5.26 (7,3.5,0.5), (7,4.6,-0.6) 3.5 to 4.67 empty8 empty

Page 12: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

OverviewOverview

Scanning PolygonsScanning Polygons– Filling PixelsFilling Pixels

Gouraud and Phong ShadingGouraud and Phong Shading Visibility Culling Re-VisitedVisibility Culling Re-Visited

– Z-bufferZ-buffer Texture MappingTexture Mapping

Page 13: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

Gouraud ShadingGouraud Shading

Recall simple model for local diffuse Recall simple model for local diffuse reflectionreflection

Gouraud interpolates this colour Gouraud interpolates this colour down edges and across scan-lines down edges and across scan-lines in the same manner as we just did in the same manner as we just did for depthfor depth

N

i ipidaa lnIkIkI1

Page 14: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

Gouraud DetailsGouraud Details

ET now containsET now contains– (y2,x1,dx,z1,dz,r1,dr,g1,dg,b1,db)(y2,x1,dx,z1,dz,r1,dr,g1,dg,b1,db)

(we are running out of registers!)(we are running out of registers!)

ProblemsProblems– not constant colour on rotation of pointsnot constant colour on rotation of points– misses specular highlightsmisses specular highlights

12

12

xx

rrdr

Page 15: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

Phong ShadingPhong Shading

Include specular componentInclude specular component Interpolate normals across the Interpolate normals across the

scan-line instead of coloursscan-line instead of colours– Expensive square root to re-normaliseExpensive square root to re-normalise

Recaptures highlights in the centre Recaptures highlights in the centre of polygonsof polygons

N

i sm

idipiaa knhklnIIkI1

Page 16: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

OverviewOverview

Scanning PolygonsScanning Polygons– Filling PixelsFilling Pixels

Gouraud and Phong ShadingGouraud and Phong Shading Visibility Culling Re-VisitedVisibility Culling Re-Visited

– Z-bufferZ-buffer Texture MappingTexture Mapping

Page 17: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

Visibility ProblemVisibility Problem

Solution 1 (scan-line Solution 1 (scan-line visibility): visibility): – Test spans in the AET Test spans in the AET

and draw front-mostand draw front-most– Requires non-Requires non-

intersecting polygonsintersecting polygons Solution 2 (pixel Solution 2 (pixel

visibility):visibility):– Do the test for every Do the test for every

pixelpixel

Page 18: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

Z-bufferZ-buffer

Allocate a full screen-sized buffer that Allocate a full screen-sized buffer that stores depth (between near and far clip)stores depth (between near and far clip)

When scan-converting a polygonWhen scan-converting a polygon– For each pixel in each polygonFor each pixel in each polygon

If z < ZBUF[x,y] If z < ZBUF[x,y] – set CBUF[x,y] = polygon_colourset CBUF[x,y] = polygon_colour– set ZBUF[x,y] = zset ZBUF[x,y] = z

– At the beginning of each frameAt the beginning of each frame Initialise buffer to Z_MAX Initialise buffer to Z_MAX

Page 19: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

Z-buffer DetailsZ-buffer Details

Can do this in several waysCan do this in several ways 1D z-buffer re-used on each scan line1D z-buffer re-used on each scan line

– Process each polygon with separate AETProcess each polygon with separate AET– Use as adjunct to extended AET for Use as adjunct to extended AET for

multiple polygonsmultiple polygons ProblemsProblems

– Aliasing on depth (z-buffer tearing)Aliasing on depth (z-buffer tearing)

Page 20: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

Scanning Depth into the Z-Scanning Depth into the Z-bufferbuffer

Now we have to write a z-value for Now we have to write a z-value for each pointeach point– directly from plane equation (re-directly from plane equation (re-

calculate for each point)calculate for each point)– interpolate …interpolate …

Page 21: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

Interpolating DepthInterpolating Depth

Interpolate z along edges AND Interpolate z along edges AND interpolate between edges on each interpolate between edges on each scan-line (bi-linear interpolation)scan-line (bi-linear interpolation)

(X1,Y1,Z1)

(X2,Y2,Z2)

(XL,YL,ZL) (XR,YR,ZR)

lr

lr

xx

zzdz

Page 22: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

Zbuffer Fill ExampleZbuffer Fill Example

0 1 2 3 4 5 6 7 8

0

1

2

3

4

5

6

a

b

c7 General form of General form of

ETET– (y2,x1,dx/(y2,x1,dx/

dy,z1,dz/dy)dy,z1,dz/dy) ET[1] =ET[1] =

– ac (7,3,1/6,1,3/6)ac (7,3,1/6,1,3/6)– ab (4,3,4/3,1,1/3)ab (4,3,4/3,1,1/3)

ET[4] =ET[4] =– cb (7,7,-1,2,2/3)cb (7,7,-1,2,2/3)

a=(3,1,1) b=(7,4,2) c=(4,7,4)

Page 23: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

……Contents of AET Contents of AET

Scanline y=1Scanline y=1– ac (7,3,1/6,1,3/6)ac (7,3,1/6,1,3/6)– ab (4,3,4/3,1,1/3) zspans 1 to 1ab (4,3,4/3,1,1/3) zspans 1 to 1

y=2y=2– ac (7,3.166,1/6,1.5,3/6) ac (7,3.166,1/6,1.5,3/6) – ab (4,4.333,4/3, 1.333, 1/3) zspans 1.5 to ab (4,4.333,4/3, 1.333, 1/3) zspans 1.5 to

1.3331.333 y3y3

– ac (7,3.333,1/6,2.0,3/6)ac (7,3.333,1/6,2.0,3/6)– ab (4,5.666, 4/3, 1.666, 1/3) zspans 2 to 1.666ab (4,5.666, 4/3, 1.666, 1/3) zspans 2 to 1.666

Page 24: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

Trade-OffsTrade-Offs

Z-buffer can be inaccurate with few Z-buffer can be inaccurate with few bitsbits– really simple to implement though!really simple to implement though!

Scan-line AET good for large polygonsScan-line AET good for large polygons– good coherency across linesgood coherency across lines– requires non-intersecting polygonsrequires non-intersecting polygons

Z-buffer good for small, sparse Z-buffer good for small, sparse polygonspolygons– AET more time consuming to maintainAET more time consuming to maintain

Page 25: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

OverviewOverview

Scanning PolygonsScanning Polygons– Filling PixelsFilling Pixels

Gouraud and Phong ShadingGouraud and Phong Shading Visibility Culling Re-VisitedVisibility Culling Re-Visited

– Z-bufferZ-buffer Texture MappingTexture Mapping

Page 26: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

Texture MappingTexture Mapping

Why?Why?– Approximation for surface colouringApproximation for surface colouring– Efficient packing of flat detailEfficient packing of flat detail

Standard texture mapping Standard texture mapping modifies modifies diffuse diffuse mappingmapping– Pasting a picture onto the polygonPasting a picture onto the polygon

Page 27: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

Inverse MappingInverse Mapping

Each vertex is associated with a Each vertex is associated with a point on an image (u,v)point on an image (u,v)

Page 28: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

Quick and Dirty SolutionQuick and Dirty Solution

Yet more parameters in the ET!Yet more parameters in the ET! Bilinear interpolation of u&v down Bilinear interpolation of u&v down

scene edges, and across scan-linesscene edges, and across scan-lines

Works, but is very uglyWorks, but is very ugly– Does not consider fore-shortening of Does not consider fore-shortening of

image in depthimage in depth

Page 29: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

The ProblemThe Problem

Same problem exists with phong and gouraud Same problem exists with phong and gouraud shading, but it much less noticeableshading, but it much less noticeable

z correct z incorrect

Page 30: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

The SolutionThe Solution

Need to compensate for fore-Need to compensate for fore-shortening by interpolating over 1/zshortening by interpolating over 1/z

Interpolate (u’,v’,q) where q=1/zInterpolate (u’,v’,q) where q=1/z– At vertices we know (uAt vertices we know (u11,v,v11,z,z11), (u), (u22,v,v22,z,z22))

– Interpolate between (uInterpolate between (u1*1*zz11,v,v1 *1 *zz11,1/z,1/z11) and ) and (u(u2*2*zz22,v,v2 *2 *zz22,1/z,1/z22) to get (u’,v’,q)) to get (u’,v’,q)

– Restore u,v, by dividing u’ and v’ by qRestore u,v, by dividing u’ and v’ by q

Page 31: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

Sanity CheckSanity Check

p0=(10,5,10,0,0)

p1=(15,15,20,0,1) (x, y, z,u,v)

scanlines

Y=5

Y=10

Y=15

At p0 (u’,v’,q) = At p0 (u’,v’,q) = (0,0.1,0.1)(0,0.1,0.1)

At p1 (u’,v’,q) = At p1 (u’,v’,q) = (0,0,0.05)(0,0,0.05)

On scanline 5 On scanline 5 (u’,v’,q) = (u’,v’,q) = (0,0.05,0.075) (0,0.05,0.075)

(u,v) = 0.6666(u,v) = 0.6666

z = 1/0.075 = z = 1/0.075 = 13.3313.33

Page 32: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

Minor IssuesMinor Issues

Now have two divides per pixel!Now have two divides per pixel! Some optimisationsSome optimisations

– only do the divide at end of the spans only do the divide at end of the spans and interpolate across spansand interpolate across spans

– or only do the divide every n pixelsor only do the divide every n pixels Remaining problemRemaining problem

– we have not touched upon how to clip we have not touched upon how to clip u,v values in 3D or 2D!u,v values in 3D or 2D!

Page 33: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

Major IssuesMajor Issues

Picking your pixel! Picking your pixel!

image

Page 34: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

SamplingSampling

A pixel maps to a non-rectangular A pixel maps to a non-rectangular regionregion

Usually only perform map on Usually only perform map on centre of pixelcentre of pixel

Still have a problem of over-Still have a problem of over-sampling (same texel maps to sampling (same texel maps to several pixels) or under-sampling several pixels) or under-sampling the image (pixels only sparsely the image (pixels only sparsely sample the texels)sample the texels)

Page 35: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

FilteringFiltering

Nearest Nearest neighbourneighbour

Pick pixel with Pick pixel with closest centreclosest centre

BilinearBilinear

Weighted average Weighted average based on distance based on distance to pixel centreto pixel centre

Page 36: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

FilteringFiltering

Bilinear filtering solves (partially) Bilinear filtering solves (partially) the oversampling problem since it the oversampling problem since it provides smooth shading between provides smooth shading between pixelspixels

scanline

Page 37: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

Mip-MappingMip-Mapping

When undersampling we use mip-When undersampling we use mip-mappingmapping

Resample image at lower Resample image at lower resolutionresolution

Create a “pyramid” of textures.Create a “pyramid” of textures. Interpolate texture between two Interpolate texture between two

adjacent layersadjacent layers

Page 38: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

Texture PyramidTexture Pyramid

128x12864x64

32x321x1...

Page 39: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

SamplingSampling

Choose two layers based on texel Choose two layers based on texel spanspan– Choice is made selecting the two Choice is made selecting the two

levels where the du and dv for dx and levels where the du and dv for dx and dy are closest to onedy are closest to one

Interpolate between four pixels in Interpolate between four pixels in higher layer and one in lower layerhigher layer and one in lower layer

Page 40: Introduction to 3D Graphics Lecture 6: Real-Time Rendering Anthony Steed University College London

ConclusionConclusion

A real-time pipeline that is fairly A real-time pipeline that is fairly close to siliconclose to silicon

Note the liberties we have taken:Note the liberties we have taken:– Screen space approximationsScreen space approximations– Interpolation schemesInterpolation schemes– Paucity of modelling presentationsPaucity of modelling presentations

The future is probably not The future is probably not polygonal!polygonal!