1 / 31

Outline

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?.

kyrie
Download Presentation

Outline

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. Outline • Color in OpenGL • Polygonal Shading • Light Source in OpenGL • Material Properties • Normal Vectors

  2. 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?

  3. 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

  4. 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.

  5. 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.

  6. 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

  7. Outline • Color in OpenGL • Polygonal Shading • Light Source in OpenGL • Material Properties • Normal Vectors

  8. 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(…)

  9. What is Color? – (cont’d) glColor3f(red, green, blue)glColor4f(red, green, blue, alpha)

  10. 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

  11. Outline • Color in OpenGL • Polygonal Shading • Light Source in OpenGL • Material Properties • Normal Vectors

  12. 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

  13. 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

  14. Flat Shading – (cont’d) • Inexpensive to compute • Appropriate for objects with flat faces • Less suitable for smooth surfaces

  15. 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

  16. 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

  17. Outline • Color in OpenGL • Polygonal Shading • Light Source in OpenGL • Material Properties • Normal Vectors

  18. 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

  19. 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

  20. Light effects – (cont’d)

  21. 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);

  22. 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);

  23. 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)

  24. Outline • Color in OpenGL • Polygonal Shading • Light Source in OpenGL • Material Properties • Normal Vectors

  25. 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);

  26. 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);

  27. Outline • Color in OpenGL • Polygonal Shading • Light Source in OpenGL • Material Properties • Normal Vectors

  28. 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

  29. 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);

  30. Assignment 2 • Gray scale image • Using BMPLoader to create a terrain – height map • Objects in OFF files • Locating objects somewhere on the terrain

  31. 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;

More Related