资源描述:
《OpenGL编程轻松入门之使用光照和材质》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、OpenGL编程轻松入门之使用光照和材质2006-05-1811:24作者:黄燕出处:天极开发责任编辑:方舟使用了光照和材质可以使物体更逼真,具有立体感。例4就是没有使用光照使呈现在我们眼前的物体茶壶和立方体没有立体感。 例6:绘制三个使用不同材质的球体。#include#includeGLfloatlight_position[]={0.0,3.0,6.0,0.0};GLfloatno_mat[]={0.0,0.0,0.0,1.0};GLfloatmat_grey_ambient[]={0
2、.5,0.5,0.5,1.0};GLfloatmat_red_ambient[]={0.0,0.0,1.0,1.0};GLfloatmat_diffuse[]={0.8,0.2,0.5,1.0};GLfloatmat_specular[]={1.0,1.0,1.0,1.0};GLfloatno_shininess[]={0.0};GLfloatlow_shininess[]={5.0};GLfloathigh_shininess[]={100.0};GLfloatmat_emission[]={0.3,0.2,0.2,0.0};void
3、myInit(void){ glLightfv(GL_LIGHT2,GL_POSITION,light_position);//设置光源 glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS);//指定深度比较中使用的数值 glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glShadeModel(GL_SMOOTH);}voiddisplay(void){ glClear(GL_COLOR_BUFFER_BIT
4、GL_DEPTH_BUFFER_BIT); glP
5、ushMatrix(); /*为光照模型指定材质参数*/ glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse); glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular); glMaterialfv(GL_FRONT,GL_SHININESS,high_shininess); glMaterialfv(GL_FRONT,GL_EMISSION,no_mat); glColorMaterial(GL_FRONT,GL_AMBIENT);//使材质色跟踪当前颜色 g
6、lEnable(GL_COLOR_MATERIAL); /*第一个球形*/ glPushMatrix(); glColor3fv(no_mat); glTranslatef(-2.5,1.5,0.0); glRotatef(15.0,1.0,0.0,0.0); glutSolidSphere(1.2,20.0,20.0); glPopMatrix(); /*第二个球形*/ glPushMatrix(); glColor3fv(mat_grey_ambient); glRotatef(15.0,1.0,0.0,0.0); glutSoli
7、dSphere(1.2,20.0,20.0); glPopMatrix(); /*第三个球形*/ glPushMatrix(); glColor3fv(mat_red_ambient); glTranslatef(2.5,-1.5,0.0); glRotatef(15.0,1.0,0.0,0.0); glutSolidSphere(1.2,20.0,20.0); glPopMatrix(); glDisable(GL_COLOR_MATERIAL); glPopMatrix(); glFlush();}voidmyReshape(int
8、w,inth){ glViewport(0,0,(GLsizei)w,(GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(w<=h) glOrtho(-5.5,5.5,-5.5*(GLfloat)h/(GLfloat)w,5.5*(GLfloat)h/(GLfloat)w,-5.5,5.5); else glOrtho(-5.5*(GLfloat)w/(GLfloat)h,5.5*(GLfloat)w/(GLfloat)h,-5.5,5.5,-5.5,5.5)
9、; glMatrixMode(GL_MODELVIEW); glLoadIdentity();}intmain(intargc,char**argv){ /*初始化*/ glutInit(&argc,arg