43 LOGE(
"radius value has to be greater than zero.");
48 if (numberOfSamples <= 0)
50 LOGE(
"numberOfSamples value has to be greater than zero.");
54 if (coordinates == NULL)
56 LOGE(
"Cannot use null pointer while calculating coordinates.");
62 const float epsilon = 0.001f;
64 int indexOfSphereArray = 0;
66 float maxTheta = (2.0f *
M_PI);
68 float thetaStep = maxTheta /
float(numberOfSamples);
70 float radiusStep = (2 * radius) /
float(numberOfSamples-1);
76 const int numberOfSphereCoordinates = numberOfSamples * numberOfSamples * 3;
79 *coordinates = (
float*) malloc (numberOfSphereCoordinates *
sizeof(
float));
81 if (*coordinates == NULL)
83 LOGE(
"Could not allocate memory for result array.");
89 for (
float r = -radius;
91 r = -radius + radiusIndex * radiusStep)
102 for (
float theta = 0.0
f;
104 theta = thetaIndex * thetaStep)
107 float x = sqrt((radius * radius) - (
r *
r)) * cosf(theta);
108 float y = sqrt((radius * radius) - (
r *
r)) * sinf(theta);
111 (*coordinates)[indexOfSphereArray] =
x;
112 indexOfSphereArray++;
114 (*coordinates)[indexOfSphereArray] =
y;
115 indexOfSphereArray++;
117 (*coordinates)[indexOfSphereArray] = z;
118 indexOfSphereArray++;
126 if (numberOfCoordinates != NULL)
128 *numberOfCoordinates = numberOfSphereCoordinates;
133 const int numberOfSamples,
134 int* numberOfCoordinates,
141 LOGE(
"radius value has to be greater than zero.");
146 if (numberOfSamples <= 0)
148 LOGE(
"numberOfSamples value has to be greater than zero.");
152 if (coordinates == NULL)
154 LOGE(
"Cannot use null pointer while calculating coordinates.");
159 float* pointCoordinates = NULL;
161 int sphereTriangleCoordinateIndex = 0;
163 int iterationIndex = 1;
165 int numberOfCoordinatesCreatedInOneLoop = 18;
179 if (pointCoordinates == NULL)
181 LOGE(
"Could not get coordinates of points which make up a sphere.");
185 *coordinates = (
float*) malloc (numberOfSphereTriangleCoordinates *
sizeof(
float));
187 if (*coordinates == NULL)
189 LOGE(
"Could not allocate memory for result array.");
194 for (
int pointIndex = 0;
195 pointIndex < (numberOfSphereTriangleCoordinates / numberOfCoordinatesCreatedInOneLoop) * 3;
204 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex];
205 sphereTriangleCoordinateIndex++;
207 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 1];
208 sphereTriangleCoordinateIndex++;
210 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 2];
211 sphereTriangleCoordinateIndex++;
214 if ((iterationIndex % numberOfSamples) == 0 )
224 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex - 3 * (
numberOfSamples) + 3];
225 sphereTriangleCoordinateIndex++;
227 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex - 3 * (
numberOfSamples) + 3 + 1];
228 sphereTriangleCoordinateIndex++;
230 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex - 3 * (
numberOfSamples) + 3 + 2];
231 sphereTriangleCoordinateIndex++;
240 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 3];
241 sphereTriangleCoordinateIndex++;
243 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 3 + 1];
244 sphereTriangleCoordinateIndex++;
246 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 3 + 2];
247 sphereTriangleCoordinateIndex++;
258 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 3];
259 sphereTriangleCoordinateIndex++;
261 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 3 + 1];
262 sphereTriangleCoordinateIndex++;
264 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 3 + 2];
265 sphereTriangleCoordinateIndex++;
275 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + numberOfSamples * 3 + 3];
276 sphereTriangleCoordinateIndex++;
278 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + numberOfSamples * 3 + 3 + 1];
279 sphereTriangleCoordinateIndex++;
281 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + numberOfSamples * 3 + 3 + 2];
282 sphereTriangleCoordinateIndex++;
292 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex];
293 sphereTriangleCoordinateIndex++;
295 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 1];
296 sphereTriangleCoordinateIndex++;
298 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 2];
299 sphereTriangleCoordinateIndex++;
302 if ((iterationIndex % numberOfSamples) == 0 )
310 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 3];
311 sphereTriangleCoordinateIndex++;
313 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 3 + 1];
314 sphereTriangleCoordinateIndex++;
316 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 3 + 2];
317 sphereTriangleCoordinateIndex++;
329 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 3 * numberOfSamples + 3];
330 sphereTriangleCoordinateIndex++;
332 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 3 * numberOfSamples + 3 + 1];
333 sphereTriangleCoordinateIndex++;
335 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 3 * numberOfSamples + 3 + 2];
336 sphereTriangleCoordinateIndex++;
344 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 3 *
numberOfSamples];
345 sphereTriangleCoordinateIndex++;
347 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 3 *numberOfSamples + 1];
348 sphereTriangleCoordinateIndex++;
350 (*coordinates)[sphereTriangleCoordinateIndex] = pointCoordinates[pointIndex + 3 * numberOfSamples + 2];
351 sphereTriangleCoordinateIndex++;
358 if (numberOfCoordinates != NULL)
362 if (numberOfPoints != NULL)
367 free(pointCoordinates);
368 pointCoordinates = NULL;
int numberOfSphereTrianglePoints
static void getTriangleRepresentation(const float radius, const int numberOfSamples, int *numberOfCoordinates, float **coordinates)
Create triangular representation of a sphere.
static void getPointRepresentation(const float radius, const int numberOfSamples, int *numberOfCoordinates, float **coordinates)
Compute coordinates of points which make up a sphere.
int numberOfSphereTriangleCoordinates
#define M_PI
The value of pi.
GLint GLint GLint GLint GLint x