3Д БУМ

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

ЗД принтеры АБС пластик

Beautification: Adding Skins

The model shown in Figure 3.4 would look a lot better if you added a texture. This can be done in one of two ways.

The first way is to use the skin names embedded inside the MD2 file itself. The number and location of the skin file names are dictated by variables in the header, m_iNumSkins and m_iOffsetSkins, respectively. Each skin name is 64 characters long, but the last part of the name in the file can be all zeros, effectively terminating the string at that point.

If you look at the SMD2Skin structure, you will notice it contains an in­stance of the CImage class. This class contains functions to load and bind various kinds of textures. There is not room to go into detail on how it works, but you are welcome to look at the source code. The files pertain­ing to CImage are in the basecode folder and are image. cpp and image. h.

Figure 3.5 A typical skin for an MD2 model. This is the skin for the HellPig MD2 created by Psionic Design (http://www. psionicdesign. com).

Each of the file names in the skin section is a different skin. A typical skin could look something like the one shown in Figure 3.5.

Beautification: Adding Skins

The other way to load a skin for the model is to use the CImage class to load a file, and then bind it to the model using the model class’s SetSkin function.

But wait, before you can actually render the model, you need to load the texture coordinates from the file.

The number of texture coordinates used in the file is given by the variable m_iNumTexCoords in the header of the MD2. This many texture coordinates are stored at m_iOffsetTexCoords bytes into the file. The texture coordinate structure that will be used looks like this:

struct SMD2TexCoord {

float m_fTex[2];

};

Each texture coordinate consists of a pair of two-byte integers. The first texture coordinate ranges from 0 to the width of the skin, the second from 0 to the height of the skin. This will not work properly with OpenGL; you must convert them into a floating-point value between 0 and 1. To do so, you take the first short integr (two bytes), and divide it by the width of the skin, which is given by the variable m_iSkinWidthPx in the header. The same thing is done to the second coordinate, only using the number in m_iSkinHeightPx instead. This is done over and over until all the texture coordinates are calculated. Once this is done, you can move to the last step.

All that is left to do now is modify your rendering function to use the texture coordinates. First thing you do is enable texturing and bind the texture using the Bind function included in the CImage class.

Then, you travel down to the loop that draws the triangle. In the same way as the vertex indexes, the triangle structure also contains indexes into the texture coordinate array, one index for each vertex.

They work the same way, except there is only one array of texture coordinates, not one for each frame because only the positions of the vertices change between frames; the texture coordinates stay the same.

Take a look at the new rendering code:

glBegin(GL_TRIANGLES); //You want to render triangles for(int i = 0; i < m_Head. m_iNumTriangles; i++)

//Loop through all the triangles, the number of which is

~ГГ^Э-

//given in the file header.

{

//each triangle has exactly three vertices and three texture coords //the triangle structure contains three vertex indexes, and each frame //a single array of texture coordinates is used for all of the frames //and they are accessed in much the same way as the vertices glTexCoord2fv(m_pTexCoords[m_pTriangles[i].m_sTexIndices[0]]); glVertex3fv(

m_pFrames[frame].m_pVerts[m_pTriangles[i].m_sVertIndices[0]]); glTexCoord2fv(m_pTexCoords[m_pTriangles[i].m_sTexIndices[1]]); glVertex3fv(m

_pFrames[frame].m_pVerts[m_pTriangles[i].m_sVertIndices[1]]); glTexCoord2fv(m_pTexCoords[m_pTriangles[i].m_sTexIndices[2]]); glVertex3fv(

m_pFrames[frame].m_pVerts[m_pTriangles[i].m_sVertIndices[2]]);

}

glEnd(); //Finish up with the list of triangles

That was pretty simple. Your model should look much better now, perhaps like Figure 3.6.

Beautification: Adding Skins

Figure 3.6 A textured model. Here’s the HellPig again!

Beautification: Adding Skins

Your email address will not be published. Required fields are marked *

*