3

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.

brozo
  • 585
  • 2
  • 13
  • 29

1 Answers1

4

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.)

Hannesh
  • 7,256
  • 7
  • 46
  • 80
Ishtar
  • 11,542
  • 1
  • 25
  • 31
  • You're right. I didn't see that connection there. This seems to work OK now, it's just that v and u are not normalized (not on the interval [0, 1]). I'm guessing that's why I'm getting a weird texture mapping (see screenshot: http://www.shrani.si/f/1Z/wY/1YtmOwkg/scr.png). Can you help me with this? – brozo Nov 01 '11 at 17:40
  • I applied the fix you suggested, but the result is still not what I was hoping for. It seems that with the fix applied, the texture is stretched over the outer side of the torus (see screenshot: http://www.shrani.si/f/17/OK/7a1oZek/scr.png). – brozo Nov 01 '11 at 20:27
  • 1
    You need to cast numc and numt to floats before dividing, or it will be done with integer maths. I've edited Ishtar's answer with the correction, try that. – Hannesh Nov 01 '11 at 23:30