OpenGL Super Bible Chapter 4 Analysis of the Code for drawing the Transform program

Source: Internet
Author: User

 

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 ();}}
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.