outline
DESCRIPTION
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?. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/1.jpg)
2
Outline
Color in OpenGL Polygonal Shading Light Source in OpenGL Material Properties Normal Vectors
![Page 2: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/2.jpg)
3
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?
![Page 3: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/3.jpg)
4
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
![Page 4: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/4.jpg)
5
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.
![Page 5: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/5.jpg)
6
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.
![Page 6: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/6.jpg)
7
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
![Page 7: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/7.jpg)
8
Outline
Color in OpenGL Polygonal Shading Light Source in OpenGL Material Properties Normal Vectors
![Page 8: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/8.jpg)
9
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)…(
![Page 9: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/9.jpg)
10
What is Color? – (cont’d)
glColor3f(red, green, blue)glColor4f(red, green, blue, alpha)
![Page 10: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/10.jpg)
11
What determines vertex color in OpenGL?
Is OpenGL lighting enabled ?
Color determined by glColor3f(…)
Ignoring:• normals• lights• material
properties
Color determined by shades
Using:• normals• lights• material
properties
YESNO
![Page 11: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/11.jpg)
12
Outline
Color in OpenGL Polygonal Shading Light Source in OpenGL Material Properties Normal Vectors
![Page 12: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/12.jpg)
13
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
![Page 13: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/13.jpg)
14
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
v1v0
v2
![Page 14: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/14.jpg)
15
Flat Shading – (cont’d)
Inexpensive to compute Appropriate for objects with flat faces Less suitable for smooth surfaces
![Page 15: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/15.jpg)
16
Smooth Shading Enable with glShadeModel(GL_SMOOTH) Interpolate color in interior Computed during rasterization Much better than flat shading More expensive to calculate
v1v0
v2
![Page 16: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/16.jpg)
17
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
![Page 17: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/17.jpg)
18
Outline
Color in OpenGL Polygonal Shading Light Source in OpenGL Material Properties Normal Vectors
![Page 18: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/18.jpg)
19
Enabling Light Lighting in general must be enabled
glEnable)GL_LIGHTING(
Each individual light must be enabledglEnable)GL_LIGHT0(
OpenGL supports at least 8 light sources
There are 3 types of effects of light source: Ambient Diffuse Specular
![Page 19: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/19.jpg)
20
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
![Page 20: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/20.jpg)
21
Light effects – (cont’d)
![Page 21: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/21.jpg)
22
Defining light source Use vectors (r, g, b, a) for light properties Beware: light positions will be transformed by the
modelview matrix
GLfloat light_ambient[] = {0.2, 0.2, 0.2, 1.0};GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0};GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat light_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);
![Page 22: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/22.jpg)
23
Point Source vs. Directional Source Directional light given by position vectorGLfloat light_position[] = {-1.0, 1.0, -1.0, 0.0};glLightfv)GL_LIGHT0, GL_POSITION, light_position(;
Point source given by position point
GLfloat light_position[] = {-1.0, 1.0, -1.0, 1.0};glLightfv)GL_LIGHT0, GL_POSITION, light_position(;
![Page 23: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/23.jpg)
24
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)
![Page 24: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/24.jpg)
25
Outline
Color in OpenGL Polygonal Shading Light Source in OpenGL Material Properties Normal Vectors
![Page 25: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/25.jpg)
26
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
GLfloat mat_a[] = {0.1, 0.5, 0.8, 1.0};GLfloat mat_d[] = {0.1, 0.5, 0.8, 1.0};GLfloat mat_s[] = {1.0, 1.0, 1.0, 1.0};GLfloat low_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(;
![Page 26: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/26.jpg)
27
Material Properties – (cont’d)
Can shortcut material properties using glColor
Must be explicitly enabled and disabled
glEnable)GL_COLOR_MATERIAL(;
/* affect front face, diffuse reflection properties */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(;
![Page 27: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/27.jpg)
28
Outline
Color in OpenGL Polygonal Shading Light Source in OpenGL Material Properties Normal Vectors
![Page 28: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/28.jpg)
29
Defining normals Define unit normal before each vertex
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
glNormal3f(nx, ny, nz);glVertex3f(x1, y1, z1);glVertex3f(x2, y2, z2);glVertex3f(x3, y3, z3);
same normalfor all vertices
![Page 29: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/29.jpg)
30
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(;
![Page 30: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/30.jpg)
31
Assignment 2 Gray scale image Using BMPLoader to create a terrain – height
map Objects in OFF files Locating objects somewhere on the terrain
![Page 31: Outline](https://reader035.vdocument.in/reader035/viewer/2022062501/568163f1550346895dd570f7/html5/thumbnails/31.jpg)
32
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;