AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > VC编程

OpenGL编程轻松入门之特殊效果操作

51自学网 2015-08-30 http://www.wanshiok.com

  例9:绘制一个被雾化的圆锥体,如图十所示,为了观察不同的雾化参数,程序中加入了键盘操作。"shift+1"," shift+2"键分别增加和减弱雾化浓度,"shift+3"设置雾化的起始点和终止点,"shift+4"和"shift+5"键改变雾化方程,"shift+6"将雾化颜色由白改为绿色,如图十一所示。

#include <stdlib.h>
#include <GL/glut.h>

GLfloat light_ambient[] = {0.1,0.1,0.1,0.0};
GLfloat light_diffuse[] = {1.0,1.0,1.0,0.0};
GLfloat light_specular[] = {1.0,1.0,1.0,0.0};
GLfloat light_position[] = {-10.0,0.0,5.0,0.0};

GLfloat material_ambient[] = {0.1745,0.01175,0.01175};
GLfloat material_diffuse[] = {0.61424,0.04136,0.04136};
GLfloat material_specular[] = {0.727811,0.626959,0.62659};

GLfloat fogColorWhite[] = {1.0,1.0,1.0,1.0};
GLfloat fogColorGreen[] = {0.0,1.0,0.0,1.0};
float fogDensity = 0.02;


void myInit(void)
{
 /*设置背景色*/
 glClearColor(0.5,0.5,0.5,1.0);

 /*设置光照*/
 glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);
 glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
 glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);
 glLightfv(GL_LIGHT0,GL_POSITION,light_position);

 /*设置材质*/
 glMaterialfv(GL_FRONT,GL_AMBIENT,material_ambient);
 glMaterialfv(GL_FRONT,GL_DIFFUSE,material_diffuse);
 glMaterialfv(GL_FRONT,GL_SPECULAR,material_specular);
 glMaterialf(GL_FRONT,GL_SHININESS,0.6*128);

 glShadeModel(GL_SMOOTH);

 glEnable(GL_DEPTH_TEST);
 glDepthFunc(GL_LESS);
 glEnable(GL_LIGHTING);
 glEnable(GL_LIGHT0);
 glEnable(GL_AUTO_NORMAL);
 glEnable(GL_NORMALIZE);
 glFrontFace(GL_CW);

 /*设置雾化*/
 glEnable(GL_FOG);
 glFogi(GL_FOG_MODE,GL_LINEAR);
 glFogfv(GL_FOG_COLOR,fogColorWhite);
 glFogf(GL_FOG_DENSITY,fogDensity);
 glFogf(GL_FOG_START,0.0);
 glFogf(GL_FOG_END,15.0);

 glHint(GL_FOG_HINT,GL_DONT_CARE);
}

/*根据不同的键值设置不同的雾化效果*/
static void myKey(unsigned char key,int x,int y)
{
 switch(key)
 {
  case 33: //shift+1
   fogDensity *= 1.10;
   glFogi(GL_FOG_MODE,GL_EXP);
   glFogf(GL_FOG_DENSITY,fogDensity);
   glutPostRedisplay();
   break;

  case 64: //shift+2
   fogDensity /= 1.10;
   glFogi(GL_FOG_MODE,GL_EXP);
   glFogf(GL_FOG_DENSITY,fogDensity);
   glutPostRedisplay();
   break;

  case 35: //shift+3
   glFogi(GL_FOG_START,0.0);
   glFogi(GL_FOG_END,8.0);
   glutPostRedisplay();
   break;

  case 36: //shift+4
   glFogi(GL_FOG_MODE,GL_EXP2);
   glutPostRedisplay();
   break;

  case 37: //shift+5
   glFogi(GL_FOG_MODE,GL_LINEAR);
   glutPostRedisplay();
   break;

  case 94: //shift+6
   glFogfv(GL_FOG_COLOR,fogColorGreen);
   glutPostRedisplay();
   break;

  case 27: //Esc
   exit(0);

  default:
   break;
 }
}

/*绘制圆锥体*/
void myDisplay(void)
{
 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

 glPushMatrix();
 glTranslatef(-4.0,4.0,0.0);
 glRotatef(30.0,1.0,1.0,0.0);
 glutSolidCone(1.0,50.0,20.0,20.0);
 glPopMatrix();
 glutSwapBuffers();
}

void myReshape(int w,int h)
{
 glViewport(0,0,(GLsizei)w,(GLsizei)h);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 gluPerspective(100.0,1.0,1.0,20.0);
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
 glTranslatef(0.0,0.0,-15.5);
}

int main(int argc,char ** argv)
{
 /*初始化*/
 glutInit(&argc,argv);
 glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
 glutInitWindowSize(300,300);
 glutInitWindowPosition(100,100);

 /*创建窗口*/
 glutCreateWindow(" FOG ");

 /*绘制与显示*/
 myInit();
 glutKeyboardFunc(myKey);
 glutReshapeFunc(myReshape);
 glutDisplayFunc(myDisplay);

 /*进入GLUT事件处理循环*/
 glutMainLoop();
 return 0;
}

 

 
 
说明
:本教程来源互联网或网友上传或出版商,仅为学习研究或媒体推广,wanshiok.com不保证资料的完整性。

上一篇:OpenGL编程轻松入门之使用颜色  下一篇:OpenGL编程轻松入门之背景简介