使用OpenGL编程实现Bresenham直线扫描转换算法

更新时间:2023-10-25 06:50:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

实验要求:

学习Visual C++ 6.0 集成编程环境的使用,OpenGL编程环境的设置,OpenGL语法及基本函数的使用等基础知识,并编程实现Bresenham直线扫描转换算法,得出相应的输出图形。

源程序:

#include void k1() //0

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(0.0,0.0,1.0); glBegin(GL_POINTS);

GLint x1=0,y1=0,x2=400,y2=200; GLint x=x1,y=y1;

GLint dx=x2-x1,dy=y2-y1,dT=2*(dy-dx),dS=2*dy; GLint d=2*dy-dx; glVertex2i(x,y); while(x

{ x++; if(d<0)

d=d+dS;

else { }

glVertex2i(x,y); }

y++; d=d+dT;

glEnd();

}

glFlush();

void k2() //k>1 {

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(0.0,1.0,0.0); glBegin(GL_POINTS);

GLint x1=0,y1=0,x2=200,y2=400; GLint x=x1,y=y1;

GLint dx=x2-x1,dy=y2-y1,dT=2*(dx-dy),dS=2*dx; GLint d=2*dx-dy; glVertex2i(x,y); while(y

{ y++; if(d<0)

d=d+dS;

else {

x++; d=d+dT;

}

glVertex2i(x,y); }

glEnd(); }

void k3() //-1

glFlush();

{

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0,0.0,0.0); glBegin(GL_POINTS);

GLint x1=0,y1=400,x2=400,y2=200; GLint x=x1,y=y1;

GLint dx=x2-x1,dy=y2-y1,dT=2*dy,dS=2*dy+2*dx; GLint d=2*dy-dx; glVertex2i(x,y); while(x

{ x++; if(d<0) { } else d=d+dT; glVertex2i(x,y); }

y--; d=d+dS;

glEnd(); }

void k4() //k<-1 {

glClear(GL_COLOR_BUFFER_BIT); glFlush();

glColor3f(1.0,0.0,1.0); glBegin(GL_POINTS);

GLint x1=0,y1=400,x2=200,y2=0; GLint x=x1,y=y1;

GLint dx=x2-x1,dy=y2-y1,dT=-2*dy-2*dx,dS=-2*dx; GLint d=-dy-2*dx; glVertex2i(x,y); while(y>y2)

{ y--; if(d>0) d=d+dS;

else {

x++;

d=d+dT; }

glVertex2i(x,y);

}

glEnd(); }

void main(int argc,char ** argv) {

glutInit(&argc,argv); glFlush();

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(400,400); glutInitWindowPosition(200,100); glutCreateWindow(\ glClearColor(1.0,1.0,0.0,0.0); gluOrtho2D(0.0,400.0,0.0,400.0);

glutDisplayFunc(k1);

glutCreateWindow(\ glClearColor(1.0,0.0,1.0,0.0); gluOrtho2D(0.0,400.0,0.0,400.0); glutDisplayFunc(k2);

glutCreateWindow(\ glClearColor(0.0,1.0,1.0,0.0); gluOrtho2D(0.0,400.0,0.0,400.0); glutDisplayFunc(k3);

glutCreateWindow(\ glClearColor(0.0,1.0,0.0,0.0); gluOrtho2D(0.0,400.0,0.0,400.0); glutDisplayFunc(k4); glutMainLoop(); }

本文来源:https://www.bwwdw.com/article/d1z2.html

Top