Polygon Shading Polygon Shading Assigning color to a

  • Slides: 38
Download presentation
Polygon Shading

Polygon Shading

Polygon Shading • Assigning color to a shape to make graphical scenes look realistic,

Polygon Shading • Assigning color to a shape to make graphical scenes look realistic, or artistic, or whatever effect we’re attempting to achieve • But first we must digress…

Graphical Drawing Primitives • In previous weeks we looked at pixels, straight lines, and

Graphical Drawing Primitives • In previous weeks we looked at pixels, straight lines, and curves • We learned that we could draw straight lines by specifying neighboring pixels using simple integer arithmetic operations • The resultant lines were 8 -connected (had no gaps)

Polygons • If we draw a series of connected lines where the end point

Polygons • If we draw a series of connected lines where the end point of one is the start point of the next the end point of the last line is the start point of the first line then we can draw a polygon 2 1 3 6 5 4

Polygons • The problem we come up against is that generalize polygons are too

Polygons • The problem we come up against is that generalize polygons are too difficult to draw efficiently • So, we restrict them to quadrilaterals 2 1 3 4

Polygons • Quadrilaterals are better than generalized polygons but they still have a problem

Polygons • Quadrilaterals are better than generalized polygons but they still have a problem • Quadrilaterals may be concave thus making them difficult to handle in certain circumstances 2 3 1 4

Polygons • Concave quadrilaterals are difficult to handle when we consider scanline algorithms •

Polygons • Concave quadrilaterals are difficult to handle when we consider scanline algorithms • Scanline algorithms are algorithms that perform their task by scanning pixels in a top-to-bottom, left-to-right ordering • These algorithms tend to be very fast due to the way memory is arranged

Scanlines • Top-bottom, left-right processing • The “kink” makes processing complicated in that additional

Scanlines • Top-bottom, left-right processing • The “kink” makes processing complicated in that additional calculations must be made partially through 2 of the scan 3 1 4

Quadrilaterals • Another issue with quadrilaterals is that we want to store shading information

Quadrilaterals • Another issue with quadrilaterals is that we want to store shading information in the vertices and usage of that information to perform the shading operation is difficult RGB = (255, 0, 0) 2 RGB = (? , ? ) RGB = (0, 255, 0) 3 RGB = (128, 255, 0) 1 4 RGB = (0, 255)

Triangles • So, we choose triangles as our area-based drawing primitive – Always convex

Triangles • So, we choose triangles as our area-based drawing primitive – Always convex 2 1 3 – Easy to shade using a scanline algorithm, as we’ll see

Triangles • Parametric representation of triangles P 1 u = P 1 – P

Triangles • Parametric representation of triangles P 1 u = P 1 – P 0 a P w b P 0 v = P 2 – P 0 P 2

Barycentric Coordinates • Through a bunch of vector math that I won’t burden you

Barycentric Coordinates • Through a bunch of vector math that I won’t burden you with define: P 1 u = P 1 – P 0 s=b/c t=a/c • Where a and b are the areas of their respective triangles and c is the area of the entire triangle a P w P 0 b v = P 2 – P 0 P 2

Barycentric Coordinates • Then any point within the triangle can be specified by the

Barycentric Coordinates • Then any point within the triangle can be specified by the formula P = P 0 + su + tv • or w = su + tv • The triangle areas can be computed through vector manipulations too (cross products) • So, what is all this used for w. r. t. shading?

Shading Triangles • To shade the interior of a triangle we need to access

Shading Triangles • To shade the interior of a triangle we need to access each pixel within the interior – We can do this using barycentric coordinates although, as we’ll see, we only do this indirectly • Once we can do this we can assign a color to each pixel

Shading • There are two basic methods for assigning color to the interior pixels

Shading • There are two basic methods for assigning color to the interior pixels of an object (shading the object) • Constant shading (with 2 sub-methods) – Per object – Per triangle • Pixel based shading – Per vertex

Per-object Shading • Recall the quadrilateral (or any general polygon with more than 3

Per-object Shading • Recall the quadrilateral (or any general polygon with more than 3 sides) which we decided not to use

Per-object Shading • To shade it in a per-object fashion we merely assign every

Per-object Shading • To shade it in a per-object fashion we merely assign every pixel within the polygon the same RGB color value

Per-object Shading • Efficient scan line algorithms for such shading are difficult, as previously

Per-object Shading • Efficient scan line algorithms for such shading are difficult, as previously discussed • Furthermore, this method does not produce very satisfying results [if photo-realistic graphics is the goal] and therefore is rarely used • The only advantage is that object colors can be specified off-line. Of course, this may also be a disadvantage. Why?

Per-triangle Shading • In this method we do first divide the polygon into triangles

Per-triangle Shading • In this method we do first divide the polygon into triangles • Various techniques exist for performing this operation (e. g. Delauney triangulation) • It is generally performed off line during the modeling of the objects

Per-triangle Shading • Then, once we have the triangles we can shade each one

Per-triangle Shading • Then, once we have the triangles we can shade each one individually

Per-triangle Shading • Efficient scan line algorithms for such shading are available, as we

Per-triangle Shading • Efficient scan line algorithms for such shading are available, as we will see – At very least the barycentric coordinates will help us • Results are better than with per-object shading but still not great • Where to store the color for each triangle may be problematic in that most efficient drawing systems allow triangle vertices to be reused for neighboring triangle specification

Triangle Vertices • Rather than specify 9 vertices for these 3 triangles • We

Triangle Vertices • Rather than specify 9 vertices for these 3 triangles • We specify the first 3 then let the next triangle share 2 • Using a prescribed order we get a triangle strip • Long strips save a lot of resources (time, space)

Triangle Vertices and Color • But, given a triangle strip, in which vertex do

Triangle Vertices and Color • But, given a triangle strip, in which vertex do you store the color for a given triangle when using per-triangle shading? • Specify a convention and stick to it – e. g. the last vertex specified

Vertex Color • What exactly does the vertex color represent and how is it

Vertex Color • What exactly does the vertex color represent and how is it represented? • Use of triangles to represent a curved (or any) 3 D surface is called tessellation and results in a faceted model of the surface – Basically, this saves memory and processing time • Each surface will have at every triangle (point) – an orientation – a reflectance model – a natural color – Possibly other attributes

Vertex Color • Perhaps the most important of these attributes is the orientation •

Vertex Color • Perhaps the most important of these attributes is the orientation • The orientation is a unit vector that is normal (perpendicular) to the surface at the given point • The normals are used to determine the displayed color of an object after the lighting and viewer parameters have been defined (at run time)

Surface Normals • To get the surface normal at the triangle vertices we average

Surface Normals • To get the surface normal at the triangle vertices we average the normals of the triangles that meet at the vertex • This method of assigning vertex normals is the basis for Gouraud Shading (Henri Gouraud)

Per-vertex (Gouraud) Shading • Points on a given triangle are assigned a color that

Per-vertex (Gouraud) Shading • Points on a given triangle are assigned a color that is a linear interpolation of the three vertex colors (as determined by their normals, color, surface material, the lighting model, and the viewer model) • Gouraud does not care where the vertex colors come from, just that they are there

Per-vertex (Gouraud) Shading • Starting again with the parametric model of the triangle… P

Per-vertex (Gouraud) Shading • Starting again with the parametric model of the triangle… P 1 u = P 1 – P 0 a P w P 0 b v = P 2 – P 0 P 2

Per-vertex (Gouraud) Shading • We can define the color at a given pixel within

Per-vertex (Gouraud) Shading • We can define the color at a given pixel within the triangle as a linear combination of the sizes of the sub-triangles a and b P 1 u = P 1 – P 0 • Color = s. CP 1 + t. CP 2 + (1 -s-t)CP 0 s=b/c t=a/c a P w P 0 b v = P 2 – P 0 P 2

Per-vertex (Gouraud) Shading • So all we have to do is loop through all

Per-vertex (Gouraud) Shading • So all we have to do is loop through all combinations of the sizes of triangles a and b! • Not really, it’s actually much easier than that • Recall that Gouraud is nothing more than a linear interpolation of the 3 vertex colors • Thus, all we have to do is interpolate the colors along the three edges – You can use the Bresenham algorithm for this • Then interpolate colors along scan lines

Per-vertex (Gouraud) Shading P 1(R 1, G 1, B 1) Interpolate between two end

Per-vertex (Gouraud) Shading P 1(R 1, G 1, B 1) Interpolate between two end points of line segment P 2(R 2, G 2, B 2) P 0(R 0, G 0, B 0)

Triangle Shading Per-triangle (constant) shading Per-vertex (Gouraud) shading

Triangle Shading Per-triangle (constant) shading Per-vertex (Gouraud) shading

Mach Bands • Linear interpolation (Gouraud) provides realistic looking smooth surfaces most of the

Mach Bands • Linear interpolation (Gouraud) provides realistic looking smooth surfaces most of the time • But, Gouraud shading is not always good enough to fool the human visual system • Due to the way cones in the eye are connected to the optic nerve, the human visual system has great sensitivity to small intensity changes – This is due to a property called lateral inhibition • It is especially apparent at sharp intensity changes and changes in intensity gradient direction

Mach Bands • The human visual system tends to “overshoot” intensities at transition points

Mach Bands • The human visual system tends to “overshoot” intensities at transition points • Although this quadrilateral is made of two triangles that share an edge, we still perceive a bright line Mach Band effect

Mach Bands • The answer to this phenomenon is to invent algorithms that provide

Mach Bands • The answer to this phenomenon is to invent algorithms that provide even more smoothness in intensity transitions… • …which results in greater computational cost

Phong Shading • Phong took Gouraud’s ideas a step further • Rather than using

Phong Shading • Phong took Gouraud’s ideas a step further • Rather than using the 3 vertex normals to define 3 vertex colors then interpolate the interior points of the triangle based on those colors, he interpolates the surface normals at each point then computes the color • This results in – Reduced [but not eliminated] Mach Banding – More accurate representation of specular highlights – More computational requirements

Assignment • Use your Bresenham code to scan convert some triangles • Shade some

Assignment • Use your Bresenham code to scan convert some triangles • Shade some of the triangles using the pertriangle (constant) technique • Shade some of the triangles using the pervertex (Gouraud) technique

 • Implement per-vertex and per-triangle shading – Parameters should be colors (1 color

• Implement per-vertex and per-triangle shading – Parameters should be colors (1 color for per-triangle, 3 colors for per-vertex) • Create a video sequence to show in class – Demonstrate with triangles of various control colors – Be creative • Due date – Next week – Turn in: – Video to be shown in class – All program listings (email) • Grading will be on completeness (following instructions) and timeliness (late projects will be docked 10% per week)