计算机图形学实验报告_第1页
计算机图形学实验报告_第2页
计算机图形学实验报告_第3页
计算机图形学实验报告_第4页
计算机图形学实验报告_第5页
已阅读5页,还剩45页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

计算机图形学实验报告计算机图形学实验报告姓名:学号:班级:专业:计算机科学与技术完成日期:2017.1.3voidInitial(void){ glClearColor(1.0f,1.0f,1.0f,1.0f); glMatrixMode(GL_PROJECTION); gluOrtho2D(0.0,200.0,0.0,150.0);}voidDisplay(void){ glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f,0.0f,0.0f); glRectf(50.0f,100.0f,150.0f,50.0f); glFlush();}intmain(intargc,char*argv[]){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(400,300); glutInitWindowPosition(100,120); glutCreateWindow("矩形"); glutDisplayFunc(Display); Initial(); glutMainLoop(); return0;}画点glPointSize(3);glBegin(GL_POINTS);glColor3f(1.0f,0.0f,0.0f);glVertex2i(10,140);glVertex2i(100,75); glVertex2i(190,10);glEnd();画直线glPointSize(3);glBegin(GL_LINE_LOOP);glColor3f(1.0f,0.0f,0.0f);glVertex2i(20,10);glVertex2i(60,50); glVertex2i(20,50); glVertex2i(60,10); glVertex2i(40,70);glEnd();画等边三角形glPointSize(3);glBegin(GL_TRIANGLES);glColor3f(1.0f,0.0f,0.0f);glVertex2i(20,10);glVertex2i(60,10); glVertex2f(40.0,44.64);glEnd();实验二直线绘制实验实验目的和实验内容掌握数值微分算法编程绘制直线掌握中点画线算法编程绘制直线掌握Bresenham算法编程绘制直线实验代码和实验结果掌握数值微分算法编程绘制直线#include<gl/glut.h>#include<math.h>voidInitial(void){ glClearColor(1.0f,1.0f,1.0f,1.0f); glMatrixMode(GL_PROJECTION); gluOrtho2D(0.0,200.0,0.0,150.0);}voidDisplay(void){ glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f,0.0f,0.0f); glPointSize(3); intdx,dy,epsl,k; floatx,y,xIncre,yIncre; intx0=10; inty0=10; intx1=20; inty1=80; dx=x1-x0; dy=y1-y0; x=x0; y=y0; if(abs(dx)>abs(dy)) epsl=abs(dx); else epsl=abs(dy); xIncre=(float)dx/(float)epsl; yIncre=(float)dy/(float)epsl; for(k=0;k<=epsl;k++){ glBegin(GL_POINTS); glVertex2i(int(x+0.5),(int)(y+0.5)); glEnd(); x+=xIncre; y+=yIncre; } glFlush();}intmain(intargc,char*argv[]){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(400,300); glutInitWindowPosition(100,120); glutCreateWindow("矩形"); glutDisplayFunc(Display); Initial(); glutMainLoop(); return0;}掌握中点画线算法编程绘制直线voidDisplay(void){ glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f,0.0f,0.0f); glPointSize(3); intdx,dy,d,UpIncre,DownIncre,x,y; intx0=10; inty0=10; intx1=80; inty1=20; if(x0>x1){ x=x1;x1=x0;x0=x; y=y1;y1=y0;y0=y; } x=x0;y=y0; dx=x1-x0;dy=y1-y0; d=dx-2*dy; UpIncre=2*dx-2*dy; DownIncre=-2*dy; while(x<=x1){ glBegin(GL_POINTS); glVertex2f(x,y); glEnd(); x++; if(d<0){ y++; d+=UpIncre; } else d+=DownIncre; } glFlush();}掌握Bresenham算法编程绘制直线voidDisplay(void){ glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f,0.0f,0.0f); glPointSize(3); intx,y,dx,dy,e; intx0=10; inty0=10; intx1=80; inty1=20; dx=x1-x0; dy=y1-y0; e=-dx;x=x0;y=y0; while(x<=x1){ glBegin(GL_POINTS); glVertex2f(x,y); glEnd(); x++; e=e+2*dy; if(e>0){ y++; e=e-2*dx; } } glFlush();}

实验三圆绘制实验实验目的和实验内容实现八分法画圆程序实现Bresenham算法绘制圆实现中点Bresenham算法绘制椭圆实验代码实验结果实现八分法画圆程序#include<gl/glut.h>#include<math.h>voidInitial(void){ glClearColor(1.0f,1.0f,1.0f,1.0f); glMatrixMode(GL_PROJECTION); gluOrtho2D(0.0,200.0,0.0,150.0);}voidputpixel(intx,inty){ x+=20; y+=20; glBegin(GL_POINTS); glVertex2i(x,y); glEnd();}voidCirclePoint(intx,inty){ putpixel(x,y); putpixel(y,x); putpixel(-y,x); putpixel(-x,y); putpixel(-x,-y); putpixel(-y,-x); putpixel(y,-x); putpixel(x,-y);}voidDisplay(void){ glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f,0.0f,0.0f); glPointSize(3); CirclePoint(5,2); glFlush();}intmain(intargc,char*argv[]){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(400,300); glutInitWindowPosition(100,120); glutCreateWindow("矩形"); glutDisplayFunc(Display); Initial(); glutMainLoop(); return0;}实现Bresenham算法绘制圆#include<gl/glut.h>#include<math.h>voidInitial(void){ glClearColor(1.0f,1.0f,1.0f,1.0f); glMatrixMode(GL_PROJECTION); gluOrtho2D(0.0,200.0,0.0,150.0);}voidputpixel(intx,inty){ x+=75; y+=75; glBegin(GL_POINTS); glVertex2i(x,y); glEnd();}voidCirclePoint(intx,inty){ putpixel(x,y); putpixel(y,x); putpixel(-y,x); putpixel(-x,y); putpixel(-x,-y); putpixel(-y,-x); putpixel(y,-x); putpixel(x,-y);}voidMidBressenhamCircle(intr){ intx,y,d; x=0;y=r;d=1-r; while(x<=y){ CirclePoint(x,y); if(d<0) d+=x*x+3; else{ d+=2*(x-y)+5; y--; } x++; }}voidDisplay(void){ glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f,0.0f,0.0f); glPointSize(3); MidBressenhamCircle(50); glFlush();}intmain(intargc,char*argv[]){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(400,300); glutInitWindowPosition(100,120); glutCreateWindow("矩形"); glutDisplayFunc(Display); Initial(); glutMainLoop(); return0;}实现中点Bresenham算法绘制椭圆#include<gl/glut.h>#include<math.h>voidInitial(void){ glClearColor(1.0f,1.0f,1.0f,1.0f); glMatrixMode(GL_PROJECTION); gluOrtho2D(0.0,200.0,0.0,150.0);}voidputpixel(intx,inty){ x+=100; y+=75; glBegin(GL_POINTS); glVertex2i(x,y); glEnd();}voidMidBressenhamElipse(inta,intb){ intx,y; floatd1,d2; x=0;y=b; d1=b*b+a*a*(-b+0.25); putpixel(x,y); putpixel(-x,-y); putpixel(-x,y); putpixel(x,-y); while(b*b*(x+1)<a*a*(y-0.5)){ if(d1<=0){ d1+=b*b*(2*x+3); x++; } else{ d1+=b*b*(2*x+3)+a*a*(-2*y+2); x++; y--; } putpixel(x,y); putpixel(-x,-y); putpixel(-x,y); putpixel(x,-y); }/*while上半部分*/ d2=b*b*(x+0.5)*(x+0.5)+a*a*(y-1)*(y-1)-a*a*b*b; while(y>0){ if(d2<=0){ d2+=b*b*(2*x+2)+a*a*(-2*y+3); x++; y--; } else{ d2+=a*a*(-2*y+3); y--; } putpixel(x,y); putpixel(-x,-y); putpixel(-x,y); putpixel(x,-y); }}voidDisplay(void){ glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f,0.0f,0.0f); glPointSize(3); MidBressenhamElipse(80,60); glFlush();}intmain(intargc,char*argv[]){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(400,300); glutInitWindowPosition(100,120); glutCreateWindow("矩形"); glutDisplayFunc(Display); Initial(); glutMainLoop(); return0;}实验四填充算法实验实验目的掌握用扫描线种子填充法实现扫描线种子填充算法填充任一多边形区域的程序实验内容堆栈库#include<stack>//实现了堆栈操作structPoint{GLintx;GLinty;};std::stack<Point>stk;structPointpoint1;stk.pop(point1);//pop从栈中弹出最上面的元素并取得它stk.push(point1);//压入一个元素stk.empty();//判空point1=stk.top();//取得栈最上面元素,但不弹出,仍在栈中获取像素颜色typedeffloatColor[3];Colorc;glReadPixels(cur_point.x,cur_point.y,1,1,GL_RGB,GL_FLOAT,c);//实验代码和实验结果#include<GL/glut.h>#include<stdlib.h>#include<math.h>#include<conio.h>#include<stack>#include<windows.h>typedeffloatColor[3];structPoint{ GLintx; GLinty;};std::stack<Point>stk;voidinit(void){ glClearColor(1.0,1.0,1.0,0.0); glMatrixMode(GL_PROJECTION); gluOrtho2D(0.0,400.0,0.0,400.0);}//种子像素坐标voidsetPixel(Pointcur_point){ glBegin(GL_POINTS); glVertex2i(cur_point.x,cur_point.y); glEnd(); glFlush();}voidgetPixel(Pointcur_point,Colorc){ glReadPixels(cur_point.x,cur_point.y,1,1,GL_RGB,GL_FLOAT,c);}boolrgbColorEqual(Colorc1,Colorc2){ if((abs(c1[0]-c2[0])>0.001)||(abs(c1[1]-c2[1])>0.001)||(abs(c1[2]-c2[2])>0.001)) return0; else return1;}intFillLineRegion(Pointcur_point,ColorfillColor,ColorborderColor,intdirection){ intcount=0; ColorinteriorColor; glColor3f(fillColor[0],fillColor[1],fillColor[2]); getPixel(cur_point,interiorColor); while(!(rgbColorEqual(interiorColor,borderColor))&&!(rgbColorEqual(interiorColor,fillColor))) { setPixel(cur_point); if(direction==0) cur_point.x++; else cur_point.x--; getPixel(cur_point,interiorColor); count++; } returncount;}intisPixelValid(Pointcur_point,ColorfillColor,ColorborderColor){ ColorinteriorColor; getPixel(cur_point,interiorColor); if(!(rgbColorEqual(interiorColor,borderColor))&&!(rgbColorEqual(interiorColor,fillColor))) return1; else return0;}voidSearchLineNewSeed(intxLeft,intxRight,inty,ColorfillColor,ColorborderColor){ intxt=xLeft; intseed_left=-1; Pointtemp_point; Pointseed_point; while(xt<=xRight){ seed_left=-1; temp_point.x=xt; temp_point.y=y; while(xt<=xRight){ if(isPixelValid(temp_point,fillColor,borderColor)){ seed_left=temp_point.x; break; } else{ xt++; temp_point.x=xt; } } while(xt<=xRight){ if(isPixelValid(temp_point,fillColor,borderColor)){ xt++; temp_point.x=xt; } else{ break; } } if(seed_left!=-1){ seed_point.x=seed_left; seed_point.y=y; stk.push(seed_point); } }}voidscanLine(Pointcur_point,ColorfillColor,ColorborderColor){ intcount=0; intright,left; Pointtemp_point; while(!stk.empty()) { Pointseed_point=stk.top(); stk.pop(); count=FillLineRegion(seed_point,fillColor,borderColor,0); right=seed_point.x+count-1; temp_point.x=seed_point.x-1; temp_point.y=seed_point.y; count=FillLineRegion(temp_point,fillColor,borderColor,1); left=seed_point.x-count; SearchLineNewSeed(left,right,seed_point.y-1,fillColor,borderColor); SearchLineNewSeed(left,right,seed_point.y+1,fillColor,borderColor); } return;}voidmyDraw(void){ glClear(GL_COLOR_BUFFER_BIT); glColor3f(0.0,0.0,0.0); glBegin(GL_LINE_LOOP); glVertex2f(20.0f,80.0f); glVertex2f(30.0f,95.0f); glVertex2f(50.0f,95.0f); glVertex2f(60.0f,80.0f); glVertex2f(70.0f,95.0f); glVertex2f(90.0f,95.0f); glVertex2f(100.0f,80.0f); glVertex2f(60.0f,30.0f); glEnd(); intx=60,y=60; structPointseed_point={65,65}; ColorfillColor,borderColor; fillColor[0]=1.0;fillColor[1]=0.0;fillColor[2]=0.0; borderColor[0]=0.0;borderColor[1]=0.0;borderColor[2]=0.0; stk.push(seed_point); scanLine(seed_point,fillColor,borderColor); glFlush();}voidmain(intargc,char**argv){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowPosition(50,100); glutInitWindowSize(400,400); glutCreateWindow("bianjie"); init(); glutDisplayFunc(myDraw); glutMainLoop();}

实验五裁剪算法实验实验目的和实验内容掌握用Cohen-Sutherland法裁减直线实验代码和实验结果#include<stdio.h>#include<gl/glut.h>#include<math.h>constwinLeftBitCode=0x1;constwinRightBitCode=0x2;constwinBottomBitCode=0x4;constwinTopBitCode=0x8;classwcPt2D{public:GLfloatx,y;};inlineintinside(intcode){ returnint(!code);}inlineintreject(intcode1,intcode2){ returnint(code1&code2);}inlineintaccept(intcode1,intcode2){ returnint(!(code1|code2));}GLubyteencode(wcPt2Dpt,wcPt2DwinMin,wcPt2DwinMax){ GLubytecode=0x00; if(pt.x<winMin.x) code=code|winLeftBitCode; if(pt.x>winMax.x) code=code+winRightBitCode; if(pt.y<winMin.y) 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;}voiddraw_pixel(intix,intiy/*,intvalue*/){ glBegin(GL_POINTS); glVertex2i(ix,iy); glEnd();}intinlineround(constfloata){ returnint(a+0.5);}voidlineDDA(intx0,inty0,intx_end,inty_end,doublea,doubleb,doublec){ glColor3f(a,b,c); intdx=x_end-x0; intdy=y_end-y0; intsteps,k; floatxlncrement,ylncrement,x=x0,y=y0; if(abs(dx)>abs(dy)) steps=abs(dx); else steps=abs(dy); xlncrement=float(dx)/float(steps); ylncrement=float(dy)/float(steps); draw_pixel(round(x),round(y)); for(k=0;k<steps;k++){ x+=xlncrement; y+=ylncrement; draw_pixel(round(x),round(y)); }}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=true; plotLine=true; } else if(reject(code1,code2)) done=true; else{ if(inside(code1)){ swapPts(&p1,&p2); swapCodes(&code1,&code2); } if(p2.x!=p1.x) m=(p2.y-p1.y)/(p2.x-p1.x); if(code1&winLeftBitCode){ p1.y+=(winMin.x-p1.x)*m; p1.x=winMin.x; } else if(code1&winRightBitCode){ p1.y+=(winMax.x-p1.x)*m; p1.x=winMax.x; } else if(code1&winBottomBitCode){ if(p2.x!=p1.x) p1.x+=(winMin.y-p1.y)/m; p1.y=winMin.y; } else if(code1&winTopBitCode){ if(p2.x!=p1.x) p1.x+=(winMax.y-p1.y)/m; p1.y=winMax.y; } } } if(plotLine) lineDDA(round(p1.x),round(p1.y),round(p2.x),round(p2.y

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论