// 范例P124.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <gl/glut.h>
static int spin=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); //清除所有像素
GLfloat light_position[]={5,5,10,1.0};//光源1位置
glPushMatrix();
glRotated((GLdouble)spin,1.0, 0.0, 0.0);
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
glPopMatrix();
glLoadIdentity();
glTranslated(0,0,-3);
glutSolidSphere(1.0,20,16);
/*在缓存中绘制4对顶点坐标的矩形*/
glFlush(); //立即显示出来
}
void init()
{
glClearColor(0.0,0.0,0.0,0.0); //用黑色清除背景
GLfloat mat_specular[]={1.0, 1.0, 1.0, 1.0}; //材质镜面反射色
GLfloat mat_shininess[]={50.0}; //光洁度,即物体反射比率
GLfloat lmodel_ambient[]={0.2,0.2,0.2,1.0}; //全局环境光
//第一个光源
GLfloat light_position[]={5,5,10,1.0}; //光源1位置
GLfloat white_light[]={1.0,1.0,1.0,1.0}; //光源1散射光与镜面反射光相同
//第二个光源
GLfloat light1_diffuse[]={1.0, 0.0, 0.0, 1.0}; //光源2散射光
GLfloat light1_specular[]={1.0, 0.0, 0.0, 1.0}; //光源2镜面反射光
GLfloat light1_position[]={-2.0, -2.0, 3.0, 1.0};//光源2位置
GLfloat spot_direction[]={0.0, 0.0, 0.0}; //光源2聚光灯方向
glShadeModel(GL_SMOOTH);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
//光源2设置
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);
glLightf(GL_LIGHT1,GL_LINEAR_ATTENUATION,0.5);
glLightf(GL_LIGHT1,GL_QUADRATIC_ATTENUATION,0.2);
glLightf(GL_LIGHT1,GL_SPOT_CUTOFF,-45.0);
glLightfv(GL_LIGHT1,GL_SPOT_DIRECTION,spot_direction);
glLightf(GL_LIGHT1,GL_SPOT_EXPONENT,100);
//光源1设置
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
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_LIGHT1);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
void reshape(int w,int h)
{
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL
评论