实验7OpenGL光照Word文档格式.docx

上传人:b****2 文档编号:4374536 上传时间:2023-05-03 格式:DOCX 页数:8 大小:35.72KB
下载 相关 举报
实验7OpenGL光照Word文档格式.docx_第1页
第1页 / 共8页
实验7OpenGL光照Word文档格式.docx_第2页
第2页 / 共8页
实验7OpenGL光照Word文档格式.docx_第3页
第3页 / 共8页
实验7OpenGL光照Word文档格式.docx_第4页
第4页 / 共8页
实验7OpenGL光照Word文档格式.docx_第5页
第5页 / 共8页
实验7OpenGL光照Word文档格式.docx_第6页
第6页 / 共8页
实验7OpenGL光照Word文档格式.docx_第7页
第7页 / 共8页
实验7OpenGL光照Word文档格式.docx_第8页
第8页 / 共8页
亲,该文档总共8页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

实验7OpenGL光照Word文档格式.docx

《实验7OpenGL光照Word文档格式.docx》由会员分享,可在线阅读,更多相关《实验7OpenGL光照Word文档格式.docx(8页珍藏版)》请在冰点文库上搜索。

实验7OpenGL光照Word文档格式.docx

stdlib.h>

staticintyear=0,day=0;

voidinit(void){

GLfloatmat_specular[]={1.0,1.0,1.0,1.0};

GLfloatmat_shininess[]={50.0};

GLfloatlight_position[]={1.0,1.0,1.0,0.0};

GLfloatwhite_light[]={1.0,1.0,1.0,1.0};

GLfloatLight_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);

//光源2GL_LIGHT1

GLfloatmat_specular1[]={1.0,1.0,1.0,1.0};

GLfloatmat_shininess1[]={50.0};

GLfloatlight_position1[]={0.0,0.0,0.0,0.0};

GLfloatred_light[]={1.0,0.0,0.0,1.0};

GLfloatLight_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);

}

voiddisplay(void){

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glPushMatrix();

//定义太阳的材质并绘制太阳

{

GLfloatsun_mat_ambient[]={1.0f,0.0f,0.0f,1.0f};

//定义材质的环境光颜色,偏红色

GLfloatsun_mat_diffuse[]={0.5f,0.5f,0.0f,1.0f};

//定义材质的漫反射光颜色,偏红色

GLfloatsun_mat_specular[]={1.0f,0.0f,0.0f,1.0f};

//定义材质的镜面反射光颜色,红色

GLfloatsun_mat_emission[]={0.0f,0.0f,0.0f,1.0f};

//定义材质的辐射光颜色,为0

GLfloatsun_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);

}

GLfloatearth_mat_ambient[]={0.0f,0.0f,1.0f,1.0f};

//定义材质的环境光颜色,偏蓝色

GLfloatearth_mat_diffuse[]={0.0f,0.0f,0.5f,1.0f};

//定义材质的漫反射光颜色,偏蓝色

GLfloatearth_mat_specular[]={1.0f,0.0f,0.0f,1.0f};

GLfloatearth_mat_emission[]={0.0f,0.0f,0.0f,1.0f};

GLfloatearth_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);

//地球

 

{

GLfloatearth_mat_ambient[]={0.0f,1.0f,0.0f,1.0f};

//定义材质的环境光颜色,偏绿色

GLfloatearth_mat_diffuse[]={0.0f,0.5f,0.0f,1.0f};

//定义材质的漫反射光颜色,偏绿色

GLfloatearth_mat_specular[]={1.0f,.0f,0.0f,1.0f};

GLfloatearth_mat_emission[]={0.0f,0.0f,0.0f,1.0f};

GLfloatearth_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();

glutSwapBuffers();

glFlush();

}

voidreshape(intw,inth){

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);

}else{glOrtho(-1.5*(GLfloat)w/(GLfloat)h,1.5*(GLfloat)w/(GLfloat)h,-1.5,1.5,-10.0,10.0);

}

glMatrixMode(GL_MODELVIEW);

gluLookAt(0.0,0.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0);

voidkeyboard(unsignedcharkey,intx,inty){

switch(key){

case'

d'

:

day=(day+10)%360;

glutPostRedisplay();

break;

D'

day=(day-10)%360;

y'

year=(year+5)%360;

Y'

year=(year-5)%360;

case27:

exit(0);

default:

break;

intmain(intargc,char**argv){

glutInit(&

argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);

glutInitWindowSize(500,500);

glutInitWindowPosition(100,100);

glutCreateWindow(argv[0]);

init();

glutDisplayFunc(display);

glutReshapeFunc(reshape);

glutKeyboardFunc(keyboard);

glutMainLoop();

return0;

五、实验结果

以下是实验结果截图:

六、实验分析

实验中,两个主要函数,glMaterialfv(材质指定,单值材质参数,具体指针),设置图形材质,glLightfv(光源,属性名,属性值),用来设置光源。

通过这两个函数的配合,实现不同的灯光效果。

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2