I'm trying to learn some lwjgl and opengl, and I was trying to make a 3d sphere etc, I found a code to do that, but I don't 100% understand the code and the math behind it and would like to get some deeper explanation about it.
This is my draw sphere method:
public void drawSphere(float radius) {
final float PI = 3.141592f;
float x, y, z, alpha, beta;
int gradation = 100;
for (alpha = 0.0f; alpha < Math.PI; alpha += PI / gradation) {
glBegin(GL_TRIANGLE_STRIP);
for (beta = 0.0f; beta < 2.01f * Math.PI; beta += PI / gradation) {
x = (float) (radius * Math.cos(beta) * Math.sin(alpha));
y = (float) (radius * Math.sin(beta) * Math.sin(alpha));
z = (float) (radius * Math.cos(alpha));
glTexCoord2f(beta / (2.0f * PI), alpha / PI);
glVertex3f(x, y, z);
x = (float) (radius * Math.cos(beta) * Math.sin(alpha + PI / gradation));
y = (float) (radius * Math.sin(beta) * Math.sin(alpha + PI / gradation));
z = (float) (radius * Math.cos(alpha + PI / gradation));
glTexCoord2f(beta / (2.0f * PI), alpha / PI + 1.0f / gradation);
glVertex3f(x, y, z);
}
glEnd();
}
}
I understand the general idea of this, and I understand how the x, y, z are calculated, but what I don't understand is the 2 nested loops aka:
for (alpha = 0.0f; alpha < Math.PI; alpha += PI / gradation)
and
for (beta = 0.0f; beta < 2.01f * Math.PI; beta += PI / gradation)
One more thing, is the glTexCoord2f
that I don't quite understand the math behind it, so an explanation about this would be really nice as well for:
glTexCoord2f(beta / (2.0f * PI), alpha / PI);
and
glTexCoord2f(beta / (2.0f * PI), alpha / PI + 1.0f / gradation);