您的位置:首页 > 理论基础

计算机图形学 8连通边界填充算法

2011-11-01 22:50 323 查看
首先, 
安装配置GLUT库:

OpenGL提供了一系列的辅助函数,用于简化Windows操作系统的窗口操作,使我们能把注意力集中到图形编程上,这次试验的程序就采用这些辅助函数。

下载“glut.zip”文件(我提供的资源中有)

将压缩包内的glut.h拷贝到...\\Microsoft Visual Studio\\VC98\\ Include \\GL目录下(若使用win7和vs2008,则拷贝到C:\Program Files\Microsoft SDKs\Windows\v6.0A \Include\gl);

将glut32.lib拷贝到...\\Microsoft Visual Studio\\VC98\\Lib目录下(若使用win7和vs2008,则拷贝到…\\Microsoft Visual Studio 9.0\VC\lib);

将glut32.dll拷贝到c:\\windows\\system32目录下(win7和vs2008配置过程相同)。

OpenGL建立了四个库:

OpenGL的基本库(GL):库文件:opengl32.lib,头文件:gl.h

OpenGL实用库(GLU):库文件:glu32.lib,头文件:glu.h 

OpenGL Programming Guide辅助库(glaux): 库文件:glaux.lib,头文件:glaux.h 

OpenGL的工具库(glut): 库文件:glut32.lib,头文件:glut.h

(l)新建一个项目: 

选择菜单File中的New选项,弹出一个分页的对话框,选中页Projects中的Win32 Console Application项,然后填入你自己的Project name,如Test,回车即可。VC为你创建一个工作区(WorkSpace),你的项目Test就放在这个工作区里。

(2)为项目添加文件 

为了使用OpenGL,我们需要在项目中加入四个相关的Lib文件:glu32.lib、glaux.lib、opengl32.lib、glut32.Lib这四个文件位于\\Microsoft Visual Studio\vc98\lib目录中。选中菜单Project->Add To Project->Files项,把这四个文件加入项目,在FileView中会有显示。或者将这四个文件名添加到Project->Setting->Link->Object/library Modules 即可。

点击工具条中New Text File按钮,新建一个文本文件,存盘为Test.cpp作为你的源程序文件,再把它加入到项目中,然后就可以开始编程了。

代码如下:

 

  #include <GL/glut.h>

 #include <math.h>

 typedef  float Color[3];

 rgbColorEqual(Color c1,Color c2)

 {

  if(abs(c1[1]-c2[1])<0.001 && abs(c1[2]-c2[2])<0.001 && abs(c1[0]-c2[0])<0.001)

   return true;

  else

   return false;

 }

 void setPixel(GLint x,  GLint y)

 {

  glBegin(GL_POINTS);

  glVertex2i(x, y);

  glEnd();

 }

 void getPixel(GLint x, GLint y, Color c)

 {

  glReadPixels(x,y,1,1,GL_RGB,GL_FLOAT,c);

 }

 

 void BoundaryFill8(int x, int y,Color fillColor,Color borderColor)

 { 

 Color currentColor;

 getPixel(x,y,currentColor);

 if((!rgbColorEqual(currentColor,fillColor))&&(!rgbColorEqual(currentColor,borderColor)))

 {

  //setColor(fillColor);

  setPixel(x,y);

  BoundaryFill8( x+1,  y, fillColor, borderColor);

  BoundaryFill8( x+1,  y+1, fillColor, borderColor);

  BoundaryFill8( x+1,  y-1, fillColor, borderColor);

  BoundaryFill8( x-1,  y, fillColor, borderColor);

  BoundaryFill8( x-1,  y-1, fillColor, borderColor);

  BoundaryFill8( x-1,  y+1, fillColor, borderColor);

  BoundaryFill8( x,  y, fillColor, borderColor);

  BoundaryFill8( x,  y+1, fillColor, borderColor);

  BoundaryFill8( x,  y-1, fillColor, borderColor);

 }

  

 }

 void init(void)

 {

  glClearColor(1.0,1.0,1.0,0.0);

  glMatrixMode (GL_PROJECTION);      

  gluOrtho2D (0.0, 200.0, 0.0, 200.0);  

  

 }

 void Draw(void)

 {

  Color a={0.0,0.0,1.0},b={0.0,1.0,1.0};

  glColor3fv(b);

  glClear(GL_COLOR_BUFFER_BIT);

  //设置边界线宽,否则填充时会溢出

  glLineWidth(4.0); 

  //绘制多边形区域

  glBegin(GL_LINE_LOOP);

      glVertex2i(90, 40);

      glVertex2i(120, 100);

      glVertex2i(90, 160);

   glVertex2i(60, 160);

   glVertex2i(60, 40);

  glEnd(); 

  glColor3fv(a);

  BoundaryFill8(70,60,a,b);

  glFlush();

 }

 void main(int argc, char *argv[])

 {

  glutInit(&argc, argv);

  glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);

  glutInitWindowPosition(100, 100);

  glutInitWindowSize(200, 200);

  glutCreateWindow("8连通边界填充算法!");

  init();

  glutDisplayFunc(Draw); 

  glutMainLoop(); 

 }

即可填充满多边形。

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息