3Д БУМ

3Д принтеры и всё что с ними связано

The Data: Frames and Vertices

To display anything, you first need to get the data out of the file. You can start with the frames. Each frame starts with six floating-point values representing the scale and translation of the vertices on the X, Y, and Z axes. These values are used to decompress the vertices (more on this in a second). Immediately after these values comes a 16-character string that designates the frame’s name. The frame name is not overly useful in the code in this book, but you may be able to find a good use for it.

Immediately following the name comes all the vertex positions for that frame. In every frame, there is exactly the number of vertices indicated by the m_iNumVerts variable in the header.

My frame structure looks like this:

struct SMD2Frame {

float m_fScale[3]; float m_fTrans[3] char m_caName[16];

SMD2Vert * m_pVertss SMD2Frame()

{

m_pVerts = 0;

}

~SMD2Frame()

{

if(m_pVerts)

delete [] m_pVerts;

}

};

The first thing you probably notice about the structure is that it has a constructor and a destructor. This is perfectly legal in C++, and allows your frame structures to clean up after themselves so you don’t have to worry about it. Another way to do it is to statically allocate an array to hold the maximum number of vertices allowed by the MD2 format (2048 vertices). However, this method wastes a considerable amount of

CAUTION

You need to be careful when working with structures, particularly ones that include pieces other than variables, such as functions, in them. The size of these structures (found with sizeof()) might not match the size of the structure in the model file. This is also true with structures such as SMd2Vertex, discussed in the next section, because the data that it stores is in a different format than is stored in the file. Be sure to watch out for situations like these.

The Data: Frames and Vertices

~ГГ^Э-

memory if the model is small. It is best to allocate memory for each frame with the C++ keyword new.

You can’t easily use the previous structure if you do not know what the vertex structure looks like. Take a look:

struct SMD2Vert {

float m_fVert[3];

};

That’s pretty simple; m_fVert is an array of three floating-point values representing the X, Y, and Z positions of the vertex.

The vertices are not actually stored as floating point values. They are compressed into one byte for each vertex component. To decompress them, you must multiply each byte component by its respective float­ing-point scale value, and add the appropriate translation value. When this is done, the result can be stored in the SMD2Vert structure.

For each vertex, you read four bytes of the file into a temporary array. Following that, you multiply the first byte by the first scaling value, the second value by the second scaling factor, and so on. Then, in the same fashion, add the appropriate translations.

Once you have all this loaded, you can actually draw your model. You simply draw a single point at each vertex and you should see a rough image of the final model, similar to Figure 3.3. Not very exciting perhaps, but more than you had just a few pages ago. To really make it look good, you need to make it solid.

Для любых предложений по сайту: [email protected]