计算机图形图像绘制技术第8讲上次课内容小结Colorambient,reflection=Colorambient,lightxColorambient,materialColordiffuse,reflection=L.NxColordiffuse,lightxColordiffuse,materialColorspecular,reflection=N.HshininessxColorspecular,lightxColorspecular,material上次课小结物体看上去有颜色,是它被光线照射的结果;如果光是白色的,那么呈现的就是物体本身的颜色,否则会是光色和表面本色综合的结果;物体表面被点光源照射后,会呈现三个区域:高光区、过渡色区和环境色区高光区是镜面反射的结果,亮度特别高,光源越强越明显;过渡色区是漫反射的结果,通常面积较大;与物体本色相一致环境色区则是不受光照部份的颜色,通常较暗;为物体的阴影部分;一个例子GLfloatmat_specular[]={1.0,1.0,1.0,1.0};GLfloatmat_shininess[]={50.0};GLfloatlight_position[]={1.0,1.0,1.0,0.0);glShadeModel(GL_SMOOTH);glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);glLightfv(GL_LIGHT0,GL_POSITION,light_position);glEnable(GL_LIGHTING);glEnable(GL_LIGHT0);glEnable(GL_DEPTH_TEST);glutSolidSphere(1.0,20,16);光源类型判断以下两图中,哪个图使用的是平行光源?主要内容光强的衰减聚光光源材质的发射光属性光与表面法线透明度雾光强的衰减glLightf(GL_LIGHT0,GL_CONSTANT_ATTENUATION,1.0);//setskcglLightf(GL_LIGHT0,GL_LINEAR_ATTENUATION,0.0);//setsklglLightf(GL_LIGHT0,GL_QUADRATIC_ATTENUATION,0.5);//setskqTIONIC_ATTENUAGL_QUADRATNATTENUATIOGL_LINEAR_IONT_ATTENUATGL_CONSTAN12qlcqlckkkddkdkk光源与目标点的距离衰减因子voidglLight{if}[v](GLenumlight,GLenumpname,TYPEparam);GLfloatamb_color[]={0.2,0.2,0.2,1.0};GLfloatdiff_color[]={1.0,1.0,1.0,1.0};GLfloatspec_color[]={1.0,1.0,1.0,1.0};GLfloatpos[]={0.0,1.0,0.0,1.0};//pointlightsourceat(0,1,0)glLightfv(GL_LIGHT1,GL_AMBIENT,amb_color);glLightfv(GL_LIGHT1,GL_DIFFUSE,diff_color);glLightfv(GL_LIGHT1,GL_SPECULAR,spec_color);glLightfv(GL_LIGHT1,GL_POSITION,pos);glLightf(GL_LIGHT1,GL_CONSTANT_ATTENUATION,1.0);//setsk0glLightf(GL_LIGHT1,GL_LINEAR_ATTENUATION,0.0);//setsk1glLightf(GL_LIGHT1,GL_QUADRATIC_ATTENUATION,0.5);//setsk2glEnable(GL_LIGHT1);例子程序启用衰减与未启用衰减的比较未启用衰减启用衰减聚光源光锥半角GL_SPOT_CUTOFF光锥方向GL_SPOT_DIRECTION光锥强度分布GL_EXPONENT衰减因子GL_SPOT_CUTOFFGL_SPOT_DIRECTIONfloatlightDirection[]={-1.0,-2.5,0.0};glLightf(GL_LIGHT2,GL_SPOT_CUTOFF,10.0);glLightfv(GL_LIGHT2,GL_SPOT_DIRECTION,lightDirection);glLightf(GL_LIGHT2,GL_SPOT_EXPONENT,5.0);一个聚光灯的例子floatLightPos[]={5.0,10,0,1};floatlightDiffuse[]={1.0f,.0f,1.0f};floatlightSpecular[]={0.92f,0.92f,0.92f};floatlightDirection[]={-1.0,-2.5,0.0};glEnable(GL_LIGHTING);glEnable(GL_LIGHT2);glLightfv(GL_LIGHT2,GL_POSITION,LightPos);//设置光源位置glLightfv(GL_LIGHT2,GL_DIFFUSE,lightDiffuse);//设置散射光颜色glLightfv(GL_LIGHT2,GL_SPECULAR,lightSpecular);//设置镜面反射光颜色glLightf(GL_LIGHT2,GL_SPOT_CUTOFF,10.0);glLightfv(GL_LIGHT2,GL_SPOT_DIRECTION,lightDirection);glLightf(GL_LIGHT2,GL_SPOT_EXPONENT,5.0);floatshininess=10;floatmaterialDiffuse[]={1.0f,0.0f,1.0f,1.0f};floatmaterialSpecular[]={1.0f,1.0f,1.0f,1.0f};glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,materialDiffuse);glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,materialSpecular);glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,&shininess);glutSolidTeapot(0.6);物体的发光特性M为RGBA强度,andlemissionis为物体发射光属性.•在OpenGL中,物发射光不依赖于其它光源与不作为一个新的光源,它只是计算自己的颜色•.IleMlemission=EmissiveComponentParameterNameDefaultValueMeaningGL_AMBIENT(0.0,0.0,0.0,1.0)ambientRGBAintensityoflightGL_DIFFUSE(1.0,1.0,1.0,1.0)diffuseRGBAintensityoflightGL_SPECULAR(1.0,1.0,1.0,1.0)specularRGBAintensityoflightGL_POSITION(0.0,0.0,1.0,0.0)(x,y,z,w)positionoflightGL_SPOT_DIRECTION(0.0,0.0,-1.0)(x,y,z)directionofspotlightGL_SPOT_EXPONENT0.0spotlightexponentGL_SPOT_CUTOFF180.0spotlightcutoffangleGL_CONSTANT_ATTENUATION1.0constantattenuationfactorGL_LINEAR_ATTENUATION0.0linearattenuationfactorGL_QUADRATIC_ATTENUATION0.0quadraticattenuationfactorvoidglLight{i,f}[v](GLenumlight,GLenumpname,TYPEparam);表面法线与光照光照后的颜色取决于表面法线光源位置视点位置NglNormal3f(x,y,z)多边形面的法线定义法线必须将法线标准化,即让其长度为单位长度;可调用函数glEnable(GL_NORMALIZE),让opengl自动进行法线的标准化;对于glut和glu库定义的对象,如glutSolidSphere,这些对象定义时已给出了法线。但对于程序员使用GL_TRIANGLES,GL_QUADSandGL_POLYGON生成的面,需要给出法线,才能进行光照计算。glBegin(GL_QUADS);glNormal3f(0,0,1);glVertex3f(0,0,1);glVertex3f(1,0,1);glVertex3f(1,1,1);glVertex3f(0,1,1);glEnd();光照计算总模型102materiallightmodelmaterial0,max**0,max****_*1*nishininessmateriallightmateriallightmateriallightiiqlcnsspecularspecularnLdiffusediffuseambientambienteffectspotlightdkdkkambientambientemissionrvertexcolo符号说明i=光源编号,asin{LIGHT0,LIGHT1,LIGHT2,…LIGHT7}d=所求点与光源的距离kc=GL_CONSTANT_ATTENUATIONkl=GL_LINEAR_ATTENUATIONkq=GL_QUADRATIC_ATTENUATION注意:如果光源为聚光源(spotlight),衰减因子被忽略spotlight_effect1,如果不是聚光光源0,如果是聚光光源,且顶点位于光照锥外max{vd,0}GL_SPOT_EXPONENT这里的v为单位向量,从聚光源位置指向所求顶点。d为聚光源方向L=单位向量,由所求顶点指向光源位置n=所求顶点的单位法向量s=(顶点与光源位置连线的单位向量)+(顶点与相机连线的单位向量)glLightModel[f,i]v该函数用于设设置光源模型参数voidglLightModel[f,i]v(GLenumpname,constGLfloat*params);参数说明pname表示所设置的光照模型,取值可以为GL_LIGHT_MODEL_AMBIENT//全局环境光GL_LIGHT_MODEL_LOCAL_VIEWER//局部视点GL_LIGHT_MODEL_TWO_SIDE//双面光照param是指定的光照模型的属性值3.4设置光照模型属性全局环境光该光源为场景提供不来自于任何特定光源的全局环境光例如:GLfloatImodel_ambient[]={0.2,0.2,0.2,1.0};glLightModelfv(GL_LIGHT_MODEL_AMBIENT,Imodel_ambient);3.4设置光照模型属性局部视点和无穷远视点镜面面反射的高光受视觉点位置的影响;镜面反射光强不但与顶点的法矢N相关,还与光源方向L和视线方向V密切相关;设置GL_LIGHT_MODEL_LOCAL_VIEWER属性设为GL_TRUE或GL_FALSE,可将场景中的视点定位在坐标原点或无穷远点。gl