1 #include <GL/glut.h>
 2 #include<cstdio>
 3 #include<cmath>
 4 
 5 #define PI 3.14159265358979323846
 6 #define PI2 6.28318530717958647692
 7 #define r 80
 8 
 9 GLsizei width = 600,height = 600;
10 
11 int uStepsNum = 25;
12 
13 
14 void Init()
15 {
16       glClearColor(1,1,1,1);
17       glClearDepth(1);
18       glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
19       glOrtho(100.0, -100.0 ,100.0, -100.0, 100.0, -100.0);
20 
21 
22       glRotated(30,1,0,0);
23       glRotated(60,0,1,0);
24       glRotated(0,0,0,1);
25 
26 }
27 
28 void displayFunc()
29 {
30 
31     double i,j;
32     double w = 1/(double)uStepsNum;    
33     glColor3f(1.0,0.0,0.0);
34     for(i=0;i<=1;i+=w)
35     {
36         glBegin(GL_LINE_LOOP);
37         for(j=0;j<=1;j+=w)
38         {     
39            double x=r*sin(PI*i)*cos(PI2*j);
40            double y=r*sin(PI*i)*sin(PI2*j);
41            double z=r*cos(PI*i);
42            glVertex3d(x,y,z);   
43         }
44         glEnd();
45     }
46     glColor3f(0.0,1.0,0.0);
47     glBegin(GL_LINE_LOOP);
48    for(i=0;i<=1+w;i+=w)
49     {
50         
51         for(j=0;j<=1+w;j+=w)
52         {     
53            double x=r*sin(PI*j)*cos(PI2*i);
54            double y=r*sin(PI*j)*sin(PI2*i);
55            double z=r*cos(PI*j);
56            glVertex3d(x,y,z);   
57         }
58         
59     }
60     glEnd();
61 
62     glutSwapBuffers();
63 }
64 
65 
66 int main(int argc,char* argv[])
67 {
68     glutInit(&argc,argv);
69     glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
70     glutInitWindowPosition(100,100);
71     glutInitWindowSize(width,height);
72     glutCreateWindow("球体");
73     Init();
74     glutDisplayFunc(displayFunc);
75     glutMainLoop();
76 }

 

posted on 2016-06-14 14:17  pb2016  阅读(759)  评论(0编辑  收藏  举报