OpenGL画圆

Posted 32朱杰

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OpenGL画圆相关的知识,希望对你有一定的参考价值。

中点画圆

 1 #include<gl/glut.h>
 2 
 3 
 4 void MidPointCirle(int r)
 5 {
 6     int x,y;
 7     float d;
 8     x=0;y=r;d=1.25-r;
 9     glColor3f(1.0f,1.0f,1.0f);
10     glVertex2i(x,y);
11     while(x<=y)
12     {
13         if(d<0)
14             d+=2*x+3;
15         else
16         {
17             d+=2*(x-y)+5;
18             y--;
19         }
20         x++;
21     glColor3f(1.0f,1.0f,1.0f);
22     glVertex2i(x+125,y+125);
23     glColor3f(1.0f,1.0f,1.0f);
24     glVertex2i(x+125,-y+125);
25     glColor3f(1.0f,1.0f,1.0f);
26     glVertex2i(-x+125,y+125);
27     glColor3f(1.0f,1.0f,1.0f);
28     glVertex2i(-x+125,-y+125);
29     glColor3f(1.0f,1.0f,1.0f);
30     glVertex2i(y+125,x+125);
31     glColor3f(1.0f,1.0f,1.0f);
32     glVertex2i(y+125,-x+125);
33     glColor3f(1.0f,1.0f,1.0f);
34     glVertex2i(-y+125,x+125);
35     glColor3f(1.0f,1.0f,1.0f);
36     glVertex2i(-y+125,-x+125);
37     }
38     glFlush(); 
39 
40 }
41 void ChangeSize(GLsizei w,GLsizei h)
42 {
43     if(h==0)
44         h=1;
45     glMatrixMode(GL_PROJECTION);
46     glLoadIdentity();
47     if(w<=h)
48         glOrtho(0.0f,250.0f,0.0f,250.0f*h/w,1.0f,-1.0f);
49     else
50         glOrtho(0.0f,250.0f*w/h,0.0f,250.0f,1.0f,-1.0f);
51     glMatrixMode(GL_MODELVIEW);
52     glLoadIdentity();
53 }
54 
55 void myDisplay(void)
56 {
57     glClear(GL_COLOR_BUFFER_BIT);
58     glPointSize(3);
59     glBegin(GL_POINTS);
60     glVertex2i(125,125);
61     MidPointCirle(125);
62     glEnd();
63 //    glDrawpixels(width,height,GL_RGBA,GL_UNSIGNED_BYTE,pixels);
64     glFlush();
65 }
66 int main(int argc,char *argv[])
67 {
68 
69     glutInit(&argc,argv);
70     glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
71     glutInitWindowPosition(100,100);
72     glutInitWindowSize(500,500);
73     glutCreateWindow("");
74     glutDisplayFunc(*myDisplay);
75 //    glutDisplayFunc(MidPointCirle);
76     glutReshapeFunc(ChangeSize);
77     glutMainLoop();
78     return 0;
79 }

多边形画圆

 1 #include<gl/glut.h>
 2 #include<math.h>
 3 const GLfloat Pi=301415927;
 4 const GLfloat R=0.5f;
 5 const int n=1000;
 6 
 7 void myDisplay(void)
 8 {
 9     int i;
10     glClear(GL_COLOR_BUFFER_BIT);
11     glColor3f(1.0f,1.0f,1.0f);
12     glBegin(GL_POINTS);
13     for(i=0;i<n;++i)
14     glVertex2f(R*cos(2*Pi/n*i),R*sin(2*Pi/n*i));
15     glEnd();
16 
17 
18     glFlush();
19 }
20 void ChangeSize(GLsizei w,GLsizei h)
21 {
22     if(h==0)
23         h=1;
24     glMatrixMode(GL_PROJECTION);
25     glLoadIdentity();
26     if(w<=h)
27         glOrtho(0.0f,250.0f,0.0f,250.0f*h/w,1.0f,-1.0f);
28     else
29         glOrtho(0.0f,250.0f*w/h,0.0f,250.0f,1.0f,-1.0f);
30     glMatrixMode(GL_MODELVIEW);
31     glLoadIdentity();
32 }
33 
34 
35 int main(int argc,char *argv[])
36 {
37 
38     glutInit(&argc,argv);
39     glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
40     glutInitWindowPosition(100,100);
41     glutInitWindowSize(500,500);
42     glutCreateWindow("");
43     glutDisplayFunc(*myDisplay);
44 
45 //    glutReshapeFunc(ChangeSize);
46     glutMainLoop();
47     return 0;
48 }

 

以上是关于OpenGL画圆的主要内容,如果未能解决你的问题,请参考以下文章

openGL画圆

OpenGL用鼠标移动画圆

OpenGL片段着色器不照亮场景

片段着色器中未使用纹理数据 - OpenGL

带有顶点/片段着色器的光。使用不同的变量。 (openGL)

OpenGL、GLSL 片段着色器无法读取 Sampler2D 纹理