目录实验一直线的DDA算法一、【实验目的】1.掌握DDA算法的基本原理。
2.掌握DDA直线扫描转换算法。
3.深入了解直线扫描转换的编程思想。
二、【实验内容】1.利用DDA的算法原理,编程实现对直线的扫描转换。
2.加强对DDA算法的理解和掌握。
三、【测试数据及其结果】四、【实验源代码】#include<stdlib.h>#include<math.h>#include<GL/glut.h>#include<stdio.h>GLsizei winWidth=500;GLsizei winHeight=500;void Initial(void){glClearColor(1.0f,1.0f,1.0f,1.0f); glMatrixMode(GL_PROJECTION); gluOrtho2D(0.0,200.0,0.0,150.0);}void DDALine(int x0,int y0,int x1,int y1) {glColor3f(1.0,0.0,0.0);int dx,dy,epsl,k;float x,y,xIncre,yIncre;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++){glPointSize(3);glBegin(GL_POINTS);glV ertex2i(int(x+0.5),(int)(y+0.5));glEnd();x+=xIncre;y+=yIncre;}}void Display(void){glClear(GL_COLOR_BUFFER_BIT); DDALine(100,100,200,180);glFlush();}void winReshapeFcn(GLint newWidth, GLint newHeight){glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0, GLdouble(newWidth), 0.0, GLdouble(newHeight));glClear(GL_COLOR_BUFFER_BIT);winWidth=newWidth;winHeight=newHeight;}int main(int argc,char*argv[]){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(400,300);glutInitWindowPosition(100,120);glutCreateWindow("line");Initial();glutDisplayFunc(Display);glutReshapeFunc(winReshapeFcn);glutMainLoop();return 0;}实验二Bresenham绘制直线和圆一、【实验目的】1.掌握Bresenham算法扫描转换圆和直线的基本原理。
二、【实验内容】1.利用Bresenham算法扫描转换圆和直线的基本原理编程实现对圆和直线的扫描转换。
三、【测试数据及其结果】四、【实验源代码】绘制直线:#include<stdlib.h>#include<math.h>#include<GL/glut.h>#include<stdio.h>GLsizei winWidth=500;GLsizei winHeight=500;void lineBres(int x0, int y0, int xEnd, int yEnd) {glColor3f(0.0, 0.0, 1.0);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;}glPointSize(6);glBegin(GL_POINTS);glVertex2i(x, y);glEnd();while (x<xEnd){x++;if (p<0)p+=twoDy;else{y++;p+=twoDyMinusDx;}glPointSize(2);glBegin(GL_POINTS);glVertex2i(x, y);glEnd();}}void init (void){glClearColor(1.0, 1.0, 1.0, 1.0);glShadeModel(GL_FLAT);}void display (void){glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); lineBres(10, 10, 400, 300);glFlush();}void winReshapeFcn(GLint newWidth, GLint newHeight){glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0, GLdouble(newWidth), 0.0, GLdouble(newHeight));glClear(GL_COLOR_BUFFER_BIT);winWidth=newWidth;winHeight=newHeight;}void main(int argc, char** argv){glutInit(&argc, argv);glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition(10, 10);glutInitWindowSize(winWidth, winHeight);glutCreateWindow("lineBres");init();glutDisplayFunc(display); glutReshapeFunc(winReshapeFcn); glutMainLoop();}绘制圆:#include<gl/glut.h>void init(){glClearColor(0,0,0,0);}void MidBresenhamCircle(int r){int x,y,d;x=0;y=r;d=1-r;glBegin(GL_LINE_STRIP);while(x<=y){glVertex2f(x,y);if(d<0) d+=2*x+3;else{d+=2*(x-y)+5;y--;}x++;}glEnd();}void display(){glClearColor(1,1,1,1);glClear(GL_COLOR_BUFFER_BIT);glColor3f(1,0,0);MidBresenhamCircle(8);glRotated(45,0,0,1);MidBresenhamCircle(8);glRotated(45,0,0,1);MidBresenhamCircle(8);glRotated(45,0,0,1);MidBresenhamCircle(8);glRotated(45,0,0,1);MidBresenhamCircle(8);glRotated(45,0,0,1);MidBresenhamCircle(8);glRotated(45,0,0,1);MidBresenhamCircle(8);glRotated(45,0,0,1);MidBresenhamCircle(8);glutSwapBuffers();}void reshape(int w,int h){glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION); glLoadIdentity();gluOrtho2D(-10,10,-10,10);}int main(int argc,char**argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); glutInitWindowSize(400,400); glutInitWindowPosition(100,100); glutCreateWindow("扫描转换圆"); glutDisplayFunc(display);glutReshapeFunc(reshape);glutMainLoop();return 0;}实验三反走样及五环的绘制一、【实验目的】1.了解走样和反走样的内容,熟练掌握用opengl实现图形的反走样。
2.学会用反走样消除走样现象。
3.学会五环的绘制方法。
二、【实验内容】1.通过学习反走样相关课程,用opengl实现光栅图形的反走样。
2.绘制五环。
三、【测试数据及其结果】四、【实验源代码】反走样:#include<gl/glut.h>#pragma comment(linker,"/subsystem:\"windows\" /entry:\"mainCRTStartup\"") GLuint lineList; //指定显示列表void Initial(){glClearColor(1.0f,1.0f,1.0f,0.0f);glLineWidth(12.0f);glColor4f(0.0,0.6,1.0,1.0);lineList=glGenLists(1); //获得一个显示列表标识glNewList(lineList,GL_COMPILE); //定义显示列表glBegin(GL_LINE_LOOP);glV ertex2f(1.0f,1.0f);glV ertex2f(4.0f,2.0f);glV ertex2f(2.0f,5.0f);glEnd();glEndList();}void ChangeSize(GLsizei w,GLsizei h){if(h==0) h=1;glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION); //指定设置投影参数glLoadIdentity();if(w<=h)gluOrtho2D(0.0,5.0,0.0,6.0*(GLfloat)h/(GLfloat)w);elsegluOrtho2D(0.0,5.0*(GLfloat)w/(GLfloat)h,0.0,6.0);glMatrixMode(GL_MODELVIEW); //指定设置模型视图变换参数glLoadIdentity();}void Displayt(void){glClear(GL_COLOR_BUFFER_BIT);glCallList(lineList); //调用显示列表glFlush();}void Displayw(void){glClear(GL_COLOR_BUFFER_BIT);glEnable(GL_LINE_SMOOTH); //使用反走样glEnable(GL_BLEND); //启用混合函数glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); //指定混合函数glCallList(lineList); //调用显示列表glFlush();}void main(void){glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(300,300);glutCreateWindow("原始图形");glutDisplayFunc(Displayt);glutReshapeFunc(ChangeSize);Initial();glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowPosition(300,300);glutInitWindowSize(300,300);glutCreateWindow("反走样图形");glutDisplayFunc(Displayw);glutReshapeFunc(ChangeSize);Initial();glutMainLoop();}五环:#include<gl/glut.h>#include <MA TH.H>#pragma comment(linker,"/subsystem:\"windows\" /entry:\"mainCRTStartup\"")const float PI=3.1415;void DrawCircle(GLfloat radius){GLfloat x,y,z;glBegin(GL_LINE_LOOP);for (int alpha=0;alpha<360;alpha++){x=radius*cos(alpha*PI/180);y=radius*sin(alpha*PI/180);z=0;glVertex3f(x,y,z);}glEnd();}void Display(){glClearColor(1,1,1,1);glClear(GL_COLOR_BUFFER_BIT);glLoadIdentity();glTranslatef(0,0,-25);glColor3f(0,1,0);glLineWidth(3);DrawCircle(3.0);glPopMatrix();glPushMatrix();glTranslatef(7,0,0);glColor3f(1,0,0);DrawCircle(3.0);glPopMatrix();glPushMatrix();glTranslatef(-7,0,0);glColor3f(0,0,1);DrawCircle(3.0);glPopMatrix();glPushMatrix();glTranslatef(-3.5,-3.5,0);glColor3f(0.3,0.5,0.7);DrawCircle(3.0);glPopMatrix();glPushMatrix();glTranslatef(3.5,-3.5,0);glColor3f(0.7,0.0,0.3);DrawCircle(3.0);glPopMatrix();glutSwapBuffers();}void reshape(int w,int h){glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(45,GLdouble(w)/h,1,100);glMatrixMode(GL_MODELVIEW);}void main(int argc,char **argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE);glutInitWindowPosition(10,10);glutInitWindowSize(500,500);glutCreateWindow("Test");glutDisplayFunc(Display);glutReshapeFunc(reshape);glutMainLoop();}实验四多视区一、【实验目的】1.熟练掌握各种裁剪算法和二维观察变换。