# CS计算机代考程序代写 data structure 15-462 Computer Graphics I Lecture 8

15-462 Computer Graphics I Lecture 8
Light Source in OpenGL
Light Source in OpenGL
Material Properties in OpenGL
Material Properties in OpenGL
Normal Vectors in OpenGL
Normal Vectors in OpenGL
Approximating a Sphere
Approximating a Sphere
[Angel 6.5-6.9]
[Angel 6.5-6.9]
February 6, 2003
Frank Pfenning
Carnegie Mellon University
http://www.cs.cmu.edu/~fp/courses/graphics/

• Curved surfaces are approximated by polygons
– Phong shading (different from Phong illumination)
• Two questions:
– How do we determine normals at vertices?
– How do we calculate shading at interior points?
02/06/2003 15-462 Graphics I 2

• Normal: given explicitly before vertex
glNormal3f(nx, ny, nz); glVertex3f(x, y, z);
• Single polygon: first vertex
• Triangle strip:Vertex n+2 for triangle n
02/06/2003 15-462 Graphics I 3

• Inexpensivetocompute
• Appropriateforobjectswithflatfaces • Lesspleasantforsmoothsurfaces
02/06/2003 15-462 Graphics I 4

• Calculatecolorateachvertex
• Interpolatecolorininterior
• Compute during scan conversion (rasterization) • Much better image (see Assignment 1)
• Moreexpensivetocalculate
02/06/2003 15-462 Graphics I 5

• How do we calculate vertex normals?
• Requires knowledge about which faces share a vertex
02/06/2003 15-462 Graphics I 6

• Sometimesvertexnormalscanbecomputed directly (e.g. height field with uniform mesh)
• More generally, need data structure for mesh • Key: which polygons meet at each vertex
02/06/2003 15-462 Graphics I 7

• Interpolatenormalsratherthancolors
• Significantly more expensive
• Mostlydoneoff-line(notsupportedinOpenGL)
02/06/2003 15-462 Graphics I 8

Michael Gold, Nvidia
Single pass Phong Lighting Gouraud Shading
02/06/2003
Two pass Phong Lighting, Gouraud Shading
15-462 Graphics I
Two pass Phong Lighting, Phong Shading
9

• Gouraud shading – Set vertex normals
– Calculate colors at vertices
– Interpolate colors across polygon
• Must calculate vertex normals!
• Must normalize vertex normals to unit length!
02/06/2003 15-462 Graphics I 10

Outline
Outline
• Light Sources in OpenGL
• MaterialPropertiesinOpenGL
• NormalVectorsinOpenGL
• Example: Approximating a Sphere
02/06/2003 15-462 Graphics I 11

Enabling Lighting and Lights
Enabling Lighting and Lights
• Lighting in general must be enabled
glEnable(GL_LIGHTING);
• Each individual light must be enabled
glEnable(GL_LIGHT0);
• OpenGLsupportsatleast8lightsources
02/06/2003 15-462 Graphics I 12

Global Ambient Light
Global Ambient Light
• Setambientintensityforentirescene
GLfloat al[] = {0.2, 0.2, 0.2, 1.0}; glLightModelfv(GL_LIGHT_MODEL_AMBIENT, al);
• The above is default
• Also: local vs infinite viewer
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
• Moreexpensive,butsometimesmoreaccurate 02/06/2003 15-462 Graphics I 13

Defining a Light Source
Defining a Light Source
• Usevectors{r,g,b,a}forlightproperties • Beware: light source will be transformed!
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);
02/06/2003 15-462 Graphics I 14

Point Source vs Directional Source
Point Source vs Directional Source
• Directional light given by “position” vector GLfloat 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);
02/06/2003 15-462 Graphics I 15

Spotlights
Spotlights
• Create point source as before
GLfloat sd[] = {-1.0, -1.0, 0.0}; glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, sd); glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 45.0); glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 2.0);
[Demo: Lighting Position Tutor]
02/06/2003 15-462 Graphics I 16

Outline
Outline
• Light Sources in OpenGL
• MaterialPropertiesinOpenGL
• NormalVectorsinOpenGL
• Example: Approximating a Sphere
02/06/2003 15-462 Graphics I 17

Defining Material Properties
Defining Material Properties
• Materialpropertiesstayineffect
• Setbothspecularcoefficientsandshininess
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_d); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_s); glMaterialfv(GL_FRONT, GL_SHININESS, low_sh);
• Diffusecomponentisanalogous
[Demo: Light material Tutor]
02/06/2003 15-462 Graphics I 18

• CanshortcutmaterialpropertiesusingglColor • 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);
02/06/2003 15-462 Graphics I 19

Outline
Outline
• Light Sources in OpenGL
• MaterialPropertiesinOpenGL
• NormalVectorsinOpenGL
• Example: Approximating a Sphere
02/06/2003 15-462 Graphics I 20

Defining and Maintaining Normals
Defining and Maintaining Normals
• Defineunitnormalbeforeeachvertex glNormal3f(nx, ny, nz);
glVertex3f(x, y, z);
• Lengthchangesundersometransformations
• Ask OpenGL to re-normalize (all tfms)
glEnable(GL_NORMALIZE);
• Works for uniform scaling (and rotate, translate) 02/06/2003 15-462 Graphics I 21

Example: Icosahedron
Example: Icosahedron
• Define the vertices
#define X .525731112119133606 #define Z .850650808352039932
static GLfloat vdata[12][3] = {
{-X, 0.0, Z}, {X, 0.0, Z}, {-X, 0.0, -Z}, {X, 0.0, -Z}, {0.0, Z, X}, {0.0, Z, -X}, {0.0, -Z, X}, {0.0, -Z, -X}, {Z, X, 0.0}, {-Z, X, 0.0}, {Z, -X, 0.0}, {-Z, -X, 0.0}
};
• For simplicity, avoid the use of vertex arrays
02/06/2003 15-462 Graphics I 22

Defining the Faces
Defining the Faces
• Index into vertex data array
static GLuint tindices[20][3] = {
{1,4,0}, {4,9,0}, {4,9,5}, {8,5,4}, {1,8,4}, {1,10,8}, {10,3,8}, {8,3,5}, {3,2,5}, {3,7,2}, {3,10,7}, {10,6,7}, {6,11,7}, {6,0,11}, {6,1,0}, {10,1,6}, {11,0,9}, {2,11,9}, {5,2,9}, {11,2,7}
};