








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
shading models with opengl functions
Typology: Essays (university)
1 / 14
This page cannot be seen from the preview
Don't miss anything!









Rendering or image synthesis is the automatic process of generating a photorealistic or non-photorealistic image from a 2D or 3D model (or models in what collectively could be called a scene file) by means of computer programs. Also, the results of displaying such a model can be called a rendering.
Shading:
Shading refers to the process of altering the color of an object/surface/ polygon in the 3D scene, based on things like (but not limited to) the surface's angle to lights, its distance from lights, its angle to the camera and material properties. Shading is performed during the rendering process by a program called a shader.
Shading is also dependent on the lighting used. Usually, upon rendering a scene a number of different lighting techniques will be used to make the rendering look more realistic.
Shading Model:
A shading model is used in computer graphics to simulate the effects of light shining on a surface. The intensity that we see on a surface is dependent upon - The type of light sources. - The surface characteristics. (eg. Shinning, matte, dull, and opaque or transparent).
A shading model dictates how light is scattered or reflected from a surface. The shading models described here focuses on achromatic light. Achromatic light has brightness and no color, it is a shade of gray so it is described by a single value its intensity.
A shading model uses two types of light source to illuminate the objects in a scene: Point light sources and ambient light. Incident light interacts with the surface in three different ways:
If all incident light is absorbed the object appears black and is known as a black body. If the entire incident light is transmitted the object is visible only through the effects of reflection.
Some amount of the reflected light travels in the right direction to reach the eye causing the object to be seen. The amount of light that reaches the eye depends on the orientation of the surface, light and the observer. There are two different types of reflection of incident light
Diffuse scattering occurs when some of the incident light slightly penetrates the surface and is re-radiated uniformly in all directions. Scattered light interacts strongly with the surface and so its color is usually affected by the nature of the surface material.
Specular reflections are more mirror like and highly directional. Incident light is directly reflected from its outer surface. This makes the surface looks shinny. In the simplest model the reflected light has the same color as the incident light; this makes the material look like plastic. In a more complex model the color of the specular light varies, providing a better approximation to the shininess of metal surfaces.
The total light reflected from the surface in a certain direction is the sum of the diffuse component and the specular component. For each surface point of interest we compute the size of each component that reaches the eye.
Basic Geometric model:
Computing Diffuse Component:
Suppose that a light falls from a point source onto one side of a face , a fraction of it is re-radiated diffusely in all directions from this side. Some fraction of the re-radiated part reaches the eye, with an intensity denoted by I (^) d.
An important property assumed for diffuse scattering is that it is independent of the direction from the point P, to the location of the viewer’s eye. This is called omnidirectional scattering, because scattering is uniform in all directions. Therefore Id is independent of the angle between m and v.
But for simplicity and to reduce computation time, these effects are usually suppressed when rendering images. A reasonable value for ρd is chosen for each surface
Specular Reflection
Real objects do not scatter light uniformly in all directions and so a specular component is added to the shading model. Specular reflection causes highlights which can add reality to a picture when objects are shinny. The behaviour of specular light can be explained with Phong model
Phong Model It is easy to apply and the highlights generated by the phong model given a plastic like appearance, so the phong model is good when the object is made of shinny plastic or glass. The Phong model is less successful with objects that have a shinny metallic surface. Fig a) shows a situation where light from a source impinges on a surface and is reflected in different directions.
In this model we discuss the amount of light reflected is greatest in the direction of perfect mirror reflection , r, where the angle of incidence θ equals the angle of reflection. This is the direction in which all light would travel if the surface were a perfect mirror. At the other nearby angles the amount of light reflected diminishes rapidly.
Fig (b) shows this with beam patterns. The distance from P to the beam envelope shows the relative strength of the light scattered in that direction..
Fig(c) shows how to quantify this beam pattern effect. The direction r of perfect reflection depends on both s and the normal vector m to the surface, according to:
For surfaces that are shiny but are not true mirrors, the amount of light reflected falls off as the angle φ between r and v increases. In Phong model the φ is said to vary as some power f of the cosine of φ i.e., ( cos (φ ))f in which f is chosen experimentally and usually lies between 1 and
Ambient Sources and Ambient Reflections To overcome the problem of totally dark shadows we imagine that a uniform background glow called ambient light exists in the environment. The ambient light source spreads in all directions uniformly. The source is assigned an intensity Ia. Each face in the model is assigned a value for its ambient reflection coefficient ρd, and the term Ia, ρa is added to the diffuse and specular light that is reaching the eye from each point P on that face. Ia and ρa are found experimentally. Too little ambient light makes shadows appear too deep and harsh. too much makes the picture look washed out and bland.
Combining Light Contributions:
We sum the three light contributions –diffuse, specular and ambient to form the total amount of light I that reaches the eye from point P:
I = ambient + diffuse + specular
Different objects require different shading effects. In the modeling process we attached a normal vector to each vertex of each face. If a certain face is to appear as a distinct polygon, we attach the same normal vector to all of its vertices, the normal vector chosen is that indicating the direction normal to the plane of the face.
The information obtained from the normal vector at each vertex is used to perform different kinds of shading. The main distinction is between a shading method that accentuates the individual polygons ( flat shading ) and a method that blends the faces to de-emphasize the edges between them ( smooth shading ).
In both kinds of shading, the vertices are passed down the graphics pipeline, shading calculations are performed to attach a color to each
Smooth shading attempts to de-emphasize edges between faces by computing colors at more points on each face. The two types of smooth shading
Gouraud Shading :
Gouraud shading computes a different value of c for each pixel. For the scan line ys in the fig. , it finds the color at the leftmost pixel, colorleft, by linear interpolation of the colors at the top and bottom of the left edge of the polygon. For the same scan line the color at the top is color4, and that at the bottom is color1, so colorleft will be calculated as
colorleft = lerp(color1, color4,f)
Gouraud shading is established in OpenGL using the function:
glShadeModel(GL_SMOOTH);
Phong Shading
Highlights are better reproduced using Phong Shading. Greater realism can be achieved with regard to highlights on shiny objects by a better approximation of the normal vector to the face at each pixel this type of shading is called as Phong Shading.
When computing Phong Shading we find the normal vector at each point on the face of the object and we apply the shading model there to fig the color we compute the normal vector at each pixel by interpolating the normal vectors at the vertices of the polygon. The fig shows a projected face with the normal vectors m1, m2, m3 and m4 indicated at the four vertices.
Interpolating normals
In Phong Shading the direction of the normal vector varies smoothly from point to point and more closely approximates that of an underlying smooth surface the production of specular highlights are good and more realistic renderings produced.
Drawbacks of Phong Shading
Adding texture to faces: The realism of an image is greatly enhanced by adding surface texture to various faces of a mesh object. The basic technique begins with some texture function, texture(s, t) in texture space , which has two parameters s and t. The function texture(s,t) produces a color or intensity value for each value of s and t between 0(dark)and 1(light). The two common sources of textures are ✓ Bitmap Textures ✓ Procedural Textures
Bitmap Texture: Textures are formed from bitmap representations of images, such as digitized photo. Such a representation consists of an array txtr[c][r] of color values. If the array has C columns and R rows, the indices c and r vary from 0 to C-1 and R-1 resp.,. The function texture(s,t) accesses samples in the array as in the code:
Color3 texture (float s, float t) { return txtr[ (int) (s * C)][(int) (t * R)]; } Where Color3 holds an RGB triple.
Procedural Textures: Textures are defined by a mathematical function or procedure. For example a spherical shape could be generated by a function: float fakesphere( float s, float t) { float r= sqrt((s-0.5) * (s-0.5)+ (t-0.5) * (t-0.5)); if (r < 0.3) return 1-r/0.3; //sphere intensity else return 0.2; //dark background } This function varies from 1(white) at the center to 0 (black) at the edges of the sphere.
Painting the Textures onto a Flat Surface: Texture space is flat so it is simple to paste texture on a flat surface.
The fig. shows the use of texture coordinates that tile the texture, making it to repeat. To do this some texture coordinates that lie outside the interval[0,1] are used. When rendering routine encounters a value of s and t outside the unit square, such as s=2.67, it ignores the integral part and uses only the fractional part 0.67. A point on a face that requires (s,t)=(2.6,3.77) is textured with texture (0.6,0.77). The points inside F will be filled with texture values lying inside P, by finding the internal coordinate values (s,t) through the use of interpolation.
Adding Texture Coordinates to Mesh Objects A mesh objects has three lists
We need to add texture coordinate to this list, which stores the coordinates (si, ti) to be associated with various vertices. We can add an array of elements of the type class TxtrCoord(public : float s,t;); to hold all of the coordinate pairs of the mesh. The two important techniques to treat texture for an object are:
ADDING SHADOWS OF OBJECTS:
Shadows make an image more realistic. The way one object casts a shadow on another object gives important visual clues as to how the two objects are positioned with respect to each other. Shadows conveys lot of information as such you are getting a second look at the object from the
view point of the light source. There are two methods for computing shadows:
The technique of “painting“ shadows as a texture works for shadows that are cast onto a flat surface by a point light source. The problem is to compute the shape of the shadow that is cast.
Fig(a) shows a box casting a shadow onto the floor. The shape of the shadow is determined by the projections of each of the faces of the box onto the plane of the floor, using the light source as the centre of projection.
Fig(b) shows the superposed projections of two of the faces. The top faces projects to top‟ and the front face to front‟. This provides the key to drawing the shadow.
After drawing the plane by the use of ambient, diffuse and specular light contributions, draw the six projections of the box’s faces on the plane, using only the ambient light. This technique will draw the shadow in the right shape and color. Finally draw the box.
Building the “Projected” Face:
To make the new face F‟ produced by F, we project each of the vertices of F onto the plane. Suppose that the plane passes through point A and has a normal vector n. Consider projecting vertex V, producing V‟. V‟ is the point where the ray from source at S through V hits the plane, this point is
V^1 =S+ (V-S) n.(A-S)/n.(V-S)
2) Rendering the scene : Each face in the scene is rendered using the eye camera. Suppose the eye camera sees point P through pixel p[c][r]. When rendering p[c][r], we need to find
If d[i][j] is less than D, the point P is in the shadow and p[c][r] is set using only ambient light. Otherwise P is not in shadow and p[c][r] is set using ambient, diffuse and specular light.
BUILDING A CAMERA IN A PROGRAM:
To have a finite control over camera movements, we create and manipulate our own camera in a program. After each change to this camera is made, the camera tells OpenGL what the new camera is. We create a Camera class that does all things a camera does. In a program we create a Camera object called cam, and adjust it with functions such as the following: cam.set(eye, look, up); // initialize the camera cam.slide(-1, 0, -2); //slide the camera forward and to the left cam.roll(30); // roll it through 30 degree cam.yaw(20); // yaw it through 20 degree
The Camera class definition: class Camera { private: Point3 eye; Vector3 u, v, n; double viewAngle, aspect, nearDist, farDist; //view volume shape void setModelViewMatrix(); //tell OpenGL where the camera is public: Camera(); //default constructor void set(Point3 eye, Point3 look, Vector3 up); //likegluLookAt() void roll(float, angle); //roll it void pitch(float, angle); // increase the pitch void yaw(float, angle); //yaw it void slide(float delU, float delV, float delN); //slide it Void setShape (float vAng, float asp, float nearD, float farD); };
The utility routine setModelViewMatrix() communicates the modelview matrix to OpenGL. It is used only by member functions of the class and needs to be called after each change is made to the camera’s position.