Visualization 2: STL data display

Source: Internet
Author: User
The first task is to draw STL data OpenGL, which can be rotated, scaled, picked up (after picking, display the title number, node number), move the vertex position to change the shape of the object; the first stage: read and show how to read STL files generated by Solidworks model in STL:
ASCII format of STL Files

The ASCII format STL file provides the geometric information of the triangle surface line by line. Each line starts with one or two keywords. In the STL file, the information unit facet of the triangle surface is a triangle surface with vector direction. The STL model is composed of a group of such triangle surfaces. In an STL file, each facet is composed of seven rows of data, and the fifth row is the amount of data lost due to the triangle surface pointing to the external object, and the third row indicates the subsequent 3, 4, the five rows of data are the three vertices of the triangle, which are arranged in the clockwise direction along the normal vector (1st rows of data) pointing to the external body. The ASCII format STL file structure is as follows:
Solid filename STL folder file path and file name
Facet normal x y z x
Outer Loop
Vertex x y z coordinate of the first vertex of the Triangle Surface
Vertex x y z coordinate of the second vertex of the Triangle Surface
Vertex x y z coordinate of the third vertex of the Triangle Surface
Endloop
The first triangle surface of endfacet watermark is defined.
...........................
...............................
The VC code for reading STL Files in OpenGL is as follows: File * fp = NULL;
Fp = fopen (".. \ Active synchronization Belt Wheel. STL", "R ");
If (FP)
{
Cstring strline;
Cstring strhead;
Glfloat normal [3];
Glfloat vertex [3];

Glnewlist (3, gl_compile );
Glpushmatrix ();
// Gldisable (gl_lighting );
Glenable (gl_depth_test );
Glable (gl_lighting );
Glable (gl_line_smooth );
Glscalef (0.001, 0.001, 0.001 );

While (1)
{
Fscanf (FP, "% s \ n", strhead. getbuffer (20 ));
Strhead. trimleft ();

If (strhead = "solid ")
{
Fscanf (FP, "% s \ n", strline. getbuffer (20 ));
Continue;
}
Else if (strhead = "facet ")
{
Fscanf (FP, "% s \ n", strline. getbuffer (20 ));
Fscanf (FP, "% F \ n", normal, normal + 1, normal + 2 );
Glnormal3fv (normal );
}
If (strhead = "outer ")
{
Fscanf (FP, "% s \ n", strline. getbuffer (20 ));

Glbegin (gl_polygon );

For (INT I = 0; I <3; I ++)
{
Fscanf (FP, "% s \ n", strline. getbuffer (20 ));
Strline. trimleft ();
If (strline = "vertex ")
{
Fscanf (FP, "% F \ n", vertex, vertex + 1, vertex + 2 );
Glvertex3d (vertex [0], vertex [1], vertex [2]);
}
}

Glend ();

Continue;
}
Else if (strhead = "endloop" | strhead = "endfacet ")
{
Continue;
}
Else if (strhead = "endsolid ")
{
Break;
}
}

// Glable (gl_depth_test );
// Glenable (gl_lighting );
Gldisable (gl_line_smooth );
Glpopmatrix ();
Glendlist ();

Fclose (FP );
} The above is the code segment that reads STL and displays in;

Visualization 2: STL data display

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.