Void DrawTorus (M3DMatrix44f mTransform) {// The large circle exists only in the xy plane, // the small circle exists in the xyz space, and its center is the point on the circumference of the large circle. // The radius of the large circle of the small ring is formed by the first one week of rotation. // Because the z axis is perpendicular to the xy plane and the radius of the large circle is located at the xy plane, // The z axis is perpendicular to the radius of the large circle (perpendicular to the surface, vertical to the line), // Therefore, the radius direction of the z axis and the large circle is orthogonal. // The small circle is located at the plane formed in the direction of the z axis and the radius of the large circle. // The Position of the specific vertex after calculation is based on the above description. // GLfloat majorRadius = 0.35f; // GLfloat minorRadius = 0.15f; // GLint numMajor = 40; // GLint numMinor = 20; M3DVector3f objectVertex; // Vertex in object/eye space M3DVector3f transformedVertex; // New Transformed vertex // Number of radians corresponding to each vertex double majorStep = 2.0f * M3D_PI/numMajor; double minorStep = 2.0f * M3D_PI/numMinor; int I, j; // Iteration for (I = 0; I
// The radian double a0 = I * majorStep corresponding to the first vertex; // The radian double a1 = a0 + majorStep corresponding to the second vertex; // The unit length of the first Vertex on the x and y axes. GLfloat x0 = (GLfloat) cos (a0); GLfloat y0 = (GLfloat) sin (a0 ); // The unit length of the second Vertex on the x and y axes GLfloat x1 = (GLfloat) cos (a1); GLfloat y1 = (GLfloat) sin (a1); glBegin (GL_TRIANGLE_STRIP ); // Iteration for (j = 0; j // radian double B = j * minorStep corresponding to the point on the small circle; // The unit length of the vertex on the incircle in the radius direction GLfloat c = (GLfloat) cos (B); // The incircle Point, the length of the component in the xy plane GLfloat r = minorRadius * c + majorRadius; // The length of the GLfloat z = minorRadius * (GLfloat) sin (B) on the z axis on the small circle ); // process for confirming the coordinate of the point on a small circle: The point is divided into the z axis and the radius of the large circle. Since the radius of the large circle only exists in the xy plane, it is relatively easy to obtain the x and y coordinates. // First point objectVertex [0] = x0 * r; // The x coordinate objectVertex [1] = y0 * r corresponding to the point on the incircle; // The y coordinate objectVertex [2] = z For the vertex on the incircle; // The z coordinate m3dTransformVector3 (transformedVertex, objectVertex, mTransform) for the vertex on the incircle; glVertex3fv (transformedVertex ); // Second point objectVertex [0] = x1 * r; objectVertex [1] = y1 * r; objectVertex [2] = z; m3dTransformVector3 (transformedVertex, objectVertex, mTransform ); glVertex3fv (transformedVertex);} glEnd ();}}