OPENGL—改進Bresenham畫直線
阿新 • • 發佈:2019-01-11
執行結果:// 改進Bresenham畫直線 #include "stdafx.h" #include <gl/glut.h> #include <cmath> void init(void) { glClearColor(1.0,1.0,1.0,1.0); //設定背景顏色為白色 glMatrixMode(GL_PROJECTION); //對投影相關進行操作 gluOrtho2D(0.0, 30.0, 0.0, 30.0); } void putpixel(int x, int y) { glColor3f(1.0, 0.0, 0.0); glPointSize(2.0f); glBegin(GL_POINTS); glVertex2f(15+x, 15+y); glEnd(); glFlush(); } void BresenhamLine(int x0,int y0,int x1,int y1) { int dx,dy,e,x,y; dx=x1-x0; dy=y1-y0; e=-dx; x=x0; y=y0; while(x<=x1) { putpixel(x,y); x++; e=e+2*dy; if(e>0) { y++; e=e-2*dx; } } } void display() { glClear(GL_COLOR_BUFFER_BIT); BresenhamLine(0,0,8,6); } int main(int argc,char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(400,400); glutInitWindowPosition(0,0); glutCreateWindow("改進Bresenham畫直線"); glutDisplayFunc(display); init(); glutMainLoop(); return 0; }