1 #include "gl/glut.h" 2 3 void Initialization() 4 { 5 glClearColor(0.0f,0.0f,0.0f,1.0f); 6 7 glEnable(GL_BLEND);//启用混合状态 8 } 9 10 void OnDisplay(void) 11 { 12 GLUquadricObj *obj; //定义实用库中的二次曲面指针 13 14 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 15 16 glMatrixMode(GL_MODELVIEW); 17 glLoadIdentity(); 18 19 glBlendFunc(GL_ONE_MINUS_DST_COLOR,GL_ONE_MINUS_SRC_COLOR); //混合函数 20 21 glPushMatrix(); 22 glTranslatef(0.0f,1.0f,0.0f); 23 glColor4f(1.0f,0.0f,0.0f,0.5f); 24 obj=gluNewQuadric(); //生成二次曲面对象 25 gluDisk(obj,0,1,30,1); //绘制圆盘 26 glPopMatrix(); 27 28 glPushMatrix(); 29 glTranslatef(-0.5f,0.0f,0.0f); 30 glColor4f(0.0f,1.0f,0.0f,0.5f); 31 obj=gluNewQuadric(); //再次生成二次曲面对象 32 gluDisk(obj,0,1,30,1); //绘制另一个圆盘 33 glPopMatrix(); 34 35 glPushMatrix(); 36 glTranslatef(0.5f,0.0f,0.0f); 37 glColor4f(0.0f,0.0f,1.0f,0.5f); 38 obj=gluNewQuadric(); //再次生成二次曲面对象 39 gluDisk(obj,0,1,30,1); //绘制第三个圆盘 40 glPopMatrix(); 41 42 glutSwapBuffers(); 43 } 44 45 void OnReShape(int w,int h) 46 { 47 glViewport(0,0,w,h); 48 49 glMatrixMode(GL_PROJECTION); //将当前矩阵指定为投影模式 50 glLoadIdentity(); 51 52 if (h!=0) 53 { 54 GLfloat aspect =(float)w/(float)h; 55 56 if(w<h) 57 gluOrtho2D(-3,3,-3*aspect,3*aspect);//二维正交投影 58 else 59 gluOrtho2D(-3/aspect,3/aspect,-3,3); 60 61 } 62 glMatrixMode(GL_MODELVIEW); 63 glLoadIdentity(); 64 } 65 66 67 void main(int argc,char* argv[]) 68 { 69 glutInit(&argc,argv); 70 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); 71 glutInitWindowSize(600,480); 72 glutCreateWindow("OpenGL颜色混合"); 73 74 glutReshapeFunc(OnReShape); 75 glutDisplayFunc(OnDisplay); 76 77 Initialization(); 78 79 glutMainLoop(); 80 }
![](https://img2024.cnblogs.com/blog/35695/202403/35695-20240328092537879-1189167464.jpg)