南昌大学图形学实验报告完整版.doc_第1页
南昌大学图形学实验报告完整版.doc_第2页
南昌大学图形学实验报告完整版.doc_第3页
南昌大学图形学实验报告完整版.doc_第4页
南昌大学图形学实验报告完整版.doc_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

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

文档简介

实验一一、实验项目名称 熟悉opengl环境二、实验目的 1、掌握在利用OpenGL图形库进行图形程序设计的基本方法。 2、掌握Windows环境下的消息处理方法。 3、理解OpenGL运行机制。三、实验基本原理和内容 使用opengl绘制一个图形四、主要仪器设备及耗材PC机一台,win7 VC+6.0环境五、实验步骤1、打开VC+6.02、新建工程,键入代码3、记录结果六、实验数据及处理结果7、 思考讨论题或体会或对改进实验的建议 通过第一次实验体会到opengl的魅力8、 参考资料:计算机图形学(第三版)Donald Hearn 电子工业出版社 九、源代码18#includeusing namespace std;#include #include const int n = 1000;const GLfloat R = 0.5f;const GLfloat Pi = 3.1415926536f;void DrawCircle() int i; GLfloat x,y; glBegin(GL_LINE_LOOP); for(i=0; in; +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();void myDisplay() glClear(GL_COLOR_BUFFER_BIT); DrawCircle();void init (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); int main(int argc, char *argv) glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition(100, 100); glutInitWindowSize(500, 500); glutCreateWindow(心形);/init(); glutDisplayFunc(myDisplay); glutMainLoop(); return 0;实验二一、实验项目名称 任意斜率直线和中点画圆二、实验目的 理解DDA直线算法和中点画圆算法三、实验基本原理和内容 四、主要仪器设备及耗材PC机一台,win7 VC+6.0环境五、实验步骤六、实验数据及处理结果7、 思考讨论题或体会或对改进实验的建议画线的核心是通过算法来调整直线的生成方式,这只是其中一种方式八、参考资料:计算机图形学(第三版)Donald Hearn 电子工业出版社 附源码:1:任意直线斜率#include#include#includeusing namespace std;static float m=0;float x,y;void init(void)glClearColor(1.0,1.0,1.0,0);glMatrixMode(GL_PROJECTION);gluOrtho2D(0.0,200.0,0.0,150.0);void line()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(); else if(fabs(m)=1)glBegin(GL_POINTS);x=70;y=70;for(int i=0;i=1000;i+)glVertex2f(x,y);x+=0.1;y=y+m*0.1;glEnd();elseglBegin(GL_POINTS);x=70;y=70;for(int i=0;i=1000;i+)glVertex2f(x,y);y+=0.1;x=x+0.1/m;glEnd();glFlush();void main(int argc,char *argv)coutm;glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowPosition(50,100);glutInitWindowSize(400,300);glutCreateWindow(任意斜率的直线);init();glutDisplayFunc(line);glutMainLoop();2:中点画圆#includeusing namespace std;#include #include const int n = 1000;const GLfloat R = 0.5f;const GLfloat Pi = 3.1415926536f;/以下为一般圆生成算法,不采纳/*void DrawCircle() int i; glBegin(GL_LINE_LOOP); for(i=0; in; +i) glVertex2f(R*cos(2*Pi/n*i), R*sin(2*Pi/n*i); glEnd(); glFlush();void myDisplay() glClear(GL_COLOR_BUFFER_BIT); DrawCircle();*/以下代码为中点画圆算法class screenPtprivate:GLint x,y;public:sceenPt()x=y=0;void setCoords(GLint xcoordValue,GLint ycoordValue)x=xcoordValue;y=ycoordValue;GLint getx() constreturn x;GLint gety() constreturn y;void incrementx()x+;void decrementy()y-;void setPixel(GLint xcoord,GLint ycoord) /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);void circleMidpoint(GLint xc,GLint yc,GLint radius)screenPt circPt;GLint p=1-radius;circPt.setCoords(0,radius);void circlePlotPoints(GLint ,GLint ,screenPt);circlePlotPoints(xc,yc,circPt);while(circPt.getx()circPt.gety()circPt.incrementx();if(p0)p+=2*circPt.getx()+1;elsecircPt.decrementy();p+=2*(circPt.getx()-circPt.gety()+1;circlePlotPoints(xc,yc,circPt);void circlePlotPoints(GLint xc,GLint yc,screenPt circPt)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();void myDisplay()glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0f,0.0f,0.0f);glViewport(0,0,400,400);circleMidpoint(0,0,360);glFlush();void init (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);int main(int argc, char *argv) glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition(100, 100); glutInitWindowSize(500, 500); glutCreateWindow(画一个圆);init(); glutDisplayFunc(&myDisplay); glutMainLoop(); return 0;实验三一、实验项目名称直线裁剪算法二、实验目的 掌握Sutherland-Cohen 或Liang-Barsky 直线裁剪算法3、 实验基本原理和内容4、 四、主要仪器设备及耗材PC机一台,win7 VC+6.0环境五、实验步骤六、实验数据及处理结果 7、 思考讨论题或体会或对改进实验的建议剪裁的时候要注意视点的位置,以及剪裁点的设置八、参考资料:计算机图形学(第三版)Donald Hearn 电子工业出版社 附源码:/*#include class wcPt2Dprivate:GLfloat x,y;public:wcPt3D()x=0.0;y=0.0;setCoords(GLfloat xCoord,GLfloat yCoord)x=xCoord;y=yCoord;GLfloat getx() const return x;GLfloat gety() constreturn y;inline GLint round(const GLfloat a)return GLint(a+0.5);GLint clipTest(GLfloat p,GLfloat q,GLfloat *u1,GLfloat *u2)GLfloat r;GLint returnValue=true;if(p*u2)returnValue=false;elseif(r*u1)*u1=r;elseif(p0.0)r=q/p;if(r*u1)returnValue=false;else if(r*u2)*u2=r;elseif(q0.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;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(u20.0)p1.setCoords(p1.getx()+u1*dx,p1.gety()+u1*dy);lineBres(round(p1.getx(),round(p1.gety(),round(p2.getx(),round(p2.gety);void myDisplay(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(); void init (void) glClearColor(1.0 ,1.0, 1.0,0.0); glMatrixMode (GL_PROJECTION); gluOrtho2D(0.0,200.0,0.0,150.0);int main(int argc, char *argv)glutInit(&argc, argv);glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);glutInitWindowPosition(50, 100);glutInitWindowSize(400,300);glutCreateWindow(OpenGL);init ();glutDisplayFunc(&myDisplay);glutMainLoop();return 0;*/*利用VC+OpenGL实现直线的编码裁剪算法,在屏幕上用一个封闭矩形裁剪任意一条直线。#include #include int flag;void setPixel(GLint x,GLint y) glBegin(GL_POINTS); glVertex2i(x, y);glEnd(); /* Bresenham line-drawing procedure for |m| xEnd) x = xEnd; y = yEnd; xEnd = x0; else x = x0; y = y0; setPixel (x, y);while (x xEnd) x+; if (p 0) p += twoDy; else y+; p += twoDyMinusDx; setPixel (x, y); /*/以下未变,见课本P264/*class wcPt2D public: GLfloat x, y;inline GLint round (const GLfloat a) return GLint (a + 0.5); /* Define a four-bit code for each of the outside regions of a * rectangular clipping window.const GLint winLeftBitCode = 0x1;const GLint winRightBitCode = 0x2;const GLint winBottomBitCode = 0x4;const GLint winTopBitCode = 0x8;/* A bit-mask region code is also assigned to each endpoint of an input * line segment, according to its position relative to the four edges of * an input rectangular clip window. * * An endpoint with a region-code value of 0000 is inside the clipping * window, otherwise it is outside at least one clipping boundary. If * the or operation for the two endpoint codes produces a value of * false, the entire line defined by these two endpoints is saved * (accepted). If the and operation between two endpoint codes is * true, the line is completely outside the clipping window, and it is * eliminated (rejected) from further processing. */inline GLint inside (GLint code) return GLint (!code); inline GLint reject (GLint code1, GLint code2) return GLint (code1 & code2); inline GLint accept (GLint code1, GLint code2) return GLint (!(code1 | code2); GLubyte encode (wcPt2D pt, wcPt2D winMin, wcPt2D winMax) GLubyte code = 0x00;if (pt.x winMax.x) code = code | winRightBitCode; if (pt.y winMax.y) code = code | winTopBitCode; return (code);void swapPts (wcPt2D * p1, wcPt2D * p2) wcPt2D tmp;tmp = *p1; *p1 = *p2; *p2 = tmp;void swapCodes (GLubyte * c1, GLubyte * c2) GLubyte tmp;tmp = *c1; *c1 = *c2; *c2 = tmp;void lineClipCohSuth (wcPt2D winMin, wcPt2D winMax, wcPt2D p1, wcPt2D p2) GLubyte code1, code2; GLint done = false, plotLine = false;GLfloat m; 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 /* Label the endpoint outside the display window as p1. */ if (inside (code1) swapPts (&p1, &p2); swapCodes (&code1, &code2); /* Use slope m to find line-clipEdge intersection. */ 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) /* Need to update p1.x for nonvertical lines only. */ 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) lineBres (round (p1.x), round (p1.y), round (p2.x), round (p2.y);/lineBres直线生成算法前面/*/以上未变/*void myDisplay() wcPt2D winMin , winMax , p1 , p2; winMin.x=100; winMin.y=200;/定义裁剪窗口 winMax.x=400; winMax.y=400; p1.x=50; p1.y=50; / 定义被裁剪的直线端点 p2.x=450; p2.y=450; glClear(GL_COLOR_BUFFER_BIT); glColor3f (1.0f, 0.0f, 0.0f); glRectf(winMin.x ,winMin.y ,winMax.x,winMax.y); / 绘制红色裁剪矩形 glColor3f (1.0f, 1.0f, 1.0f); if (flag) lineClipCohSuth (winMin, winMax,p1,p2);/ 绘制裁剪线段 else glBegin(GL_LINES); glVertex2i(p1.x,p1.y); glVertex2i(p2.x,p2.y); glEnd(); glFlush(); void Init() glClearColor(0.0, 0.0, 0.0, 0.0); glMatrixMode (GL_PROJECTION); gluOrtho2D (0.0, 640.0, 0.0, 480.0); 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) if (key=c | key=C) flag=true; if (key=r | key=R) flag=false; glutPostRedisplay(); int main(int argc, char *argv) glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutInitWindowPosition(100, 100); glutInitWindowSize(640, 480); glutCreateWindow(Cohen-Sutherland直线裁减算法,C 键裁减,R 键复原); Init(); glutDisplayFunc(myDisplay); glutReshapeFunc(Reshape); glutKeyboardFunc(keyboard); glutMainLoop(); return 0; 实验四一、实验项目名称 Bezier曲线的绘制二、实验目的 1、进一步曲线的表示原理; 2、掌握Bezier 曲线的定义; 3、掌握Bezier 曲线的绘制算法;三、实验基本原理和内容四、主要仪器设备及耗材PC机一台,win7 VC+6.0环境五、实验步骤六、实验数据及处理结果7、 思考讨论题或体会或对改进实验的建议Bazier曲线的生成非常的美妙,算法相对其他的偏难了点,要花时间去思考。八、参考资料:计算机图形学(第三版)Donald Hearn 电子工业出版社 附源码:#include#include#includeGLsizei winWidth=600,winHeight=600;GLfloat xwcMin=-50.0,xwcMax=50.0;GLfloat ywcMin=-50.0,ywcMax=50.0;class wcPt3Dpublic:GLfloat x,y,z;void init()glClearColor(1.0,1.0,1.0,0.0);void plotPoint(wcPt3D bez)glBegin(GL_POINTS);glVertex2f(bez.x,bez.y);glEnd();void bino(GLint n,GLint *c)GLint k,j;for(k=0;k=k+1;j-)ck*=j;for(j=n-k;j=2;j-)ck/=j;void computebez(GLfloat u,wcPt3D *bez,GLint nctrl,wcPt3D *ctrl,GLint *c)GLint k,n=nctrl-1;GLfloat bezBlendFcn;bez-x=bez-y=bez-z=0.0;for(k=0;kx+=ctrlk.x*bezBlendFcn;bez-y+=ctrlk.y*bezBlendFcn;bez-z+=ctrlk.z*bezBlendFcn;void bezier(wcPt3D *ctrl,GLint nctrl,GLint nbez)wcPt3D bez;GLfloat u;GLint *c,k;c=new GLintnctrl;bino(nctrl-1,c);for(k=0;k=nbez;k+)u=GLfloat(k)/GLfloat(nbez);computebez(u,&bez,nctrl,ctrl,c);plotPoint(bez); delete c;void display()GLint nctrl=4,nbez=1000;wcPt3D ctrl4=-20.0,20.0,0.0,-5.0,100.0,0.0,5.0,-100.0,0.0,20.0,20.0,0.0;glClear(GL_COLOR_BUFFER_BIT);glPointSize(4);glColor3f(1.0,0.0,0.0);bezier(ctrl,nctrl,nbez);glFlush();void winReshape(GLint newWidth,GLint newHeight)glViewport(0,0,newHeight,newHeight);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(xwcMin,xwcMax,ywcMin,ywcMax);glClear(GL_COLOR_BUFFER_BIT);void main(int argc,char * argv)glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowPosition(50,50);glutInitWindowSize(600,600);glutCreateWindow(bezeier);init();glutDisplayFunc(display);glutReshapeFunc(winReshape);glutMainLoop();实验五一、实验项目名称 B样条曲线二、实验目的 掌握B样条曲线的定义生成算法三、实验基本原理和内容四、主要仪器设备及耗材PC机一台,win7 VC+6.0环境五、实验步骤六、实验数据及处理结果 7、 思考讨论题或体会或对改进实验的建议:B样条曲线很有特点,可以通过一些容易理解的方式去理解八、参考资料:计算机图形学(第三版)Donald Hearn 电子工业出版社 附源码:/B样条曲线(实现鼠标右键在屏幕中选点,左键可拖动点)(三次,四点确定一条曲线) #include #include #include #include #include GLfloat x_coord100, y_coord100;int nPoints = 0; int j=0; inline GLfloat x_convert (int x) return -5.0+x/249.0*10; inline GLfloat y_convert (int y) return 5.0 - y/249.0*10; void init() glClearColor(1,1,1,0); void myReshape(int w,int h) glViewport(0,0,(GLsizei)w,(GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); /gluPerspective(45.0,(GLfloat)w/(GLfloat)h,1.0,50.0); if(w=h)glOrtho(-5.0,5.0,-5.0*(GLfloat)h/(GLfloat)w,5.0*(GLfloat)h/(GLfloat)w,-5.0,5.0); elseglOrtho(-5.0*(GLfloat)h/(GLfloat)w,5.0*(GLfloat)h/(GLfloat)w,-5.0,5.0,-5.0,5.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); / (1/2)*(t-1)*(t-1) / (1/2)*(-2*t*t+2*t+1) / (1/2)*t*t void B2(int n) /B样条3次曲线 float t, dt, t2, t3, f1, f2, f3, f4; dt = 1.0/n; / t runs from 0 to 1. glBegin(GL_LINE_STRIP); for(int j=0; j (nPoin

温馨提示

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

评论

0/150

提交评论