了解二维图形裁剪的原理(点的裁剪、直线的裁剪、多边形的裁剪),利用VC+OpenGL实现直线的裁剪算法。
2. 实验内容
(1) 理解直线裁剪的原理(Cohen-Surtherland算法、梁友栋算法)
(2) 利用VC+OpenGL实现直线的编码裁剪算法,在屏幕上用一个封闭矩形裁剪任意一条直线。
(3) 调试、编译、修改程序。
(4) 尝试实现梁友栋裁剪算法。
3. 实验原理
图A.4 裁剪编码
3. 实验代码
(1)示例程序:
// CG_4_编码裁剪算法.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
#include
#include
#define LEFT_EDGE 1
#define RIGHT_EDGE 2
#define BOTTOM_EDGE 4
#define TOP_EDGE 8
void LineGL(int x0,int y0,int x1,int y1)
{
glBegin(GL_LINES);
glColor3f(1.0f,0.0f,0.0f); glVertex2f(x0,y0);
glColor3f(0.0f,1.0f,0.0f); glVertex2f(x1,y1);
glEnd();
}
struct Rectangle
{
float xmin,xmax,ymin,ymax;
};
Rectangle rect;
int x0,y0,x1,y1;
int CompCode(int x,int y,Rectangle rect)
{
int code=0x00;
if(y
code=code|4;
if(y>rect.ymax)
code=code|8;
if(x>rect.xmax)
code=code|2;
if(x
code=code|1;
return code;
}
int cohensutherlandlineclip(Rectangle rect,int &x0,int &y0,int &x1,int &y1)
{
int accept,done;
float x,y;
accept=0;
done=0;
int code0,code1,codeout;
code0=CompCode(x0,y0,rect);
code1=CompCode(x1,y1,rect);
do{
if(!(code0 | code1))
{
accept=1;
done=1;
}
else if(code0 & code1)
done=1;
else
{
if(code0!=0)
codeout=code0;
else
codeout=code1;
if(codeout&LEFT_EDGE) //线段与左边界相交
{
y=y0+(y1-y0)*(rect.xmin-x0)/(x1-x0);
x=(float)rect.xmin;
}
else if(codeout&RIGHT_EDGE) //线段与右边界相交
{
y=y0+(y1-y0)*(rect.xmax-x0)/(x1-x0);
x=(float)rect.xmax;
}
else if(codeout&BOTTOM_EDGE) //线段与下边界相交
{
x=x0+(x1-x0)*(rect.ymin-y0)/(y1-y0);
y=(float)rect.ymin;
}
else if(codeout&TOP_EDGE) //线段与上边界相交
{
x=x0+(x1-x0)*(rect.ymax-y0)/(y1-y0);
y=(float)rect.ymax;
}
if(codeout==code0)
{
x0=x;y0=y;
code0=CompCode(x0,y0,rect);
}
else
{
x1=x;y1=y;
code1=CompCode(x1,y1,rect);
}
}
}while(!done);
if(accept)
LineGL(x0,y0,x1,y1);
return accept;
}
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f);
glRectf(rect.xmin,rect.ymin,rect.xmax,rect.ymax);
LineGL(x0,y0,x1,y1);
glFlush();
}
void Init()
{
glClearColor(0.0,0.0,0.0,0.0);
glShadeModel(GL_FLAT);
rect.xmin=100;
rect.xmax=300;
rect.ymin=100;
rect.ymax=300;
x0=450,y0=0,x1=0,y1=450; //设置线段的两个端点坐标
printf("Press key 'c' to Clip!\nPress key 'r' to Restore!\n");
}
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 'c':
cohensutherlandlineclip(rect,x0,y0,x1,y1);
glutPostRedisplay();
break;
case 'r':
Init();
glutPostRedisplay();
break;
case 'x':
exit(0);
break;
default:
break;
}
}
int main(int argc,char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB |GLUT_SINGLE);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,480);
glutCreateWindow("Hello World!");
Init();
glutDisplayFunc(myDisplay);
glutReshapeFunc(Reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
示例程序运行结果:
第一种情况:线段穿过裁剪窗口