1 Lecture 10 Lighting in OpenGL
2 Sources of light GLfloat myLightPosition[] = {3.0, 6.0, 5.0, 1.0}; GLLightfv(GL_LIGHT0, GL_POSITION, myLightPosition); glEnable(GL_LIGHTING); //enable lighting glEnable(GL_LIGHT0); //enable this particular source Infinitely remote source (directional light): GLfloat myLightPosition[] = {3.0, 6.0, 5.0, 0.0}
3 RGB Values for Lights and Materials For light the numbers correspond to a percentage of full intensity for each color: If the R, G, and B values for light’s color are all 1.0, the light is the brightest possible white. If the values are 0.5, the color is still white, but only half intensity, so it appears gray. If R=G=1 and B=0, light appears yellow.
4 RGB Values for Lights and Materials For materials, the number corresponds to reflected proportion of those colors. So, if R=1, G=0.5, and B=0 for a material, that material reflects all the incoming green, and none of the incoming red light, half the incoming green, and none of the incoming blue light.
5 RGB Values for Lights and Materials Hence, OpenGL light has components (LR, LG, LB), and material has corresponding components (MR, MG, MB), then ignoring all other reflective effects, the light arrives at the eye is given by ( LR*MR, LG*MG, LB*MB). Similarly, if we have two light source that sends (R1, G1, B1) and (R2, G2, B2) to the eye, OpenGL adds the components (R1+R2, G1+G2, B1+B2). If a color intensity is bigger than 1, OpenGL makes it 1.
6 Lighting Terms Ambient Light is the light that has a source, but is evenly spread around the room. Objects that have ambient light are evenly lit and always visible. Light is evenly distributed that there is no way to tell its source. For example, due to reflection of light from walls.
7 Lighting Terms Diffuse Light is a light that comes from a particular direction and reflects evenly off the surface. Points where the light is directly normal (perpendicular) from the light source to the surface appear brighter.
8 Lighting Terms Specular Light is similar to the Diffuse light where the light comes from a particular direction, but the reflection is much more dynamic. This effect can usually cause a shine or a glare.
9 Lighting Terms in OpenGL GLfloat amb0[] = {0.2, 0.4, 0.6, 1.0}; GLfloat diff0[] = {0.8, 0.9, 0.5, 1.0}; GLfloat spec0[] = {1.0, 0.8, 1.0, 1.0}; glLightfv(GL_LIGHT0, GL_AMBIENT, amb0); //attach them to LIGHT0 glLightfv(GL_LIGHT0, GL_DIFFUSE, diff0); glLightfv(GL_LIGHT0, GL_SPECULAR, spec0);
10 Normals As far as the API is concerned, a square, or any object, is just a collection of points to form an object. Even with simple objects, OpenGL knows nothing of the properties of that object. In order to give more information to OpenGL about an object and light, a normal should be defined.
11 Normals A normal vector is a line perpendicular to a plane in three-dimensional space. OpenGL uses normalized normals: a normal vector of length 1, to calculate how light may reflect off an object.
12 Normals Imagine any triangle ABC on the xy-plane (z values = 0). The coordinates starting from top left and going counter-clock-wise are as follows: ABC = { { 0, 1, 0 }, { -1, -1, 0 }, { 1, -1, 0 } } Normal vectors are only calculated at vertices, and the normals for the triangle are: Normals ABC = { { 0, 0, 1 } }
13 Normals Triangle ABC is co-planar (actually, any three points that form a triangle define a plane), so the normal to be calculated is the same anywhere on the triangle. At any angle of light, this vertex reflects the light symmetrically through the normal vector. This is how light is able to reflect off an object. This example was easy because the triangle lies completely on the xy-plane, but what about other triangles that lie anywhere in space?
14 Calculating Normals Objects may have analytic surfaces and non-analytic surfaces I. Calculating normals for analytic surfaces: Analytic surfaces are surfaces that are non-coplanar, such as spheres. Because calculating normals are only done at vertices, it becomes incredibly complicated to determine where the vertices are for an object such as a sphere. If the object being drawn is a predefined GLU shape, then using the gluQuadricNormals() function, will generate the normals for you GLUT predefined shapes automatically calculate normals).
15 Calculating Normals II. To find a normal of a surface that is non-analytic Means that the surface lies on the same plane, or is co-planar. This means that the normal vector on this surface is the same on all vertices. We have to get two vectors v1, v2 that are non- collinear and share a tail. The result of the cross product of the two vectors is a vector normal to the surface.
16 Materials OpengGL lighting model makes the approximation that a material’s color depends on the percentage of the incoming red, green, and blue light it reflects. For example, a perfectly red ball reflects all the incoming red light and absorbs all the green and blue light that strikes it. When such a ball is viewed in a white light (composed of equal amounts of red, green and blue light), all the red is reflected and you see a red ball. If the ball is viewed in a pure green light, it appears black (all the green is absorbed and there is no incoming red, so no light is reflected).
17 Materials Like lights, materials have different ambient, diffuse and specular colors, which determine the ambient, diffuse, and specular reflectance’s. A material’s -ambient reflectance is combined with the ambient component of each incoming light source, -the diffuse reflectance with the light’s diffuse component -specular reflectance with the light’s specular component.
18 Materials In addition to ambient, diffuse, and specular colors, materials have an emissive color, which simulates light originating from an object. In OpenGL, the emissive color of a surface adds intensity to the object, but is unaffected by any light source. Materials are what we use to give an object a certain type of reflective (or non-reflective) quality.
19 Materials Example: glEnable( GL_COLOR_MATERIAL ); float red[] = { 0.94, 0.2, 0.15, 1.0 }; glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red ); // draw some objects glDisable( GL_COLOR_MATERIAL );
20 Materials Enabling the material properties with a call to glEnable allow the materials to be manipulated. Next, it is needed to set which side the material colors it to be used for. The only three possible choices are GL_FRONT, GL_BACK, or GL_GL_FRONT_AND_BACK.
21 Materials The next parameter allows you to set the types of light to set (GL_AMBIENT, GL_DIFFUSE, GL_SPECULAR, or GL_AMBIENT_AND_DIFFUSE). One thing to note is that in most cases the ambient and diffuse materials are set to the same values, and this is why OpenGL has an option for combining the two in one line. The last parameter is an array (vector) of the intensities of the Red, Green, Blue and Alpha values that define the (in our case) ambience and diffuse properties (the second parameter).
22 Materials Another thing to note is our first time use of the alpha color. If you recall from color section, it was said that the alpha value is used for special effects. For use with lighting, we always set the alpha value to 1.0. Finally a call to glDisable() may speed up graphic intensive operations if the machine doesn’t have to check for material colors anymore.