MD2 Loader problems

I've created a function for loading MD2 files, along with an accompanying class,that works almost perfectly. The one problem I have has to do with displaying the model. It displays just fines except for the fact that the texture isn't shown correctly. I've reviewed my code multiple times but can't seem to pinpoint where the problem exists. It seems that some of the texture coordinates are somehow being sent with the wrong vertices. My code for loading the texture coordinates is:

1
2
3
4
5
6
7
8
9
10
11
Model.seekg( Header.ofs_st, ios_base::beg );
for ( int i = 0; i < Header.num_st; i++ )
{
    MD2_TextureCoord Temp;
    Temp.s = ReadShort(Model);
    Temp.t = ReadShort(Model);
    Temp.gl_X = (float)Temp.s/Header.skinwidth;
    Temp.gl_Y = (float)Temp.t/Header.skinheight;

    TexCoords.push_back( Temp );
}

*I've also tried Temp.gl_Y = 1 - (float)t/Header.skinheight;
**This codes exists within a larger function for loading the file

My code for displaying the model is:
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
void MD2_Model::Draw( int Attributes[], bool Which_Way )
{
    ///Get Frame indicies
    int FrameIndex1 = StartFrame, FrameIndex2 = StartFrame;
    int AnimationLength = EndFrame - StartFrame + 1;

    vector<float> FramePos;
    for ( int i = 0; i < AnimationLength; i++ )
        FramePos.push_back( i * 1.f/AnimationLength );

    for ( int i = 1; i < FramePos.size(); i++ )
        if ( Time >= FramePos[i-1] and Time <= FramePos[i] )
        {
            FrameIndex1 = i - 1 + StartFrame;
            FrameIndex2 = i + StartFrame;
        }
    if ( FrameIndex1 == FrameIndex2 )
        FrameIndex1 = EndFrame;

    MD2_Frame Frame1 = Frames[FrameIndex1], Frame2 = Frames[FrameIndex2];

    ///Get Fraction between the two frames
    float Fraction = 0;
    if ( AnimationLength > 0 )
        Fraction = float(Time - FramePos[FrameIndex1 - StartFrame] )*AnimationLength;

    ///Draw
    Skin.Bind();

    /// Usual Way
    if ( Which_Way )
    {
        glBegin(GL_TRIANGLES);
        for ( int i = 0; i < Header.num_tris; i++ )
        {
            MD2_Triangle Triangle = Triangles[i];
            for ( int j = 0; j < 3; j++ )
            {
                MD2_Vertex V1 = Frame1.Vertices[ Triangle.VertexIndex[j] ];
                MD2_Vertex V2 = Frame2.Vertices[ Triangle.VertexIndex[j] ];

                vec3 Pos = V1.Pos * (1.f-Fraction) + V2.Pos * Fraction;
                vec3 Norm = V1.Norm * (1.f-Fraction) + V2.Norm * Fraction;

                MD2_TextureCoord Coord = TexCoords[ Triangle.TextureIndex[j] ];

                glVertexAttrib3f( Attributes[0], Pos[0], Pos[1], Pos[2] );  ///Position
                glVertexAttrib4f( Attributes[1], 1, 1, 1, 1 );                 ///Color
                glVertexAttrib2f( Attributes[2], Coord.gl_X, Coord.gl_Y );  ///TexCoord
                glVertexAttrib3f( Attributes[3], Norm[0], Norm[1], Norm[2] ); ///Normal
            }
        }
        glEnd();
    }

    ///Using file's commands
    else
    {
        for ( int i = 0; i < Groups.size(); i++ )
        {
            MD2_CommandGroup Group = Groups[i];
            if ( Group.Size > 0 )
                glBegin( GL_TRIANGLE_STRIP );
            else
                glBegin( GL_TRIANGLE_FAN );

            for ( int j = 0; j < fabs(Group.Size); j++ )
            {
                MD2_glCommand Command = Group.Commands[j];

                MD2_Vertex V1 = Frame1.Vertices[ Command.VertexIndex ];
                MD2_Vertex V2 = Frame2.Vertices[ Command.VertexIndex ];

                vec3 Pos = V1.Pos * (1.f-Fraction) + V2.Pos * Fraction;
                vec3 Norm = V1.Norm * (1.f-Fraction) + V2.Norm * Fraction;

                glVertexAttrib3f( Attributes[0], Pos[0], Pos[1], Pos[2] );  ///Position
                glVertexAttrib4f( Attributes[1], 1, 1, 1, 1 );                 ///Color
                glVertexAttrib2f( Attributes[2], Command.s, Command.t );  ///TexCoord
                glVertexAttrib3f( Attributes[3], Norm[0], Norm[1], Norm[2] ); ///Normal
            }
            glEnd();
        }
    }

    Skin.UnBind();
}


Here are some of the structures I use in my code:

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
struct MD2_Header
{
    char ident[4];    ///identifer - IDP2
    int version;      ///version number - 8
    int skinwidth;    ///width of texture
    int skinheight;   ///height of texture
    int framesize;    ///size of a frame in bytes
    int num_skins;    ///number of textures
    int num_xyz;      ///number of vertices
    int num_st;       ///number of texture coordinates
    int num_tris;     ///number of triangles
    int num_glcmds;   ///number of opengl commands
    int num_frames;   ///number of frames
    int ofs_skins;    ///offset to texture names
    int ofs_st;       ///offset to texture coordinates
    int ofs_tris;     ///offset to triangles
    int ofs_frames;   ///offset to frame data
    int ofs_glcmds;   ///offset to gl commands
    int ofs_end;      ///offset to end of file
};

struct MD2_TextureCoord
{
    short s;
    short t;
    float gl_X;
    float gl_Y;
};

struct MD2_Triangle
{
    short VertexIndex[3];
    short TextureIndex[3];
};

struct MD2_Vertex
{
    vec3 Pos;
    vec3 Norm;
};

struct MD2_Frame
{
    char Name[16];
    vector<MD2_Vertex> Vertices;
};

struct MD2_glCommand
{
    float s, t;
    int VertexIndex;
};

struct MD2_CommandGroup
{
    int Size; /// positive - triangle strip/negative - triangle fan
    vector<MD2_glCommand> Commands;
};
Last edited on
bump
I know that it's probably annoying since this is my second, and last, time bumping this, but the problem is still there I can't seem to figure out what's causing it.
Topic archived. No new replies allowed.