I don’t think it’s possible to get very accurate tex coords using the glut methods. These methods call glVertex (and glNormal) internally, so there is no way to define texture coordinates per vertex. The only option (as far as I know) is using tex coord generation, but that won’t make your texture wrap nicely around the torus. If you look at the code that generates the torus (below) you see that it uses two angles, theta and phi to calculate the vertices. Theta is used to get the circular shape of the torus and phi is used to get the rings of the torus. Both of these variables loop from 0 to 2pi (i.e. 360 degrees in radians). These two variables can be mapped to the s and t axes in texture coordinate space by simply dividing by 2pi. This should wrap the texture around the torus.
public void doughnut(double innerRadius, double outerRadius, int nsides, int rings) {
float ringDelta = (float) (2.0 * Math.PI / rings);
float sideDelta = (float) (2.0 * Math.PI / nsides);
float theta = 0.0f;
float cosTheta = 1.0f;
float sinTheta = 0.0f;
for (int i = rings - 1; i >= 0; i--) {
float nextTheta = theta + ringDelta;
float cosNextTheta = (float) Math.cos(nextTheta);
float sinNextTheta = (float) Math.sin(nextTheta);
glBegin(GL_QUAD_STRIP);
float phi = 0.0f;
for (int j = nsides; j >= 0; j--) {
phi += sideDelta;
float cosPhi = (float) Math.cos(phi);
float sinPhi = (float) Math.sin(phi);
float dist = (float) (outerRadius + innerRadius * cosPhi);
glNormal3f(cosNextTheta * cosPhi, -sinNextTheta * cosPhi, sinPhi);
glVertex3f(cosNextTheta * dist, -sinNextTheta * dist, (float) innerRadius * sinPhi);
glNormal3f(cosTheta * cosPhi, -sinTheta * cosPhi, sinPhi);
glVertex3f(cosTheta * dist, -sinTheta * dist, (float) innerRadius * sinPhi);
}
glEnd();
theta = nextTheta;
cosTheta = cosNextTheta;
sinTheta = sinNextTheta;
}
}
For the teapot, glut should already generate texture coordinates for you.
I don’t know if there’s a general answer for your question, since it basically depends on the shape you are rendering and the kind of texturing effect you want to achieve. My advice would be to
- Try using one of the glTexGen modes
- If that isn’t good enough calculate the texture coorindates yourself (like in the torus example)
Maybe you can do some clever stuff with evaluators, but I haven’t used those yet.
This entire explanation assumes you are generating geometry programmatically. If your using models, it’s probably much easier to define the texture coordinates using your modeling tool and read the texture coordinates from the model itself.