How to draw a textured torus in OpenGL without using GLUT?

brozo picture brozo · Nov 1, 2011 · Viewed 8k times · Source

I need to render a torus in OpenGL, without using GLUT. I'm using C# and Tao Framework bindings. I have the following code, which I got from here.

 private void DrawTorus() {
        int numc = 100, numt = 100;

        double TWOPI = 2 * Math.PI;
        for (int i = 0; i < numc; i++) {
            Gl.glBegin(Gl.GL_QUAD_STRIP);
            for (int j = 0; j <= numt; j++) {
                for (int k = 1; k >= 0; k--) {

                    double s = (i + k) % numc + 0.5;
                    double t = j % numt;

                    double x = (1 + 0.1 * Math.Cos(s * TWOPI / numc)) * Math.Cos(t * TWOPI / numt);
                    double y = (1 + 0.1 * Math.Cos(s * TWOPI / numc)) * Math.Sin(t * TWOPI / numt);
                    double z = 0.1 * Math.Sin(s * TWOPI / numc);

                    Gl.glVertex3d(2 * x, 2 * y, 2 * z);
                }
            }
            Gl.glEnd();
        }
    }

This code draws a torus, but now I need to put a texture on it. I'm trying to use these formulas for the texture coordinates, but I can't figure out what to use for R and r (inner and outer radius respectively).

v = arccos (Y/R)/2pi
u = [arccos ((X/(R + r*cos(2pi * v))] * 2pi

Having some trouble understanding that code, I would appreciate an explanation of it or perhaps an alternative, more intuitive code with comments. Any help will be much appreciated.

Answer

Ishtar picture Ishtar · Nov 1, 2011

If we compare the formula

X = (R + r cos (2 pv)) cos (2 pu)
Y = r sin (2 pv)
Z = (R + r cos (2 pv)) sin (2 pu)

with the code

double x = (1 + 0.1 * Math.Cos(s * TWOPI / numc)) * Math.Cos(t * TWOPI / numt);
double y = (1 + 0.1 * Math.Cos(s * TWOPI / numc)) * Math.Sin(t * TWOPI / numt);
double z = 0.1 * Math.Sin(s * TWOPI / numc);

Clearly, X = x, Y = z, Z = y, R = 1, r = 0.1, 2 pv = s * TWOPI / numc and 2 pu = t * TWOPI / numt. Then

v = arccos (Y/R)/2p
u = [arccos ((X/(R + r*cos(2 pv))]2p

gives

v = arcos (z/1)/TWOPI
u = [arcos ((x/(1 + 0.1*cos(s * TWOPI / numc)]/TWOPI

EDIT: To be honest, I didn't try hard to understand the formula... Reading your code, I think this should do the trick:

u = (i + k) / (float)numc;
v = t / (float)numt;

(You may have to swap u and v.)