例8:绘制三个互相重叠的矩形,这三个矩形从左到右分别为绿、红、蓝。其中红色、蓝色矩形的透明度分别为50%的透明度,即alpha值为0.5,如图九所示。
#include <stdlib.h> #include <GL/glut.h> void myInit(void) { glClearColor(0.2,0.8,0.8,0.0);//将背景设置为湖蓝色 glEnable(GL_BLEND);//激活GL_BLEND glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);//指定像素的数学算法 glEnable(GL_FLAT); } void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT); /*绘制一个绿色的矩形 alpha = 1.0*/ glColor4f(0.0,1.0,0.0,1.0); glBegin(GL_POLYGON); glVertex3f(-0.75,0.5,0.0); glVertex3f(-0.75,-0.75,0.0); glVertex3f(0.5,-0.75,0.0); glVertex3f(0.5,0.5,0.0); glEnd(); /*绘制一个红色的矩形 alpha = 0.5*/ glColor4f(1.0,0.0,0.0,0.5); glBegin(GL_POLYGON); glVertex3f(-0.25,1.0,0.0); glVertex3f(-0.25,-0.25,0.0); glVertex3f(1.0,-0.25,0.0); glVertex3f(1.0,1.0,0.0); glEnd(); /*绘制一个蓝色的矩形 alpha = 0.5*/ glColor4f(0.0,0.0,1.0,0.5); glBegin(GL_POLYGON); glVertex3f(0.25,1.5,0.0); glVertex3f(0.25,0.25,0.0); glVertex3f(1.5,0.25,0.0); glVertex3f(1.5,1.5,0.0); glEnd(); glFlush(); } void myReshape(int w,int h) { glViewport(0,0,w,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,-1.5,1.5); else glOrtho(-1.5*(GLfloat)w/(GLfloat)h,1.5*(GLfloat)w/(GLfloat)h,-1.5,1.5,-1.5,1.5); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(-0.4,0.0,0.0); } int main(int argc,char ** argv) { /*初始化*/ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(300,400); glutInitWindowPosition(200,200); /*创建窗口*/ glutCreateWindow("BLEND POLYGON"); /*绘制与显示*/ myInit(); glutReshapeFunc(myReshape); glutDisplayFunc(myDisplay); /*进入GLUT事件处理循环*/ glutMainLoop(); return(0); } |
图九:三个互相重叠的矩形 |
·glBlendFunc(GLenum sfactor,GLenum dfactor) 指定像素算法。sfactor指定红,绿,蓝及alpha源混合因素是如何计算的。dfactor指定红,绿,蓝及alpha目标混合因素是如何计算的。
例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; } |
图十:被雾化的圆锥体 |
·glFog指定雾化参数。后面不同的字母表明参数的数据类型。f表示float,i表示integer,v表示vector,也就是表明一个指针。
GL_FOG_MODE是一个单值整数或浮点数,该数值指定了用来计算雾化融合因子f的方程。
GL_DENSITY 是一个单值整数或浮点数,该值指定雾化浓度。
GL_FOG_START 是一个单值整数或浮点数,该值指定雾化的起始值。
GL_FOG_END是一个单值整数或浮点数,该值指定雾化的终止值。
GL_FOG_INDEX是一个单值整数或浮点数,该值指定雾化索引值if,缺省的雾化索引值为0.0。
GL_FOG_COLOR包含4个整数值或浮点数值,这些数值指定的是雾化颜色Cf,整数值进行线性映射。
·glHint指定实现的线索。本例中GL_FOG_HINT指定雾化计算精度。GL_DONT_CARE指对选项不做考虑。
图十一:雾化为绿色的圆锥体 |
本例中还涉及到键盘操作,glutKeyboardFun。此函数注册当前窗口的键盘回调函数。因为这不是本节的主要内容,我们只简单提一下。此函数的 原形为void glutKeyboardFunc (void(*func)(unsigned char key,int x,int y))所以我们自己写的回调函数必须按照原形上规定的写。