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

首先,

安装配置GLUT库:

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

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

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

将glut32.lib拷贝到...\\MicrosoftVisualStudio\\VC98\\Lib目录下(若使用win7vs2008,则拷贝到…\\MicrosoftVisualStudio9.0\VC\lib);

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

OpenGL建立了四个库:

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

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

OpenGLProgrammingGuide辅助库(glaux):库文件:glaux.lib,头文件:glaux.h

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

(l)新建一个项目:

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

(2)为项目添加文件

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

点击工具条中NewTextFile按钮,新建一个文本文件,存盘为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();
}

即可填充满多边形。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值