OpenGL-聚光灯-spot

4.聚光灯

4.1 glLightModelfv()光照模型

GLfloat lmodel_ambient[]={0.2,0.2,0.2,1.0};//微弱环境光,使物体可见
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,lmodel_ambient);

4.2 光源属性参数

GLfloat light1_position[]={1.0,1.0,5.0,1.0};
GLfloat light1_diffuse[]={1.0,1.0,1.0,1.0}; 
GLfloat light1_specular[]={1.0,1.0,1.0,1.0};
glLightfv(GL_LIGHT1,GL_DIFFUSE,light1_diffuse);
glLightfv(GL_LIGHT1,GL_SPECULAR,light1_specular);
glLightfv(GL_LIGHT1,GL_POSITION,light1_position);

4.3 材质属性参数

GLfloat mat_specular[]={1.0,1.0,1.0,1.0};
GLfloat mat_shininess[]={128.0};
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);    //指定当前材质属性 镜面反射颜色
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);  //镜面反射指数

4.4 开启聚光灯功能

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT1);

代码示例:

void init()
{
	GLfloat mat_specular[]={1.0,1.0,1.0,1.0};
	GLfloat mat_shininess[]={128.0};

	GLfloat lmodel_ambient[]={0.2,0.2,0.2,1.0};//微弱环境光,使物体可见
	glClearColor(0.0,0.0,0.0,1.0);
	glShadeModel(GL_SMOOTH);//GL_SMOOTH

	//聚光灯light1
	GLfloat light1_position[]={1.0,1.0,5.0,1.0};
	GLfloat spot_direction[]={0.0,0.0,-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_ambient[]={0.2,0.2,0.2,1.0};

	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);
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT,lmodel_ambient);

	glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);    //指定当前材质属性 镜面反射颜色
	glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);  //镜面反射指数

	glLightf(GL_LIGHT1,GL_SPOT_CUTOFF,50.0);//角度
	glLightfv(GL_LIGHT1,GL_SPOT_DIRECTION,spot_direction);//方向
	glLightf(GL_LIGHT1,GL_SPOT_EXPONENT,2.0);//*/

	glEnable(GL_LIGHTING);   
	glEnable(GL_LIGHT1);
	glEnable(GL_DEPTH_TEST);  
}

实例图片:


你可能感兴趣的:(OpenGL)