您好,登錄后才能下訂單哦!
本文實例為大家分享了OpenGL繪制直線和圓的具體代碼,供大家參考,具體內容如下
#include <gl/glut.h> #include <math.h> static int i=1; void Initial(void) { glClearColor(1.0f, 1.0f, 1.0f, 1.0f); //設置窗口背景顏色為白色 glMatrixMode(GL_PROJECTION); //指定設置投影參數 gluOrtho2D(-200.0,200.0,-200.0,200.0); //設置投影參數 //指的是視圖范圍,如果(0,200,0,200)的話就只能在第一象限顯示,就1/4圓,第1,3的參數是左下角坐標,第2,4的參數是右上角坐標 } /*void Display(void) { glClear(GL_COLOR_BUFFER_BIT); //用當前背景色填充窗口 glColor3f(1.0f, 0.0f, 0.0f); //設置當前的繪圖顏色為紅色 glRectf(50.0f, 100.0f, 150.0f, 50.0f); //繪制一個矩形 glFlush(); //處理所有的OpenGL程序 } */ void DDALine(int x0,int y0,int x1,int y1) { //glVertex2f(10,10); 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; glPointSize(2); glBegin(GL_POINTS); for(k = 0;k <= epsl; k++) { glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(int(x+0.5),int(y+0.5)); x += xIncre; y += yIncre; } glEnd(); } void Mid_B(int x0,int y0,int x1,int y1) { int dx,dy,d,UpIncre,DownIncre,x,y; if(x0 > x1) { x = x1; x1 = x0; x0 = x; y = y1; y1 = y0; y0 = y; } x = x0; y = y0; dx = x1 - x0; dy = y1 - y0; d = dx - 2*dy; UpIncre = 2*dx - 2*dy; DownIncre =- 2*dy; glPointSize(2); glBegin(GL_POINTS); while(x <= x1) { glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(x,y); x++; if(d < 0) { y++; d += UpIncre; } else d += DownIncre; } glEnd(); } void G_B(int x0,int y0,int x1,int y1) { int x,y,dx,dy,e; dx = x1 - x0; dy = y1 - y0; e =- dx; x=x0; y=y0; glPointSize(2); glBegin(GL_POINTS); while(x <= x1) { glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(x,y); x++; e = e + 2*dy; if(e > 0) { y++; e = e - 2*dx; } } glEnd(); } void CirclePoint(int x, int y) { glPointSize(2); glBegin(GL_POINTS); glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(x,y); glVertex2f(y,x); glVertex2f(-y,x); glVertex2f(-x,y); glVertex2f(-x,-y); glVertex2f(-y,-x); glVertex2f(y,-x); glVertex2f(x,-y); glEnd(); } void MidBresenhamCircle(int r) { int x = 0, y = r, d = 1-r; //glPointSize(2); //glBegin(GL_POINTS); while(x <= y) { CirclePoint(x,y); if(d < 0) d += 2*x+3; else { d += 2*(x-y)+5; y--; } x++; } glEnd(); } void ProcessMenu(int value) { i = value; glutPostRedisplay(); } void Display(void) { glClear(GL_COLOR_BUFFER_BIT); //用當前背景色填充窗口 glViewport(0,0,400,400); //前兩個參數改變原點坐標,后兩個參數改變圖形長寬(放大縮小) switch(i) { case 1: DDALine(2,3,55,83); break; case 2: Mid_B(2,3,55,83); break; case 3: G_B(2,3,55,83); break; case 4: MidBresenhamCircle(50); break; } glFlush(); } int main(int argc, char* argv[]) { glutInit(&argc, argv); //初始化GLUT庫,處理命令行參數 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //初始化窗口的顯示模式 glutInitWindowSize(400,400); //設置窗口的尺寸 glutInitWindowPosition(100,100); //設置窗口的位置 glutCreateWindow("直線"); //創建一個名為矩形的窗口 int MainMenu = glutCreateMenu(ProcessMenu); //創建主菜單 glutAddMenuEntry("DDA算法",1); glutAddMenuEntry("中點Bresenham算法",2); glutAddMenuEntry("改進的Bresenham算法",3); glutAddMenuEntry("中點bresenham畫圓",4); glutAttachMenu(GLUT_RIGHT_BUTTON); glutDisplayFunc(Display); //設置當前窗口的顯示回調函數 Initial(); //完成窗口初始化 glutMainLoop(); //啟動主GLUT事件處理循環 return 0; }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。