计算机图形学真实图形
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include
#include
/* Initialize material property, light source, lighting model,
* and depth buffer.
*/
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 lightPos[]={0.0f,0.0f,75.0f,1.0f};
GLfloat ambientLight[]={0.0f,0.0f,75.0f,1.0f};
GLfloat specular[]={0.0f,0.0f,75.0f,1.0f};
GLfloat specref[]={0.0f,0.0f,75.0f,1.0f};
GLfloat spotDir[]={0.0f,0.0f,75.0f,1.0f};
glClearColor (0.0, 0.0, 0.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);//设置光源的位置
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,ambientLight);
glLightfv(GL_LIGHT1,GL_DIFFUSE,ambientLight);
glLightfv(GL_LIGHT1,GL_SPECULAR,specular);
glLightfv(GL_LIGHT1,GL_POSITION,lightPos);
glLightf(GL_LIGHT1,GL_SPOT_CUTOFF,50.0f);
glEnable(GL_LIGHT1);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
glMaterialfv(GL_FRONT,GL_SPECULAR,specref);
glMateriali(GL_FRONT,GL_SHININESS,128);
glEnable(GL_LIGHTING);//启动光照
glEnable(GL_LIGHT0);//激活光源
glEnable(GL_LIGHT1);//激活光源
glEnable(GL_DEPTH_TEST);
}
/* 调用glut函数绘制一个球*/
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidSphere (1.0, 20, 16);//半径为1,20条纬线,16条经线
glFlush ();
}
/* 定义GLUT 的reshape 函数,w、h 分别是输出图形的窗口的宽和高*/
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);//创建平行视景体
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);
glLoadIdentity();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);//环境初始化
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);//显示模式初始化glutInitWindowSize (500, 500); //定义窗口大小
glutInitWindowPosition (100, 100);//定义窗口位置
glutCreateWindow (argv[0]);//显示窗口,窗口标题为执行函数名
init ();
glutDisplayFunc(display); //注册opengl绘图函数
glutReshapeFunc(reshape);//注册窗口大小改变时的相应函数
glutKeyboardFunc(keyboard);
glutMainLoop();//进入glut消息循环,开始执行程序
return 0;
}