196fd9ecba3b5e6a99987d95fb6b3a086d64bfe3
[sdk] / ecere / src / gfx / 3D / meshes / Sphere.ec
1 namespace gfx3D::meshes;
2
3 import "Display"
4
5 public class Sphere : Object
6 {
7 private:
8    int numLat, numLon;
9    numLat = 50;
10    numLon = 50;
11 public:
12    bool Create(DisplaySystem displaySystem)
13    {
14       bool result = false;
15       if(this)
16       {
17          InitializeMesh(displaySystem);
18
19          if(mesh)
20          {
21             if(mesh.Allocate({ vertices = true, normals = true }, (numLat-3) * numLon*2 + (numLon + 2) * 2, displaySystem))
22             {
23                Vector3Df *pVertices = mesh.vertices, *pNormals = mesh.normals;
24                int index;
25                int lat, lon;
26
27                PrimitiveGroup group;
28
29                index = 0;
30                for(lat = 1; lat < numLat-2; lat++)
31                {
32                   for(lon = 0; lon < numLon; lon++)
33                   {
34                      Angle theta, omega, cosOmega;
35                      
36                      theta = lon * 2 * Pi / (numLon - 1);
37
38                      omega = (lat + 1) * Pi / (numLat - 1) - Pi / 2;
39                      cosOmega = cos(omega);
40                      pVertices[index].x = (float) (sin(theta) * cosOmega);
41                      pVertices[index].y = (float)  sin(omega);
42                      pVertices[index].z = (float) (cos(theta) * cosOmega);
43                      pNormals[index] = pVertices[index];
44                      index ++;
45
46                      omega = lat * Pi / (numLat - 1) - Pi / 2;
47                      cosOmega = cos(omega);
48                      pVertices[index].x = (float) (sin(theta) * cosOmega);
49                      pVertices[index].y = (float)  sin(omega);
50                      pVertices[index].z = (float) (cos(theta) * cosOmega);
51                      pNormals[index] = pVertices[index];
52                      index ++;
53                   }
54
55                   if((group = mesh.AddPrimitiveGroup({triStrip, true}, 0)))
56                   {
57                      group.first = (lat-1) * (numLon) * 2;
58                      group.nVertices = (numLon) * 2;
59                   }
60                }
61
62                // Create Polar Caps
63                pVertices[index].x = 0;
64                pVertices[index].y =-1;
65                pVertices[index].z = 0;
66                pNormals[index] = pVertices[index];
67
68                index += numLon + 2;
69                pVertices[index].x = 0;
70                pVertices[index].y = 1;
71                pVertices[index].z = 0;
72                pNormals[index] = pVertices[index];
73
74                for(lon = 0; lon <= numLon; lon++)
75                {
76                   Angle theta = (numLon - lon) * 2 * Pi / (numLon - 1);
77                   Angle omega = 1 * Pi / (numLat - 1) - Pi / 2;
78                   Angle cosOmega = cos(omega);
79
80                   index = (numLat - 3) * ((numLon) * 2) + lon + 1;
81                   pVertices[index].x = (float) (sin(theta) * cos(omega));
82                   pVertices[index].y = (float)  sin(omega);
83                   pVertices[index].z = (float) (cos(theta) * cos(omega));
84                   pNormals[index] = pVertices[index];
85                   
86                   omega = (numLat - 2) * Pi / (numLat - 1) - Pi / 2;
87                   cosOmega = cos(omega);
88                   index += numLon+2;
89
90                   theta = lon * 2 * Pi / (numLon - 1);
91                   pVertices[index].x = (float) (sin(theta) * cos(omega));
92                   pVertices[index].y = (float)  sin(omega);
93                   pVertices[index].z = (float) (cos(theta) * cos(omega));
94                   pNormals[index] = pVertices[index];
95                }
96
97                if(group = mesh.AddPrimitiveGroup({ triFan, true }, 0))
98                {
99                   group.first = (numLat - 3) * (numLon) * 2;
100                   group.nVertices = numLon + 2;
101                }
102
103                if(group = mesh.AddPrimitiveGroup({ triFan, true }, 0))
104                {
105                   group.first = (numLat - 3) * (numLon) * 2 + (numLon + 2);
106                   group.nVertices = numLon + 2;
107                }
108             }
109             mesh.Unlock(0);
110             SetMinMaxRadius(true);
111          }
112       }
113    }
114    property int numLat { set { numLat = value; } }
115    property int numLon { set { numLon = value; } }
116 }