计算机图形学-基于OpenGL的直线段的裁剪算法
程序员文章站
2022-07-14 10:01:04
...
计算机图形学-基于OpenGL的直线段的裁剪算法
在Opengl应用框架下实现C-S裁剪算法或L-B裁剪算法。完成一个四边形对两条线段的裁剪:四边形的左上角和右下角顶点分别为(100,100),(300,200),线段1的两个端点为(150,50),(250,150)。集成开发环境为vs2013。
实验代码-Cohen-Sutherland裁剪算法
#include <GL/glut.h>
#include <cstdio>
#define LEFT 1
#define RIGHT 2
#define BOTTOM 4
#define TOP 8
int x1=150,y1=50, x2=50, y2=250, XL=100, XR=300, YB=100, YT=200; //(x1,y1)、(x2,y2)为直线段的端点,XL为左边界,XR为右边界,YB为下边界,YT为上边界
int x1_init = 150, y1_init = 50, x2_init = 50, y2_init = 250; //将直线段端点备份,以便画出裁剪前的直线段
int encode(int x,int y)
{
int c = 0;
if (x < XL) c |= LEFT;
if (x > XR) c |= RIGHT;
if (y < YB) c |= BOTTOM;
if (y > YT) c |= TOP;
return c;
}
void CS_LineClip() //Cohen-Sutherland裁剪算法
{
int x, y;
int code1, code2, code;
code1 = encode(x1, y1);
code2 = encode(x2, y2);
while (code1 != 0 || code2 != 0)
{
if (code1 & code2)
return;
if (code1 != 0)
code = code1;
else
code = code2;
if (LEFT & code)
{
x = XL;
y = y1 + (y2 - y1)*(XL - x1) / (x2 - x1);
}
else if (RIGHT & code)
{
x = XR;
y = y1 + (y2 - y1)*(XR - x1) / (x2 - x1);
}
else if (BOTTOM & code)
{
y = YB;
x = x1 + (x2 - x1)*(YB - y1) / (y2 - y1);
}
else if (TOP & code)
{
y = YT;
x = x1 + (x2 - x1)*(YT - y1) / (y2 - y1);
}
if (code == code1)
{
x1 = x; y1 = y; code1 = encode(x1, y1);
}
else
{
x2 = x; y2 = y; code2 = encode(x2, y2);
}
}
}
void init() //初始化函数
{
glClearColor(1.0, 1.0, 1.0, 0.0); //设置背景颜色
glMatrixMode(GL_PROJECTION); // 设置投影参数
gluOrtho2D(0.0, 600.0, 0.0, 400.0); // 设置场景的大小
CS_LineClip(); //执行一次裁剪算法
}
void mydisplay() //显示函数
{
//绘制方形边界
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glPointSize(2);
glBegin(GL_LINE_LOOP);
glVertex2i(XL, YT);
glVertex2i(XL, YB);
glVertex2i(XR, YB);
glVertex2i(XR, YT);
glEnd();
glFlush();
//绘制未裁剪前的线段
glBegin(GL_LINES);
glVertex2i(x1_init, y1_init);
glVertex2i(x2_init, y2_init);
glEnd();
glFlush();
//绘制裁剪后的线段
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex2i(x1, y1);
glVertex2i(x2, y2);
glEnd();
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("Cohen-Sutherland裁剪算法");
init();
glutDisplayFunc(&mydisplay);
glutMainLoop();
return 0;
}