本文实例为大家分享了OpenGL绘制直线和圆的具体代码,供大家参考,具体内容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
#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; } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/cjx798280904/article/details/12624977