《三维电脑游戏设计3D Computer Game Design》由会员分享,可在线阅读,更多相关《三维电脑游戏设计3D Computer Game Design(35页珍藏版)》请在金锄头文库上搜索。
1、三維電腦遊戲設計 3D Computer Game Design,Chapter 5 Colors, Lighting, Blending, and Fog (Part II) 嘉大資工系 xxx,In this chapter, you will learn,Colors in OpenGL Shading OpenGL lighting Light sources Materials Blending and transparency Fog,Materials,OpenGL approximates material properties based on Reflects red, g
2、reen, and blue light Three color terms Ambient, diffuse, and specular A material with a high specular reflectance Appear shiny,Defining Materials,Setting a material is similar to creating a light source,void glmaterialif(GLenum face, GLenum pname, TYPE param); void glmaterialifv(GLenum face, GLenum
3、pname, const TYPE *params);face: how the material will be applied to the objects polygons. It can be one of three values: GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK.pname: tells OpenGL which material properties are being set.,Material Colors,The ambient, diffuse, and specular components Specify how a m
4、aterial interacts with a light source Determine the color of the material OpenGL allows you to use GL_AMBIENT_AND_DIFFUSE to specify them both together,Material Colors,Set the ambient material color to red for the front and back of polygonsSet both the ambient and diffuse materials to white for the
5、front of polygons,float red = 1.0f, 0.0f, 0.0f, 1.0f; glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, red);,float white = 1.0f, 1.0f, 1.0f, 1.0f; glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, white);,Specifying a Material (1/5),Reflectivity properties of a materialfaceGL_FRONT, GL_BACK, GL_FRONT_AND_BA
6、CK nameGL_DIFFUSE, GL_SPECLAR, GL_AMBIENT, GL_AMBIENT_AND_DIFFUSE, GL_EMISSION, GL_SHININESS,glMaterialif(GLenum face, GLenum name, TYPE value); glMaterialifv(GLenum face, GLenum name, TYPE *value);,Specifying a Material (2/5),Specifying a Material (3/5),Specifying a Material (4/5),Specifying a Mate
7、rial (5/5),Brass Material (1/2),Brass Material (2/2),Red Plastic Material (1/2),Red Plastic Material (2/2),Shininess,The illusion of shininess Caused by the bright spot, known as a specular highlight Set using GL_SHININESS Is range from 0 to 128,Shininess,The values of 128 representing an extremely
8、shiny material with a small specular highlight 0 representing a material that is not shiny with a very large specular highlight,glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 128);,Emissive Materials,Simulate that emit light Such as an area light or anything that glows It wont illuminate nearby object
9、s Cause the object to appear brighter,Emissive Materials,Use GL_EMISSION By default, the emissive term is (0.0, 0.0, 0.0, 1.0),/ use a dark gray color GLfloat emissiveColor = 0.3, 0.3, 0.3, 1.0;/ set the emissive term for both the front and back face glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, emis
10、siveColor);,Color Tracking,Another way to set material properties is by what is called color tracking Set material properties with calls to the glColor(),Color Tracking,Passing the GL_COLOR_MATERIAL parameter to the glEnable() function Use glColorMaterial() function to specify which material paramet
11、ers will be affected by calls to glColor,Color Tracking,A sample Set diffuse property of the fronts of polygons to track the current color,glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT, GL_DIFFUSE); glColor3f(1.0f, 0.0f, 0.0f); glBegin(GL_TRIANGLES);/ draw triangles glEnd();,Normals,Normals
12、are vectors that are perpendicular to a surface The orientation of that surface Specify on a per-vertex basis,Normals,Normal vectorsin OpenGL,glNormal3f(nx, ny, nz); glNormal3fv(pointer_to_normal);,Working with Normals (1/2),Flat shading,glShadeModel(GL_FLAT);glBegin(GL_TRIANGLES);glNormal3fv(n);glV
13、ertex3fv(p1);glVertex3fv(p2);glVertex3fv(p3); glEnd();,Working with Normals (2/2),Smooth shading,glShadeModel(GL_SMOOTH);glBegin(GL_TRIANGLES);glNormal3fv(n1);glVertex3fv(p1);glNormal3fv(n2);glVertex3fv(p2);glNormal3fv(n3);glVertex3fv(p3); glEnd();,Blending,Learn to use the a component in RGBA color
14、 for Blending for translucent surfaces Compositing images,Opacity and Transparency,Opaque surfaces permit no light to pass through Transparent surfaces permit all light to pass Translucent surfaces pass some lighttranslucency = 1 opacity (a),opaque surface a =1,OpenGL Blending and Compositing,Must e
15、nable blending and pick source and destination factorsglEnable(GL_BLEND)glBlendFunc(source_factor, destination_factor) Only certain factors supported GL_ZERO, GL_ONE GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA See Redbook for complete list,OpenGL Blending and Compositin
16、g,Transparency is implemented using GL_SRC_ALPHA for the source and GL_ONE_MINUS_SRC_ALPHA for the destination glEnable(GL_BLEND); glColor4f(0.0f, 0.0f, 1.0f, 0.5f); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); Example First draw a red triangle (destination) and then draw a blue triangle (source) on top of it With an alpha value of 0.5, the blue triangle is 50% transparent,Fog,Fog functions,OpenGL Fog Functions,GLfloat fcolor4 = :glEnable(GL_FOG); glFogf(GL_FOG_MODE, GL_EXP); glFogf(GL_FOG_DENSITY, 0.5); glFogfv(GL_FOG, fcolor);,