• 【OpenGL 学习笔记03】点画多边形


    1.点画多边形

    //定义填充多边形的点画模式。mask为32 x 32 的位图指针,1画0不画,使用前必须启用多边形点画功能
    void glPloygonStipple(const GLubyte* mask);
    //绘制一个矩形
    void glRectf(GLfloat x1,GLfloat y1,GLfloat x2,GLfloat y2);

    2.示例

    #include <GL/glut.h>
    #include <stdlib.h>
    
    void display(void)
    {
    	GLubyte fly[] = {
    		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, 1.0);//白色
    
    	//矩形1
    	glRectf (25.0, 25.0, 125.0, 125.0);	//绘制矩形
    
    	//矩形2
    	glEnable (GL_POLYGON_STIPPLE);		//开启多边形点画功能
    	glPolygonStipple (fly);				//定义点画模式填充内容
    	glRectf (125.0, 25.0, 225.0, 125.0);//绘制矩形
    	
    	//矩形3
    	glPolygonStipple (halftone);
    	glRectf (225.0, 25.0, 325.0, 125.0);
    	glDisable (GL_POLYGON_STIPPLE);		//关闭多边形点画功能
    
    	glFlush ();
    }
    
    void init (void) 
    {
    	glClearColor (0.0, 0.0, 0.0, 0.0);
    	glShadeModel (GL_FLAT);    //采用恒定着色模式,使用图元中某个顶点的颜色来渲染整个图元。  
    }
    
    void reshape (int w, int h)
    {
    	glViewport (0, 0, (GLsizei) w, (GLsizei) h);
    	glMatrixMode (GL_PROJECTION);	 //将当前矩阵指定为投影矩阵  
    	glLoadIdentity ();				 //把当前矩阵设为单位矩阵  
    	gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
    }
    
    void keyboard(unsigned char key, int x, int y)
    {
    	switch (key) {
    	case 27:
    		exit(0);
    		break;
    	}
    }
    
    int main(int argc, char** argv)
    {
    	glutInit(&argc, argv);
    	glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    	glutInitWindowSize (350, 150);
    	glutCreateWindow (argv[0]);
    	init ();
    	glutDisplayFunc(display);
    	glutReshapeFunc(reshape);
    	glutKeyboardFunc (keyboard);
    	glutMainLoop();
    	return 0;  
    }

    3.效果图


  • 相关阅读:
    海量数据挖掘--DB优化篇
    BabyLinux制作过程详解
    C语言中的位域、字节序、比特序、大小端
    使用Busybox制作CRAMFS文件系统成功
    使用BusyBox制作linux根文件系统(CramFS+mdev)
    常用 U-boot命令详解
    基本C库函数
    simple_strtoul()
    在u-boot中添加命令
    U-Boot添加menu命令的方法及U-Boot命令执行过程
  • 原文地址:https://www.cnblogs.com/fzll/p/3954574.html
Copyright © 2020-2023  润新知