openGL多边形内填充

openGL多边形内填充_第1张图片

#include

#include
#include




const double two_pi=3.1415926*2; //定义圆周率pi
GLsizei winwidth=400,winheight=400; //定义框架长宽
GLuint reghex;
class screenPT //对所调用数据进行实例化
{
private:
GLint x,y;
public:
screenPT()
{
x=y=0;
}
void setcoods(GLint xcoord,GLint ycoord)
{
x=xcoord;
y=ycoord;
}
GLint getx() const
{
return x;
}
GLint gety() const
{
return y;
}
};
static void init(void)
{
screenPT hexvertex,circctr;
GLdouble theta;
GLint k;
circctr.setcoods(winwidth/2,winheight/2);
glClearColor(1.0,1.0,1.0,0.0);
reghex=glGenLists(1);
glNewList(reghex,GL_COMPILE);
glColor3f(1.0,0.0,0.0);
glBegin(GL_POLYGON);
for(k=0;k<6;k++) //利用FOR循环的方法绘制一个正六边形,改变K的值可以改变多边形
{
theta =two_pi*k/6.0;
hexvertex.setcoods(circctr.getx()+150*cos(theta),circctr.gety()+150*sin(theta));
glVertex2i(hexvertex.getx(),hexvertex.gety());
}
glEnd();
glEndList();
}
void reghexagon(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glCallList(reghex);
glFlush();
}
void winreshapefcn(int newwidth,int newheight)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,(GLdouble) newwidth,0.0,(GLdouble) newheight);
glClear(GL_COLOR_BUFFER_BIT);
}


void main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(50,100);
glutInitWindowSize(winwidth,winheight);
glutCreateWindow("000");
init();
glutDisplayFunc(reghexagon);
glutReshapeFunc(winreshapefcn);
glutMainLoop();

}

资源下载地址:http://download.csdn.net/detail/qq_28597703/8937145

你可能感兴趣的:(openGL多边形内填充)