880 likes | 891 Views
Lighting & Shading. CSC4820/6820 Computer Graphics Algorithms Ying Zhu Georgia State University. Outline. Color Illumination models Global illumination model Local illumination model (our focus) Light sources Reflection model & material properties Polygonal shading. Coloring vertices.
E N D
Lighting & Shading CSC4820/6820 Computer Graphics AlgorithmsYing ZhuGeorgia State University
Outline • Color • Illumination models • Global illumination model • Local illumination model (our focus) • Light sources • Reflection model & material properties • Polygonal shading
Coloring vertices • We have learned where to draw a vertex on the final 2D image • Now we discuss how to set the color for a vertex • Different ways to assign color to a vertex • Assign color to vertex directly • Let OpenGL calculate the vertex color based on its lighting model • Write a vertex shader to do lighting your way • Use OpenGL extension or pixel shader to do per-pixel lighting • Leave it entirely to texture mapping
Color • When white light is incident on a surface some wavelengths are absorbed and others are reflected. • This gives us the perception of the color of the object. • When the human eye “sees” something, it is because the reflected light enters the eye and hits a light detector on the retina. • In computer graphics, we usually use three colors, called the primary colors, to produce a range of colors called the color gamut.
RGB Color Model • In the RGB color model, we use red, green, and blue as the 3 primary colors. • There are no real three colors that can be combined to give all possible colors. • But a good choice of three colors provides a color gamut that covers most colors.
Color Index • In color-index mode, a single number (called the color index) is stored for each pixel. • Each color index indicates an entry in a table that defines a particular set of R, G, and B values. • Such a table is called a color map. • Color map is usually controlled by window system, not by OpenGL.
Color Mode in OpenGL • In OpenGL, you choose to use either RGB mode or color-index mode, but not both. • Most of the time we use RGB mode. • Color-index is useful in some cases: • Porting an application that already uses color-index mode. • Color map animation: if the contents of an entry in the color map change, then all pixels of that color index change their color.
Color Mode in OpenGL • Specify display mode: • glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE); or • glutInitDisplayMode(GLUT_INDEX|GLUT_DOUBLE); • Cannot be changed after initialization.
Directly set color for vertices • Use the fragment shader out vec4 fragColor; void main() { fragColor = vec4(0.0, 0.0, 0.0, 1.0); }
When to set vertex color directly? • When you know exactly what color to use for each vertex • When you don’t care about lighting and realism • When you don’t have enough information for lighting calculation • E.g. you don’t have the surface normals, etc. Without lighting With lighting
Use OpenGL lighting model • Normally we use OpenGL lighting model to calculate color for each vertex • First, some background about illumination model
Main ideas about light • Light is a set of little particles (photons) flying around • Each photon has a wavelength and an energy • When photons hit things they bounce, losing at least some of their energy. • When they lose most of the energy, they are “absorbed”. • How to model the way photons bounce? • The illumination model
Illumination Models • Illumination: the transport of photons from light source via direct or indirect paths. • Illumination models: how light reflects from surfaces and produces what we perceive as color. • In general, light leaves some light source, e.g. a lamp or the sun, is reflected from many surfaces and then finally reflected to our eyes
Illumination Models shadow multiple reflection translucent surface
Illumination Models • Local illumination model: a surface point only receives light particles directly from the light source • Relatively simple and fast • The shading of any surface is independent from the shading of all other surfaces. • Not physically correct, but looks OK • OpenGL and Direct3D use only local illumination model.
Illumination Models • Global illumination model: a surface point receives light after the light rays interact with other objects in the scene. • Consider light reflected by other surfaces • Physically correct, but very expensive • E.g. ray tracing, radiosity.
Global Illumination Overview • There are several important effects that can not be simulated with a local illumination model: • Shadows • Refraction and transparency • Inter-object reflections • A better solution is global illumination. • More physically correct and produces more realistic images. • Also more computationally expensive than local illumination model.
Global Illumination Overview • Main algorithms for global illumination • Multi-pass rendering • Radiosity (primarily for indoor scenes) • Ray tracing • This is a brief overview. Details about ray tracing and radiosity will be covered later
Multi-pass Rendering • Render the same scene multiple times and combining the results. • A relatively “cheap” way to fake global illumination. • E.g. Quake III engine can do 10 passes on fast systems • This is the predominant approach for realistic imagery in real-time applications. • Many advanced rendering techniques fall into this category. • E.g. shadow, light map, etc.
Radiosity • Simulate the energy transfer between surface patches at the physical level. • The basis of the radiosity rendering algorithm. • This is the most accurate method to simulate surface interactions such as between walls inside a building. • Very expensive (hours per frame)
Ray Tracing • Ray tracing is currently the highest quality global illumination algorithm. • Many extensions include photon maps (for caustics), sky light, soft shadows, volumetric effects (fog, water) ,etc. • Can be very slow (minutes to hours per frame) • Real-time ray tracing may be on the way
Global Illumination: state-of-the-art • Commercial packages use a combination of multi-pass rendering, radiosity, and ray tracing. • Lightwave, Mental Ray, Brazil r/s, etc.
Components of Illumination Model • Light sources with following parameters • Color • Position and direction • Directional attenuation • Surface properties with following parameters • Color (Reflectance Spectrum) • Geometry (position, orientation, etc.) • Absorption
Steps in OpenGL lighting • Now back to local illumination model • Steps in OpenGL lighting: • Specify light source parameters in the OpenGL program • Specify surface material parameters in the OpenGL program • Pass the light source and surface material parameters to GLSL shaders • Perform the lighting calculation in either the vertex shader or fragment shader
Illumination Models • Illumination: the transport of photons from light source via direct or indirect paths. • Illumination models: how light reflects from surfaces and produces what we perceive as color. • In general, light leaves some light sources, e.g. a lamp or the sun, is reflected from many surfaces and then finally reflected to our eyes
Light Sources • General light sources are difficult to work with because we must integrate light coming from all points on the source • OpenGL uses simple empirical models
Typical OpenGL Light Sources • Ambient light source (e.g. reflected light) • Same amount of light everywhere in scene • No position, no direction, just a constant light value • Directional light (e.g. sun) • Has a direction but no specific location • Point light source (e.g. light bulb) • Has a location but light is emitted equally to all directions • Spotlight (e.g. headlight) • Has a position and light direction is restricted
Ambient Light Source • Even though an object in a scene is not directly lit, it will still be visible. This is because light is reflected indirectly from nearby objects. • Ambient light source models this indirect illumination. • Ambient light has no spatial or directional characteristics. The amount of ambient light is a constant for all surfaces in the scene. • A very crude way of simulating indirect illumination.
Point Light Source • The rays emitted from a point light equally to all directions. • This is an approximation to a light bulb with • Intensity ( ) • Position (P) • Factors (a, b, c) for light attenuation with distance (d) between P and surface vertex.
Directional Light Sources • All of the light rays from a directional light source have a common direction, and no point of origin. • Models a point light source at infinity. • No attenuation with distance because distance is . • Commonly used to simulate sunlight.
Spot Light Sources • Models point light source with direction: • Intensity ( ) • Position (P) • Direction (D) • Factors (a, b, c) for attenuation with distance (d). • For normalized vectors D and L, Dot Product
Spot Light Sources • Spotlights are characterized by a narrow range of angles through which light is emitted • Consider spotlight as a light cone with apex at P, direction D, and width defined by an angle • Light distribution within the cone is defined by • Where approximates the light attenuation along the cone angle
Other Light Sources • Area light sources • Light source occupies a 2D area (usually a polygon). • Extended light sources • Spherical light source • Elliptical light source • Cylindrical light source • Volumetric light source • With shaders, you can implement different kinds of light sources.
Illumination Models • Illumination models: how light reflects from surfaces and produces what we perceive as color. • In general, light leaves some light source, e.g. a lamp or the sun, is reflected from many surfaces and then finally reflected to our eyes • We have discussed light sources. Now we discuss surface material properties
Light-Material Interaction • Light-material interactions cause each point to have a different color or shade. • Light that strikes an object is partially absorbed and partially scattered (reflected) • The amount reflected determines the color and brightness of the object • A surface appears red under white light because the red component of the light is reflected and the rest is absorbed • The reflected light is scattered in a manner that depends on the smoothness and orientation of the surface
Surface Types • The smoother a surface, the more reflected light is concentrated in the direction a perfect mirror would reflected the light • A very rough surface scatters light in all directions rough surface smooth surface
Phong Reflectance Model • A simple reflectance model proposed by Bui-Thong Phong • Can be computed rapidly • Not physically correct, but the result looks right. • Adopted by OpenGL & Direct3D • Has four components • Diffuse reflection + Specular reflection + Ambient + Emission • Thus each light source (except for ambient light) has separate diffuse, specular, and ambient components
Phong Reflectance Model • Defines four vectors • vertex to light source vector (light vector l) • Vertex to eye vector (view vector v) • Vertex normal (n) • Light reflection vector (r)
OpenGL lighting equation • The final result I is a color component • This calculation is performed 3 times for R, G, B respectively Calculate for each light source separately and then add them up Diffuse component Ambient component Specular component Emission Distance Term for point/spotlight
Diffuse Reflection • Assume surface reflects equally in all directions. • An ideal diffuse surface is a very rough surface: e.g. clay. • Ideal diffuse reflectors reflect light according to Lambert’s cosine law.
Lambert’s Cosine Law • Lambert’s law determines how much of the incoming light energy is reflected. • Amount of light reflected is proportional to the vertical component of incoming light. • reflected light ~cos qi • cos qi = l · n if vectors normalized, where l is light vector, n is normal.
Diffuse Component Vector dot product • : incoming light diffuse intensity (light intensity at the vertex) • : diffuse reflection coefficient. • n : vertex’s normal. • l : normalized vector from vertex to light source
Specular Reflection • Most surfaces are neither ideal diffusers nor perfectly specular (ideal refectors). • Smooth surfaces show specular highlights due to incoming light being reflected in directions concentrated close to the direction of a perfect reflection. • E.g. mirrors, metals Specular Highlight
Specular Component • Phong proposed using a term that dropped off as the angle between the viewer and the ideal reflection increased • V is vertex-to-eye vector, r is light reflection vector Incoming Light’s specular intensity Shininess Coefficient Specular Coefficient
The Shininess Coefficient • Values of between 100 and 200 correspond to metals • Values between 5 and 10 give surface that look like plastic • The larger the value, the closer the eye vector has to be to the perfect reflection vector to see the specular highlight • This means highershininess -90 f 90
Blinn & Torrance Variation • Uses the halfway vector h between l and v h Vertex normal
Blinn & Torrance Variation • By using the halfway vector h, we do not need to compute the reflection vector r at very vertex. • Computing h is a lot cheaper.
Distance Term • The light from a point source that reaches a surface is inversely proportional to the square of the distance between them • Phong model adds a factor of the form 1/(a + bd +cd2) to the diffuse and specular components. • The constant and linear terms soften the effect of the point source.
Ambient Component • Ambient light is the result of multiple interactions between (large) light sources and the objects in the environment • Amount and color depend on both the color of the light(s) and the material properties of the object • A very crude approximation of global illumination. • May also define a global ambient light term Reflection Coefficient Intensity of Ambient light
Putting it all together • The final result I is a color component • This calculation is performed 3 times for R, G, B respectively Calculate for each light source (except for ambinet) separately and then add them up Diffuse component Ambient component Specular component Distance Term for point/spot light