320 likes | 460 Views
Outline. Color in OpenGL Polygonal Shading Light Source in OpenGL Material Properties Normal Vectors. Motivation. We know how to rasterize Given a 3D triangle and a 3D viewpoint, we know which pixels represent the triangle But what color should those pixels be?.
E N D
Outline • Color in OpenGL • Polygonal Shading • Light Source in OpenGL • Material Properties • Normal Vectors
Motivation • We know how to rasterize • Given a 3D triangle and a 3D viewpoint, we know which pixels represent the triangle • But what color should those pixels be?
Motivation – Why we need light? • Adds realism to our objects • Suppose we create a model of a sphere using many polygons and color it with glColor. • But our objective is
Why we need light? - Light example 1 • Keyboard key l turns on and off the lights. • Keyboard key d turns on and off depth test. • The keys 1,2,3 allow turn on and of 3 light sources. • Without lights, it looks like a simple 2d red circle. • When we use lights definitions, we can see its a sphere.
Depth Test - Light Example 1 • In this version of OpenGL, the sphere is drawn from the front to the back. • So if the depth test is cancelled, what we will see is the back of the sphere. • In another version of OpenGL the results can be different.
Motivation – (cont’d) • Light-material interactions affect vertices appearance • Due to this interaction, each vertex would have different color and shade • Need to consider • Light sources • Material properties • Location of viewer • Surface orientation
Outline • Color in OpenGL • Polygonal Shading • Light Source in OpenGL • Material Properties • Normal Vectors
What is Color ? • Color is simply a wavelength of light that is visible to the human eye • Reflection/absorption • Where is the black? • What about white? Light Spectrum glColor(…)
What is Color? – (cont’d) glColor3f(red, green, blue)glColor4f(red, green, blue, alpha)
What determines vertex color in OpenGL? Is OpenGL lighting enabled ? NO YES • Color determined by shades • Using: • normals • lights • material properties • Color determined by glColor3f(…) • Ignoring: • normals • lights • material properties
Outline • Color in OpenGL • Polygonal Shading • Light Source in OpenGL • Material Properties • Normal Vectors
Polygonal Shading • Shading is defined as a smooth transition from one color to another • Curved surfaces are approximated by polygons • Types of shading are: • Flat shading • Smooth Shading • Gouraud Shading
Flat Shading • Enabled with glShadeModel(GL_FLAT) • Shading is constant across polygon • Color of last vertex determines interior color • Only suitable for very small polygons v2 v0 v1
Flat Shading – (cont’d) • Inexpensive to compute • Appropriate for objects with flat faces • Less suitable for smooth surfaces
Smooth Shading • Enable with glShadeModel(GL_SMOOTH) • Interpolate color in interior • Computed during rasterization • Much better than flat shading • More expensive to calculate v2 v0 v1
Gouraud Shading • Special case of smooth shading • Need to calculate vertices normals, HOW? • Average all adjacent face normals • Requires knowledge about which faces share a vertex
Outline • Color in OpenGL • Polygonal Shading • Light Source in OpenGL • Material Properties • Normal Vectors
Enabling Light • Lighting in general must be enabled glEnable(GL_LIGHTING) • Each individual light must be enabled glEnable(GL_LIGHT0) • OpenGL supports at least 8 light sources • There are 3 types of effects of light source: • Ambient • Diffuse • Specular
Light effects • Ambient light : • Directionless • Objects are evenly lit on all surfaces in all directions • Objects are evenly shaded regardless of their viewing angle • Diffuse light: • Comes from a particular direction • Reflected evenly off a surface • Specular light: • Comes from a particular direction • Reflected sharply and in a particular direction • To make objects specular, both light source and object must have specular property set
Defining light source • Use vectors (r, g, b, a) for light properties • Beware: light positions will be transformed by the modelview matrix GLfloatlight_ambient[] = {0.2, 0.2, 0.2, 1.0}; GLfloatlight_diffuse[] = {1.0, 1.0, 1.0, 1.0}; GLfloatlight_specular[] = {1.0, 1.0, 1.0, 1.0}; GLfloatlight_position[] = {-1.0, 1.0, -1.0, 0.0}; glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); glLightfv(GL_LIGHT0, GL_POSITION, light_position);
Point Source vs. Directional Source • Directional light given by position vector GLfloatlight_position[] = {-1.0, 1.0, -1.0, 0.0}; glLightfv(GL_LIGHT0, GL_POSITION, light_position); • Point source given by position point GLfloatlight_position[] = {-1.0, 1.0, -1.0, 1.0}; glLightfv(GL_LIGHT0, GL_POSITION, light_position);
Moving Light Sources • Light sources are geometric objects whose positions or directions are affected by the modelview matrix • It is possible to: • Move the light source(s) with the object(s) • Move the light source(s) and object(s) independently • Fix the object(s) and move the light source(s) • Fix the light source(s) and move the object(s)
Outline • Color in OpenGL • Polygonal Shading • Light Source in OpenGL • Material Properties • Normal Vectors
Material Properties • Material have certain reflective color properties • Put a blue ball in a dark room with only yellow light, how would it looks like? • Material properties stay in effect until changed GLfloatmat_a[] = {0.1, 0.5, 0.8, 1.0}; GLfloat mat_d[] = {0.1, 0.5, 0.8, 1.0}; GLfloatmat_s[] = {1.0, 1.0, 1.0, 1.0}; GLfloatlow_sh[] = {5.0}; glMaterialfv(GL_FRONT, GL_AMBIENT, mat_a); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_d); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_s); glMaterialfv(GL_FRONT, GL_SHININESS, low_sh);
Material Properties – (cont’d) • Can shortcut material properties using glColor • Must be explicitly enabled and disabled glEnable(GL_COLOR_MATERIAL); /* affect front face, diffuse reflectionproperties */ glColorMaterial(GL_FRONT, GL_DIFFUSE); glColor3f(0.0, 0.0, 0.8); /* draw some objects here in blue */ glColor3f(1.0, 0.0, 0.0); /* draw some objects here in red */ glDisable(GL_COLOR_MATERIAL);
Outline • Color in OpenGL • Polygonal Shading • Light Source in OpenGL • Material Properties • Normal Vectors
Defining normals • Define unit normal before each vertex glNormal3f(nx, ny, nz); glVertex3f(x1, y1, z1); glVertex3f(x2, y2, z2); glVertex3f(x3, y3, z3); same normal for all vertices glNormal3f(nx1, ny1, nz1); glVertex3f(x1, y1, z1); glNormal3f(nx2, ny2, nz2); glVertex3f(x2, y2, z2); glNormal3f(nx3, ny3, nz3); glVertex3f(x3, y3, z3); different normals
Maintain Normals • Length of normals changes under some modelview transformations (but not under translations and rotations) • Ask OpenGL to automatically re-normalize glEnable(GL_NORMALIZE);
Assignment 2 • Gray scale image • Using BMPLoader to create a terrain – height map • Objects in OFF files • Locating objects somewhere on the terrain
Loading BMP files • By this class, you can load bmp images and access the cells directly BMPClass bmp; BMPLoad(imgName,bmp); _width = bmp.width; _hight = bmp.height;