Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 450: COMPUTER GRAPHICS BASIC ILLUMINATION SPRING 2015 DR. MICHAEL J. REALE.

Similar presentations


Presentation on theme: "CS 450: COMPUTER GRAPHICS BASIC ILLUMINATION SPRING 2015 DR. MICHAEL J. REALE."— Presentation transcript:

1 CS 450: COMPUTER GRAPHICS BASIC ILLUMINATION SPRING 2015 DR. MICHAEL J. REALE

2 INTRODUCTION Let’s say we have a triangle on the screen We’ve already projected it and let’s assume it’s entirely within the clipping window The rasterizer needs to fill in each of the fragments/pixels inside of this triangle If we need to fill in one color  straightforward, but not realistic-looking How do we make surfaces look realistic?

3 VERTEX ATTRIBUTES  PIXEL/FRAGMENT VALUES Most of the time, a model is broken up into a triangle mesh For a given triangle, each vertex can be given different values (attributes): Color Texture coordinates Normal vector Etc. These values are interpolated across the triangle to get the individual pixel/fragment values

4 BARYCENTRIC COORDINATES

5 Barycentric coordinates – parameterizes the space that can be formed as a weighted combination of a set of reference points In other words  any location is a weighted average of the reference points Weights all sum to 1 For 2 reference points: (u,v) = barycentric coordinates of P with respect to A and B P is on segment AB IFF 0 <= u <= 1 AND 0 <= v <= 1 Barycentric coordinates of A = (1,0) Barycentric coordinates of B = (0,1)

6 BARYCENTRIC COORDINATES: TRIANGLES For 3 reference points (triangles): (u,v,w) = barycentric coordinates of P with respect to A, B, and C A  (1,0,0) B  (0,1,0) C  (0,0,1) Inside or on triangle IFF 0 <= u,v,w <= 1 Centroid or barycenter = (1/3, 1/3, 1/3) http://en.wikipedia.org/wiki/Barycentric_coordinate_system#/media/Fi le:TriangleBarycentricCoordinates.svg

7 BARYCENTRIC COORDINATES: WHY DO WE CARE? Given a point P in a triangle, the barycentric coordinates give us a weight for each vertex in the triangle  can use to interpolate vertex attribute values! Example: if each vertex has a different color (C 0, C 1, C 2, respectively), and the barycentric coordinates for P are (u,v,w), the interpolated color for P is given by:

8 BARYCENTRIC COORDINATES AS A PLANE One can rearrange the previous barycentric equations to form a point and two axes (effectively defining a plane): Note: We don’t really need u

9 SOLVING FOR BARYCENTRIC COORDINATES Let’s say we have a point P and we want to find the barycentric coordinates for it Let the following be true: Then we can rewrite our previous equation:

10 SOLVING FOR BARYCENTRIC COORDINATES We can turn this into a 2 x 2 system of linear equations by taking the dot product of both sides first with V 0 and then with V 1 :

11 SOLVING FOR BARYCENTRIC COORDINATES Fortunately, this can be solved using Cramer’s Rule!

12 SOLVING FOR BARYCENTRIC COORDINATES The following code is taken from “Real-Time Collision Detection” by Christer Ericson (pg. 47-48) void Barycentric(Point a, Point b, Point c, Point p, float &u, float &v, float &w) { Vector v0 = b – a, v1 = c – a, v2 = p – a; float d00 = Dot(v0,v0); float d01 = Dot(v0,v1); float d11 = Dot(v1,v1); float d20 = Dot(v2,v0); float d21 = Dot(v2,v1); float denom = d00 * d11 – d01 * d01; v = (d11 * d20 – d01 * d21) / denom; w = (d00 * d21 – d01 * d20) / denom; u = 1.0f – v – w; }

13 LIGHT SOURCES

14 LIGHTING MODEL AND LIGHTS Lighting model (or shading model) = used to calculate the color of an illuminated position on the surface of an object Light source (or “light”) = emits light (radiant energy)

15 TYPES OF LIGHT SOURCES There are three basic types of light sources: Point lights Directional lights “Spotlights”

16 POINT LIGHTS Point light Located at a single point in space Emit light in all directions

17 DIRECTIONAL LIGHT (“SUN”) Directional Light Emits light in one direction Position of light doesn’t matter  only the direction matters Simulates a light “infinitely” far away (e.g., the sun)

18 “SPOTLIGHTS” Spotlight Has a position, direction, and angle θ defining how wide the cone of light is If V light = direction of the light and V obj = direction to object, then:

19 RADIAL INTENSITY ATTENUATION Let’s say we have a point light source The farther an object is from the light  the less light reaches the object (less intense) Officially: attenuation factor = 1/d 2 In practice, however, this doesn’t look realistic: Objects close to light  too much intensity variation Objects far from light  very little variation Reason why it doesn’t look right: lights aren’t REALLY infinitesimal points  only an approximation Alternative: use inverse quadratic function with tunable parameters:

20 ATTENUATION AND DIRECTIONAL LIGHTS Attenuation doesn’t make sense with directional lights  since light is “infinitely” far away, all points in scene equidistant from light

21 ANGULAR INTENSITY ATTENUATION For spotlights, as angle between object and light direction gets larger  cosine gets smaller Can use as part of attenuation factor

22 BASIC LIGHTING MODEL

23 INTRODUCTION When light hits an opaque surface, part of it reflected and part of it absorbed For transparent surfaces, light is also transmitted through the surface We’ll start by assuming a monochrome world (i.e., only black-and-white-and-shades-of-gray) So, we’ll refer to the intensity of the light as a single value (we’ll deal with RGB color later) For simplicity, we often determine the shading of an object based on three factors: Diffuse reflection Ambient light Specular reflection

24 DIFFUSE REFLECTION Diffuse reflection = when white light hits an object, what we see as the “color” of an object Example: apple  absorbs all frequencies except red  has red diffuse color Underlying physics: surfaces with microfacets (bumpy, grainy, matte)  reflects light in lots of different directions Ideal diffuse reflectors or Lambertian reflectors Incident light scattered with equal intensity in ALL directions, INDEPENDENT of viewing angle Depends on angle between NORMAL and DIRECTION-to-LIGHT  angle of incidence

25 DIFFUSE REFLECTION: SIMPLE VERSION k d = diffuse intensity (“color”) of surface I = light intensity N = normal L = vector to light from point The larger the angle between N and L  the less light hits the surface (and gets reflected off) Use dot product of N and L (as long as it’s greater than 0)

26 DIFFUSE REFLECTION: DIFFERENT LIGHT SOURCES Point lights  L = vector from current position to light position Directional light  L = direction vector for light

27 AMBIENT LIGHT Ambient light = sets the overall brightness of the scene Applied to ALL objects equally Approximating global illumination (in real world, light bouncing around different objects) To compute ambient light contribution to point on surface: Sometimes graphics packages will use a separate ambient coefficient for the surface:

28 DIFFUSE + AMBIENT LIGHT Note: all these factors vary from 0 to 1

29 SPECULAR REFLECTION Specular reflection = when all (or almost all) of the incident light is reflected back A shiny or reflective spot Depends on: Normal N Light vector L View vector V = vector from point to camera R = ideal reflection angle When R is in line with V  most reflective

30 SPECULAR REFLECTION: DULL AND SHINY Different surfaces reflect light over finite range of viewing positions Shiny surfaces  narrow range Duller surfaces  wider range

31 SPECULAR RELFECTION: PHONG SPECULAR-REFLECTION MODEL Phong specular-reflection model Angle φ = angle between R and V n s = specular-reflection exponent Sometimes called “shininess” If we use a constant coefficent for specular reflection, then:

32 SPECULAR REFLECTION: GETTING R L projected onto N is the same as R projected onto N; therefore: Our specular component can then be computed by:

33 SPECULAR REFLECTION: A SIMPLER WAY A simplified way to calculate specular reflection is to look at the halfway vector H between L and V: Now, we just look at the angle between N and H (or rather the dot product between them): Problem: if V, L, and N are not coplanar  slightly off Another problem: need to check if V and L are on same side of N (L · V > L · N)  if so, don’t use specular effect

34 GETTING THE VIEW VECTOR To get the vector from the current point to the viewer, you need: 3D WORLD position P of the point on the polygon PROBLEM: In vertex/pixel shader, have NORMALIZED DEVICE COORDINATES SOLUTION: Also pass in INVERSE Model-View-Projection transform to UNDO transformation P = invMVP*position 3D position of camera E To get the vector to the camera:

35 DIFFUSE + AMBIENT + SPECULAR So, for a single point light source, our combined diffuse, ambient, and specular reflections from a position on an illuminated surface are given by: NOTE: If light is behind surface, only use ambient light ALSO NOTE: If V and L are on the same side of N, no specular effects

36 MULTIPLE LIGHT SOURCES To deal with multiple light sources, just sum all values up for diffuse and specular components (ambient light is only added once, however):

37 SURFACE LIGHT EMISSIONS In addition to lights, one can also have surfaces that emit light Examples: fluorescent light bulbs, neon signs, anything from Tron,… Have addition term k e that is handled like an ambient term (except that it is specific to the surface) TECHNICALLY, light coming from the surface should affect other objects in the world: Use directional light with cone/box around it to determine area of effect Use lots of point lights Ignore it to save time (more common) Radiosity model (covered later) http://thedisneyblog.com/wp-content/uploads/2012/06/tron-uprising-art.jpg

38 DIFFUSE + AMBIENT + SPECULAR + SURFACE EMISSION + ATTENUATION The general, monochromatic illumination model for surface reflection that includes: Multiple light sources Attenuation factors Point lights, directional lights, spotlights, and surface emissions …is given by:

39 THE ILLUMINATION FROM THIS PIXEL IS TOO HIGH! The final output intensity has to be between 0 and 1  how to do we keep it in that range when we have lots of lights, etc.? Set a max value for each term in equation If exceed max value, set to max Normalize individual terms by dividing each by magnitude of largest term More complicated: get ALL pixel intensities for scene, and then scale set of intensities to range [0,1]

40 RGB COLOR For color lights and surfaces, our intensity values now become vectors: Examples: Our equations now become vector equations

41 COLOR REFLECTIONS? In the original specular-reflection model, k s was set to a constant value INDEPENDENT of the surface color  usually same color as light Gives surface a “plastic” appearance For nonplastic surfaces  specular color may be different from both light color and diffuse color of object!

42 POLYGON SURFACE RENDERING

43 INTRODUCTION Given a surface’s color information (diffuse, specular, etc.) and the light color information, how do we actually render the polygon? There are three major approaches for this, and the difference between them is at what level we compute the lighting equation: Flat shading  per primitive (triangle) Gouraud shading  per vertex Phong shading  per pixel/fragment

44 FLAT SHADING Flat shading = compute shading per primitive (triangle) All pixels/fragments have the same color Basically use the same normal for the whole triangle Advantages: Fastest Accurate if surface that the polygon is part of is NOT a curved surface Disadvantages: Curved surfaces look terrible  transitions between polygons is sudden

45 GOURAUD SHADING Gouraud shading = compute shading per-vertex Interpolate COLOR values to fill in pixels Use per-vertex normal Pronounced “guh-row”

46 GOURAUD SHADING: PER-VERTEX NORMAL? Usually, we think of the POLYGON as having a normal For a CURVED surface: To get the per-vertex normal, we get the average of all normals from the polygons the vertex is a part of: For a NON-CURVED surface: Sometimes we actually don’t want the surface to smoothly transition from one polygon to another To address this, duplicate the vertex so that you get two different average normals: Curved surface Duplicate vertex to create crease

47 GOURAUD SHADING: PROS AND CONS Advantages: Looks better than flat shading for curved surfaces Fairly efficient Disadvantages: Depending on vertex tessellation (i.e., how many vertices/polygons), specular highlights don’t look right Kind of smearing the highlight across the polygon Fewer polygons  More polygons

48 PHONG SHADING Phong shading = compute shading per-pixel/fragment Interpolate NORMALS across surface (from vertex normals) Advantages: Highest quality rendering Disadvantages: More computationally intensive (have to compute shading formula at each pixel) Overkill for non-curved surfaces (and we will have to duplicate vertices to get the creases we want)

49 FLAT VS. GOURAUD VS. PHONG Flat shading - Fastest - Lowest quality - Bad for curved surfaces Gouraud shading - Not as fast - Better quality - Specular highlights may not look right Phong shading - Slowest - Highest quality

50 OPENGL? In legacy, handling lights, what kind of shading you were using, etc. was a LOT more complicated In modern OpenGL, you pretty much handle this yourself with the vertex/pixel shaders: Gouraud shading: Compute per-vertex color Pass to fragment shader to be interpolated Use interpolated color directly for fragment Phong shading: Pass per-vertex NORMAL to fragment shader to be interpolated Compute fragment color in fragment shader For flat shading, pass in same normal for all vertices that are part of the same triangle


Download ppt "CS 450: COMPUTER GRAPHICS BASIC ILLUMINATION SPRING 2015 DR. MICHAEL J. REALE."

Similar presentations


Ads by Google