1 namespace gfx3D::meshes;
4 import "Object" // TOFIX: If this is not here, Cube/Object gets registered as a 'class' rather than class 'struct'
6 public class Sphere : Object
15 bool Create(DisplaySystem displaySystem)
20 InitializeMesh(displaySystem);
24 if(mesh.Allocate({ vertices = true, normals = true, texCoords1 = true, tangents = true }, (numLat+1) * w, displaySystem))
26 Vector3Df *pVertices = mesh.vertices, *pNormals = mesh.normals, *pTangents = mesh.tangents;
27 Pointf *pTexCoords = mesh.texCoords;
30 PrimitiveGroup group = mesh.AddPrimitiveGroup({triangles, false}, numLat * w * 6);
33 for(lat = 0; lat <= numLat; lat++)
35 Angle omega = 0.0001 + lat * (Pi - 0.0002) / (numLat) - Pi / 2;
36 //Angle omega = lat * Pi / (numLat) - Pi / 2;
37 float rounded = flattenedBody * numLat;
38 double r = flattenedBody ? (lat < rounded ? (rounded-lat) / rounded : lat > numLat - rounded ? (lat - (numLat - rounded)) / rounded : 0) : 1;
39 double cosOmega = cos(omega) * r + 1 * (1-r);
40 double sinOmega = sin(omega);
41 for(lon = 0; lon < w; lon++)
43 Angle theta = lon == w-1 ? 0 : lon * 2 * Pi / (w-1);
44 double l = lon == w-1? lon - 0.0001 : lon;
47 (float) (sin(theta) * cosOmega);
49 (float) (cos(theta) * cosOmega);
52 pNormals[index] = pVertices[index];
56 (float) (sin(theta) * cos(omega - Pi/2));
57 (float) sin(omega - Pi/2);
58 (float) (cos(theta) * cos(omega - Pi/2));
60 pTangents[index*2+1] =
62 (float) (sin(theta - Pi/2) * cosOmega);
64 (float) (cos(theta - Pi/2) * cosOmega);
67 //pTangents[index*2 + 1].CrossProduct(pNormals[index], pTangents[index*2]);
68 pTexCoords[index] = { (float)l / (w-1), (float)lat / (numLat) };
73 for(index = 0; index < group.nIndices; index++)
74 group.indices[index] = 0;
79 for(lat = 1; lat <= numLat; lat++)
80 for(lon = 0; lon < w; lon++)
82 int n = (lon + 1) % w;
83 group.indices[index++] = (uint16)((lat-1) * w + lon);
84 group.indices[index++] = (uint16)((lat-1) * w + n);
85 group.indices[index++] = (uint16)((lat) * w + n);
87 group.indices[index++] = (uint16)((lat) * w + n);
88 group.indices[index++] = (uint16)((lat) * w + lon);
89 group.indices[index++] = (uint16)((lat-1) * w + lon);
92 mesh.UnlockPrimitiveGroup(group);
95 //mesh.ComputeNormals();
99 SetMinMaxRadius(true);
105 property int numLat { set { numLat = value; } }
106 property int numLon { set { numLon = value; } }
107 property float flattenedBody { set { flattenedBody = value; } }