Results 1 to 3 of 3

Thread: Texture coordinates for a torus

  1. #1

    Texture coordinates for a torus

    System: Windows XP SP1, Pentium D 805 2,66 GHz 2 GB RAM, GeForce 7600 GS
    Compiler/IDE: Delphi 2007
    Libraries/API: JEDI-SDL
    ---

    Hello there!

    I've found this code somewhere to render a torus:
    [pascal]
    procedure TTorus.Render;
    var
    I, J: Integer;
    theta, phi, theta1: Single;
    cosTheta, sinTheta: Single;
    cosTheta1, sinTheta1: Single;
    ringDelta, sideDelta: Single;
    cosPhi, sinPhi, dist: Single;
    begin
    glTranslatef(FPos.X, FPos.Y, FPos.Z);
    glRotatef(FAngle, FRot.X, FRot.Y, FRot.Z);
    glScalef(FScale.X, FScale.Y, FScale.Z);

    sideDelta := 2.0 * PI / FSides;
    ringDelta := 2.0 * PI / FRings;
    theta := 0.0;
    cosTheta := 1.0;
    sinTheta := 0.0;

    for I := FRings -1 downto 0 do
    begin
    theta1 := theta + ringDelta;
    cosTheta1 := Cos(theta1);
    sinTheta1 := Sin(theta1);

    glBegin(GL_QUAD_STRIP);
    phi := 0.0;
    for J := FSides downto 0 do
    begin
    phi := phi + sideDelta;
    cosPhi := Cos(phi);
    sinPhi := Sin(phi);
    dist := FRadius + (FTubeRadius * cosPhi);

    glNormal3f(cosTheta1 * cosPhi, -sinTheta1 * cosPhi, sinPhi);
    //glTexCoord2f(cosTheta1 * cosPhi, -sinTheta1 * cosPhi);
    glVertex3f(cosTheta1 * dist, -sinTheta1 * dist, FTubeRadius * sinPhi);

    glNormal3f(cosTheta * cosPhi, -sinTheta * cosPhi, sinPhi);
    //glTexCoord2f(cosTheta * cosPhi, -sinTheta * cosPhi);
    glVertex3f(cosTheta * dist, -sinTheta * dist, FTubeRadius * sinPhi);
    end;
    glEnd();

    theta := theta1;
    cosTheta := cosTheta1;
    sinTheta := sinTheta1;
    end;
    end;
    [/pascal]

    I've got a problem with setting up the texture coordinates. As you can see, I've tried to do it before, but I couldn't make it. Can you help me?

    Thanks in advance!

  2. #2

    Texture coordinates for a torus

    You can use this code, which is originally from Asphyre 4.1 (it uses vertex & index buffers):

    Code:
     Theta:= 0.0;
     ThetaInc:= 2.0 * Pi / Rings;
     PhiInc:= 2.0 * Pi / Sides;
    
     for j:= 0 to Rings do
      begin
       CosTheta:= Cos(Theta);
       SinTheta:= Sin(Theta);
    
       Phi:= 0.0;
       for i:= 0 to Sides do
        begin
         CosPhi:= Cos(Phi);
         SinPhi:= Sin(Phi);
         Delta := Radius + TubeRadius * CosPhi;
    
         IncludeVertex(
          Vector3(CosTheta * Delta, TubeRadius * SinPhi, -SinTheta * Delta),
          Vector3(CosTheta * CosPhi, SinPhi, -SinTheta * CosPhi),
          Point2(Theta * TexTilesX / (2.0 * Pi), Phi * TexTilesY / (2.0 * Pi)));
    
         ni:= i + 1;
         nj:= j + 1;
    
         if &#40;i < Sides&#41;and&#40;j < Rings&#41; then
          begin
           IncludeIndex&#40;i + j * SideBlock&#41;;
           IncludeIndex&#40;i + nj * SideBlock&#41;;
           IncludeIndex&#40;ni + nj * SideBlock&#41;;
    
           IncludeIndex&#40;i + j * SideBlock&#41;;
           IncludeIndex&#40;ni + nj * SideBlock&#41;;
           IncludeIndex&#40;ni + j * SideBlock&#41;;
          end;
    
         Phi&#58;= Phi + PhiInc;
        end;
    
       Theta&#58;= Theta + ThetaInc;
      end;
    Porting the code to OpenGL should be a matter of changing "IncludeVertex" to rendering calls.

  3. #3

    Texture coordinates for a torus

    Oh, that's cool, but I've already solved the problem myself. Anyway, thanks for you tip. Here is the code I use now (might be helpful for others):
    [pascal]
    procedure TTorus.Render;
    var
    I, J: Integer;
    theta, phi, theta1: Single;
    cosTheta, sinTheta: Single;
    cosTheta1, sinTheta1: Single;
    ringDelta, sideDelta: Single;
    cosPhi, sinPhi, dist: Single;
    begin
    inherited Render();

    sideDelta := 2.0 * PI / FSides;
    ringDelta := 2.0 * PI / FRings;
    theta := 0.0;
    cosTheta := 1.0;
    sinTheta := 0.0;

    for I := FRings -1 downto 0 do
    begin
    theta1 := theta + ringDelta;
    cosTheta1 := Cos(theta1);
    sinTheta1 := Sin(theta1);

    glBegin(GL_QUAD_STRIP);
    phi := 0.0;
    for J := FSides downto 0 do
    begin
    phi := phi + sideDelta;
    cosPhi := Cos(phi);
    sinPhi := Sin(phi);
    dist := FRadius + (FTubeRadius * cosPhi);

    glNormal3f(cosTheta1 * cosPhi, -sinTheta1 * cosPhi, sinPhi);
    glTexCoord2f(I / FRings, J / FSides);
    glVertex3f(cosTheta1 * dist, -sinTheta1 * dist, FTubeRadius * sinPhi);

    glNormal3f(cosTheta * cosPhi, -sinTheta * cosPhi, sinPhi);
    glTexCoord2f((I + 1) / FRings, J / FSides);
    glVertex3f(cosTheta * dist, -sinTheta * dist, FTubeRadius * sinPhi);
    end;
    glEnd();

    theta := theta1;
    cosTheta := cosTheta1;
    sinTheta := sinTheta1;
    end;
    end;
    [/pascal]

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •