480 likes | 597 Views
CIS 441/541: Introduction to Computer Graphics Lecture 4: Interpolation & Zbuffer ( redux ) + basic transformations. Hank Childs, University of Oregon. April 12th, 2013. Outline. Interpolation along a triangle The Z-Buffer: How to resolve when triangles overlap on the screen
E N D
CIS 441/541: Introduction to Computer GraphicsLecture 4: Interpolation & Zbuffer (redux) + basic transformations Hank Childs, University of Oregon April 12th, 2013
Outline • Interpolation along a triangle • The Z-Buffer: How to resolve when triangles overlap on the screen • Parallel Rendering • Basic Transformations
Linear Interpolation • Assume • F(v1) = A • F(v2) = B • General equation to interpolate: • F(v) = A + ((v-v1)/(v2-v1))*(B-A) • You can think of ((v-v1)/(v2-v1)) as the proportion of v between v1 and v2. • Think about v=v1 and v=v2 v2-v1 v1 v v2 v-v1
It was clearly a mistake for me to try to leverage the existing knowledge of a line equation. • Regardless… • Consider two points on a line: • (v1, A) • (v2, B) • Then the slope (m) is: • (B-A)/(v2-v1) • And the intercept (b) is: • b = A-v1*m = A-v1*(B-A)/(v2-v1)
Consider the y-value of some point v on the line: • y = mx+b • y = m*v+b • y = (B-A)/(v2-v1)*v + A-v1*(B-A)/(v2-v1) • y = (B-A)/(v2-v1)*(v-v1) + A • = A + ((v-v1)/(v2-v1))*(B-A) • F(v) = A + ((v-v1)/(v2-v1))*(B-A)
Consider a single scalar field defined on a triangle. Y-axis Y=1 Y=0.5 X-axis Y=0 X=1.5 X=2 X=0 X=1 X=0.5
Consider a single scalar field defined on a triangle. Y-axis V2 F(V2) = 2 Y=1 Y=0.5 V1 V3 F(V1) = 10 X-axis F(V3) = -2 Y=0 X=1.5 X=2 X=0 X=1 X=0.5
What is F(V4)? Y-axis V2 F(V2) = 2 Y=1 Y=0.5 V4, at (0.5, 0.25) V1 V3 F(V1) = 10 X-axis F(V3) = -2 Y=0 X=1.5 X=2 X=0 X=1 X=0.5
What is F(V4)? Y-axis V2 F(V2) = 2 Y=1 Y=0.5 V6 V5 V4, at (0.5, 0.25) V1 V3 F(V1) = 10 X-axis F(V3) = -2 Y=0 X=1.5 X=2 X=0 X=1 X=0.5
Steps to follow: • Calculate V5, the left intercept for Y=0.25 • Calculate V6, the right intercept for Y=0.25 • Calculate V4, which is between V5 and V6
What is the X-location of V6? Y-axis V2 F(V2) = 2 Y=1 • F(v1) = A F(1) = 1 • F(v2) = B F(2) = 0 • F(v) = A + ((v-v1)/(v2-v1))*(B-A): • F(v) = 0.25, find v • 0.25 = 1 + ((v-1)/(2-1)*(0-1) • = 1 + (v-1)*(-1) • 0.25 = 2 - v • v = 1.75 Y=0.5 V6 V5 V4, at (0.5, 0.25) V1 V3 F(V1) = 10 X-axis F(V3) = -2 Y=0 X=1.5 X=2 X=0 X=1 X=0.5
What is the F-value of V6? Y-axis V2 F(V2) = 2 Y=1 • F(v1) = A F(1) = 2 • F(v2) = B F(2) = -2 • F(v) = A + ((v-v1)/(v2-v1))*(B-A): • v = 1.75, find F(v) • F(v) = 2 + ((1.75-1)/(2-1)*(-2 - +2) • = 2 + (.75)*(-4) • = 2 - 3 • = -1 Y=0.5 V6 V5 V4, at (0.5, 0.25) V1 V3 F(V1) = 10 X-axis F(V3) = -2 Y=0 X=1.5 X=2 X=0 X=1 X=0.5
What is the X-location of V5? Y-axis V2 F(V2) = 2 Y=1 • F(v1) = A F(0) = 0 • F(v2) = B F(1) = 1 • F(v) = A + ((v-v1)/(v2-v1))*(B-A): • F(v) = 0.25, find v • 0.25 = 0 + ((v-0)/(1-0)*(1-0) • v = 0.25 Y=0.5 V6 V5 V4, at (0.5, 0.25) V1 V3 F(V1) = 10 X-axis F(V3) = -2 Y=0 X=1.5 X=2 X=0 X=1 X=0.5
What is the F-value of V5? Y-axis V2 F(V2) = 2 Y=1 • F(v1) = A F(0) = 10 • F(v2) = B F(1) = 2 • F(v) = A + ((v-v1)/(v2-v1))*(B-A): • v = 0.25, find F(v) • F(v) = 10 + ((0.25-0)/(1-0))*(2-10) • = 10 + 0.25*-8 = 10 -2 = 8 Y=0.5 V6 V5 V4, at (0.5, 0.25) V1 V3 F(V1) = 10 X-axis F(V3) = -2 Y=0 X=1.5 X=2 X=0 X=1 X=0.5
What is the F-value of V5? Y-axis V2 Y=1 L(V6) = (1.75, 0.25) F(V6) = -1 L(V5) = (0.25, 0.25) F(V5) = 8 Y=0.5 V6 V5 V4, at (0.5, 0.25) V1 V3 F(V1) = 10 X-axis F(V3) = -2 Y=0 X=1.5 X=2 X=0 X=1 X=0.5
What is the F-value of V5? • F(v1) = A F(0.25) = 8 • F(v2) = B F(1.75) = -1 • F(v) = A + ((v-v1)/(v2-v1))*(B-A): • v = 0.5, find F(v) • F(v) = 8 + ((0.5-0.25)/(1.75-0.25))*(-1-8) • = 8 + (0.25/1.5)*9 = 8-1.5 = 6.5 Y-axis V2 Y=1 L(V6) = (1.75, 0.25) F(V6) = -1 L(V5) = (0.25, 0.25) F(V5) = 8 Y=0.5 V6 V5 V4, at (0.5, 0.25) V1 V3 F(V1) = 10 X-axis F(V3) = -2 Y=0 X=1.5 X=2 X=0 X=1 X=0.5
Consider 4 triangles with constant Z values Z=-0.35 Z=-0.5 Z=-0.8 Z=-0.65
But there is a problem… (0, 1.5, -0.4) (0, 1, -0.4) (-1, -1, -0.3) (1, -1, -0.5) (-2, -1.5, -0.5) (2, -1.5, -0.3)
Outline • Interpolation along a triangle • The Z-Buffer: How to resolve when triangles overlap on the screen • Parallel Rendering • Basic Transformations
The Z-Buffer Algorithm: Data Structure • Existing: for every pixel, we store 3 bytes: • Red channel, green channel, blue channel • New: for every pixel, we store a floating point value: • Depth buffer • Now 7 bytes per pixel (*) • (*): 8 with RGBA
The Z-Buffer Algorithm:Initialization • Existing: • For each pixel, for each R/G/B, set to 0. • New: • For each pixel, for each depth value, set to -1. • Valid depth values go from -1 (back) to 0 (front) • This is partly convention and partly because it “makes the math easy” when doing transformations.
Scanline algorithm • Determine rows of pixels triangles can possibly intersect • Call them rowMin to rowMax • rowMin: ceiling of smallest Y value • rowMax: floor of biggest Y value • For r in [rowMin rowMax] ; do • Find end points of r intersected with triangle • Call them leftEnd and rightEnd • For c in [ceiling(leftEnd) floor(rightEnd) ] ; do • ImageColor(r, c) triangle color
Scanline algorithm w/ Z-Buffer • Determine rows of pixels triangles can possibly intersect • Call them rowMin to rowMax • rowMin: ceiling of smallest Y value • rowMax: floor of biggest Y value • For r in [rowMin rowMax] ; do • Find end points of r intersected with triangle • Call them leftEnd and rightEnd • Interpolate z(leftEnd) and z(rightEnd) from triangle vertices • For c in [ceiling(leftEnd) floor(rightEnd) ] ; do • Interpolate z(r,c) from z(leftEnd) and z(rightEnd) • If (z(r,c) > depthBuffer(r,c)) • ImageColor(r, c) triangle color • depthBuffer(r,c) = z(r,c)
The Z-Buffer Algorithm:Example (0,12) (12,12) (2.5,10.5, -0.25) Y=5 (2.5,2.5, -0.5) (10.5,2.5, -1) (0,0) (12,0)
The Z-Buffer Algorithm:Example (0,12) (12,12) (6.3, 8.5, -0.75) Y=5 (1.3,3.5, -1) (11.3, 3.5, -0.5) (0,0) (12,0)
Interpolation and Triangles • We introduced the notion of interpolating a field on a triangle • We used the interpolation in two settings: • 1) to interpolate colors (discussed Weds) • 2) to interpolate depths for z-buffer algorithm • Project 1D will be discussed on Friday, but… • You will be adding color interpolation and the z-buffer algorithm to your programs.
Outline • Interpolation along a triangle • The Z-Buffer: How to resolve when triangles overlap on the screen • Parallel Rendering • Basic Transformations
Parallel Rendering • Three forms: • Sort first • Sort middle • Sort last • We know enough to discuss “sort last”
Sort last rendering • Assumptions: • So many triangles that you need multiple computers to render them • The triangles are partitioned over the computers CPU 1’s image CPU 0’s image Final composited image (done w/ z-buffer)
Project #1D (5%), Due Fri 4/19 • Goal: interpolation of color and zbuffer • Extend your project1C code • File proj1d_geometry.vtk available on web (1.4MB) • File “reader1d.cxx” has code to read triangles from file. • No Cmake, project1d.cxx
Changes to data structures class Triangle { public: double X[3], Y[3], Z[3]; double colors[3][3]; }; reader1d.cxx will not compile until you make these changes
Outline • Interpolation along a triangle • The Z-Buffer: How to resolve when triangles overlap on the screen • Parallel Rendering • Basic Transformations
Starting easy … two-dimensional MP = P’ Matrix M transforms point P to make new point P’. (a b) (x) (a*x + b*y) (c d) X (y) = (c*x + d*y) M takes point (x,y) to point (a*x+b*y, c*x+d*y)
MP = P’ Matrix M transforms point P to make new point P. (1 0) (x) (x) (0 1) X (y) = (y) Identity Matrix
MP = P’ Matrix M transforms point P to make new point P. (2x,y) (x,y) (2 0) (x) (2x) (0 1) X (y) = (y) Scale in X, not in Y
MP = P’ Matrix M transforms point P to make new point P. (x,y) (s 0) (x) (sx) (0 t) X (y) = (ty) (sx,ty) Scale in both dimensions
MP = P’ Matrix M transforms point P to make new point P. (x,y) (0 1) (x) (y) (1 0) X (y) = (x) (y,x) Switch X and Y
MP = P’ Matrix M transforms point P to make new point P. (x,y) (0 1) (x) (y) (-1 0) X (y) = (-x) (y,-x) Rotate 90 degrees clockwise
MP = P’ Matrix M transforms point P to make new point P. (x,y) (-y,x) (0 -1) (x) (-y) (1 0) X (y) = (x) Rotate 90 degrees counter-clockwise
MP = P’ Matrix M transforms point P to make new point P. (x’, y’) (x,y) a (cos(a) sin(a)) (x) (cos(a)*x+sin(a)*y)) (-sin(a) cos(a)) X (y) = (-sin(a)*x+cos(a)*y) Rotate “a” degrees counter-clockwise
Combining transformations • How do we rotate by 90 degrees clockwise and then scale X by 2? • Answer: multiply by matrix that multiplies by 90 degrees clockwise, then multiple by matrix that scales X by 2. • But can we do this efficiently? (0 1) (2 0) (0 1) (-1 0) X (0 1) = (-2 0)
Reminder: matrix multiplication (a b) (ef) (a*e+b*ga*f+b*h) (cd) X (gh) = (c*e+d*gc*f+d*h)
Combining transformations • How do we rotate by 90 degrees clockwise and then scale X by 2? • Answer: multiply by matrix that rotates by 90 degrees clockwise, then multiply by matrix that scales X by 2. • But can we do this efficiently? (0 1) (2 0) (0 1) (-1 0) X (0 1) = (-2 0)
Combining transformations • How do we scale X by 2 and then rotate by 90 degrees clockwise? • Answer: multiply by matrix that scales X by 2, then multiply by matrix that rotates 90 degrees clockwise. (2 0) (0 1) (0 2) (01) X (-1 0) = (-1 0) (0 1) (2 0) (0 1) (-1 0) X (0 1) = (-2 0) Multiply then scale Order matters!!
Translations • Translation is harder: (a) (c) (a+c) (b) +(d) = (b+d) But this doesn’t fit our nice matrix multiply model… What to do??
Homogeneous Coordinates (1 0 0) (x) (x) (0 1 0) X (y) =(y) (0 0 1) (1) = (1) Add an extra dimension. A math trick … don’t overthink it.
Homogeneous Coordinates (1 0 dx) (x) (x+dx) (0 1 dy) X (y)=(y+dy) (0 0 1) (1) (1) Translation We can now fit translation into our matrix multiplication system.