OpenGL is a set of open 3D graphics software interfaces recently released by SGI. It is suitable for a wide range of computer environments. From personal computers to workstations, OpenGL can achieve high-performance 3D graphics. OpenGL not only provides operations and control over simple elements, but also provides many functions for modeling complex objects. However, we usually like to use tools such as AutoCAD, 3DS, and 3dmax to create models, and we already have many such models. How can we share resources and avoid repeated efforts? With the Standard CAD graphics data exchange format-DXF format, we can easily achieve resource sharing without repeated modeling.
The structure of the DXF file is clear as follows:
1. Header)
The general information about the graph can be found in this section of the DXF file. Each parameter has a variable name and
Related value.
2. Table segment
This section contains the definition of the specified item, which includes:
A,
Linear table (ltype)
B,
Layer table (lyer)
C,
Font table (style)
D,
View)
E,
User coordinate system table (UCS)
F,
Windows configuration table (vport)
G,
Dimstyle)
H,
Apply for symbol table (appid)
3. Block (blocks)
This section contains block-defined entities, which describe the entities of each block in the graphic type.
4. entity Section) Copyright dedecms
This section contains entities, including calls to any block.
5. End of file (end of file)
The following is an example of the basic structure of DXF:
0 0 followed by section
Section indicates that this is the beginning of a segment.
2 2 followed by the segment name
The header indicates the header segment (the title segment)
9
$ Acadver files are generated by AutoCAD.
1
Ac1008
After 9, connect $ ucsorg
$ Coordinates of the ucsorg user coordinate system origin in the world coordinate system
10 10 corresponds to X
The value of 0.0 x
20 20 corresponds to Y
The value of 0.0 y
30 30 corresponding to Z
The value of 0.0 Z
9
$ Ucsxdir this is a piece of unrelated content, skipped
10
1.0
.......
Connect $ extmin after 9
$ Extmin: Minimum value of a 3D object model in the world coordinate system
10 10 corresponds to X
-The value of 163.925293 x
20 20 corresponds to Y
-18.5415860.0 y value
30 30 corresponding to Z
The value of 78.350945 Z
Connect $ extman after 9
$ Extmax indicates the maximum value of a 3D object model in the world coordinate system.
10 10 corresponds to X
The value of 202.492279 x
20 20 corresponds to Y
The value of 112.634300 y
30 30 corresponding to Z
The value of 169.945602 Z
0 0 followed by endsec
Endsec indicates that this section is over
0 0 followed by section
Section indicates that this is the beginning of a segment.
2 2 followed by the segment name
Tables indicates that this segment is a tables segment (Table segment)
This section is not relevant to us. Skip this section and do not describe it.
0 0 followed by endsec
Endsec indicates that this section is over
0 0 followed by section
Section indicates that this is the beginning of a segment.
2 2 followed by the segment name
Entities indicates that this section is an entities section (entity Section). This is my
0. It must be described in detail. This section contains
The coordinates of the polyline points and the order of the points that constitute the surface. 0 followed by polyline
8 indicates that the following data is for a new entity;
The string after object01 8 is the object name
66
1
70 from 66 1 to 70 64
64 this entity is composed of many small planes
71
38 71 38 indicates that the entity has 38 points.
72
72 72 indicates that the object is composed of 72 triangles.
0 0 Vertex
Vertex indicates that the object data follows closely
8
Object01
10 corresponds to X coordinate
-The value of 163.925293 x
20 corresponds to Y coordinate
-17.772665 y value
30 corresponds to Z coordinate
The value of 128.929947 Z
70 192
192 indicates that the above data information is the coordinate of the point
0: each vertex ranges from 0 vertex to 70 192
A short section of vertex is the coordinate of a vertex.
.........
70
192
0
Vertex
Content from dedecms
8
Object01
10
0
20
0
30
0 when 70 and 128, it indicates that the coordinate data of each point of the object has been recorded
After 70, what follows is the way to record these points into different
128 triangles.
71 71, 72, 73 followed by the value indicates that a triangle is the second
2. It consists of one or four vertices. The order of vertices is based on the order recorded in the DXF file.
72. When a value is negative, it indicates that the line from this point to the next point is not drawn,
1 If you want to draw a line chart of a three-dimensional object, you must use this feature; otherwise, the line
73. disorder may occur.
-4
0
Vertex
............
After 0, seqend indicates that all data of the object has been recorded.
Seqend
8
Object01
0
Polyline 0 followed by polyline indicates that the following is a new entity
............
0
Endsec 0 followed by endsec indicates this is the end of the paragraph
0
EOF 0 followed by EOF indicates that the DXF file has ended
In the DXF file, we are most concerned with how to obtain the coordinates of each point in the model, and use these points to connect multiple
Form Three, form a surface, and then draw the entire model. In the structure of the DXF file, we can see that the DXF File
First, the coordinates of each point on the object are described, and then the number of faces on the object, which points each plane consists. This way
Dedecms.com
We need at least two arrays to store the information of an object, one for storing the coordinate of a vertex, and the other for storing
We can put these two arrays into a structure in the order of vertices. If there is more than one entity in the model, we
This structure is used to define an array. In this article, we use visual c ++ 6.0 to write a read DX
Small F FileProgram.
In actual application, the number of entities in the model and the number of points and faces in the entity are not fixed.
With memory, we select the vertex and sequence object created by the cobarray class of the aggregation class in the MFC class library to store
And manage the coordinate and order of objects.
The cobarray class is an aggregation class used to store arrays. It can be used to store arrays (or structures) according to the number
Automatic High Speed of its own size, and the member functions of this class allow us to operate on its objects
It is more convenient and convenient, and the program compiled with it is easy to understand.
A part of the model information of a 3D object model can be read in the title segment. by reading the variable named $ UC
Three Variables of SORG can be used to obtain the three
Dimension coordinates. By reading $ extmax and $ extmin, you can know the range of a 3D object in the world coordinate system.
Some of its information can be determined by computation only after all the DXF files are read. For all 3D object models
The coordinate and order can be read from the object segment according to the basic structure of the DXF file described above. Now let's start writing
Copyright dedecms
this program.
first, create a head file head. h to define the following structures: vertex, sequence and cvertex, csequence
.
typedef struct {
float x, y, z;
}vertex; structure vertex is used to store the coordinates of a vertex
typedef struct {
int A, B, C;
} sequence; the structure sequence is used to store the composition of the object surface
typedef struct {
char obname [20]; defines the structure myvertex to store the object name, coordinate of the vertex, and composition of the surface,
cobarray vertex. The coordinate and Plane Composition of a vertex are defined by the aggregation class cobarray.
cobarray sequence, we can add the vertex structure and sequence structure to
} myvertex; these two objects are saved
class cvertex: public cobject
{because the cobarray class object can only be added by cob Object, so
protected: we also need to create a cvertex class derived from the cobject class. In the cvertex class
cvertex ();, there is a vertex structure variable: m_vertex. The information is actually stored in the
declare_dyncreate (cvertex) variable.
virtual ~ Cvertex ();
// attributes
Public: we also need to create a cvertex class derived from the cobject class. In the cvertex class
cvertex (vertex & Ver); There is a vertex structure variable: m_vertex, the information is actually stored in
In this variable, the function cvertex (vertex & Ver) converts the vertex Structure Variable
Vertex m_vertex; saved to the cobarray object.
};
Class csequence: Public cobject
{This is also a class derived from the cobject class. It serves the same purpose as the cvertex class,
Protected: However, the csequence class is used to store the composition (vertex order) of objects.
Csequence ();
Declare_dyncreate (csequence)
Virtual ~ Csequence ();
Public:
Csequence (sequence & sequ );
Sequence m_sequence;
};
After declaring the structure and class, we also need to create a. cpp file to define several functions.
Implement_dyncreate (cvertex, cobject)
Cvertex: cvertex ()
{
}
Cvertex ::~ The cvertex () constructor and the destroy function are empty.
{
}
Cvertex: cvertex (vertex & Ver)
{The function is used to store data in the vertex variable m_vertex.
M_vertex = ver; it is the most important part of this class.
}
Implement_dyncreate (csequence, cobject)
Csequence: csequence ()
{
} The definition of the csequence class is similar to that of the cvertex class, but only the Parameter
The m_sequence type is different from that of the my_vertex parameter in the cvertex class.
Dedecms.com
Csequence ::~ Csequence ()
{
}
Csequence: csequence (sequence & sequ)
{
M_sequence = sequ;
}
Then, define a pointer * mydata with the structure myvertex (as defined previously)
Assign proper memory to the pointer to make it a structured array.
Define a function to determine the number of entities in the model. The return value of the function is the number of entities.
Int cjupiterview: getobjectnumber ()
{
Char str1 [10], str2 [10];
Char name [] = "thefirst ";
Int num;
Num = 0;
File * FP;
Fp = fopen ("data. DXF", "R"); open the DXF file, Data. DXF
While (! Feof (FP )&&! The ferror (FP) function is used to determine the number of Objects Based on the object name.
{So the function only reads the object name. Once a new object name appears,
Fscanf (FP, "% s \ n", str1); add one to the number of entities.
If (strcmp (str1, "vertex") = 0)
{
Fscanf (FP, "% s \ n", str2); open the DXF file, Data. DXF
Fscanf (FP, "% s \ n", str2); this function determines the number of Objects Based on the object name.
If (strcmp (name, str2 )! = 0) So the function only reads the object name. Once a new object name appears,
Content from dedecms
{Add one to the number of entities.
Strcpy (name, str2 );
Num ++;
}
}
}
Fclose (FP );
Return num;
}
The following is a program for reading the coordinates and order of object points.CodeIn this program, the coordinates of the midpoint of the model are read.
The maximum and minimum values of the object, the coordinate of the vertex, and the order of the vertex.
Void cjupiterview: onfileinput ()
{
// Todo: add your command handler code here
File * FP, * fp2;
Int I, K, J;
Float tempx, Tempy, tempz;
Float xmin, ymin, zmin, xmax, Ymax, zmax, Max;
Int lab;
Char str1 [20], str2 [20], STR [20], HT;
Char myname [20];
Int mynumber;
Vertex tempvertex;
Sequence tempsequence;
Typedef struct {
Float X, Y, Z, Max;
} Max;
Max;
Ht = 9;
Objectnumber = getobjectnumber ();
Mydata = new myvertex [objectnumber];
Fp = fopen (filename, "R ");
I = 0;
J = 0;
K = 0;
Mynumber =-1;
Strcpy (myname, "objectname ");
While (! Feof (FP )&&! Ferror (FP ))
{
Fscanf (FP, "% s \ n", STR );
If (strcmp (STR, "$ extmin") = 0)
Dedecms.com
{
Fscanf (FP, "% s \ n", str1 );
Fscanf (FP, "% F \ n", & xmin );
Fscanf (FP, "% s \ n", str1 );
Fscanf (FP, "% F \ n", & ymin );
Fscanf (FP, "% s \ n", str1 );
Fscanf (FP, "% F \ n", & zmin );
}
If (strcmp (STR, "$ extmax") = 0)
{
Fscanf (FP, "% s \ n", str1 );
Fscanf (FP, "% F \ n", & xmax );
Fscanf (FP, "% s \ n", str1 );
Fscanf (FP, "% F \ n", & Ymax );
Fscanf (FP, "% s \ n", str1 );
Fscanf (FP, "% F \ n", & zmax );
Max. x = max (ABS (xmax), ABS (xmin ));
Max. Y = max (ABS (Ymax), ABS (ymin ));
Max. z = max (ABS (zmax), ABS (zmin ));
Max. Max = max (max. X, Max. y );
Max. Max = max (Max. Max, Max. z );
}
If (strcmp (STR, "vertex") = 0)
{
Fscanf (FP, "% s \ n", str1 );
Fscanf (FP, "% s \ n", str1 );
If (strcmp (myname, str1 )! = 0)
{
Mynumber ++;
Strcpy (myname, str1 );
Strcpy (mydata + mynumber)-> obname, myname );
}
Fscanf (FP, "% s \ n", str2 );
Content from dedecms
fscanf (FP, "% F \ n", & tempx);
fscanf (FP, "% s \ n", str2 );
fscanf (FP, "% F \ n", & Tempy);
fscanf (FP, "% s \ n", str2 );
fscanf (FP, "% F \ n", & tempz);
fscanf (FP, "% d \ n", & Lab );
fscanf (FP, "% d \ n", & Lab);
If (lab = 192)
{< br> tempvertex. X = tempx/max. max;
tempvertex. y = Tempy/max. max;
tempvertex. z = tempz/max. max;
(mydata + mynumber)-> vertex. add (New cvertex (tempvertex);
}< br> If (lab = 128)
{< br> fscanf (FP, "% s \ n", str1);
fscanf (FP, "% F \ n", & tempx);
fscanf (FP, "% s \ n", str1);
fscanf (FP, "% F \ n", & Tempy);
fscanf (FP, "% s \ n", str1);
fscanf (FP, "% F \ n", & tempz);
tempsequence. A = ABS (tempx);
tempsequence. B = ABS (Tempy);
tempsequence. C = ABS (tempz);
(mydata + mynumber)-> sequence. add (New csequence (tempsequence);
}< BR >}< br> fclose (FP);
}