1 namespace gfx3D::meshes;
5 public class Sphere : Object
12 bool Create(DisplaySystem displaySystem)
17 InitializeMesh(displaySystem);
21 if(mesh.Allocate({ vertices = true, normals = true }, (numLat-3) * numLon*2 + (numLon + 2) * 2, displaySystem))
23 Vector3Df *pVertices = mesh.vertices, *pNormals = mesh.normals;
30 for(lat = 1; lat < numLat-2; lat++)
32 for(lon = 0; lon < numLon; lon++)
34 Angle theta, omega, cosOmega;
36 theta = lon * 2 * Pi / (numLon - 1);
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];
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];
55 if((group = mesh.AddPrimitiveGroup({triStrip, true}, 0)))
57 group.first = (lat-1) * (numLon) * 2;
58 group.nVertices = (numLon) * 2;
63 pVertices[index].x = 0;
64 pVertices[index].y =-1;
65 pVertices[index].z = 0;
66 pNormals[index] = pVertices[index];
69 pVertices[index].x = 0;
70 pVertices[index].y = 1;
71 pVertices[index].z = 0;
72 pNormals[index] = pVertices[index];
74 for(lon = 0; lon <= numLon; lon++)
76 Angle theta = (numLon - lon) * 2 * Pi / (numLon - 1);
77 Angle omega = 1 * Pi / (numLat - 1) - Pi / 2;
78 Angle cosOmega = cos(omega);
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];
86 omega = (numLat - 2) * Pi / (numLat - 1) - Pi / 2;
87 cosOmega = cos(omega);
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];
97 if(group = mesh.AddPrimitiveGroup({ triFan, true }, 0))
99 group.first = (numLat - 3) * (numLon) * 2;
100 group.nVertices = numLon + 2;
103 if(group = mesh.AddPrimitiveGroup({ triFan, true }, 0))
105 group.first = (numLat - 3) * (numLon) * 2 + (numLon + 2);
106 group.nVertices = numLon + 2;
110 SetMinMaxRadius(true);
114 property int numLat { set { numLat = value; } }
115 property int numLon { set { numLon = value; } }