每个物体在不同的环境在会有不同的视觉效果。为了使效果更加真实需要增加特殊效果。
例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目标混合因素是如何计算的。
<  
说明:本教程来源互联网或网友上传或出版商,仅为学习研究或媒体推广,wanshiok.com不保证资料的完整性。
1/2 1 2 下一页 尾页 |