OpenGL入门示例6——黑色背景绘制彩色点画多边形

#include<GL/glut.h>			//OpenGL实用工具包

void init(void)
{
	glClearColor(0.0,0.0,0.0,0.0);		//指定清除颜色(黑色),即背景颜色
	glShadeModel(GL_FLAT);				//设置着色模式,采用恒定着色
}

void display(void)
{

	GLubyte fly[]={								//32*32位图的指针,该位图被视为0和1组成的掩码,1表示绘制多边形中对应的像素,0表示不绘制
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x03,0x80,0x01,0xC0,0x06,0xC0,0x03,0x60,
	0x04,0x60,0x06,0x20,0x04,0x30,0x0C,0x20,
	0x04,0x18,0x18,0x20,0x04,0x0C,0x30,0x20,
	0x04,0x06,0x60,0x20,0x44,0x03,0xC0,0x22,
	0x44,0x01,0x80,0x22,0x44,0x01,0x80,0x22,
	0x44,0x01,0x80,0x22,0x44,0x01,0x80,0x22,
	0x44,0x01,0x80,0x22,0x44,0x01,0x80,0x22,
	0x66,0x01,0x80,0x66,0x33,0x01,0x80,0xCC,
	0x19,0x81,0x81,0x98,0x0C,0xC1,0x83,0x30,
	0x07,0xE1,0x87,0xE0,0x03,0x3F,0xFC,0xC0,
	0x03,0x31,0x8C,0xC0,0x03,0x33,0xCC,0xC0,
	0x06,0x64,0x26,0x60,0x0C,0xCC,0x33,0x30,
	0x18,0xCC,0x33,0x18,0x10,0xC4,0x23,0x08,
	0x10,0x63,0xC6,0x08,0x10,0x30,0x0C,0x08,
	0x10,0x18,0x18,0x08,0x10,0x00,0x00,0x08};

	GLubyte halftone[]={
	0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
	0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
	0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
	0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
	0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
	0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
	0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
	0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
	0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
	0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
	0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
	0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
	0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
	0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
	0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
	0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,};
	
	glClear(GL_COLOR_BUFFER_BIT);			//清除所有的像素

	glColor3f(1.0,1.0,0.0);					//绘制颜色
	glRectf(25.0,25.0,125.0,125.0);
	
	glEnable(GL_POLYGON_STIPPLE);			//启用多边形点画功能

	glColor3f(0.0,1.0,1.0);					//绘制颜色
	glPolygonStipple(fly);					//指定点画模式(默认情况下使用纯色绘制填充多边形),参数是一个指向32*32位图的指针
	glRectf(125.0,25.0,225.0,125.0);

	glColor3f(1.0,0.0,1.0);					//绘制颜色
	glPolygonStipple(halftone);
	glRectf(225.0,25.0,325.0,125.0);

	glFlush();								//立刻开始处理保存在缓冲区中的OpenGL函数调用
}

void reshape(int w,int h)
{
	glViewport(0,0,(GLsizei)w,(GLsizei)h);			 //为了选择一个更小的绘图区域,在窗口中定义一个像素矩形,将图像映射到这个矩形中
	glMatrixMode(GL_PROJECTION);			  		 //指定哪一个矩阵是当前矩阵(GL_PROJECTION,对投影矩阵应用随后的矩阵操作)
	glLoadIdentity();							  	 //将当前的用户坐标系的原点移到了屏幕中心:类似于一个复位操作  
	gluOrtho2D(0.0,(GLdouble)w,0.0,(GLdouble)h);	 //将当前的可视空间设置为正投影空间,这个函数描述了一个平行修剪空间,意味着离观察者较远的对象看上去不会变小  
}

int main(int argc,char** argv)						//注意参数
{
	glutInit(&argc,argv);							//初始化GLUT并处理命令行参数
	glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);		//指定模式:单缓存OR双缓存;RGBA模式OR颜色索引模式
	glutInitWindowSize(350,150);					//指定窗口大小(像素)
	glutInitWindowPosition(100,100);				//指定窗口左上角在屏幕上的位置
	glutCreateWindow(argv[0]);						//使用OpenGL场景创建一个窗口,参数为窗口名称
	init();											//调用初始化函数
	glutDisplayFunc(display);						//注册用于显示图形的回调函数,每当GLUT认为需要重绘窗口时,都会执行该函数,故应将重绘场景所需调用的函数都放到显示回调函数中。
	glutReshapeFunc(reshape);						//重绘回调函数
	glutMainLoop();									//进入主循环并处理事件,此时创建的所有窗口都会显示出来,被渲染到这些窗口中的内容也将显示出来,程序开始处理事件,注册的显示回调函数被触发

	return 0;										//ANSI C要求函数main()返回一个int值
}

//补充说明:为最大限度提高效率,可使用显示列表来存储点画模式

运行结果:


阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页