24

2 3D Viewing Process 3D viewing process MC Model Space Model Transformation Model Transformation…

Embed Size (px)

DESCRIPTION

Simplified View The Data Flow: 3D Polygons (+Colors, Lights, Normals, Texture Coordinates…etc.)  2D Polygons  2D Pixels (I.e., Output Images) Transform (& Lighting) Rasterization

Citation preview

Page 1: 2 3D Viewing Process  3D viewing process MC Model Space Model Transformation Model Transformation…
Page 2: 2 3D Viewing Process  3D viewing process MC Model Space Model Transformation Model Transformation…

2

3D Viewing Process

3D viewing process

MC

ModelSpace

ModelTransformation

WC

WorldSpace

ViewingTransformation

VC

ViewSpace

ViewportTransformation

DC

DisplaySpace

Normalization & Clipping

NC

NormalizeSpace

ProjectionTransformation

PC

ProjectionSpace

Page 3: 2 3D Viewing Process  3D viewing process MC Model Space Model Transformation Model Transformation…

Simplified View

• The Data Flow:3D Polygons (+Colors, Lights, Normals,

Texture Coordinates…etc.)2D Polygons2D Pixels (I.e., Output Images)

Transform(& Lighting)

Rasterization

Page 4: 2 3D Viewing Process  3D viewing process MC Model Space Model Transformation Model Transformation…

Attributes VaryingTransform(& Lighting)

Rasterization

vertex attributes:Color,Normal,Tex_coord,…etc.

vertexshader

fragmentshader

varying variables:interpolated from vertices to internal pixels

Page 5: 2 3D Viewing Process  3D viewing process MC Model Space Model Transformation Model Transformation…

Key Questions• Q1: What screen pixels are covered by

the triangles?• Q2: How to interpolate the attributes?

Page 6: 2 3D Viewing Process  3D viewing process MC Model Space Model Transformation Model Transformation…

Rasterization

Page 7: 2 3D Viewing Process  3D viewing process MC Model Space Model Transformation Model Transformation…

Triangles Only• We will discuss the rasterization of

triangles only.• Why?

– Polygon can be decomposed into triangles.– A triangle is always convex.– Results in algorithms that are more

hardware friendly.

Page 8: 2 3D Viewing Process  3D viewing process MC Model Space Model Transformation Model Transformation…

Being Hardware Friendly• [Angel 5e] Section 7.11.3 :

– Intersect scan lines with polygon edges.

– Sort the intersections, first by scan lines, then by order of x on each scan line.

– It works for polygons in general, not just in triangles.

– O(n log n) complexity feasible in software implementation only (i.e., not hardware friendly)

Page 9: 2 3D Viewing Process  3D viewing process MC Model Space Model Transformation Model Transformation…

Using Edge Equations• Find the edge equations

for P1-P2, P2-P3, P1-P3• For each pixel, test which

sides of the edges it is at.• Q: Do we need to test

every pixel on the screen?

P1

P2

P3

ege defined byAx+By+c=0

Page 10: 2 3D Viewing Process  3D viewing process MC Model Space Model Transformation Model Transformation…

Example

P1

P2

P3

ege defined byAx+By+C=0

Page 11: 2 3D Viewing Process  3D viewing process MC Model Space Model Transformation Model Transformation…

Example• Now we have P1-P3 defined

by 2x+y-4=0• To test if a point (u,v) is inside

the triangle, we calculate 2(u)+(v)-4. For example, (0,0) leads to 2(0)+(0)-4=-4

• So the negative side is inside.• Use P2 to decide which side is

inside.• Negate A,B,C if you prefer

the positive side

P1

P2

P3

Ax+By+C=0

Page 12: 2 3D Viewing Process  3D viewing process MC Model Space Model Transformation Model Transformation…

Color and Z• Now we know which pixels must be

drawn. The next step is to find their colors and Z’s.

• Gouraud shading: linear interpolation of the vertex colors.

• Isn’t it straightforward?– Interpolate along the edges. (Y direction)– Then interpolate along the span. (X

direction)

Page 13: 2 3D Viewing Process  3D viewing process MC Model Space Model Transformation Model Transformation…

13

Attribute Derivation• Color Interpolation

(255, 0, 0)

(0, 255, 0)(0, 0, 255)

(255, 0, 0)

(0, 0, 0) (0, 0, 0)

Red (0, 0, 0)

(0, 0, 0) (0, 255, 0)Green

(0, 0, 255)

(0, 0, 0)

(0, 0, 0)

Blue

Page 14: 2 3D Viewing Process  3D viewing process MC Model Space Model Transformation Model Transformation…

Interpolation in World Space vs Screen Space

• P1=(x1, y1, z1, c1); P2=(x2, y2, z2, c2); P3=(x3, y3, z3, c3) in world space

• If (x3, y3) = (1-t)(x1, y1) + t(x2, y2) then z3=(1-t)z1+t z2; c3=(1-t)c1+t c2

P1=(x1,y1,z1)

P3=(x3,y3,z3)

P2=(x2,y2,z2)

Page 15: 2 3D Viewing Process  3D viewing process MC Model Space Model Transformation Model Transformation…

Interpolation in World Space vs Screen Space

• But, remember that we are interpolating on screen coordinates (x’, y’):

1'''

zyx

qpnmlkjihgfedcba

wwzwywx

P’2

P’1

P2

P1P3

x’

y’

Page 16: 2 3D Viewing Process  3D viewing process MC Model Space Model Transformation Model Transformation…

• Let P’1=(x’1, y’1); P’2=(x’2, y’2) and P’3=(x’3, y’3)= (1-s)(x’1, y’1) + s(x’2, y’2)

• Does s=t? If not, should we compute z3 and c3 by s or t?

• Express s in t (or vice versa), we get something like:

• So, if we interpolate z on screen space, we get the z of some other point on the line

• This is OK for Z’s, but may be a problem for texture coordinates (topic of another lecture)

)( 121

2

wwtwwts

Page 17: 2 3D Viewing Process  3D viewing process MC Model Space Model Transformation Model Transformation…

InterpolationOpenGL uses interpolation to find proper texels

from specified texture coordinates

Page 18: 2 3D Viewing Process  3D viewing process MC Model Space Model Transformation Model Transformation…

Perspectively Correct Interpolation

• The previous slide is not “perspectively correct”

• Consider the interpolation along the vertical (Y) direction. Should the checkers spaced equally?

• Use the s to t conversion shown earlier, then you get the perspectively correct interpolation.

Page 19: 2 3D Viewing Process  3D viewing process MC Model Space Model Transformation Model Transformation…

Appendix

Page 20: 2 3D Viewing Process  3D viewing process MC Model Space Model Transformation Model Transformation…

Derivation of s and t• Two end points P1=(x1, y1, z1) and

P2=(x2, y2, z2). Let P3=(1-t)P1+(t)P2

• After projection, P1, P2, P3 are projected to (x’1, y’1), (x’2, y’2), (x’3, y’3) in screen coordinates. Let (x’3, y’3)=(1-s)(x’1, y’1) + s(x’2, y’2).

Page 21: 2 3D Viewing Process  3D viewing process MC Model Space Model Transformation Model Transformation…

• (x’1, y’1), (x’2, y’2), (x’3, y’3) are obtained from P1, P2, P3 by:

)

11

)1((

1'''

1'''

,

1'''

2

2

2

1

1

1

3

3

3

3

33

33

33

2

2

2

2

22

22

22

1

1

1

1

11

11

11

zyx

tzyx

tMzyx

M

wwzwywx

zyx

M

wwzwywx

zyx

M

wwzwywx

Page 22: 2 3D Viewing Process  3D viewing process MC Model Space Model Transformation Model Transformation…

Since

We have:

2

22

22

22

2

2

2

1

11

11

11

1

1

1

'''

1

,'''

1 wwzwywx

zyx

M

wwzwywx

zyx

M

2

22

22

22

1

11

11

11

2

2

2

1

1

1

3

33

33

33

'''

'''

)1(

11

)1('''

wwzwywx

t

wwzwywx

t

zyx

Mtzyx

Mt

wwzwywx

Page 23: 2 3D Viewing Process  3D viewing process MC Model Space Model Transformation Model Transformation…

When P3 is projected to the screen, we get (x’3, y’3) by dividing by w, so:

But remember that (x’3, y’3)=(1-s)(x’1, y’1) + s(x’2, y’2)

Looking at x coordinate, we have

))1(

'')1(,)1(

'')1(()','(21

2211

21

221133 wtwt

wytwytwtwtwxtwxtyx

21

221121 )1(

'')1()1(wtwtwxtwxtxsxs

Page 24: 2 3D Viewing Process  3D viewing process MC Model Space Model Transformation Model Transformation…

We may rewrite s in terms of t, w1, w2, x’1, and x’2.

In fact,

or conversely

Surprisingly, x’1 and x’2 disappear.

)()1( 121

2

21

2

wwtwwt

wtwtwts

221

1

21

1

)()1( wwwsws

wswswst