1 / 52

Shading and Illumination

Shading and Illumination. OpenGL Shading. Without Shading. With Shading. Physics. Bidirectional R eflectance D istribution F unction (BRDF). n: Surface normal L: Radiance (“intensity along a ray”). Light Events. Reflection. Refraction. Scattering. Bidirectional Reflectance

catori
Download Presentation

Shading and Illumination

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Shading and Illumination

  2. OpenGL Shading Without Shading With Shading

  3. Physics

  4. Bidirectional Reflectance Distribution Function (BRDF) n: Surface normal L: Radiance (“intensity along a ray”)

  5. Light Events Reflection Refraction Scattering Bidirectional Reflectance Distribution Function (BRDF) Bidirectional transmittance Distribution Function (BTDF) Scattering Function Bidirectional Scattering Distribution Function (BSDF)

  6. Rendering Equation • Given any point x in a large environment, let L(x, ω) be the radiance along a ray, we have: Le: Emitted intensity (light source) Ω: A hemisphere over x

  7. Light Transport • The rendering equation describes a light transport problem: • Each point has an integral equation. • Light can be reflected multiple times, so those integral equations are highly related. • Solution: global illumination. • Methods: ray tracing, radiosity.

  8. Example: Caustics

  9. Example: Color Bleeding

  10. OpenGL Illumination • Unfortunately, OpenGL doesn’t support global illumination. • Instead, it only considers local illumination: light can only be reflected once. • Even for local illumination, OpenGL is not physically based. It only provides an approximation. • To accurately get multiple reflections, you need to better rendering algorithms. • You can approximate some global illumination effects using GPU programmable shaders.

  11. OpenGL Illumination • Light sources • Surface material properties • Surface normal • Incoming (light) and outgoing (eye) directions

  12. Point Light Source GLfloat light_position[] = {1.0, 1.0, 1.0}; glLightfv(GL_LIGHT0, GL_POSITION, light_position);

  13. Directional Light Source GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0}; glLightfv(GL_LIGHT0, GL_POSITION, light_position);

  14. Spot Light Source GLfloat light_position[] = {1.0, 1.0, 1.0}; glLightfv(GL_LIGHT0, GL_POSITION, light_position); Glfloat spot_position[] = {1.0, 1.0, 1.0}; glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spot_position); GlfloatEXPONENT=10; glLightfv(GL_LIGHT0, GL_SPOT_EXPONENT, EXPONENT); Glfloat cutoff=30; glLightfv(GL_LIGHT0, GL_SPOT_CUTOFF, cutoff);

  15. OpenGL Light Intensity • OpenGL separates surface reflection into three components: • Therefore, you should define three light intensities. Each of them contributes to a reflection component: • Ambient • Diffuse • Specular Ambient Diffuse Specular Total

  16. OpenGL Light Source //To specify the light position and intensity GlfloatLightPosition []={100, 0.0, 100}; GlfloatLightAmbient []={0.2, 0.2, 0.2}; GlfloatLightDiffuse []={1.0, 1.0, 1.0}; GlfloatLightSpecular []={0.4, 0.4, 0.4}; glLightfv(GL_LIGHT1, GL_POSITION, LightPosition); glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient); glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse); glLightfv(GL_LIGHT1, GL_SPECULAR, LightSpecular);

  17. OpenGL Light Source • For a point light source, the light intensity drops when the light moves away from the source. So OpenGL multiplies the intensity with an attenuation factor: glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 2.0); glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 1.0); glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.5);

  18. Default Values No ambient Unit diffuse Unit specular Directional light along z No spot light No attenuation

  19. Multiple Light Sources //Specify the position and properties for GL_LIGHT0 glEnable(GL_LIGHT0); //Specify the position and properties for GL_LIGHT1 glEnable(GL_LIGHT1); //Create other light sources here. glEnable(GL_LIGHTING); //You can turn on and off lights like this: glDisable(GL_LIGHT0); glEnable(GL_LIGHT0); glDisable(GL_LIGHTING); glEnable(GL_LIGHTING);

  20. OpenGL Light Source • The light source is subject to the MODELVIEW matrix as well. • You can create the light source in the eye space, so it always attached to the camera. • You can create the light source after camera motion, so it is fixed in the world space. • You can even apply extra transformation/animation to light source.

  21. OpenGL Material • OpenGL separates surface reflection into three components: • Therefore, you should define three material properties. Each of them contributes to a reflection component: • Ambient • Diffuse • Specular Ambient Diffuse Specular Total

  22. Ambient Reflection Ambient Light Ambient Radiance (Pixel Intensity) Ambient Surface Material Coefficient • The object will have a uniform color. • It looks similar to the result without lighting. • The color is not directly assigned. It’s controlled by the light source and surface reflection coefficient.

  23. Diffuse Reflection • Diffuse reflection assumes that the light is equally reflected in every direction. • In other words, if the light source and the object has fixed positions in the world space, the camera motion doesn’t affect the appearance of the object. • The reflection only depends on the incoming direction. It is independent of the outgoing (viewing) direction. • Real objects with only a diffuse reflection property are called: Lambertian Surfaces, for example, clay.

  24. Diffuse Reflection • Lambert’s law says that the outgoing radiance depends on the cosine of the incident angle. • Because the irradiance (photons per unit surface area) becomes smaller (as in the rendering function before). dA dA θi dA dA/cosθi

  25. Diffuse Reflection Diffuse Light Diffuse Radiance Diffuse Surface Material Coefficient θi Brighter Darker

  26. An Example Kdiffuse Kambient

  27. Specular Reflection • Some materials, such as plastic and metal, can have shiny highlight spots. • This is due to a glossy mirror-like reflection. • Materials have different shininess/glossiness.

  28. Specular Reflection N • For a mirror, the light is reflected into a single direction. • Specular reflection assumes that the glossy surface is not even, so the light is reflected into a small cone. • If your viewing direction is close to the mirror direction, you receive high radiance. θi θi N θi ϕ

  29. Specular Reflection • Given L, N and V, we first calculate the mirror reflection direction: • Then we have: V N L R θi ϕ ϕis the angle between V and R. nindicates surface shininess.

  30. An Example Kspecular Shininess n

  31. Total Reflection • Illumination from a single light source i: • Illumination from multiple light source:

  32. OpenGL Material • OpenGL defines material properties like this: • Face: GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK • Property: GL_AMBIENT, GL_DIFFUSE, … • Value: A vector array glMaterialfv("face”, "property”, “value”)

  33. For Example Glfloatmat_amb_diff[]={1.0, 0.5, 0.8, 10.0}; Glfloatmat_specular[]={1.0, 1.0, 1.0, 1.0}; Glfloatshiniess []={5.0}; glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_amb_diff); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, shininess); • Specular highlights are usually in the light source’s color. • Ambient and diffuse can be defined together or separately. • If you don’t mind dark shadow, just ignore ambient. • Shininess is from 0 (dull) to 128 (shiny).

  34. Light Model glLightModelfv(property, value); • Enable two-sided lighting • Property=GL_LIGHT_MODEL_TWO_SIDE • If value=0, only GL_FRONT is rendered; • If value=1, both GL_FRONT and GL_BACK are rendered • Global ambient color • Property=GL_LIGHT_MODEL_AMBIENT • Value is a color array • And more…

  35. Surface Normal • Normals are used in calculating reflection. Normals are also used to decide the surface front and back side. (x, y, z) It’s normal is normalized(x, y, z) V2 E20=V2-V0 V0 E10=V1-V0 V1

  36. Surface Normal • We can define a triangle normal for the whole triangle. The result is a surface made of triangles. for(inti=0; i<t_number; i++) { glNormal3f(…); glBegin(GL_TRIANGLES); glVertex3f(…); glVertex3f(…); glVertex3f(…); glEnd(); }

  37. Surface Normal • But the mesh represents a smooth surface. We define a normal for each vertex, by taking the normalized average of its adjacent triangle normals. for(inti=0; i<t_number; i++) { glBegin(GL_TRIANGLES); glNormal3f(…); //XN0 glVertex3f(…); //X0 glNormal3f(…); //XN1 glVertex3f(…); //X1 glNormal3f(…); //XN2 glVertex3f(…); //X2 glEnd(); }

  38. Lighting and Shading • Each pixel corresponds to a visible 3D point. • We need to calculate the pixel intensity for that 3D point, based on the reflection model. Given a 2D Pixel Location Given a 3D polygon Inverse Projection Projection Selection Ray A 2D polygon Intersection Test Rasterization A 3D point on a polygon A number of pixels Reflection Calculation Ray Tracing The Graphics Pipeline

  39. Lighting and Shading • OpenGL calculates reflection at each vertex. Given a 3D polygon Given a 3D primitive Lighting Projection A 3D polygon, with intensity info A 2D polygon, with 3D info Projection Rasterization A 2D polygon, with intensity info Pixels with 3D info Fragment Shader Raserization Pixels with intensities Pixels with intensities OpenGL + GPU (Phong) OpenGL

  40. Shading Given a 3D polygon • Now given a 2D polygon with the value on each vertex, how to interpolate the value for each pixel? • Flat shading • Gouraud Shading* • Phong Shading* Lighting A 3D polygon, with info Projection A 2D polygon, with info Interpolation/Raserization Pixels with intensities *: Both were proposed by researchers at the University of Utah in 1970s.

  41. Flat Shading • Flat shading uses one vertex’s intensity value for the whole polygon. glShadeModel(GL_FLAT) What’s the difference? Vertex Normal + Flat Shading Triangle Normal + Smooth Shading

  42. Flat Shading VS. Smooth Shading • Flat shading has this discontinuous artifact. • It is fast, since only needs one calculation per polygon. • Only useful when the polygons are very small. • Smooth shading avoid the discontinuous artifact. • Slightly slower. • More realistic.

  43. Smooth Shading • OpenGL uses a smooth Gouraud shading technique. • The basic idea is to calculate the color intensity at each vertex, and then interpolate over the polygon. • To use smooth shading: glShadeModel(GL_SMOOTH) Vertex Normal + Flat Shading Vertex Normal + Smooth Shading

  44. Review: Linear Interpolation • Given a line segment with two end points (P and Q) and a sample point R, we can interpolate a value function V over the line as: Vq Vr=? Q Vp R P

  45. OpenGL Interpolation • OpenGL does 2D interpolation in a scanline way. • The result is identical to the interpolation using barycentric coordinates. But the scanline method is faster. Why? For every scanline

  46. Gouraud Shading • Gouraud shading calculates the color per vertex. • It has artifacts too. • To solve it, use smaller polygons. dark dark dark or bright? dark dark

More Related