GLfloat no_mat[]={0.0,0.0,0.0,1.0};
GLfloat mat_diffuse[]={0.6,0.6,0.6,1.0};
GLfloat mat_specular[]={0.8,0.8,0.8,1.0};
GLfloat no_shininess[]={50.0};
GLfloat mat_emission[]={0.1,0.1,0.1,0.0};
GLfloat light_position[] = {1.0,1.0,1.0,0.0};
GLfloat light_ambient[] = {0.0,0.0,0.0,1.0};
GLfloat light_diffuse[] = {1.0,1.0,1.0,1.0};
GLfloat light_specular[] = {1.0,1.0,1.0,1.0};
GLfloat light1_position[] = {-20.0,20.0,10.0,1.0};
GLfloat light1_ambient[] = {0.1,0.1,0.1,1.0};
GLfloat light1_diffuse[] = {1.0,1.0,1.0,1.0};
GLfloat light1_specular[] = {1.0,1.0,1.0,1.0};
GLfloat light1_direction[] = {2.0,-2.0,-1.0};
glPushMatrix();
//
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);//启用光源
glEnable(GL_DEPTH_TEST);//启用深度测试
//
glEnable(GL_COLOR_MATERIAL);////开启光线跟踪
glLightfv(GL_LIGHT0, GL_POSITION, light_position);//光的位置
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); //光的环境强度
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);//光的散射强度
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);//光的镜面强度
glEnable(GL_LIGHT1);//启用光源(聚光灯)
//
glEnable(GL_DEPTH_TEST);
glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient); //光的环境强度
glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);//光的散射强度
glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular);//光的镜面强度
glLightfv(GL_LIGHT1, GL_POSITION, light1_position);//光的位置
glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 1.5);//常量衰减因子1.0
glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.1);//线性衰减因子0.0
glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.0);//二次衰减因子0.0
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 45.0);//聚光灯的切角180.0
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, light1_direction);//聚光灯方向的向量
glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 2.0);//聚光指数0.0
glEnable(GL_LIGHTING);//启用材质
glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);//材料的环境颜色
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);//材料的散射颜色
glMaterialfv(GL_FRONT, GL_SPECULAR,mat_specular);//材料的镜面颜色
glMaterialfv(GL_FRONT, GL_SHININESS,no_shininess);//镜面指数
glMaterialfv(GL_FRONT, GL_EMISSION,mat_emission);//材料的发射颜色
glutSolidSphere(10.0,50,50);
glPopMatrix();
同样的代码在artoolkit中效果
而在其他opengl程序中效果
|