OpenGL学习十四:光照1

环境光:那些在环境中进行了充分的散射,无法辨别其方向的光,它似乎来自所有方向
散射光:来自某个方向。因此从它正面照射的表面,看起来更明亮以下。
镜面光:来自特定的方向,并且倾向于从表面的某个特定的方向放射

右图1:环境光 右图2:散射光 右图3:环境光与散射光的混合
 
OpenGL学习十四:光照1     OpenGL学习十四:光照1   OpenGL学习十四:光照1
创建光源
GLAPI void GLAPIENTRY glLightfv (GLenum light, GLenum pname, const GLfloat *params);

light:对应光源,一般支持0~7号光源
pname:光照信息参数
GL_AMBIENT                    环境光
GL_DIFFUSE                    散射光
GL_SPECULAR                   镜面光
GL_POSITION                   光源位置
GL_CONSTANT_ATTENUATION       常量衰减因子
GL_LINEAR_ATTENUATION         线性衰减因子
GL_QUADRATIC_ATTENUATION      二次衰减因子
params:参数

对于GL_LIGHT0与其他光源默认值不一致GL_LIGHT0的环境光和散射光的默认值是(1,1,1,1)
光源位置
GLfloat light_position[] = { x,y,z,w};
glLightfv(GL_LIGHT2, GL_POSITION, light_position);

w=0 为方向性光源
w!=0 为位置性光源
右图1:方向性光源(1,1,1,0)
右图2:位置性光源(1,1,1,1)
右图3:位置性光源(1,1,5,1)
 OpenGL学习十四:光照1    OpenGL学习十四:光照1   OpenGL学习十四:光照1
光的衰减
衰减因子=1/k1+k2d+k3d*d
d=光源与顶点之间的距离
k1=GL_CONSTANT_ATTENUATION
k2=GL_LINEAR_ATTENUATION
k3=GL_QUADRATIC_ATTENUATION

说明光的衰减特效要求光源位位置性光源
右图1:光源位置z=5,k1=1
右图2:光源位置z=5,k1=3

OpenGL学习十四:光照1    OpenGL学习十四:光照1
材料属性
GLAPI void GLAPIENTRY glMaterialfv (GLenum face, GLenum pname, const GLfloat *params);

face:GL_FRONT,GL_BACK,GL_FRONT_AND_BACK
pname:GL_AMBIENT                    材料的环境颜色
      GL_DIFFUSE                    材料的散射颜色
      GL_SPECULAR                   材料的镜面颜色
      GL_SHININESS                  镜面指数
      GL_AMBIENT_AND_DIFFUSE        材料的环境和散射颜色
      GL_EMSSION                    材料的发射颜色
      GL_COLOR_INDEXS               颜色索引

球1:
  GLfloat mat_diffuse[] = { 0.1, 0.5, 0.8, 1.0 };
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);

球2:
   GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
   glMaterialfv(GL_FRONT, GL_SHININESS, 5);
球3:
   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
   glMaterialfv(GL_FRONT, GL_SHININESS, 180)
球4:
   GLfloat mat_emission[] = {0.3, 0.2, 0.2, 0.0};
   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
   glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
 OpenGL学习十四:光照1
镜面反射
OPENGL允许我们在不存在反射光的情况下通过设置反射材料达到效果,并控制亮点的大小和亮点(GL_SHININESS)其实范围在【0~128】值越小亮度越大 亮点越小
发射颜色光
通过GL_EMISSION参数指定RGBA颜色值,可以使物体看上去好像发射这种颜色的光

材料颜色模式
glEnable(GL_COLOR_METERIAL)
glcolor3f(0.5,0.2,0.6);
glColorMeterial(GL_FRONT,GL_AMBIENT)

 

 

环境散射衰减光:

#include "header.h"	

 float roar=0.0f;
 GLfloat attentation[]={1.0};
 GLfloat light_position[] = { 1.0, 1.0,1, 0};

void init(void) 
{

   GLfloat ambient[] =  { 0.5, 0.5, 0.5, 0.0 };
   GLfloat diffuse[] = { 1.0, 0.0, 0.0, 0.0 };

   glClearColor (0.0, 0.0, 0.0, 0.0);


   glLightfv(GL_LIGHT1, GL_POSITION, light_position);
   glLightfv(GL_LIGHT1,GL_AMBIENT,ambient);
   glLightfv(GL_LIGHT2, GL_POSITION, light_position);
   glLightfv(GL_LIGHT2,GL_DIFFUSE,diffuse);
   glLightfv(GL_LIGHT3, GL_POSITION, light_position);
   glLightfv(GL_LIGHT3,GL_DIFFUSE,diffuse);
   glLightfv(GL_LIGHT3,GL_AMBIENT,ambient);

   glLightfv(GL_LIGHT1,GL_CONSTANT_ATTENUATION,attentation);
   glLightfv(GL_LIGHT2,GL_CONSTANT_ATTENUATION,attentation);
   glLightfv(GL_LIGHT3,GL_CONSTANT_ATTENUATION,attentation);


   glEnable(GL_LIGHTING);
   glEnable(GL_LIGHT0);

   glEnable(GL_DEPTH_TEST);
}

void display(void)
{
   glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glLoadIdentity();
    glutSolidSphere(1.0, 16, 16);

   glFlush ();
}

void reshape (int w, int h)
{
   glViewport (0, 0, (GLsizei) w, (GLsizei) h);
   glMatrixMode (GL_PROJECTION);
   glLoadIdentity();
   if (w <= h)
      glOrtho (-2.5, 2.5, -2.5*(GLfloat)h/(GLfloat)w,
         2.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
   else
      glOrtho (-2.5*(GLfloat)w/(GLfloat)h,
         2.5*(GLfloat)w/(GLfloat)h, -2.5, 2.5, -10.0, 10.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}

void keyboard(unsigned char key, int x, int y)
{
   switch (key) 
   {
	 case '1':
		 glEnable(GL_LIGHTING);
		 glEnable(GL_LIGHT1);
		 glDisable(GL_LIGHT0);
		 glDisable(GL_LIGHT2);
		 glDisable(GL_LIGHT3);
     break;

	 case '2':
		 glEnable(GL_LIGHTING);
		 glEnable(GL_LIGHT2);
		 glDisable(GL_LIGHT0);
		 glDisable(GL_LIGHT1);
		 glDisable(GL_LIGHT3);
		 break;
	 case '3':
		 glEnable(GL_LIGHTING);
		 glEnable(GL_LIGHT3);
		 glDisable(GL_LIGHT0);
		 glDisable(GL_LIGHT2);
		 glDisable(GL_LIGHT1);
		 break;
	 case '0':
		 glEnable(GL_LIGHTING);
		 glEnable(GL_LIGHT0);
		 glDisable(GL_LIGHT1);
		 glDisable(GL_LIGHT2);
		 glDisable(GL_LIGHT3);
		 break;
	 case 'A':
		light_position[0]=1;
		light_position[1]=1;
		light_position[2]=1;
		light_position[3]=0;
		glLightfv(GL_LIGHT1, GL_POSITION, light_position);
		glLightfv(GL_LIGHT2, GL_POSITION, light_position);
		glLightfv(GL_LIGHT3, GL_POSITION, light_position);
		break;
	 case 'S':
		 light_position[0]=1;
		 light_position[1]=1;
		 light_position[2]=1;
		 light_position[3]=1;
		 glLightfv(GL_LIGHT1, GL_POSITION, light_position);
		 glLightfv(GL_LIGHT2, GL_POSITION, light_position);
		 glLightfv(GL_LIGHT3, GL_POSITION, light_position);
		 break;
	 case 'D':
		 light_position[0]=1;
		 light_position[1]=1;
		 light_position[2]=5;
		 light_position[3]=1;
		 glLightfv(GL_LIGHT1, GL_POSITION, light_position);
		 glLightfv(GL_LIGHT2, GL_POSITION, light_position);
		 glLightfv(GL_LIGHT3, GL_POSITION, light_position);
		 break;
	 case 'Z':
		 light_position[0]=1;
		 light_position[1]=1;
		 light_position[2]=5;
		 light_position[3]=1;
		 glLightfv(GL_LIGHT1, GL_POSITION, light_position);
		 glLightfv(GL_LIGHT2, GL_POSITION, light_position);
		 glLightfv(GL_LIGHT3, GL_POSITION, light_position);
		 attentation[0]=1;
		 glLightfv(GL_LIGHT1,GL_CONSTANT_ATTENUATION,attentation);
		 glLightfv(GL_LIGHT2,GL_CONSTANT_ATTENUATION,attentation);
		 glLightfv(GL_LIGHT3,GL_CONSTANT_ATTENUATION,attentation);

		 break;
	 case 'X':
		 light_position[0]=1;
		 light_position[1]=1;
		 light_position[2]=5;
		 light_position[3]=1;
		 attentation[0]=3;
		 glLightfv(GL_LIGHT1, GL_POSITION, light_position);
		 glLightfv(GL_LIGHT2, GL_POSITION, light_position);
		 glLightfv(GL_LIGHT3, GL_POSITION, light_position);
		 glLightfv(GL_LIGHT1,GL_CONSTANT_ATTENUATION,attentation);
		 glLightfv(GL_LIGHT2,GL_CONSTANT_ATTENUATION,attentation);
		 glLightfv(GL_LIGHT3,GL_CONSTANT_ATTENUATION,attentation);
		 break;
   }
   glutPostRedisplay();
} 

void rotate()
{
	roar+=0.0001f;
	glutPostRedisplay();
}

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); 
   glutReshapeFunc(reshape);
   glutKeyboardFunc(keyboard);
   glutMainLoop();
   return 0;
}

 移动光源

#include "header.h"

static int spin = 0;

void init(void) 
{
   glClearColor (0.0, 0.0, 0.0, 0.0);
   glShadeModel (GL_SMOOTH);
   glEnable(GL_LIGHTING);
   glEnable(GL_LIGHT0);
   glEnable(GL_DEPTH_TEST);
}

void display(void)
{
   GLfloat position[] = { 0.0, 0.0, 1.5, 1.0 };

   glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glPushMatrix ();
   gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

   glPushMatrix ();
   glRotated ((GLdouble) spin, 1.0, 0.0, 0.0);
   glLightfv (GL_LIGHT0, GL_POSITION, position);

   glTranslated (0.0, 0.0, 1.5);
   glDisable (GL_LIGHTING);
   glColor3f (0.0, 1.0, 1.0);
   glutWireCube (0.1);
   glEnable (GL_LIGHTING);
   glPopMatrix ();

   glutSolidTorus (0.275, 0.85, 8, 15);
   glPopMatrix ();
   glFlush ();
}

void reshape (int w, int h)
{
   glViewport (0, 0, (GLsizei) w, (GLsizei) h);
   glMatrixMode (GL_PROJECTION);
   glLoadIdentity();
   gluPerspective(40.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}

void mouse(int button, int state, int x, int y)
{
   switch (button) {
      case GLUT_LEFT_BUTTON:
         if (state == GLUT_DOWN) {
            spin = (spin + 30) % 360;
            glutPostRedisplay();
         }
         break;
      default:
         break;
   }
}

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); 
   glutReshapeFunc(reshape);
   glutMouseFunc(mouse);
   glutKeyboardFunc(keyboard);
   glutMainLoop();
   return 0;
}

 

 光源材料

#include "header.h"


void init(void)
{
   GLfloat ambient[] = { 0.0, 0.0, 0.0, 1.0 };
   GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
   GLfloat position[] = { 0.0, 3.0, 2.0, 0.0 };
   GLfloat lmodel_ambient[] = { 0.4, 0.4, 0.4, 1.0 };
   GLfloat local_view[] = { 0.0 };

   glClearColor(0.0, 0.1, 0.1, 0.0);
   glEnable(GL_DEPTH_TEST);
   glShadeModel(GL_SMOOTH);

   glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
   glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
   glLightfv(GL_LIGHT0, GL_POSITION, position);
   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
   glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);

   glEnable(GL_LIGHTING);
   glEnable(GL_LIGHT0);
}


void display(void)
{
   GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 };
   GLfloat mat_ambient[] = { 0.7, 0.7, 0.7, 1.0 };
   GLfloat mat_ambient_color[] = { 0.8, 0.8, 0.2, 1.0 };
   GLfloat mat_diffuse[] = { 0.1, 0.5, 0.8, 1.0 };
   GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
   GLfloat no_shininess[] = { 0.0 };
   GLfloat low_shininess[] = { 5.0 };
   GLfloat high_shininess[] = { 100.0 };
   GLfloat mat_emission[] = {0.3, 0.2, 0.2, 0.0};

   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


   glPushMatrix();
   glTranslatef (-3.75, 3.0, 0.0);
   glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
   glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
   glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
   glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
   glutSolidSphere(1.0, 16, 16);
   glPopMatrix();


   glPushMatrix();
   glTranslatef (-1.25, 3.0, 0.0);
   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, low_shininess);
   glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
   glutSolidSphere(1.0, 16, 16);
   glPopMatrix();

   glPushMatrix();
   glTranslatef (1.25, 3.0, 0.0);
   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, high_shininess);
   glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
   glutSolidSphere(1.0, 16, 16);
   glPopMatrix();


   glPushMatrix();
   glTranslatef (3.75, 3.0, 0.0);
   glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
   glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
   glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
   glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
   glutSolidSphere(1.0, 16, 16);
   glPopMatrix();


   glPushMatrix();
   glTranslatef (-3.75, 0.0, 0.0);
   glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
   glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
   glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
   glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
   glutSolidSphere(1.0, 16, 16);
   glPopMatrix();

   glPushMatrix();
   glTranslatef (-1.25, 0.0, 0.0);
   glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
   glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
   glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
   glutSolidSphere(1.0, 16, 16);
   glPopMatrix();

   glPushMatrix();
   glTranslatef (1.25, 0.0, 0.0);
   glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
   glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
   glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
   glutSolidSphere(1.0, 16, 16);
   glPopMatrix();


   glPushMatrix();
   glTranslatef (3.75, 0.0, 0.0);
   glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
   glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
   glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
   glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
   glutSolidSphere(1.0, 16, 16);
   glPopMatrix();

   glPushMatrix();
   glTranslatef (-3.75, -3.0, 0.0);
   glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
   glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
   glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
   glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
   glutSolidSphere(1.0, 16, 16);
   glPopMatrix();


   glPushMatrix();
   glTranslatef (-1.25, -3.0, 0.0);
   glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
   glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
   glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
   glutSolidSphere(1.0, 16, 16);
   glPopMatrix();


   glPushMatrix();
   glTranslatef (1.25, -3.0, 0.0);
   glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
   glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
   glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
   glutSolidSphere(1.0, 16, 16);
   glPopMatrix();


   glPushMatrix();
   glTranslatef (3.75, -3.0, 0.0);
   glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
   glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
   glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
   glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
   glutSolidSphere(1.0, 16, 16);
   glPopMatrix();

   glFlush();
}

void reshape(int w, int h)
{
   glViewport(0, 0, w, h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   if (w <= (h * 2))
      glOrtho (-6.0, 6.0, -3.0*((GLfloat)h*2)/(GLfloat)w, 
         3.0*((GLfloat)h*2)/(GLfloat)w, -10.0, 10.0);
   else
      glOrtho (-6.0*(GLfloat)w/((GLfloat)h*2), 
         6.0*(GLfloat)w/((GLfloat)h*2), -3.0, 3.0, -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 (600, 450);
   glutCreateWindow(argv[0]);
   init();
   glutReshapeFunc(reshape);
   glutDisplayFunc(display);
   glutKeyboardFunc (keyboard);
   glutMainLoop();
   return 0; 
}

 材料颜色模式

#include "header.h"

GLfloat diffuseMaterial[4] = { 0.5, 0.5, 0.5, 1.0 };


void init(void) 
{
   GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
   GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };

   glClearColor (0.0, 0.0, 0.0, 0.0);
   glShadeModel (GL_SMOOTH);
   glEnable(GL_DEPTH_TEST);
   glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMaterial);
   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
   glMaterialf(GL_FRONT, GL_SHININESS, 25.0);
   glLightfv(GL_LIGHT0, GL_POSITION, light_position);
   glEnable(GL_LIGHTING);
   glEnable(GL_LIGHT0);

   glColorMaterial(GL_FRONT, GL_DIFFUSE);
   glEnable(GL_COLOR_MATERIAL);
}

void display(void)
{
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glutSolidSphere(1.0, 20, 16);
   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();
}

void mouse(int button, int state, int x, int y)
{
   switch (button) {
      case GLUT_LEFT_BUTTON:
         if (state == GLUT_DOWN) {
            diffuseMaterial[0] += 0.1;
            if (diffuseMaterial[0] > 1.0)
               diffuseMaterial[0] = 0.0;
            glColor4fv(diffuseMaterial);
            glutPostRedisplay();
         }
         break;
      case GLUT_MIDDLE_BUTTON:
         if (state == GLUT_DOWN) {
            diffuseMaterial[1] += 0.1;
            if (diffuseMaterial[1] > 1.0)
               diffuseMaterial[1] = 0.0;
            glColor4fv(diffuseMaterial);
            glutPostRedisplay();
         }
         break;
      case GLUT_RIGHT_BUTTON:
         if (state == GLUT_DOWN) {
            diffuseMaterial[2] += 0.1;
            if (diffuseMaterial[2] > 1.0)
               diffuseMaterial[2] = 0.0;
            glColor4fv(diffuseMaterial);
            glutPostRedisplay();
         }
         break;
      default:
         break;
   }
}

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); 
   glutReshapeFunc(reshape);
   glutMouseFunc(mouse);
   glutKeyboardFunc(keyboard);
   glutMainLoop();
   return 0;
}

 

你可能感兴趣的:(OpenGL)