OpenGL: 颜色混合

代码如下:

#include 
#pragma comment(lib, "glut32.lib")

void Initialization()
{
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

	glEnable(GL_BLEND);//启用混合状态
}

void OnDisplay(void)
{
	GLUquadricObj *obj; //定义实用库中的二次曲面指针

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ONE_MINUS_SRC_COLOR); //混合函数

	glPushMatrix();
	{
		glTranslatef(0.0f, 1.0f, 0.0f);
		glColor4f(1.0f, 0.0f, 0.0f, 0.5f);
		obj = gluNewQuadric();		//生成二次曲面对象
		gluDisk(obj, 0, 1, 30, 1);	//绘制圆盘
	}
	glPopMatrix();

	glPushMatrix();
	{
		glTranslatef(-0.5f, 0.0f, 0.0f);
		glColor4f(0.0f, 1.0f, 0.0f, 0.5f);
		obj = gluNewQuadric();		//再次生成二次曲面对象
		gluDisk(obj, 0, 1, 30, 1);	//绘制另一个圆盘
	}
	glPopMatrix();

	glPushMatrix();
	{
		glTranslatef(0.5f, 0.0f, 0.0f);
		glColor4f(0.0f, 0.0f, 1.0f, 0.5f);
		obj = gluNewQuadric();		//再次生成二次曲面对象
		gluDisk(obj, 0, 1, 30, 1);	//绘制第三个圆盘
	}
	glPopMatrix();

	glutSwapBuffers();
}

void OnReShape(int w,int h)
{
	glViewport(0, 0, w, h);

	glMatrixMode(GL_PROJECTION); //将当前矩阵指定为投影模式
	glLoadIdentity();

	if (h != 0)
	{
		GLfloat aspect =(float)w / (float)h;

		if(w < h)
		{
			gluOrtho2D(-3, 3, -3 * aspect, 3 * aspect);//二维正交投影
		}
		else
		{
			gluOrtho2D(-3 / aspect, 3 / aspect, -3, 3);
		}
	}
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void main(int argc,char* argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(600, 480);
	glutCreateWindow("OpenGL颜色混合");

	glutReshapeFunc(OnReShape);
	glutDisplayFunc(OnDisplay);

	Initialization();

	glutMainLoop();
}

运行效果如下:
OpenGL: 颜色混合_第1张图片

你可能感兴趣的:(OpenGL)