四、光照
相关函数
glMaterialfv
指定用于光照计算的当前材质属性。参数face的取值可以是GL_FRONT、GL_BACK或GL_FRONT_AND_BACK,指出材质属性将应用于物体的哪面。
#include<Windows.h>
#include<gl/glut.h>
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
void init(void)
{
GLfloat mat_specular[]={1.0,1.0,1.0,1.0};
GLfloat mat_sunshine[]={50.0};
GLfloat light_postion[]={1.0,1.0,1.0,0.0};
GLfloat white_light []={1.0,1.0,1.0,1.0};
GLfloat lmodel_ambient[]={0.1,0.1,0.1,1.0};
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_sunshine);
glLightfv(GL_LIGHT0,GL_POSITION,light_postion);
glLightfv(GL_LIGHT0,GL_DIFFUSE,white_light);
glLightfv(GL_LIGHT0,GL_SPECULAR,white_light);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,lmodel_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
void disply()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSolidSphere(1.0,100,100);
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);
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();
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);//指定显示模式
glutInitWindowSize(500,500);//指定窗口大小
glutInitWindowPosition(100,100);//指定窗口左上角位置
glutCreateWindow("OpenGL");//创建窗口标题为OpenGL的窗口
init();
glutDisplayFunc(disply);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}