Loading MD2 files with Opengl

I'm looking for help with being able to load an MD2 file. Right now I'm trying to be able to load and display the model before I write code for texturing it. The code I have so far compiles but the program stops unexpectadly without me being able to see anything that's not a blank screen.

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();
            }
    }

}


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
//MD2.h

#ifndef MD2_H_INCLUDED
#define MD2_H_INCLUDED

#include <GL/freeglut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <iostream>
#include <fstream>

using namespace std;

struct MD2_Header
{
    int ident;         /// 844121161
    int version;       /// 8
    int skinwidth;     /// Width of the texture
    int skinheight;    /// Height of the texture
    int framesize;     /// Size of one 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 skin names
    int ofs_st;        /// Offset to texture coordinates
    int ofs_tris;      /// Offset to triangles
    int ofs_frames;    /// Offset to frames
    int ofs_glcmds;    /// Offset to opengl commands
    int ofs_end;       /// Offset to end of file
};

struct MD2_TexCoord
{
    short s;
    short t;
};

struct MD2_Triangles
{
    short vertexindex[3];
    short textureindex[3];
};

struct MD2_frame
{
    float scale[3];
    float translate[3];
    char name[16];
};

struct MD2_verticies
{
    int v[3];
    int lightnormalindex;
    float GL_Verticies[3];
};

struct GL_TexCoord
{
    float s;
    float t;
};

class MD2_Model
{
private:
    MD2_Header Header;
    MD2_frame Frames[];
    MD2_TexCoord TexCoords[];
    GL_TexCoord GL_TexCoords[];
    MD2_Triangles Triangles[];
    MD2_verticies Verticies[];

public:
    MD2_Model() {}
    bool Load(const char* Filename);
    void Draw(int frame);

};


#endif // MD2_H_INCLUDED 


Topic archived. No new replies allowed.