实验7OpenGL光照

上传人:lisu****2020 文档编号:147613848 上传时间:2022-09-02 格式:DOC 页数:8 大小:50KB
返回 下载 相关 举报
实验7OpenGL光照_第1页
第1页 / 共8页
实验7OpenGL光照_第2页
第2页 / 共8页
实验7OpenGL光照_第3页
第3页 / 共8页
点击查看更多>>
资源描述
实验7 OpenGL光照一、 实验目的了解掌握OpenGL程序的光照与材质,能正确使用光源与材质函数设置所需的绘制效果。二、 实验内容(1)下载并运行Nate Robin教学程序包中的lightmaterial程序,试验不同的光照与材质系数;(2)运行示范代码1,了解光照与材质函数使用。三、 实验原理为在场景中增加光照,需要执行以下步骤:(1) 设置一个或多个光源,设定它的有关属性;(2) 选择一种光照模型;(3) 设置物体的材料属性。具体见教材第8章8.6节用OpenGL生成真实感图形的相关内容。四、 实验代码#include#includestatic int year =0,day=0;void init(void)GLfloat mat_specular=1.0,1.0,1.0,1.0;GLfloat mat_shininess=50.0;GLfloat light_position=1.0,1.0,1.0,0.0;GLfloat white_light=1.0,1.0,1.0,1.0;GLfloat Light_Model_Ambient=0.2,0.2,0.2,1.0;glClearColor(0.0,0.0,0.0,0.0);glShadeModel(GL_SMOOTH);/glMaterialfv(材质指定,单值材质参数,具体指针);glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);/镜面反射光的反射系数glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);/镜面反射指数/glLightfv(光源,属性名,属性值); glLightfv(GL_LIGHT0, GL_POSITION, light_position); /光源位置 glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light); /漫放射光分量强度 glLightfv(GL_LIGHT0, GL_SPECULAR, white_light); /折射光强度glLightModelfv(GL_LIGHT_MODEL_AMBIENT,Light_Model_Ambient);/光源2 GL_LIGHT1GLfloat mat_specular1=1.0,1.0,1.0,1.0;GLfloat mat_shininess1=50.0;GLfloat light_position1=0.0,0.0,0.0,0.0;GLfloat red_light=1.0,0.0,0.0,1.0;GLfloat Light_Model_Ambient1=0.2,0.2,0.2,1.0; glLightfv(GL_LIGHT1, GL_POSITION, light_position1); /光源位置 glLightfv(GL_LIGHT1, GL_DIFFUSE, red_light); /漫放射光分量强度 glLightfv(GL_LIGHT1, GL_SPECULAR, red_light); /折射光强度glLightModelfv(GL_LIGHT_MODEL_AMBIENT,Light_Model_Ambient1);/开启灯光glEnable(GL_LIGHTING);glEnable(GL_LIGHT0);glEnable(GL_LIGHT1);glEnable(GL_DEPTH_TEST); void display(void)glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glPushMatrix(); / 定义太阳的材质并绘制太阳 GLfloat sun_mat_ambient = 1.0f, 0.0f, 0.0f, 1.0f; /定义材质的环境光颜色,偏红色 GLfloat sun_mat_diffuse = 0.5f, 0.5f, 0.0f, 1.0f; /定义材质的漫反射光颜色,偏红色 GLfloat sun_mat_specular = 1.0f,0.0f, 0.0f, 1.0f; /定义材质的镜面反射光颜色,红色 GLfloat sun_mat_emission = 0.0f, 0.0f, 0.0f, 1.0f; /定义材质的辐射光颜色,为0 GLfloat sun_mat_shininess = 32.0f; glMaterialfv(GL_FRONT,GL_AMBIENT,sun_mat_ambient); glMaterialfv(GL_FRONT,GL_DIFFUSE,sun_mat_diffuse); glMaterialfv(GL_FRONT,GL_SPECULAR,sun_mat_specular); glMaterialfv(GL_FRONT,GL_EMISSION,sun_mat_emission); glMaterialf (GL_FRONT,GL_SHININESS,sun_mat_shininess); glutSolidSphere(0.5,40,16);/太阳glRotatef(GLfloat) year,0.0,1.0,0.0); glPushMatrix(); GLfloat earth_mat_ambient = 0.0f, 0.0f, 1.0f, 1.0f; /定义材质的环境光颜色,偏蓝色 GLfloat earth_mat_diffuse = 0.0f, 0.0f, 0.5f, 1.0f; /定义材质的漫反射光颜色,偏蓝色 GLfloat earth_mat_specular = 1.0f, 0.0f, 0.0f, 1.0f; /定义材质的镜面反射光颜色,红色 GLfloat earth_mat_emission = 0.0f, 0.0f, 0.0f, 1.0f; /定义材质的辐射光颜色,为0 GLfloat earth_mat_shininess = 30.0f; glMaterialfv(GL_FRONT, GL_AMBIENT, earth_mat_ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, earth_mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, earth_mat_specular); glMaterialfv(GL_FRONT, GL_EMISSION, earth_mat_emission); glMaterialf (GL_FRONT, GL_SHININESS, earth_mat_shininess); glTranslatef(0.8,0.0,0.0);glRotatef(GLfloat) day,0.0,1.0,0.5);/位置变化glutSolidSphere(0.2,20,8);/地球GLfloat earth_mat_ambient = 0.0f, 1.0f, 0.0f, 1.0f; /定义材质的环境光颜色,偏绿色 GLfloat earth_mat_diffuse = 0.0f, 0.5f, 0.0f, 1.0f; /定义材质的漫反射光颜色,偏绿色 GLfloat earth_mat_specular = 1.0f, .0f, 0.0f, 1.0f; /定义材质的镜面反射光颜色,红色 GLfloat earth_mat_emission = 0.0f, 0.0f, 0.0f, 1.0f; /定义材质的辐射光颜色,为0 GLfloat earth_mat_shininess = 30.0f; glMaterialfv(GL_FRONT, GL_AMBIENT, earth_mat_ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, earth_mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, earth_mat_specular); glMaterialfv(GL_FRONT, GL_EMISSION, earth_mat_emission); glMaterialf (GL_FRONT, GL_SHININESS, earth_mat_shininess); glTranslatef(0.4,0.0,0.0);glRotatef(GLfloat) day,0.0,1.0,0.0);glutSolidSphere(0.1,20,8);/月亮 glPopMatrix();glPopMatrix();glutSwapBuffers();glFlush();void reshape(int w,int h)glViewport(0,0,(GLsizei) w,(GLsizei) h);glMatrixMode(GL_PROJECTION);glLoadIdentity();if(w=h)glOrtho(-1.5,1.5,-1.5*(GLfloat)h/(GLfloat)w,1.5*(GLfloat)h/(GLfloat)w,-10.0,10.0);elseglOrtho(-1.5*(GLfloat)w/(GLfloat)h,1.5*(GLfloat)w/(GLfloat)h,-1.5,1.5,-10.0,10.0);glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(0.0,0.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0);void keyboard(unsigned char key, int x,int y)switch (key)case d:day=(day+10)%360;glutPostRedisplay();break;case D:day=(day-10)%360;glutPostRedisplay();break;case y:year=(year + 5)%360;glutPostRedisplay();break;case Y:year=(year-5)%360;glutPostRedisplay();break;case 27:exit(0);break;default:break;int main(int argc,char *argv)glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);glutInitWindowSize(500,500);glutInitWindowPosition(100,100);glutCreateWindow(argv0);init();glutDisplayFunc(display);glutReshapeFunc(reshape);glutKeyboardFunc(keyboard);glutMainLoop();return 0;五、 实验结果以下是实验结果截图:六、 实验分析实验中,两个主要函数,glMaterialfv(材质指定,单值材质参数,具体指针),设置图形材质,glLightfv(光源,属性名,属性值),用来设置光源。通过这两个函数的配合,实现不同的灯光效果。
展开阅读全文
相关资源
相关搜索

最新文档


当前位置:首页 > 建筑环境 > 建筑资料


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!