openGL画直线、圆、椭圆.doc_第1页
openGL画直线、圆、椭圆.doc_第2页
openGL画直线、圆、椭圆.doc_第3页
openGL画直线、圆、椭圆.doc_第4页
openGL画直线、圆、椭圆.doc_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

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

文档简介

使用openGl画直线(DDA算法)、画圆、椭圆(Bresenham算法)#include#include #include /* initialization: */ void myinit(void) /* attributes */ glClearColor(1.0, 1.0, 1.0, 0.0); /* white background */ glColor3f(1.0, 0.0, 0.0); /* draw in red */ /* set up viewing: */ /* 500 x 500 window with origin lower left */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 500.0, 0.0, 500.0); glMatrixMode(GL_MODELVIEW); void plot_circle_points(int xc,int yc,int x,int y)/画圆 glBegin(GL_POINTS); glVertex3f(xc+x,yc+y,0); glVertex3f(xc-x,yc+y,0); glVertex3f(xc+x,yc-y,0); glVertex3f(xc-x,yc-y,0); glVertex3f(xc+y,yc+x,0); glVertex3f(xc-y,yc+x,0); glVertex3f(xc+y,yc-x,0); glVertex3f(xc-y,yc-x,0); glEnd();void drawcircle(int xc,int yc,int radius) int x,y,p; x=0; y=radius; p=3-2*radius; glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POINTS); while(xy) plot_circle_points(xc,yc,x,y); if(p0) p=p+4*x+6; else p=p+4*(x-y)+10; y-=1; x+=1; if(x=y) plot_circle_points(xc,yc,x,y); void putpixel(int xc,int yc,int x,int y)/画椭圆glBegin(GL_POINTS); glVertex3f(xc+x,yc+y,0); glEnd();void drawEllipse(int xc,int yc,int a,int b) int x,y;float d1,d2;x=0;y=b;d1=b*b+a*a*(-b+0.25);putpixel(xc,yc,x,y);putpixel(xc,yc,-x,-y);putpixel(xc,yc,-x,y);putpixel(xc,yc,x,-y);while(b*b*(x+1)a*a*(y-0.5)if(d10)if(d2abs(dy) steps=abs(dx); else steps=abs(dy); delta_x=(GLfloat)dx/(GLfloat)steps; delta_y=(GLfloat)dy/(GLfloat)steps; x=xa; y=ya; glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POINTS); glVertex3f(x,y,0); for(int k=1;k=steps;k+) x+=delta_x; y+=delta_y; glBegin(GL_POINTS); glVertex3f(x,y,0); glEnd(); /* the display callback: */ void display1() /画圆 glClear(GL_COLOR_BUFFER_BIT); /*clear the window */ /*-*/ /* viewport stuff */ /*-*/ /* set up a viewport in the screen window */ /* args to glViewport are left, bottom, width, height */ glViewport(0, 0, 500, 500); /* NB: default viewport has same coords as in myinit, */ /* so this could be omitted: */dda_line(20,20,420,350); /* and flush that buffer to the screen */ glFlush(); void display2() /画圆 glClear(GL_COLOR_BUFFER_BIT); /*clear the window */ /*-*/ /* viewport stuff */ /*-*/ /* set up a viewport in the screen window */ /* args to glViewport are left, bottom, width, height */ glViewport(0, 0, 500, 500); /* NB: default viewport has same coords as in myinit, */ /* so this could be omitted: */drawcircle(250,250,100); /* and flush that buffer to the screen */ glFlush(); void display3() /画圆 glClear(GL_COLOR_BUFFER_BIT); /*clear the window */ /*-*/ /* viewport stuff */ /*-*/ /* set up a viewport in the screen window */ /* args to glViewport are left, bottom, width, height */ glViewport(0, 0, 500, 500); /* NB: default viewport has same coords as in myinit, */ /* so this could be omitted: */drawEllipse(250,250,60,40); /* and flush that buffer to the screen */ glFlush(); int main(int argc, char* argv) /* Standard GLUT initialization */ glutInit(&argc,argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); /* default, not needed */ glutInitWindowSize(500,500); /* 500 x 500 pixel window */ glutInitWindowPosition(0,0); /* place window top left on display */cout*endl;cout* 请选择如下操作: *endl;cout* 1.用DDA画线 *endl;cout* 2.用Bresenham算法画圆 *endl;cout* 3.用Bresenham算法画椭圆 *endl;cout*endl;coutchoice;switch(choice)case 1: glutCreateWindow(Bresenham circile); /* window title */ glutDisplayFunc(display1); /* display callback invoked when window opened */ myinit(); /* set attributes */ glutMainLoop(); /* enter event loop */break;case 2: glutCreateWindow(Digital Differential Analyser Line); /* window title */ glutDisplayFunc(display2); /* display callback invoked when window opened */ myinit(); /* set attributes */ glutMainLoop(); /* enter event loop */break;case 3:glutCreateWindow(MidBresenhamEllipse

温馨提示

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

评论

0/150

提交评论