Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 480/680 Computer Graphics Shading.

Similar presentations


Presentation on theme: "CS 480/680 Computer Graphics Shading."— Presentation transcript:

1 CS 480/680 Computer Graphics Shading

2 The Phong Reflection Model
Although we could approach light-material interactions through physical models, we have chosen to use a model that leads to efficient computations and to be a close enough approximation to physical reality to produce good renderings under a variety of lighting conditions and material properties.

3 Phong Model A simple model that can be computed rapidly
Has three components Diffuse Specular Ambient Uses four vectors To source To viewer Normal Perfect reflector This is determined by n and l

4 Ideal Reflector Normal is determined by local orientation
Angle of incidence = angle of reflection The three vectors must be coplanar r = 2 (l · n ) n - l

5 Lambertian Surface Perfectly diffuse reflector
Light scattered equally in all directions Amount of light reflected is proportional to the vertical component of incoming light reflected light ~cos qi cos qi = l · n if vectors normalized There are also three coefficients, kr, kb, kg that show how much of each color component is reflected

6 Specular Surfaces Most surfaces are neither ideal diffusers nor perfectly specular (ideal reflectors) Smooth surfaces show specular highlights due to incoming light being reflected in directions concentrated close to the direction of a perfect reflection specular highlight

7 Modeling Specular Relections
Phong proposed using a term that dropped off as the angle between the viewer and the ideal reflection increased the angle f is the angle between r and the reflector v Ir ~ ks I cosaf shininess coef reflected intensity incoming intensity absorption coef

8 The Shininess Coefficient
Values of a between 100 and 200 correspond to metals Values between 5 and 10 give surface that look like plastic cosa f -90 f 90

9

10 Computation of Vectors
The illumination and reflection models that we have derived are sufficiently general that they can be applied to either curved or flat surfaces, to parallel or perspective views, and to distant or near surfaces. Most require vectors and dot products. In this section we see what additional techniques can be applied when our objects are composed of flat polygons.

11 Vectors - Normal Plane Typically the definition is given as
ax+ by+ cz + d = 0 It could also be given as n . (p-p0) = 0 If we were given 3 noncollinear points, we could find the normal by n = (p2-p0) x (p1-p0) we must be careful about the order of the vectors. Reversing the order changes the surface from outward pointing to inward pointing.

12 Vectors - Normal Curved Surfaces
We could go through the math of how a sphere is defined. We could go through parametric equations of a sphere But we are only interested in the direction of the normal, so if the sphere is centered at the origin, we can say the normal is p

13 Vectors – Ange of Reflection
Once we have calculated the normal at a point, we can use this normal and the direction of light to compute the direction of a perfect reflection angle of incidence is equal to the angle of reflection

14 Vectors – Half Way Vector
If we use the Phong model with specular reflections in our rendering, the dot product r.v should be recalculated at every point on the surface We can obtain an interesting approximation by using the unit vector halfway between the viewer vector and the light-source vector:

15 Vectors – Ange of Reflection
If we replace r.v with n.h, we avoid calculation of r However, the halfway angle is smaller than the angle used for specular computation, so if we use the same exponent e in (n.h)e the specular highlights will be smaller, so we use a different value of e to fix this.

16 Transmitted Light Consider a surface that transmits all the light that strikes it If the speed of light differs in the two materials, the light is bent at the surface Let hl and ht be the indices of refraction Snell’s law states that

17 Transmitted Light Therefore, we can calculate the direction of the transmitted light. A more general expression for what happens at a transmitting surface corresponds to this figure some light is transmitted, some is reflected, and the rest is absorbed.

18 Polygonal Shading Assuming that we can compute normal vectors, given a set of light sources and a viewer, both the lighting and illumination models that we have developed can be applied at every point on a surface. Consider the polygon mesh shown here. We will consider three ways to shade the polygons: flat, interpolative or Gourand, and Phong shading

19 Flat Shading The three vectors - l, n, and v - can vary
but for a flat polygon, n is constant if we assume the viewer does not change, v is constant if the light source is distance, l is constant If all three are constant, the shading calculations only need to be carried out once for each polygon.

20 Interpolative and Gourand Shading
The normals are computed at each vertex. Colors and intensities of interior points are interpolated between vertices. Problem: normals are discontinuous at the vertex Solution: average them

21 Phong Shading Even the smoothness introduced by Gourand shading may not prevent bands. Phong proposed that instead of interpolating the intensities, interpolate the normals and then do calculation of intensities using the interpolated normal (typically at scan conversion) This is much smoother, but at a greater computational cost.

22 Lighting Examples Which one is which?

23 Global Rendering There are limitations imposed by the local lighting model that we have used. Consider, for example, an array of spheres

24 Global Rendering Our lighting model cannot handle these situations.
It also cannot produce shadows. If these effects are important, than we can use a more sophisticated (and slower) rendering technique, such as ray tracing and radiosity. Ray Tracing works well for highly specular surfaces; for example, in scenes composed of highly reflective and translucent objects, such as glass balls. Radiosity is best suited for scenes with perfectly diffuse surfaces, such as the interior of buildings.

25 Suggested Readings The use of lighting and reflection in computer graphics has followed two parallel paths: the physical and the computational Foley (90) gives a great discussion on these models. Phong put together his model in 1975 Ray tracing was introduced to computer graphics by Appel in 1968. Radiosity is based upon a method first used in heat transfer (Sie 81) and was applied to computer graphics in 1984. The OpenGL Programmer’s Guide contains many good hints on effective use of OpenGL’s rendering capability.

26

27 Vertex Lighting Shaders I
// vertex shader in vec4 vPosition; in vec3 vNormal; out vec4 color; //vertex shade // light and material properties uniform vec4 AmbientProduct, DiffuseProduct, SpecularProduct; uniform mat4 ModelView; uniform mat4 Projection; uniform vec4 LightPosition; uniform float Shininess;

28 Vertex Lighting Shaders II
void main() { // Transform vertex position into eye coordinates vec3 pos = (ModelView * vPosition).xyz; vec3 L = normalize( LightPosition.xyz - pos ); vec3 E = normalize( -pos ); vec3 H = normalize( L + E ); // Transform vertex normal into eye coordinates vec3 N = normalize( ModelView*vec4(vNormal, 0.0) ).xyz;

29 Vertex Lighting Shaders III
// Compute terms in the illumination equation vec4 ambient = AmbientProduct; float Kd = max( dot(L, N), 0.0 ); vec4 diffuse = Kd*DiffuseProduct; float Ks = pow( max(dot(N, H), 0.0), Shininess ); vec4 specular = Ks * SpecularProduct; if( dot(L, N) < 0.0 ) specular = vec4(0.0, 0.0, 0.0, 1.0); gl_Position = Projection * ModelView * vPosition; color = ambient + diffuse + specular; color.a = 1.0; }

30 Vertex Lighting Shaders IV
// fragment shader in vec4 color; void main() { gl_FragColor = color; }

31 Fragment Lighting Shaders I
// vertex shader in vec4 vPosition; in vec3 vNormal; // output values that will be interpolatated per-fragment out vec3 fN; out vec3 fE; out vec3 fL; uniform mat4 ModelView; uniform vec4 LightPosition; uniform mat4 Projection;

32 Fragment Lighting Shaders II
void main() { fN = vNormal; fE = vPosition.xyz; fL = LightPosition.xyz; if( LightPosition.w != 0.0 ) { fL = LightPosition.xyz - vPosition.xyz; } gl_Position = Projection*ModelView*vPosition;

33 Fragment Lighting Shaders III
// fragment shader // per-fragment interpolated values from the vertex shader in vec3 fN; in vec3 fL; in vec3 fE; uniform vec4 AmbientProduct, DiffuseProduct, SpecularProduct; uniform mat4 ModelView; uniform vec4 LightPosition; uniform float Shininess;

34 Fragment Lighting Shaders IV
void main() { // Normalize the input lighting vectors vec3 N = normalize(fN); vec3 E = normalize(fE); vec3 L = normalize(fL); vec3 H = normalize( L + E ); vec4 ambient = AmbientProduct;

35 Fragment Lighting Shaders V
float Kd = max(dot(L, N), 0.0); vec4 diffuse = Kd*DiffuseProduct; float Ks = pow(max(dot(N, H), 0.0), Shininess); vec4 specular = Ks*SpecularProduct; // discard the specular highlight if the light's behind the vertex if( dot(L, N) < 0.0 ) specular = vec4(0.0, 0.0, 0.0, 1.0); gl_FragColor = ambient + diffuse + specular; gl_FragColor.a = 1.0; }

36


Download ppt "CS 480/680 Computer Graphics Shading."

Similar presentations


Ads by Google