Trouble with drawing torus

Hi all, I’m having a problem again with a sample program from OpenGL super bible book. After converted the program to C#, I got this output

And this is the original output from the sample in C++

I used FLAT shading in my C# program so it is showing those little black triangles. Could any one please tell me how get rid of those black triangles?

I think that there is “1 line to 1 line” of code link between my C# function to draw the torus and the C++ function. But how my func got wrong ? or somewhere else my code got wrong ? :frowning:

This is my func in C#:

public static void DrawTorus(float majorRadius, float minorRadius, int numMajor, int numMinor)
      Vector3D3f vNormal = new Vector3D3f();
      double majorStep = 2.0f * Math.PI / numMajor;
      double minorStep = 2.0f * Math.PI / numMinor;
      int i, j;

      for (i = 0; i < numMajor; i++)
        double a0 = i * majorStep;
        double a1 = a0 + majorStep;
        float x0 = (float)Math.Cos(a0);
        float y0 = (float)Math.Sin(a0);
        float x1 = (float)Math.Cos(a1);
        float y1 = (float)Math.Sin(a1);

        for (j = 0; j <= numMinor; j++)
          double b = j * minorStep;
          float c = (float)Math.Cos(b);
          float r = minorRadius * c + majorRadius;
          float z = minorRadius * (float)Math.Sin(b);

          //First point
          GL.TexCoord2f((float)i / (float)(numMajor), (float)(j) / (float)(numMinor));
          vNormal[0] = x0 * c;
          vNormal[1] = y0 * c;
          vNormal[2] = z / minorRadius;
          GL.Normal3f(vNormal.X, vNormal.Y, vNormal.Z);
          GL.Vertex3f(x0 * r, y0 * r, z);

          GL.TexCoord2f((float)(i + 1) / (float)(numMajor), (float)(j) / (float)(numMinor));
          vNormal[0] = x1 * c;
          vNormal[1] = y1 * c;
          vNormal[2] = z / minorRadius;
          GL.Normal3f(vNormal.Z, vNormal.Y, vNormal.Z);
          GL.Vertex3f(x1 * r, y1 * r, z);


If you’re not using texturing it looks like the winding order of half of the triangles is wrong, and you’re seeing backfaces being rendered in black. But I’m not sure how that could happen with triangle strips.
You might want to change the backface color to blue or something so you can see the difference.
And you should also check if the normals are calculated correctly.

If I remove the normal vector for the second point in each loop then I got a multifaceted torus, so I think the normal for first point is corrected ?

But how the original program could run very well ? I thought these calculating lines of code will be identical b/w C++ and C# ? I did not make any typo.

I tried to calculate normal vector for the second point but failed many times. Wish some one could help, I’m very new to OpenGL so calculating normal is quite headache to me.

Oh, I made a typo in my C# function.It’s working now. I’m very sorry :slight_smile: