3Д БУМ

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

Looking at the Source Code

Before you begin, you might want to pull up the source code for this chapter. The files pertaining to the MD2 models are named md2.h and md2.cpp. Both of these files can be found in the Code/Chapter_3 direc­tory on the CD. The first thing to look at is a general layout of the file. The MD2 file contains geometry, texture, and animation information, all in a very specific order, as shown in Figure 3.2.

The first thing in every good 3D graphics file is the header. A header is simply information about the particular file located right at the start of the file. Headers are very useful because they can reveal lots of informa­tion about a file, without forcing you to dig through piles of data. Headers

may include features such as “magic numbers” to identify the file type and the version numbers to prevent old file versions from being used when a program no longer contains support for it, as well as information about the data that is in the file. MD2 is no different. The header contains a plethora of useful information. Let’s see what it looks like.

Looking at the Source Code

Figure 3.2 The general layout of the data within an MD2 file.

struct SMD2Header {

int m_iMagicNum; int m_iVersion; int m_iSkinWidthPx; int m_iSkinHeightPx; int m_iFrameSize; int m_iNumSkins; int m_iNumVertices; int m_iNumTexCoords; int m_iNumTriangles; int m_iNumGLCommands; int m_iNumFrames;

};

Wow, that’s a lot of information. Now that you have that list, let’s see what it is all for.

■ First up is the “magic number”. The magic number lets you check to make sure that the file you are loading is indeed an MD2. The magic number is IDP2, which stands for ID Polygon 2. Combined into one integer, IDP2 turns out to be 844121161.

■ Following the magic number is the version number. This is always 8, always. These two values should be checked upon load. If either is wrong, the file is not a valid MD2 model, and there­fore will not load correctly.

■ The next two variables deal with the skin, or texture, that is used to cover the model. Each MD2 can use exactly one skin at a time, although multiple skins may be loaded and used at differ­ent times (more on that in a bit). The two four-byte integers, m_iSkinWidthPx and m_iSkinHeightPx, represent the skin’s height and width, respectively, in pixels.

■ Always one in every family, the next variable is a bit of an odd one. m_iFrameSize, also an integer, gives you the size of each keyframe in bytes. It saves a bit of time later, but it’s nothing that couldn’t be easily computed later.

The next six variables are all about the numbers of things, as described:

■ The first (m_iNumSkins) is the number of skins defined in the file. This variable does not refer to the number of skins used to texture the model, but rather to the total number of skin op­tions for the model. Different skins may be used to change the appearance slightly for things such as team colors and to add a bit of variation within the game.

■ m_iNumVertices, as the name implies, gives you the number of vertices per frame. Every frame in the model will contain exactly this many vertices.

~ГГ^Э-

■ m_iNumTexCoords gives you the number of texture coordinates within the file. This does not need to be the same as the number of vertices. All the frames use the same set of texture coordinates.

■ m_iNumTriangles is the number of triangles in the whole model.

MD2 models contain only triangles; there are no other primi­tives such as quadrilaterals present.

■ The next integer, m_iNumGLCommands, is a special one. It specifies the number of special commands that can be used to optimize the MD2’s mesh into triangle fans and strips for the renderer.

The GL commands are not necessary to load the model, but provide an alternative way of rendering the model.

■ The last variable in this section, m_iNumFrames, gives you the number of frames in the MD2 file. Each frame contains a full set of vertex positions for that stage in the animation. Every frame of the model is like a snapshot of its current position.

The next section of five integers gives the offset in bytes of each of the major parts of the file, enabling you to simply skip to them when you are loading. Keep in mind that these are the distances from the begin­ning of the file, not the variable in the header or from the previous section of data. If you measure the distances from the wrong place, you will end up in the wrong section of the file.

The very last variable, m_iFileSize, gives you the file size from the start of the header, to the end of the file in bytes.

Just remember that all the integers are four bytes, giving the header a total size of 68 bytes. Now, to really get anything useful, you need to use that information along with the data structures discussed next. Read on!

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