简单光照和材质 2Word下载.docx
《简单光照和材质 2Word下载.docx》由会员分享,可在线阅读,更多相关《简单光照和材质 2Word下载.docx(16页珍藏版)》请在冰豆网上搜索。
比例:
50%
#include"
stdafx.h"
3dcaizhi.h"
#include<
math.h>
glut.h"
mmsystem.h>
//播放音乐函数的头文件
fmod.h"
////音频库的头文件
#pragmacomment(lib,"
fmodvc.lib"
)//把音频库加入到链接器中
FSOUND_STREAM*mp3back;
#definePI3.14159
floattheta=-90.0;
//rotatingangle
intinner=10,outer=80;
//torus'
sinner&
outerradius
floats=outer+4*inner+50;
floateyex=0,eyey=0,eyez=s;
//eyepointinitialposition
floatatx=0,aty=0,atz=0;
//atpointinitialposition
intww,hh;
//variableforwindows'
swidth
boolflag=true;
floatangle=0;
//foranglebetweenview-lineandzaxis
floatr=s;
//fordistancebetweeneyeandat
floatstep=r/10;
//forforwardandbackwordstep;
voidmymenu(intvalue);
//在程序头部声明菜单子函数
voidDisplay(void);
voidReshape(intw,inth);
voidmytime(intvalue);
voiddrawground();
voiddrawsphere();
voiddrawwall();
voidinit();
voidmykeyboard(unsignedcharkey,intx,inty);
intAPIENTRY_tWinMain(HINSTANCEhInstance,
HINSTANCEhPrevInstance,
LPTSTRlpCmdLine,
intnCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
char*argv[]={"
hello"
"
"
};
intargc=2;
//must/shouldmatchthenumberofstringsinargv
glutInit(&
argc,argv);
//初始化GLUT库;
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(500,500);
glutInitWindowPosition(1024/2-250,768/2-250);
glutCreateWindow("
Rotating3DWorld"
);
//创建窗口,标题为“Rotating3DWorld”;
glutReshapeFunc(Reshape);
init();
glutDisplayFunc(Display);
//用于绘制当前窗口;
glutKeyboardFunc(mykeyboard);
FSOUND_Stream_Play(FSOUND_FREE,mp3back);
glutCreateMenu(mymenu);
//创建菜单
glutAddMenuEntry("
线框模型"
1);
//第一个菜单名“线框模型”,设置为该菜单参数
实体模型"
2);
//第二个菜单名“实体模型”,设置为该菜单参数
音乐播放"
3);
//第三个菜单名“音乐播放”,设置为该菜单参数
音乐停止"
4);
//第四个菜单名“音乐停止”,设置为该菜单参数
glutAttachMenu(GLUT_RIGHT_BUTTON);
//菜单附到鼠标右键
glutTimerFunc(100,mytime,10);
glutMainLoop();
//表示开始运行程序,用于程序的结尾;
return0;
}
voidinit()
glClearColor(1,1,1,1);
glEnable(GL_DEPTH_TEST);
glPixelStorei(GL_PACK_ALIGNMENT,1);
//glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glShadeModel(GL_SMOOTH);
//颜色设为光滑模式k
//definelightposition1
GLfloatlight_position1[]={-outer,outer,outer+4*inner+50,0.0};
//GLfloatlight_position1[]={0,0,0,0.0};
GLfloatlight_position2[]={+outer,-outer,outer+inner,0.0};
//GLfloatlightcolor1
GLfloatlight_ambient1[]={1.0,1.0,1.0,1.0};
GLfloatlight_diffuse1[]={1.0,1.0,1.0,1.0};
GLfloatlight_specular1[]={1.0,1.0,1.0,1.0};
//GLfloatlightcolor2
GLfloatlight_ambient2[]={0.8,0.8,0.8,1.0};
GLfloatlight_diffuse2[]={0.8,0.8,0.8,1.0};
GLfloatlight_specular2[]={0.8,0.8,0.8,1.0};
//lightmodel-globallight
GLfloatlmodel_ambient[]={0.8,0.2,0.2,1.0};
//环境光
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,lmodel_ambient);
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_TRUE);
//setlightsourcelocation
glLightfv(GL_LIGHT0,GL_POSITION,light_position1);
glLightfv(GL_LIGHT1,GL_POSITION,light_position2);
//setlightsourcecolor;
glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient1);
glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse1);
glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular1);
glLightfv(GL_LIGHT1,GL_AMBIENT,light_ambient2);
glLightfv(GL_LIGHT1,GL_DIFFUSE,light_diffuse2);
glLightfv(GL_LIGHT1,GL_SPECULAR,light_specular2);
//setmaterialletmaterialfitscolor
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
//makelightsourceenable;
glEnable(GL_LIGHTING);
//启用光沿
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
//Colorshademode
//glShadeModel(GL_FLAT);
//Materialparametersetting
GLfloatmat_specular1[]={1.0,1.0,1.0,1.0};
GLfloatmat_shininess1[]={80.0};
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular1);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess1);
if(FSOUND_Init(44100,32,0))//把声音初始化为khz
{
//载入文件.mp3
mp3back=FSOUND_Stream_OpenFile("
JoelHanson-TravelingLight.mp3"
FSOUND_LOOP_NORMAL,0);
}
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
voidmykeyboard(unsignedcharkey,intx,inty)
switch(key)
{case'
W'
:
case'
w'
//向前直走
//yourcodeHere
eyex=eyex-step*sin(angle*PI/180.0);
eyez=eyez-step*cos(angle*PI/180.0);
atx=atx-step*sin(angle*PI/180.0);
atz=atz-step*cos(angle*PI/180.0);
break;
S'
s'
//向后退
eyex=eyex+step*sin(angle*PI/180.0);
eyez=eyez+step*cos(angle*PI/180.0);
atx=atx+step*sin(angle*PI/180.0);
atz=atz+step*cos(angle*PI/180.0);
A'
a'
//左看
angle=angle+1;
atx=eyex-r*sin(angle*PI/180.0);
atz=eyez-r*cos(angle*PI/180.0);
D'
d'
//右看
angle=angle-1;
}
glutPostRedisplay();
//参数修改后调用重画函数,屏幕图形将发生改变
voidDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eyex,eyey,eyez,atx,aty,atz,0,1,0);
glPushMatrix();
glColor3f(0.0,0.0,1.0);
drawwall();
glColor3f(1.0,0,0);
drawground();
drawsphere();
glPopMatrix();
glutSwapBuffers();
voiddrawsphere()
floattr;
tr=(outer+3*inner);
glRotatef(theta,0,1,0);
glColor3f(1.0,0,1.0);
//glutWireTorus(inner,outer,30,50);
glutSolidTorus(inner,outer,30,50);
glTranslatef(outer,0,0);
glTranslatef(-outer,0,0);
glTranslatef(tr,0,0);
glRotatef(-45,1,0,0);
glColor3f(0.0,1.0,0);
glutSolidSphere(inner,20,20);
voiddrawground()
//ground
for(inti=-outer-4*inner;
i<
outer+4*inner;
i+=2*inner)
if(flag==0)
flag=1;
else
flag=0;
for(intj=-outer-4*inner;
j<
j+=2*inner)
{
if(flag==0)
{
glColor3f(1.0,1.0,0.0);
flag=1;
}
else
glColor3f(1.0,1.0,1.0);
flag=0;
glBegin(GL_QUADS);
glVertex3d(j,-outer-4*inner,i);
glVertex3d(j,-outer-4*inner,i+2*inner);
glVertex3d(j+2*inner,-outer-4*inner,i+2*inner);
glVertex3d(j+2*inner,-outer-4*inner,i);
glEnd();
}
//top
for(inti=-outer-4*inner;
glVertex3d(j,outer+4*inner,i);
glVertex3d(j,outer+4*inner,i+2*inner);
glVertex3d(j+2*inner,outer+4*inner,i+2*inner);
glVertex3d(j+2*inner,outer+4*inner,i);
voiddrawwall()
{inti,j;
intflag=0;
//left
for(i=-outer-4*inner;
if(flag==0)
flag=1;
else
flag=0;
for(j=-outer-4*inner;
{
if(flag==0)
glColor3f(1.0,1.0,0.0);
else
glColor3f(1.0,1.0,1.0);
glBegin(GL_QUADS);
glVertex3d(-outer-4*inner,j,i);
glVertex3d(-outer-4*inner,j+2*inner,i);
glVertex3d(-outer-4*inner,j+2*inner,i+2*inner);
glVertex3d(-outer-4*inner,j,i+2*inner);
glEnd();
//right
=outer+4*inner-2*inner;
i+=2*inner)//forz
j+=2*inner)//fory
{
glVertex3f(outer+4*inner,j,i);
glVertex3f(outer+4*inner,j+2*inner,i);
glVertex3f(outer+4*inner,j+2*inner,i+2*inner);
glVertex3f(outer+4*inner,j,i+2*inner);
//front
glVertex3f(j,i,-outer-4*inner);
glVertex3f(j+2*inner,i,-outer-4*inner);
glVertex3f(j+2*inner,i+2*inner,-outer-4*inner);
glVertex3f(j,i+2*inner,-outer-4*inner);
voidmytime(intvalue)
theta+=0.5;
if(theta>
=360.0)theta-=360.0;
voidReshape(GLsizeiw,GLsizeih)
glMatrixMode(GL_PROJECTION);
gluPerspective(90,w/h,10,2*outer+8*inner+250+1000);
glViewport(0,0,w,h);
ww=w;
hh=h;
voidmymenu(intvalue)
//如果菜单参数为,则执行线框模式
if(value==1)
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
//如果菜单参数为,则执行实心模式
if(value==2)
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
//如果菜单参数为,则播放音乐
if(value==3)
FSOUND_Stream_Play(FSOUND_FREE,mp3back);
//如果菜单参数为,则音乐停止
if(value==4)
FSOUND_Stream_Stop(mp3back);
}
实验课完成程度与表现自我评价:
20%
实验的体会、收获与建议(结论)
30%
评阅教师特殊评语:
评阅教师:
日期: