实验一OpenGL直线圆的绘制Word格式.docx
- 文档编号:17776768
- 上传时间:2022-12-10
- 格式:DOCX
- 页数:15
- 大小:86.39KB
实验一OpenGL直线圆的绘制Word格式.docx
《实验一OpenGL直线圆的绘制Word格式.docx》由会员分享,可在线阅读,更多相关《实验一OpenGL直线圆的绘制Word格式.docx(15页珍藏版)》请在冰豆网上搜索。
=pEndy)
17.{
18.for(i=pStarty;
i<
=pEndy;
i++)
19.glVertex2f(pStartx,i);
20.}
21.else
22.{
23.for(i=pEndy;
=pStarty;
24.glVertex2f(pStartx,i);
25.}
26.
27.return;
28.}
29.
30.//为横线
31.if(pStarty==pEndy)
32.{
33.if(pStartx<
=pEndx)
34.{
35.for(i=pStartx;
=pEndx;
36.glVertex2f(i,pStarty);
37.}
38.else
39.{
40.for(i=pEndx;
=pStartx;
41.glVertex2f(i,pStarty);
42.}
43.
44.return;
45.}
46.
47.//为斜线
48.floatm=(pEndy-pStarty)*1.0/(pEndx-pStartx);
49.floatp;
50.p=2*m-1;
51.if(m>
0&
&
m<
=1)
52.{
53.if(pStartx<
pEndx)
54.{
55.while(pStartx<
56.{
57.glVertex2f(pStartx++,pStarty);
58.if(p>
=0)
59.{
60.p+=2*m-2;
61.pStarty++;
62.}
63.else
64.p+=2*m;
65.}
66.}
67.else
68.{
69.while(pEndx<
=pStartx)
70.{
71.glVertex2f(pEndx++,pEndy);
72.if(p>
73.{
74.p+=2*m-2;
75.pEndy++;
76.}
77.else
78.p+=2*m;
79.}
80.}
81.
82.return;
83.}
84.
85.p=-2*m-1;
86.if(m<
m>
=-1)
87.{
88.if(pStartx<
89.{
90.while(pStartx<
91.{
92.glVertex2f(pStartx++,pStarty);
93.if(p>
94.{
95.p+=-2*m-2;
96.pStarty--;
97.}
98.else
99.p+=-2*m;
100.}
101.}
102.else
103.{
104.while(pEndx<
105.{
106.glVertex2f(pEndx++,pEndy);
107.if(p>
108.{
109.p+=-2*m-2;
110.pEndy--;
111.}
112.else
113.p+=-2*m;
114.}
115.}
116.
117.return;
118.}
119.
120.p=2/m-1;
121.if(m>
1)
122.{
123.if(pStarty<
pEndy)
124.{
125.while(pStarty<
126.{
127.glVertex2f(pStartx,pStarty++);
128.if(p>
129.{
130.p+=2/m-2;
131.pStartx++;
132.}
133.else
134.p+=2/m;
135.}
136.}
137.else
138.{
139.while(pEndy<
=pStarty)
140.{
141.glVertex2f(pEndx,pEndy++);
142.if(p>
143.{
144.p+=2/m-2;
145.pEndx++;
146.}
147.else
148.p+=2/m;
149.}
150.}
151.
152.return;
153.}
154.
155.p=-2/m-1;
156.if(pStarty<
157.{
158.while(pStarty<
159.{
160.glVertex2f(pStartx,pStarty++);
161.if(p>
162.{
163.p+=-2/m-2;
164.pStartx--;
165.}
166.else
167.p+=-2/m;
168.}
169.}
170.else
171.{
172.while(pEndy<
173.{
174.glVertex2f(pEndx,pEndy++);
175.if(p>
176.{
177.p+=-2/m-2;
178.pEndx--;
179.}
180.else
181.p+=-2/m;
182.}
183.}
184.}
185.
186.//////////////////画圆////////////
187./////////其他象限绘制
188.voidCirclePoints(intx,inty){
189.//第1象限
190.glVertex2f(x,y);
191.glVertex2f(y,x);
192.//第2象限
193.glVertex2f(-x,y);
194.glVertex2f(-y,x);
195.//第3象限
196.glVertex2f(-y,-x);
197.glVertex2f(-x,-y);
198.//第4象限
199.glVertex2f(x,-y);
200.glVertex2f(y,-x);
201.
202.}
203.//////////中点算法画圆
204.voidDrawCircle(intcx,intcy,intradis){
205.
206.glPushMatrix();
207.glTranslatef(cx,cy,0);
208.glPointSize
(1);
209.glColor3f(0.5f,0.5f,1.0f);
210.glBegin(GL_POINTS);
211.
212.intx,y;
213.doublep;
214.x=0;
215.y=radis;
216.p=1.25-radis;
217.while(x<
=y+1)
218.{
219.CirclePoints(x,y);
220.x++;
221.if(p>
222.{
223.y--;
224.p+=2.0*(x-y)+5;
225.}
226.else
227.p+=2*x+3;
228.}
229.glEnd();
230.glPopMatrix();
231.}
232.
233.
234.//////////绘制坐标轴
235.voidDrawOx(){
236.glColor3f(0.95,0.7,0.8);
237.glPointSize
(1);
238.glBegin(GL_LINES);
239.glVertex2f(-winWidth/2,0);
240.glVertex2f(winWidth/2,0);
241.glVertex2f(0,winHeight/2);
242.glVertex2f(0,-winHeight/2);
243.glEnd();
244.}
245.
246.//////////显示函数
247.voidDisplay(){
248.////////GL_COLOR_BUFFER_BIT(用背景颜色填充)
249.glClear(GL_COLOR_BUFFER_BIT);
250.
251.DrawOx();
252.
253.glColor3f(0.0,0.0,1.0);
254.
255.glBegin(GL_POINTS);
256.//BresenhamLine(px1,py1,px2,py2);
257.Draw_Bresenham(px1,py1,px2,py2);
258.glEnd();
259.glBegin(GL_POINTS);
260.Draw_Bresenham(py1,px1,py2,px2);
261.glEnd();
262.//glBegin(GL_LINES);
263.//glVertex2f(px1,py1);
264.//glVertex2f(px2,py2);
265.//glVertex2f(py1,px1);
266.//glVertex2f(py2,px2);
267.//glEnd();
268.DrawCircle(cx,cy,r);
269.DrawCircle(cy,cx,r);
270.
271.///交换缓冲区
272.glutSwapBuffers();
273.//glFlush();
//刷新绘图命令
274.}
275.
276.
277.//设置渲染状态(听起来满下人,实际上很简单)
278.voidSetupRC(void)
279.{
280.//清除颜色(这里为黑色,为了方便找画的那个点),可以理解成背景颜色
281.//和glColor4f(1.0f,0.0f,0.0f,1.0f)一样,所有参数都在0.0到1.0之间,后缀f是表示参数是浮点型的
282.//最后的那个1.0f是透明度,0.0f表示全透明,1.0f是完全不透明
283.glClearColor(1.0f,1.0f,1.0f,1.0f);
284.}
285.
286.//当绘制的窗口大小改变时重新绘制,使绘制的图形同比例变化,
287.//几乎所有OpenGL程序中的这个函数都是一样的,所以,放心大胆的拷贝吧
288.voidChangeSize(intw,inth)
289.{
290.winWidth=w;
291.winHeight=h;
292.//设置观察视野为窗口大小(用FLASH里面的话来说应该叫设置摄象机视野)
293.glViewport(0,0,w,h);
294.//重置坐标系统,指定设置投影参数
295.glMatrixMode(GL_PROJECTION);
296.///////调用单位矩阵,去掉以前的投影参数设置
297.glLoadIdentity();
298.//////设置投影参数
299.gluOrtho2D(-w/2,w/2,-h/2,h/2);
300.}
301.
302./////////////////鼠标点击
303.voidMousePlot(GLintbutton,GLintaction,GLintxMouse,GLintyMouse){
304.if(button==GLUT_LEFT_BUTTON&
action==GLUT_DOWN){
305.px1=xMouse-winWidth/2;
306.py1=winHeight/2-yMouse;
307.}
308.if(button==GLUT_LEFT_BUTTON&
action==GLUT_UP){
309.px2=xMouse-winWidth/2;
310.py2=winHeight/2-yMouse;
311.glutPostRedisplay();
312.}
313.if(button==GLUT_RIGHT_BUTTON&
314.cx=xMouse-winWidth/2;
315.cy=winHeight/2-yMouse;
316.}
317.}
318.////////////////鼠标移动
319.voidMouseMove(GLintxMouse,GLintyMouse){
320.px2=xMouse-winWidth/2;
321.py2=winHeight/2-yMouse;
322.r=sqrt(pow((xMouse-winWidth/2-cx),2)+pow((winHeight/2-yMouse-cy),2));
323.glutPostRedisplay();
324.}
325.
326.//主函数
327.intmain(intargc,char*argv[])
328.{
329.glutInit(&
argc,argv);
330.//设置显示模式
331.glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
332.//设置窗口大小像素
333.glutInitWindowSize(600,600);
334.////设置窗口出现在屏幕的位置
335.glutInitWindowPosition(300,160);
336.//建立一个叫OpenGL的窗口
337.glutCreateWindow("
OpenGL-Line"
);
338.
339.//调用函数Display进行绘制
340.glutDisplayFunc(Display);
341.//////调用鼠标移动函数
342.//glutPassiveMotionFunc(PassiveMouseMove);
343.glutMotionFunc(MouseMove);
344.//////调用鼠标点击函数
345.glutMouseFunc(MousePlot);
346.
347.//如果窗口大小改变则调用函数ChangeSize重新进行绘制
348.glutReshapeFunc(ChangeSize);
349.//清屏
350.SetupRC();
351.//循环绘制
352.glutMainLoop();
353.
354.return0;
355.}
356.如有侵权请联系告知删除,感谢你们的配合!
357.
358.
359.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验 OpenGL 直线 绘制
![提示](https://static.bdocx.com/images/bang_tan.gif)