Blog
Blog
... and a torus too
Friday, 17 April 2009
Following on from the previous post, here’s a similar function to create the points for a torus:
GLfloat * createTorus (GLfloat aRadius, GLfloat bRadius, GLfloat step, int *pointCount)
{
NSLog(@"Torus");
GLfloat uStep = DEGREES_TO_RADIANS (step);
GLfloat vStep = uStep;
int totalPoints = ((2 * M_PI) / uStep) * ((2 * M_PI) / vStep) * 12.0 *1.5f;
GLfloat * torusPoints = calloc(totalPoints, sizeof(GLfloat));
int point = 0;
for (GLfloat u = 0.0f; u <= (2 * M_PI); u += uStep) {
for (GLfloat v = -M_PI; v <= M_PI; v += vStep) {
point++;
torusPoints[(point - 1) * 3] = (aRadius + bRadius * cosf(v)) * cosf(u); // x
torusPoints[((point - 1) * 3) + 1] = (aRadius + bRadius * cosf(v)) * sinf(u); // y
torusPoints[((point - 1) * 3) + 2] = bRadius * sinf(v); // z
point++;
torusPoints[(point - 1) * 3] = (aRadius + bRadius * cosf(v + vStep)) * cosf(u);
torusPoints[((point - 1) * 3) + 1] = (aRadius + bRadius * cosf(v + vStep)) * sinf(u);
torusPoints[((point - 1) * 3) + 2] = bRadius * sinf(v + vStep);
point++;
torusPoints[(point - 1) * 3] = (aRadius + bRadius * cosf(v)) * cosf(u + uStep);
torusPoints[((point - 1) * 3) + 1] = (aRadius + bRadius * cosf(v)) * sinf(u + uStep);
torusPoints[((point - 1) * 3) + 2] = bRadius * sinf(v);
point++;
torusPoints[(point - 1) * 3] = (aRadius + bRadius * cosf(v + vStep)) * cosf(u + uStep);
torusPoints[((point - 1) * 3) + 1] = (aRadius + bRadius * cosf(v + vStep)) *
sinf(u + uStep);
torusPoints[((point - 1) * 3) + 2] = bRadius * sinf(v + vStep);
}
}
*pointCount = point;
return torusPoints;
}
There’s something not quite right with the calculation of totalPoints though (hence the 1.5 multiplier), so if you figure it out, please let me know :-)