OpenGL计算机图形学梁友栋裁剪算法实验代码及运行结果
《计算机图形学实验》报告
任课教师:钱文华
2016年春季学期
实验:
实验时间:2016年11月17日
实验地点:信息学院2204
实验目的:掌握梁友栋裁剪
程序代码:#include
#include
#include
#include
class wcPt2D{
public:
GLfloat x,y;
void setCoords(GLfloat xCoord,GLfloat yCoord){x=xCoord;y=yCoord;}
GLfloat getx() const{return x;}
GLfloat gety() const{return y;}
};
inline GLint round(const GLfloat a){return GLint(a+0.5);}
void setPixel(int x,int y){
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
void init(){
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode (GL_PROJECTION);
gluOrtho2D(-200.0,200.0,-200.0,200.0);
}
void lineBres(GLfloat x0,GLfloat y0,GLfloat xEnd,GLfloat yEnd){
int dx = fabs(xEnd - x0),dy = fabs(yEnd - y0);
int p = 2*dy - dx;
int twoDy = 2*dy,twoDyMinusDx = 2*(dy - dx);
int x,y;
if(x0>xEnd){
x = xEnd;
y = yEnd;
xEnd = x0;
}
else{
x = x0;
y = y0;
}
setPixel(x,y);
while(x
x++;
if(p<0)
p+=twoDy;
else{
y++;
p+=twoDyMinusDx;
}
setPixel(x,y);
}
}
GLint clipTest(GLfloat p,GLfloat q,GLfloat *u1,GLfloat *u2){
GLfloat r;
GLint returnValue = true;
if(p<0.0){
r = q/p;
if(r>*u2)
returnValue = false;
else
if(r>*u1)
*u1 = r;
}
else
if(p>0.0){
r = q/p;
if(r
returnValue = false;
else if(r
*u2 = r;
}
else
if(q<0.0)
returnValue = false;
return(returnValue);
}
void lineClipLiangBarsk(wcPt2D winMin,wcPt2D winMax,wcPt2D p1,wcPt2D p2){
GLfloat u1 = 0.0,u2 = 1.0,dx = p2.getx()-p1.getx(),dy;
GLfloat x1 = p1.getx(),y1 = p1.gety();
GLfloat x2 = p2.getx(),y2 = p2.gety();
if(clipTest(-dx,p1.getx()-winMin.getx(),&u1,&u2))
if(clipTest(dx,winMax.getx()-p1.getx(),&u1,&u2)){
dy = p2.gety()-p1.gety();
if(clipTest(-dy,p1.gety()-winMin.gety(),&u1,&u2)){
if(clipTest(dy,winMax.gety()-p1.gety(),&u1,&u2)){
if(u2<1.0){
p2.setCoords(p1.getx()+u2*dx,p1.gety()+u2*dy);
}
if(u1>0.0){
p1.setCoords(p1.getx()+u1*dx,p1.gety()+u1*dy);
}
glColor3f(0.0,0.0,0.0);
lineBres(