OpenGL: 反走样

#include <gl/glut.h>
#pragma comment(lib, "glut32.lib")

#define NO 0
#define YES 1

int Drawing;

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

	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_DST_ALPHA);

	glEnable(GL_POINT_SMOOTH);           //启用点反走样
	glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);

	glEnable(GL_LINE_SMOOTH);            //启用直线反走样,初始化函数中所调用的那3条命令才真正起作用
	glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);


	Drawing=NO;
}

void OnDisplay(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	if(Drawing == YES)		//启用混合状态,初始化函数中所调用的那3条命令才真正起作用
	{
		glEnable(GL_BLEND);
	}
	else					//关闭混合状态
	{
		glDisable(GL_BLEND);
	}
	glColor4f(1.0f, 0.0f, 0.0f, 1.0f); //当前绘图色为绿色

	glLineWidth(10);
	glBegin(GL_LINE_STRIP);
	{
		glVertex2f(-3.0f, -0.5f);
		glVertex2f(0.0f, 0.5f);
		glVertex2f(3.0f, -0.5f);
	}
	glEnd();

	glPointSize(10);
	glBegin(GL_POINTS);
	{
		glVertex2f(0.0f, 1.0f);
	}
	glEnd();

	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 CreateMenu(void)
{
	glutAddMenuEntry("正常显示", NO);
	glutAddMenuEntry("反走样", YES);

	glutAttachMenu(GLUT_RIGHT_BUTTON);
}

void OnMenu(int value)
{
	Drawing = value;
	glutPostRedisplay();
}

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);
	glutCreateMenu(OnMenu);

	Initialization();
	CreateMenu();


	glutMainLoop();
}

运行效果如下:


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值