1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
//MD2.cpp
#include "MD2.h"
bool MD2_Model::Load(const char* Filename)
{
int Buffer[17];
ifstream File;
File.open(Filename, istream::binary);
if (!File) return false;
for (int i=0; i<17; i++)
{
File.read((char*)&Buffer[i], sizeof(int));
}
Header.ident = Buffer[0];
Header.version = Buffer[1];
Header.skinwidth = Buffer[2];
Header.skinheight = Buffer[3];
Header.framesize = Buffer[4];
Header.num_skins = Buffer[5];
Header.num_xyz = Buffer[6];
Header.num_st = Buffer[7];
Header.num_tris = Buffer[8];
Header.num_glcmds = Buffer[9];
Header.num_frames = Buffer[10];
Header.ofs_skins = Buffer[11];
Header.ofs_st = Buffer[12];
Header.ofs_tris = Buffer[13];
Header.ofs_frames = Buffer[14];
Header.ofs_glcmds = Buffer[15];
Header.ofs_end = Buffer[16];
if ( Header.ident != 844121161 or Header.version != 8 ) return false;
File.seekg( Header.ofs_st, ios_base::beg );
for (int x = 0; x < Header.num_st; x++)
{
File.read( (char*)&TexCoords[x].s, sizeof(short) );
File.read( (char*)&TexCoords[x].t, sizeof(short) );
}
for (int x = 0; x < Header.num_st; x++)
{
GL_TexCoords[x].s = (float)TexCoords[x].s / (float)Header.skinwidth;
GL_TexCoords[x].t = (float)TexCoords[x].t / (float)Header.skinheight;
}
File.seekg( Header.ofs_tris, ios_base::beg ); // stops working here
for (int x = 0; x < Header.num_tris; x++ )
{
for (int y = 0; y < 3; y ++)
{
File.read( (char*)&Triangles[x].vertexindex[y], sizeof(short) );
}
for (int y = 0; y < 3; y ++)
{
File.read( (char*)&Triangles[x].textureindex[y], sizeof(short) );
}
}
File.seekg( Header.ofs_frames, ios_base::beg );
for (int x = 0; x < Header.num_frames; x ++)
{
for (int y = 0; y < 3; y++)
{
File.read( (char*)&Frames[x].scale[y], sizeof(float) );
}
for (int y = 0; y < 3; y++)
{
File.read( (char*)&Frames[x].translate[y], sizeof(float) );
}
File.read(Frames[x].name, 16);
}
for (int x = 0; x < Header.num_xyz; x++)
{
for (int y = 0; y < 3; y++)
{
File.read( (char*)&Verticies[x].v[y], sizeof(int) );
}
for (int y = 0; y < 3; y++)
{
Verticies[x].GL_Verticies[y] = (float)Verticies[x].v[y];
}
File.read( (char*)&Verticies[x].lightnormalindex, sizeof(int) );
}
File.close();
return true;
}
void MD2_Model::Draw(int frame)
{
for ( int x = 0; x < Header.num_tris; x++){
for (int y = 0; y < 3; y++){
glPushMatrix();
glTranslatef( Frames[frame].translate[0], Frames[frame].translate[1], Frames[frame].translate[2] );
glScalef( Frames[frame].scale[0], Frames[frame].scale[1], Frames[frame].scale[2] );
glBegin(GL_TRIANGLES);
glVertex3f(Verticies[Triangles[x].vertexindex[y]].GL_Verticies[0], Verticies[Triangles[x].vertexindex[y]].GL_Verticies[1], Verticies[Triangles[x].vertexindex[y]].GL_Verticies[2]);
glEnd();
glPopMatrix();
}
}
}
|