南昌大学图形学实验报告完整版.docx
《南昌大学图形学实验报告完整版.docx》由会员分享,可在线阅读,更多相关《南昌大学图形学实验报告完整版.docx(32页珍藏版)》请在冰豆网上搜索。
南昌大学图形学实验报告完整版
实验一
一、实验项目名称
熟悉opengl环境
二、实验目的
1、掌握在利用OpenGL图形库进行图形程序设计的基本方法。
2、掌握Windows环境下的消息处理方法。
3、理解OpenGL运行机制。
三、实验基本原理和内容
使用opengl绘制一个图形
四、主要仪器设备及耗材
PC机一台,win7VC++6.0环境
五、实验步骤
1、打开VC++6.0
2、新建工程,键入代码
3、记录结果
六、实验数据及处理结果
7、思考讨论题或体会或对改进实验的建议
通过第一次实验体会到opengl的魅力
8、参考资料:
《计算机图形学》(第三版)DonaldHearn电子工业出版社
九、源代码
#include
usingnamespacestd;
constintn=1000;
constGLfloatR=0.5f;
constGLfloatPi=3.1415926536f;
voidDrawCircle()
{
inti;
GLfloatx,y;
glBegin(GL_LINE_LOOP);
for(i=0;i{x=R*cos(2*Pi/n*i)*(1-cos(2*Pi/n*i));y=R*sin(2*Pi/n*i)*(1-cos(2*Pi/n*i));glVertex2f(y,x+0.2f);}glEnd();glFlush();}voidmyDisplay(){glClear(GL_COLOR_BUFFER_BIT);DrawCircle();}voidinit(void){glClearColor(1.0f,1.0f,1.0f,0.0f);glMatrixMode(GL_PROJECTION);gluOrtho2D(-400.0,400.0,-400.0,400.0);glMatrixMode(GL_MODELVIEW);}intmain(intargc,char*argv[]){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowPosition(100,100);glutInitWindowSize(500,500);glutCreateWindow("心形");//init();glutDisplayFunc(myDisplay);glutMainLoop();return0;} 实验二一、实验项目名称任意斜率直线和中点画圆二、实验目的理解DDA直线算法和中点画圆算法三、实验基本原理和内容四、主要仪器设备及耗材PC机一台,win7VC++6.0环境五、实验步骤六、实验数据及处理结果7、思考讨论题或体会或对改进实验的建议画线的核心是通过算法来调整直线的生成方式,这只是其中一种方式八、参考资料:《计算机图形学》(第三版)DonaldHearn电子工业出版社附源码:1:任意直线斜率#include#include#includeusingnamespacestd;staticfloatm=0;floatx,y;voidinit(void){glClearColor(1.0,1.0,1.0,0);glMatrixMode(GL_PROJECTION);gluOrtho2D(0.0,200.0,0.0,150.0);}voidline(){glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0,0.0,0.0);if(fabs(m)==0){glBegin(GL_LINES);glVertex2i(18,70);glVertex2i(300,70);glEnd();}elseif(fabs(m)<=1){glBegin(GL_POINTS);x=70;y=70;for(inti=0;i<=1000;i++){glVertex2f(x,y);x+=0.1;y=y+m*0.1;}glEnd();}else{glBegin(GL_POINTS);x=70;y=70;for(inti=0;i<=1000;i++){glVertex2f(x,y);y+=0.1;x=x+0.1/m;}glEnd();}glFlush();}voidmain(intargc,char**argv){cout<<"输入斜率:";cin>>m;glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowPosition(50,100);glutInitWindowSize(400,300);glutCreateWindow("任意斜率的直线");init();glutDisplayFunc(line);glutMainLoop();}2:中点画圆#includeusingnamespacestd;#include#includeconstintn=1000;constGLfloatR=0.5f;constGLfloatPi=3.1415926536f;//以下为一般圆生成算法,不采纳/*voidDrawCircle(){inti;glBegin(GL_LINE_LOOP);for(i=0;iglVertex2f(R*cos(2*Pi/n*i),R*sin(2*Pi/n*i));glEnd();glFlush();}voidmyDisplay(){glClear(GL_COLOR_BUFFER_BIT);DrawCircle();}*///以下代码为中点画圆算法classscreenPt{private:GLintx,y;public:sceenPt(){x=y=0;}voidsetCoords(GLintxcoordValue,GLintycoordValue){x=xcoordValue;y=ycoordValue;}GLintgetx()const{returnx;}GLintgety()const{returny;}voidincrementx(){x++;}voiddecrementy(){y--;}};voidsetPixel(GLintxcoord,GLintycoord){//glClear(GL_COLOR_BUFFER_BIT);//glColor3f(1.0,0.0,0.0);glBegin(GL_POINTS);glVertex2i(xcoord,ycoord);glEnd();//glRectf(-0.5f,-0.5f,0.5f,0.5f);}voidcircleMidpoint(GLintxc,GLintyc,GLintradius){screenPtcircPt;GLintp=1-radius;circPt.setCoords(0,radius);voidcirclePlotPoints(GLint,GLint,screenPt);circlePlotPoints(xc,yc,circPt);while(circPt.getx(){circPt.incrementx();if(p<0)p+=2*circPt.getx()+1;else{circPt.decrementy();p+=2*(circPt.getx()-circPt.gety())+1;}circlePlotPoints(xc,yc,circPt);}}voidcirclePlotPoints(GLintxc,GLintyc,screenPtcircPt){setPixel(xc+circPt.getx(),yc+circPt.gety());//1setPixel(xc-circPt.getx(),yc+circPt.gety());setPixel(xc+circPt.getx(),yc-circPt.gety());setPixel(xc-circPt.getx(),yc-circPt.gety());setPixel(xc+circPt.gety(),yc+circPt.getx());//5setPixel(xc-circPt.gety(),yc+circPt.getx());setPixel(xc+circPt.gety(),yc-circPt.getx());setPixel(xc-circPt.gety(),yc-circPt.getx());}voidmyDisplay(){glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0f,0.0f,0.0f);glViewport(0,0,400,400);circleMidpoint(0,0,360);glFlush();}voidinit(void){glClearColor(1.0f,1.0f,1.0f,0.0f);glMatrixMode(GL_PROJECTION);gluOrtho2D(-400.0,400.0,-400.0,400.0);glMatrixMode(GL_MODELVIEW);}intmain(intargc,char*argv[]){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowPosition(100,100);glutInitWindowSize(500,500);glutCreateWindow("画一个圆");init();glutDisplayFunc(&myDisplay);glutMainLoop();return0;} 实验三一、实验项目名称直线裁剪算法二、实验目的掌握Sutherland-Cohen或Liang-Barsky直线裁剪算法3、实验基本原理和内容4、四、主要仪器设备及耗材PC机一台,win7VC++6.0环境五、实验步骤六、实验数据及处理结果7、思考讨论题或体会或对改进实验的建议剪裁的时候要注意视点的位置,以及剪裁点的设置八、参考资料:《计算机图形学》(第三版)DonaldHearn电子工业出版社附源码:/*#includeclasswcPt2D{private:GLfloatx,y;public:wcPt3D(){x=0.0;y=0.0;}setCoords(GLfloatxCoord,GLfloatyCoord){x=xCoord;y=yCoord;}GLfloatgetx()const{returnx;}GLfloatgety()const{returny;}};inlineGLintround(constGLfloata){returnGLint(a+0.5);}GLintclipTest(GLfloatp,GLfloatq,GLfloat*u1,GLfloat*u2){GLfloatr;GLintreturnValue=true;if(p<0.0){r=q/p;if(r>*u2)returnValue=false;elseif(r>*u1)*u1=r;}elseif(p>0.0){r=q/p;if(r<*u1)returnValue=false;elseif(r<*u2){*u2=r;}}elseif(q<0.0)returnValue=false;returnreturnValue;}voidlineClipLiangBarsk(wcPt2DwinMin,wcPt2DwinMax,wcPt2Dp1,wcPt2Dp2){GLfloatu1=0.0,u2=1.0,dx=p2.getx()-p1.getx(),dy;if(clipTest(-dx,p1.getx()-winMin.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);lineBres(round(p1.getx()),round(p1.gety()),round(p2.getx()),round(p2.gety));}}}voidmyDisplay(void){glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0,0.0,0.0);glBegin(GL_LINES);glVertex2i(180,15);glVertex2i(10,145);glEnd();glRectf(-0.5f,-0.5f,0.5f,0.5f);glFlush();}voidinit(void){glClearColor(1.0,1.0,1.0,0.0);glMatrixMode(GL_PROJECTION);gluOrtho2D(0.0,200.0,0.0,150.0);}intmain(intargc,char*argv[]){glutInit(&argc,argv);glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);glutInitWindowPosition(50,100);glutInitWindowSize(400,300);glutCreateWindow("OpenGL");init();glutDisplayFunc(&myDisplay);glutMainLoop();return0;}*//*利用VC+OpenGL实现直线的编码裁剪算法,在屏幕上用一个封闭矩形裁剪任意一条直线。#include#includeintflag;voidsetPixel(GLintx,GLinty){glBegin(GL_POINTS);glVertex2i(x,y);glEnd();}/*Bresenhamline-drawingprocedurefor|m|<1.0.*/voidlineBres(intx0,inty0,intxEnd,intyEnd){intdx=fabs(xEnd-x0),dy=fabs(yEnd-y0);intp=2*dy-dx;inttwoDy=2*dy,twoDyMinusDx=2*(dy-dx);intx,y;/*Determinewhichendpointtouseasstartposition.*/if(x0>xEnd){x=xEnd;y=yEnd;xEnd=x0;}else{x=x0;y=y0;}setPixel(x,y);while(xx++;if(p<0)p+=twoDy;else{y++;p+=twoDyMinusDx;}setPixel(x,y);}}//***********************************//以下未变,见课本P264//***************************************classwcPt2D{public:GLfloatx,y;};inlineGLintround(constGLfloata){returnGLint(a+0.5);}/*Defineafour-bitcodeforeachoftheoutsideregionsofa*rectangularclippingwindow.constGLintwinLeftBitCode=0x1;constGLintwinRightBitCode=0x2;constGLintwinBottomBitCode=0x4;constGLintwinTopBitCode=0x8;/*Abit-maskregioncodeisalsoassignedtoeachendpointofaninput*linesegment,accordingtoitspositionrelativetothefouredgesof*aninputrectangularclipwindow.**Anendpointwitharegion-codevalueof0000isinsidetheclipping*window,otherwiseitisoutsideatleastoneclippingboundary.If*the'or'operationforthetwoendpointcodesproducesavalueof*false,theentirelinedefinedbythesetwoendpointsissaved*(accepted).Ifthe'and'operationbetweentwoendpointcodesis*true,thelineiscompletelyoutsidetheclippingwindow,anditis*eliminated(rejected)fromfurtherprocessing.*/inlineGLintinside(GLintcode){returnGLint(!code);}inlineGLintreject(GLintcode1,GLintcode2){returnGLint(code1&code2);}inlineGLintaccept(GLintcode1,GLintcode2){returnGLint(!(code1|code2));}GLubyteencode(wcPt2Dpt,wcPt2DwinMin,wcPt2DwinMax){GLubytecode=0x00;if(pt.xcode=code|winLeftBitCode;if(pt.x>winMax.x)code=code|winRightBitCode;if(pt.ycode=code|winBottomBitCode;if(pt.y>winMax.y)code=code|winTopBitCode;return(code);}voidswapPts(wcPt2D*p1,wcPt2D*p2){wcPt2Dtmp;tmp=*p1;*p1=*p2;*p2=tmp;}voidswapCodes(GLubyte*c1,GLubyte*c2){GLubytetmp;tmp=*c1;*c1=*c2;*c2=tmp;}voidlineClipCohSuth(wcPt2DwinMin,wcPt2DwinMax,wcPt2Dp1,wcPt2Dp2){GLubytecode1,code2;GLintdone=false,plotLine=false;GLfloatm;while(!done){code1=encode(p1,winMin,winMax);code2=encode(p2,winMin,winMax);if(accept(code1,code2)){done=tru
x=R*cos(2*Pi/n*i)*(1-cos(2*Pi/n*i));
y=R*sin(2*Pi/n*i)*(1-cos(2*Pi/n*i));
glVertex2f(y,x+0.2f);
}
glEnd();
glFlush();
voidmyDisplay()
glClear(GL_COLOR_BUFFER_BIT);
DrawCircle();
voidinit(void)
glClearColor(1.0f,1.0f,1.0f,0.0f);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-400.0,400.0,-400.0,400.0);
glMatrixMode(GL_MODELVIEW);
intmain(intargc,char*argv[])
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(100,100);
glutInitWindowSize(500,500);
glutCreateWindow("心形");
//init();
glutDisplayFunc(myDisplay);
glutMainLoop();
return0;
实验二
任意斜率直线和中点画圆
理解DDA直线算法和中点画圆算法
画线的核心是通过算法来调整直线的生成方式,这只是其中一种方式
八、参考资料:
附源码:
1:
任意直线斜率
staticfloatm=0;
floatx,y;
glClearColor(1.0,1.0,1.0,0);
gluOrtho2D(0.0,200.0,0.0,150.0);
voidline()
glColor3f(1.0,0.0,0.0);
if(fabs(m)==0)
glBegin(GL_LINES);
glVertex2i(18,70);
glVertex2i(300,70);
elseif(fabs(m)<=1)
glBegin(GL_POINTS);
x=70;
y=70;
for(inti=0;i<=1000;i++)
glVertex2f(x,y);
x+=0.1;
y=y+m*0.1;
else
y+=0.1;
x=x+0.1/m;
voidmain(intargc,char**argv)
cout<<"输入斜率:
";
cin>>m;
glutInitWindowPosition(50,100);
glutInitWindowSize(400,300);
glutCreateWindow("任意斜率的直线");
init();
glutDisplayFunc(line);
2:
中点画圆
//以下为一般圆生成算法,不采纳
/*voidDrawCircle()
for(i=0;iglVertex2f(R*cos(2*Pi/n*i),R*sin(2*Pi/n*i));glEnd();glFlush();}voidmyDisplay(){glClear(GL_COLOR_BUFFER_BIT);DrawCircle();}*///以下代码为中点画圆算法classscreenPt{private:GLintx,y;public:sceenPt(){x=y=0;}voidsetCoords(GLintxcoordValue,GLintycoordValue){x=xcoordValue;y=ycoordValue;}GLintgetx()const{returnx;}GLintgety()const{returny;}voidincrementx(){x++;}voiddecrementy(){y--;}};voidsetPixel(GLintxcoord,GLintycoord){//glClear(GL_COLOR_BUFFER_BIT);//glColor3f(1.0,0.0,0.0);glBegin(GL_POINTS);glVertex2i(xcoord,ycoord);glEnd();//glRectf(-0.5f,-0.5f,0.5f,0.5f);}voidcircleMidpoint(GLintxc,GLintyc,GLintradius){screenPtcircPt;GLintp=1-radius;circPt.setCoords(0,radius);voidcirclePlotPoints(GLint,GLint,screenPt);circlePlotPoints(xc,yc,circPt);while(circPt.getx(){circPt.incrementx();if(p<0)p+=2*circPt.getx()+1;else{circPt.decrementy();p+=2*(circPt.getx()-circPt.gety())+1;}circlePlotPoints(xc,yc,circPt);}}voidcirclePlotPoints(GLintxc,GLintyc,screenPtcircPt){setPixel(xc+circPt.getx(),yc+circPt.gety());//1setPixel(xc-circPt.getx(),yc+circPt.gety());setPixel(xc+circPt.getx(),yc-circPt.gety());setPixel(xc-circPt.getx(),yc-circPt.gety());setPixel(xc+circPt.gety(),yc+circPt.getx());//5setPixel(xc-circPt.gety(),yc+circPt.getx());setPixel(xc+circPt.gety(),yc-circPt.getx());setPixel(xc-circPt.gety(),yc-circPt.getx());}voidmyDisplay(){glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0f,0.0f,0.0f);glViewport(0,0,400,400);circleMidpoint(0,0,360);glFlush();}voidinit(void){glClearColor(1.0f,1.0f,1.0f,0.0f);glMatrixMode(GL_PROJECTION);gluOrtho2D(-400.0,400.0,-400.0,400.0);glMatrixMode(GL_MODELVIEW);}intmain(intargc,char*argv[]){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowPosition(100,100);glutInitWindowSize(500,500);glutCreateWindow("画一个圆");init();glutDisplayFunc(&myDisplay);glutMainLoop();return0;} 实验三一、实验项目名称直线裁剪算法二、实验目的掌握Sutherland-Cohen或Liang-Barsky直线裁剪算法3、实验基本原理和内容4、四、主要仪器设备及耗材PC机一台,win7VC++6.0环境五、实验步骤六、实验数据及处理结果7、思考讨论题或体会或对改进实验的建议剪裁的时候要注意视点的位置,以及剪裁点的设置八、参考资料:《计算机图形学》(第三版)DonaldHearn电子工业出版社附源码:/*#includeclasswcPt2D{private:GLfloatx,y;public:wcPt3D(){x=0.0;y=0.0;}setCoords(GLfloatxCoord,GLfloatyCoord){x=xCoord;y=yCoord;}GLfloatgetx()const{returnx;}GLfloatgety()const{returny;}};inlineGLintround(constGLfloata){returnGLint(a+0.5);}GLintclipTest(GLfloatp,GLfloatq,GLfloat*u1,GLfloat*u2){GLfloatr;GLintreturnValue=true;if(p<0.0){r=q/p;if(r>*u2)returnValue=false;elseif(r>*u1)*u1=r;}elseif(p>0.0){r=q/p;if(r<*u1)returnValue=false;elseif(r<*u2){*u2=r;}}elseif(q<0.0)returnValue=false;returnreturnValue;}voidlineClipLiangBarsk(wcPt2DwinMin,wcPt2DwinMax,wcPt2Dp1,wcPt2Dp2){GLfloatu1=0.0,u2=1.0,dx=p2.getx()-p1.getx(),dy;if(clipTest(-dx,p1.getx()-winMin.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);lineBres(round(p1.getx()),round(p1.gety()),round(p2.getx()),round(p2.gety));}}}voidmyDisplay(void){glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0,0.0,0.0);glBegin(GL_LINES);glVertex2i(180,15);glVertex2i(10,145);glEnd();glRectf(-0.5f,-0.5f,0.5f,0.5f);glFlush();}voidinit(void){glClearColor(1.0,1.0,1.0,0.0);glMatrixMode(GL_PROJECTION);gluOrtho2D(0.0,200.0,0.0,150.0);}intmain(intargc,char*argv[]){glutInit(&argc,argv);glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);glutInitWindowPosition(50,100);glutInitWindowSize(400,300);glutCreateWindow("OpenGL");init();glutDisplayFunc(&myDisplay);glutMainLoop();return0;}*//*利用VC+OpenGL实现直线的编码裁剪算法,在屏幕上用一个封闭矩形裁剪任意一条直线。#include#includeintflag;voidsetPixel(GLintx,GLinty){glBegin(GL_POINTS);glVertex2i(x,y);glEnd();}/*Bresenhamline-drawingprocedurefor|m|<1.0.*/voidlineBres(intx0,inty0,intxEnd,intyEnd){intdx=fabs(xEnd-x0),dy=fabs(yEnd-y0);intp=2*dy-dx;inttwoDy=2*dy,twoDyMinusDx=2*(dy-dx);intx,y;/*Determinewhichendpointtouseasstartposition.*/if(x0>xEnd){x=xEnd;y=yEnd;xEnd=x0;}else{x=x0;y=y0;}setPixel(x,y);while(xx++;if(p<0)p+=twoDy;else{y++;p+=twoDyMinusDx;}setPixel(x,y);}}//***********************************//以下未变,见课本P264//***************************************classwcPt2D{public:GLfloatx,y;};inlineGLintround(constGLfloata){returnGLint(a+0.5);}/*Defineafour-bitcodeforeachoftheoutsideregionsofa*rectangularclippingwindow.constGLintwinLeftBitCode=0x1;constGLintwinRightBitCode=0x2;constGLintwinBottomBitCode=0x4;constGLintwinTopBitCode=0x8;/*Abit-maskregioncodeisalsoassignedtoeachendpointofaninput*linesegment,accordingtoitspositionrelativetothefouredgesof*aninputrectangularclipwindow.**Anendpointwitharegion-codevalueof0000isinsidetheclipping*window,otherwiseitisoutsideatleastoneclippingboundary.If*the'or'operationforthetwoendpointcodesproducesavalueof*false,theentirelinedefinedbythesetwoendpointsissaved*(accepted).Ifthe'and'operationbetweentwoendpointcodesis*true,thelineiscompletelyoutsidetheclippingwindow,anditis*eliminated(rejected)fromfurtherprocessing.*/inlineGLintinside(GLintcode){returnGLint(!code);}inlineGLintreject(GLintcode1,GLintcode2){returnGLint(code1&code2);}inlineGLintaccept(GLintcode1,GLintcode2){returnGLint(!(code1|code2));}GLubyteencode(wcPt2Dpt,wcPt2DwinMin,wcPt2DwinMax){GLubytecode=0x00;if(pt.xcode=code|winLeftBitCode;if(pt.x>winMax.x)code=code|winRightBitCode;if(pt.ycode=code|winBottomBitCode;if(pt.y>winMax.y)code=code|winTopBitCode;return(code);}voidswapPts(wcPt2D*p1,wcPt2D*p2){wcPt2Dtmp;tmp=*p1;*p1=*p2;*p2=tmp;}voidswapCodes(GLubyte*c1,GLubyte*c2){GLubytetmp;tmp=*c1;*c1=*c2;*c2=tmp;}voidlineClipCohSuth(wcPt2DwinMin,wcPt2DwinMax,wcPt2Dp1,wcPt2Dp2){GLubytecode1,code2;GLintdone=false,plotLine=false;GLfloatm;while(!done){code1=encode(p1,winMin,winMax);code2=encode(p2,winMin,winMax);if(accept(code1,code2)){done=tru
glVertex2f(R*cos(2*Pi/n*i),R*sin(2*Pi/n*i));
}*/
//以下代码为中点画圆算法
classscreenPt
private:
GLintx,y;
public:
sceenPt(){x=y=0;}
voidsetCoords(GLintxcoordValue,GLintycoordValue)
x=xcoordValue;
y=ycoordValue;
GLintgetx()const{returnx;}
GLintgety()const{returny;}
voidincrementx(){x++;}
voiddecrementy(){y--;}
};
voidsetPixel(GLintxcoord,GLintycoord)
//glClear(GL_COLOR_BUFFER_BIT);
//glColor3f(1.0,0.0,0.0);
glVertex2i(xcoord,ycoord);
//glRectf(-0.5f,-0.5f,0.5f,0.5f);
voidcircleMidpoint(GLintxc,GLintyc,GLintradius)
screenPtcircPt;
GLintp=1-radius;
circPt.setCoords(0,radius);
voidcirclePlotPoints(GLint,GLint,screenPt);
circlePlotPoints(xc,yc,circPt);
while(circPt.getx(){circPt.incrementx();if(p<0)p+=2*circPt.getx()+1;else{circPt.decrementy();p+=2*(circPt.getx()-circPt.gety())+1;}circlePlotPoints(xc,yc,circPt);}}voidcirclePlotPoints(GLintxc,GLintyc,screenPtcircPt){setPixel(xc+circPt.getx(),yc+circPt.gety());//1setPixel(xc-circPt.getx(),yc+circPt.gety());setPixel(xc+circPt.getx(),yc-circPt.gety());setPixel(xc-circPt.getx(),yc-circPt.gety());setPixel(xc+circPt.gety(),yc+circPt.getx());//5setPixel(xc-circPt.gety(),yc+circPt.getx());setPixel(xc+circPt.gety(),yc-circPt.getx());setPixel(xc-circPt.gety(),yc-circPt.getx());}voidmyDisplay(){glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0f,0.0f,0.0f);glViewport(0,0,400,400);circleMidpoint(0,0,360);glFlush();}voidinit(void){glClearColor(1.0f,1.0f,1.0f,0.0f);glMatrixMode(GL_PROJECTION);gluOrtho2D(-400.0,400.0,-400.0,400.0);glMatrixMode(GL_MODELVIEW);}intmain(intargc,char*argv[]){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowPosition(100,100);glutInitWindowSize(500,500);glutCreateWindow("画一个圆");init();glutDisplayFunc(&myDisplay);glutMainLoop();return0;} 实验三一、实验项目名称直线裁剪算法二、实验目的掌握Sutherland-Cohen或Liang-Barsky直线裁剪算法3、实验基本原理和内容4、四、主要仪器设备及耗材PC机一台,win7VC++6.0环境五、实验步骤六、实验数据及处理结果7、思考讨论题或体会或对改进实验的建议剪裁的时候要注意视点的位置,以及剪裁点的设置八、参考资料:《计算机图形学》(第三版)DonaldHearn电子工业出版社附源码:/*#includeclasswcPt2D{private:GLfloatx,y;public:wcPt3D(){x=0.0;y=0.0;}setCoords(GLfloatxCoord,GLfloatyCoord){x=xCoord;y=yCoord;}GLfloatgetx()const{returnx;}GLfloatgety()const{returny;}};inlineGLintround(constGLfloata){returnGLint(a+0.5);}GLintclipTest(GLfloatp,GLfloatq,GLfloat*u1,GLfloat*u2){GLfloatr;GLintreturnValue=true;if(p<0.0){r=q/p;if(r>*u2)returnValue=false;elseif(r>*u1)*u1=r;}elseif(p>0.0){r=q/p;if(r<*u1)returnValue=false;elseif(r<*u2){*u2=r;}}elseif(q<0.0)returnValue=false;returnreturnValue;}voidlineClipLiangBarsk(wcPt2DwinMin,wcPt2DwinMax,wcPt2Dp1,wcPt2Dp2){GLfloatu1=0.0,u2=1.0,dx=p2.getx()-p1.getx(),dy;if(clipTest(-dx,p1.getx()-winMin.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);lineBres(round(p1.getx()),round(p1.gety()),round(p2.getx()),round(p2.gety));}}}voidmyDisplay(void){glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0,0.0,0.0);glBegin(GL_LINES);glVertex2i(180,15);glVertex2i(10,145);glEnd();glRectf(-0.5f,-0.5f,0.5f,0.5f);glFlush();}voidinit(void){glClearColor(1.0,1.0,1.0,0.0);glMatrixMode(GL_PROJECTION);gluOrtho2D(0.0,200.0,0.0,150.0);}intmain(intargc,char*argv[]){glutInit(&argc,argv);glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);glutInitWindowPosition(50,100);glutInitWindowSize(400,300);glutCreateWindow("OpenGL");init();glutDisplayFunc(&myDisplay);glutMainLoop();return0;}*//*利用VC+OpenGL实现直线的编码裁剪算法,在屏幕上用一个封闭矩形裁剪任意一条直线。#include#includeintflag;voidsetPixel(GLintx,GLinty){glBegin(GL_POINTS);glVertex2i(x,y);glEnd();}/*Bresenhamline-drawingprocedurefor|m|<1.0.*/voidlineBres(intx0,inty0,intxEnd,intyEnd){intdx=fabs(xEnd-x0),dy=fabs(yEnd-y0);intp=2*dy-dx;inttwoDy=2*dy,twoDyMinusDx=2*(dy-dx);intx,y;/*Determinewhichendpointtouseasstartposition.*/if(x0>xEnd){x=xEnd;y=yEnd;xEnd=x0;}else{x=x0;y=y0;}setPixel(x,y);while(xx++;if(p<0)p+=twoDy;else{y++;p+=twoDyMinusDx;}setPixel(x,y);}}//***********************************//以下未变,见课本P264//***************************************classwcPt2D{public:GLfloatx,y;};inlineGLintround(constGLfloata){returnGLint(a+0.5);}/*Defineafour-bitcodeforeachoftheoutsideregionsofa*rectangularclippingwindow.constGLintwinLeftBitCode=0x1;constGLintwinRightBitCode=0x2;constGLintwinBottomBitCode=0x4;constGLintwinTopBitCode=0x8;/*Abit-maskregioncodeisalsoassignedtoeachendpointofaninput*linesegment,accordingtoitspositionrelativetothefouredgesof*aninputrectangularclipwindow.**Anendpointwitharegion-codevalueof0000isinsidetheclipping*window,otherwiseitisoutsideatleastoneclippingboundary.If*the'or'operationforthetwoendpointcodesproducesavalueof*false,theentirelinedefinedbythesetwoendpointsissaved*(accepted).Ifthe'and'operationbetweentwoendpointcodesis*true,thelineiscompletelyoutsidetheclippingwindow,anditis*eliminated(rejected)fromfurtherprocessing.*/inlineGLintinside(GLintcode){returnGLint(!code);}inlineGLintreject(GLintcode1,GLintcode2){returnGLint(code1&code2);}inlineGLintaccept(GLintcode1,GLintcode2){returnGLint(!(code1|code2));}GLubyteencode(wcPt2Dpt,wcPt2DwinMin,wcPt2DwinMax){GLubytecode=0x00;if(pt.xcode=code|winLeftBitCode;if(pt.x>winMax.x)code=code|winRightBitCode;if(pt.ycode=code|winBottomBitCode;if(pt.y>winMax.y)code=code|winTopBitCode;return(code);}voidswapPts(wcPt2D*p1,wcPt2D*p2){wcPt2Dtmp;tmp=*p1;*p1=*p2;*p2=tmp;}voidswapCodes(GLubyte*c1,GLubyte*c2){GLubytetmp;tmp=*c1;*c1=*c2;*c2=tmp;}voidlineClipCohSuth(wcPt2DwinMin,wcPt2DwinMax,wcPt2Dp1,wcPt2Dp2){GLubytecode1,code2;GLintdone=false,plotLine=false;GLfloatm;while(!done){code1=encode(p1,winMin,winMax);code2=encode(p2,winMin,winMax);if(accept(code1,code2)){done=tru
circPt.incrementx();
if(p<0)
p+=2*circPt.getx()+1;
circPt.decrementy();
p+=2*(circPt.getx()-circPt.gety())+1;
voidcirclePlotPoints(GLintxc,GLintyc,screenPtcircPt)
setPixel(xc+circPt.getx(),yc+circPt.gety());//1
setPixel(xc-circPt.getx(),yc+circPt.gety());
setPixel(xc+circPt.getx(),yc-circPt.gety());
setPixel(xc-circPt.getx(),yc-circPt.gety());
setPixel(xc+circPt.gety(),yc+circPt.getx());//5
setPixel(xc-circPt.gety(),yc+circPt.getx());
setPixel(xc+circPt.gety(),yc-circPt.getx());
setPixel(xc-circPt.gety(),yc-circPt.getx());
glColor3f(1.0f,0.0f,0.0f);
glViewport(0,0,400,400);
circleMidpoint(0,0,360);
glutCreateWindow("画一个圆");
glutDisplayFunc(&myDisplay);
实验三
直线裁剪算法
掌握Sutherland-Cohen或Liang-Barsky直线裁剪算法
3、实验基本原理和内容
4、四、主要仪器设备及耗材
剪裁的时候要注意视点的位置,以及剪裁点的设置
/*#include
classwcPt2D
wcPt3D()
x=0.0;
y=0.0;
setCoords(GLfloatxCoord,GLfloatyCoord)
x=xCoord;
y=yCoord;
GLfloatgetx()const
returnx;
GLfloatgety()const
returny;
inlineGLintround(constGLfloata)
returnGLint(a+0.5);
GLintclipTest(GLfloatp,GLfloatq,GLfloat*u1,GLfloat*u2)
GLfloatr;
GLintreturnValue=true;
if(p<0.0)
r=q/p;
if(r>*u2)
returnValue=false;
if(r>*u1)
*u1=r;
if(p>0.0)
if(r<*u1)
elseif(r<*u2)
*u2=r;
if(q<0.0)
returnreturnValue;
voidlineClipLiangBarsk(wcPt2DwinMin,wcPt2DwinMax,wcPt2Dp1,wcPt2Dp2)
GLfloatu1=0.0,u2=1.0,dx=p2.getx()-p1.getx(),dy;
if(clipTest(-dx,p1.getx()-winMin.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);
lineBres(round(p1.getx()),round(p1.gety()),round(p2.getx()),round(p2.gety));
voidmyDisplay(void)
glVertex2i(180,15);
glVertex2i(10,145);
glRectf(-0.5f,-0.5f,0.5f,0.5f);
glClearColor(1.0,1.0,1.0,0.0);
glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
glutCreateWindow("OpenGL");
}*//*
利用VC+OpenGL实现直线的编码裁剪算法,在屏幕上用一个封闭矩形裁剪任意一条直线。
intflag;
voidsetPixel(GLintx,GLinty)
glVertex2i(x,y);
/*Bresenhamline-drawingprocedurefor|m|<1.0.*/
voidlineBres(intx0,inty0,intxEnd,intyEnd)
intdx=fabs(xEnd-x0),dy=fabs(yEnd-y0);
intp=2*dy-dx;
inttwoDy=2*dy,twoDyMinusDx=2*(dy-dx);
intx,y;
/*Determinewhichendpointtouseasstartposition.*/
if(x0>xEnd){
x=xEnd;
y=yEnd;
xEnd=x0;
else{
x=x0;
y=y0;
setPixel(x,y);
while(xx++;if(p<0)p+=twoDy;else{y++;p+=twoDyMinusDx;}setPixel(x,y);}}//***********************************//以下未变,见课本P264//***************************************classwcPt2D{public:GLfloatx,y;};inlineGLintround(constGLfloata){returnGLint(a+0.5);}/*Defineafour-bitcodeforeachoftheoutsideregionsofa*rectangularclippingwindow.constGLintwinLeftBitCode=0x1;constGLintwinRightBitCode=0x2;constGLintwinBottomBitCode=0x4;constGLintwinTopBitCode=0x8;/*Abit-maskregioncodeisalsoassignedtoeachendpointofaninput*linesegment,accordingtoitspositionrelativetothefouredgesof*aninputrectangularclipwindow.**Anendpointwitharegion-codevalueof0000isinsidetheclipping*window,otherwiseitisoutsideatleastoneclippingboundary.If*the'or'operationforthetwoendpointcodesproducesavalueof*false,theentirelinedefinedbythesetwoendpointsissaved*(accepted).Ifthe'and'operationbetweentwoendpointcodesis*true,thelineiscompletelyoutsidetheclippingwindow,anditis*eliminated(rejected)fromfurtherprocessing.*/inlineGLintinside(GLintcode){returnGLint(!code);}inlineGLintreject(GLintcode1,GLintcode2){returnGLint(code1&code2);}inlineGLintaccept(GLintcode1,GLintcode2){returnGLint(!(code1|code2));}GLubyteencode(wcPt2Dpt,wcPt2DwinMin,wcPt2DwinMax){GLubytecode=0x00;if(pt.xcode=code|winLeftBitCode;if(pt.x>winMax.x)code=code|winRightBitCode;if(pt.ycode=code|winBottomBitCode;if(pt.y>winMax.y)code=code|winTopBitCode;return(code);}voidswapPts(wcPt2D*p1,wcPt2D*p2){wcPt2Dtmp;tmp=*p1;*p1=*p2;*p2=tmp;}voidswapCodes(GLubyte*c1,GLubyte*c2){GLubytetmp;tmp=*c1;*c1=*c2;*c2=tmp;}voidlineClipCohSuth(wcPt2DwinMin,wcPt2DwinMax,wcPt2Dp1,wcPt2Dp2){GLubytecode1,code2;GLintdone=false,plotLine=false;GLfloatm;while(!done){code1=encode(p1,winMin,winMax);code2=encode(p2,winMin,winMax);if(accept(code1,code2)){done=tru
x++;
p+=twoDy;
y++;
p+=twoDyMinusDx;
}//***********************************//以下未变,见课本P264
//***************************************
classwcPt2D{
inlineGLintround(constGLfloata){returnGLint(a+0.5);}
/*Defineafour-bitcodeforeachoftheoutsideregionsofa
*rectangularclippingwindow.
constGLintwinLeftBitCode=0x1;
constGLintwinRightBitCode=0x2;
constGLintwinBottomBitCode=0x4;
constGLintwinTopBitCode=0x8;
/*Abit-maskregioncodeisalsoassignedtoeachendpointofaninput
*linesegment,accordingtoitspositionrelativetothefouredgesof
*aninputrectangularclipwindow.
*
*Anendpointwitharegion-codevalueof0000isinsidetheclipping
*window,otherwiseitisoutsideatleastoneclippingboundary.If
*the'or'operationforthetwoendpointcodesproducesavalueof
*false,theentirelinedefinedbythesetwoendpointsissaved
*(accepted).Ifthe'and'operationbetweentwoendpointcodesis
*true,thelineiscompletelyoutsidetheclippingwindow,anditis
*eliminated(rejected)fromfurtherprocessing.
*/
inlineGLintinside(GLintcode){returnGLint(!
code);}
inlineGLintreject(GLintcode1,GLintcode2)
{returnGLint(code1&code2);}
inlineGLintaccept(GLintcode1,GLintcode2)
{returnGLint(!
(code1|code2));}
GLubyteencode(wcPt2Dpt,wcPt2DwinMin,wcPt2DwinMax)
GLubytecode=0x00;
if(pt.xcode=code|winLeftBitCode;if(pt.x>winMax.x)code=code|winRightBitCode;if(pt.ycode=code|winBottomBitCode;if(pt.y>winMax.y)code=code|winTopBitCode;return(code);}voidswapPts(wcPt2D*p1,wcPt2D*p2){wcPt2Dtmp;tmp=*p1;*p1=*p2;*p2=tmp;}voidswapCodes(GLubyte*c1,GLubyte*c2){GLubytetmp;tmp=*c1;*c1=*c2;*c2=tmp;}voidlineClipCohSuth(wcPt2DwinMin,wcPt2DwinMax,wcPt2Dp1,wcPt2Dp2){GLubytecode1,code2;GLintdone=false,plotLine=false;GLfloatm;while(!done){code1=encode(p1,winMin,winMax);code2=encode(p2,winMin,winMax);if(accept(code1,code2)){done=tru
code=code|winLeftBitCode;
if(pt.x>winMax.x)
code=code|winRightBitCode;
if(pt.ycode=code|winBottomBitCode;if(pt.y>winMax.y)code=code|winTopBitCode;return(code);}voidswapPts(wcPt2D*p1,wcPt2D*p2){wcPt2Dtmp;tmp=*p1;*p1=*p2;*p2=tmp;}voidswapCodes(GLubyte*c1,GLubyte*c2){GLubytetmp;tmp=*c1;*c1=*c2;*c2=tmp;}voidlineClipCohSuth(wcPt2DwinMin,wcPt2DwinMax,wcPt2Dp1,wcPt2Dp2){GLubytecode1,code2;GLintdone=false,plotLine=false;GLfloatm;while(!done){code1=encode(p1,winMin,winMax);code2=encode(p2,winMin,winMax);if(accept(code1,code2)){done=tru
code=code|winBottomBitCode;
if(pt.y>winMax.y)
code=code|winTopBitCode;
return(code);
voidswapPts(wcPt2D*p1,wcPt2D*p2)
wcPt2Dtmp;
tmp=*p1;*p1=*p2;*p2=tmp;
voidswapCodes(GLubyte*c1,GLubyte*c2)
GLubytetmp;
tmp=*c1;*c1=*c2;*c2=tmp;
voidlineClipCohSuth(wcPt2DwinMin,wcPt2DwinMax,wcPt2Dp1,wcPt2Dp2)
GLubytecode1,code2;
GLintdone=false,plotLine=false;
GLfloatm;
while(!
done){
code1=encode(p1,winMin,winMax);
code2=encode(p2,winMin,winMax);
if(accept(code1,code2)){
done=tru
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1