Opengl - loading md2 file

I have created a program that loads a md2 file (following the wikipedia page on it) and am having some troubles rendering it, below is my source code any help would be great.

md2.cpp
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//c/c++
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <stdlib.h>

//SDL/Opengl
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_mixer.h>
#include <GL/gl.h>
#include <GL/glu.h>
using namespace std;

//project
#include "md2.h"

md2::md2()
{
}
void md2::render()
{
    glBegin(GL_TRIANGLES);
    for (int i=0; i<header.num_tris; i++)
    {
    glVertex3f(tris[i].vertex_index[0] * frame_head.scale[0] + frame_head.translate[0],
               tris[i].vertex_index[1] * frame_head.scale[1] + frame_head.translate[1],
               tris[i].vertex_index[2] * frame_head.scale[2] + frame_head.translate[2]);
    }
    glEnd;
}
bool md2::load(const char* filename)
{
    int buffer [17];
    ifstream file;
    file.open(filename, istream::binary);

    if (file == false)
    {
        cout << "cannot find file"<<endl;
        return false;
    }
    for (int i=0; i<17; i++)
        {
            file.read((char*)&buffer[i], sizeof(int));
        }
        header.ident = buffer[0];      //must be IDP2 (844121161)
        header.version = buffer[1];    //must be 8
        if (header.ident != 844121161 || header.version != 8)
        {
            cout << "File is incorrect, version or magic number is wrong."<<endl;
            return false;
        }
        header.skinwidth = buffer[2];  //width of texture
        header.skinheight = buffer[3]; //height of texture
        header.framesize = buffer[4];  //size of one frame in bytes
        header.num_skins = buffer[5];  //number of textures
        header.num_xyz = buffer[6];    //number of vertices
        header.num_st = buffer[7];     //number of texture coordinates
        header.num_tris = buffer[8];   //number of triangles
        header.num_glcmds = buffer[9]; //number of opengl commands
        header.num_frames = buffer[10]; //number of frames
        header.ofs_skins = buffer[11];  //offset to skin names
        header.ofs_st = buffer[12];     //offest to s-t texture coordinates
        header.ofs_tris = buffer[13];   //offset to triangles
        header.ofs_frames = buffer[14]; //offset to frame data
        header.ofs_glcmds = buffer[15]; //offset to opengl commands
        header.ofs_end = buffer[16];    //offset to end of file

        //load int texture coordinates
        file.seekg ( header.ofs_st, ios::beg );
        for (int i=0; i<header.num_st; i++)
        {
            file.read((char*)&texCoord[i].s, sizeof(short));
            file.read((char*)&texCoord[i].t, sizeof(short));
        }

        //recover the floating-point texture coordinates from int texture coordinates
        for (int i=0; i<header.num_st; i++)
        {
            realst[i].s = texCoord[i].s / header.skinwidth;
            realst[i].t = texCoord[i].t / header.skinheight;
        }

        //load tris data
        file.seekg ( header.ofs_tris, ios::beg );
        for (int i=0; i< header.num_tris; i++)
        {
                for (int j=0; j<3; j++)
                {
                file.read((char*) &tris[i].vertex_index[j], sizeof(float));
                }
                for (int j=0; j<3; j++)
                {
                file.read((char*)&tris[i].texture_index[j], sizeof(float));
                }
        }

        //load frame header
        file.seekg ( header.ofs_frames, ios::beg );



        for (int i =0; i<3; i++)
        {
            file.read((char*)&frame_head.scale[i], sizeof(int));
        }
        for (int i =0; i<3; i++)
        {
            file.read((char*)&frame_head.translate[i], sizeof(int));
        }
        file.read(frame_head.name, 16);


        //load vertex data as int
        for (int i=0; i<header.num_xyz; i++)
        {
                for (int j=0; j<3; j++)
                {
                file.read((char*)&frame_coords[i].v_i[j], sizeof(int));
                }
            file.read((char*)&frame_coords[i].lightnormalindex, sizeof(int));

        //convert int vertex data to float
            frame_coords[i].v_f[0] = (frame_coords[i].v_i[0] * frame_head.scale[0]) + frame_head.translate[0];
            frame_coords[i].v_f[1] = (frame_coords[i].v_i[1] * frame_head.scale[1]) + frame_head.translate[1];
            frame_coords[i].v_f[2] = (frame_coords[i].v_i[2] * frame_head.scale[2]) + frame_head.translate[2];
        }

        file.close();

        cout << "Finished loading MD2" <<endl;
    return true;
}

md2.h
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
#ifndef MD2_H
#define MD2_H
typedef struct
{
    int ident;      //must be IDP2
    int version;    //must be 8
    int skinwidth;  //width of texture
    int skinheight; //height of 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;     //offest to s-t texture coordinates
    int ofs_tris;   //offset to triangles
    int ofs_frames; //offset to frame data
    int ofs_glcmds; //offset to opengl commands
    int ofs_end;    //offset to end of file
}s_header;
typedef struct
{
    short s;
    short t;
}texCoord_s;
typedef struct
{
    float s;
    float t;
}texCoord_f;
typedef struct
{
    short vertex_index[3];
    short texture_index[3];
}tris_coord;
typedef struct
{
    short scale[3];
    short translate[3];
    char name[16];
}frame_header;

typedef struct
{
    int v_i[3];
    int lightnormalindex;
    float v_f[3];
}frame_coord;

class md2
{
public:
    md2();
    bool load(const char* filename);
    void render();

private:
    s_header header;
    texCoord_s texCoord[];
    texCoord_f realst[];
    tris_coord tris [];
    frame_header frame_head;
    frame_coord frame_coords[];
};

#endif // MD2_H
Topic archived. No new replies allowed.