Sign in to follow this  
MrPickle

MD2 Loader

Recommended Posts

I am trying to load a cube in MD2 format but it is stretching off into infinity. It is loading correctly by looking at my output files so I think the problem may be with the conversion into a format I can easily make into a VBO.
	MD2::MD2(const std::string filename, LPDIRECT3DDEVICE9& d3dd) {
		std::ifstream in(filename.c_str(), std::ios_base::in | std::ios_base::binary);
		if(in.fail()) {
			throw std::runtime_error("Unable to open \"" + filename + "\"");
		}

		std::ofstream out("MD2.txt");
		if(out.fail()) {
			throw std::runtime_error("Unable to open MD2.txt for writing");
		}

		out << "--" << filename << "\n";
		MD2Header header;
		ReadHeader(header, in);
		if(header.Magic != 0x32504449) {
			throw std::runtime_error("\"" + filename + "\" isn't a MD2 file");
		}

		if(header.Verion != 8) {
			throw std::runtime_error("\"" + filename + "\" isn't the correct version MD2");
		}

		numFrames = header.numFrames;
		skin_width = header.SkinWidth;
		skin_height = header.SkinHeight;
		std::vector<std::string> skins;
		std::vector<MD2UV> tex_coords(header.numTexCoords);
		std::vector<MD2Triangle> triangles(header.numTriangles);
		std::vector<MD2Frame> t_frames(header.numFrames);
		std::string skin_name;
		in.seekg(header.offsetSkins, std::ios_base::beg);
		for(int i = 0; i < header.numSkins; i++) {
			char c;
			in.read(&c, sizeof(char));

			while(c != '\0') {
				out << c;
				skin_name += c;
				in.read(&c, sizeof(char));
			}
			skins.push_back(skin_name);
			out << "\n";
			skin_name.clear();
		}

		in.seekg(header.offsetTexCoords, std::ios_base::beg);
		for(int i = 0; i < header.numTexCoords; i++) {
			Read<short>(tex_coords[i].u, in);
			Read<short>(tex_coords[i].v, in);
			out << tex_coords[i].u << " " << tex_coords[i].v << "\n";
		}

		in.seekg(header.offsetTriangles, std::ios_base::beg);
		for(int i = 0; i < header.numTriangles; i++) {
			out << "Triangle[" << i << "]\n";
			for(int j = 0; j < 3; j++) {
				Read<unsigned short>(triangles[i].v[j], in);
				out << "\t" << triangles[i].v[j] << " ";
			}
			out << "\n";

			for(int j = 0; j < 3; j++) {
				Read<unsigned short>(triangles[i].uv[j], in);
				out << "\t" << triangles[i].uv[j] << " ";
			}
			out << "\n";
		}

		in.seekg(header.offsetFrames, std::ios_base::beg);
		for(int i = 0; i < header.numFrames; i++) {
			out << "Frame[" << i << "]\n";
			t_frames[i].Vertices.resize(header.numVertices);
			
			Read<float>(t_frames[i].Scale.x, in);
			Read<float>(t_frames[i].Scale.y, in);
			Read<float>(t_frames[i].Scale.z, in);
			out << "\t" << t_frames[i].Scale.x << " " << t_frames[i].Scale.y << " " << t_frames[i].Scale.z << "\n";
			Read<float>(t_frames[i].Translate.x, in);
			Read<float>(t_frames[i].Translate.y, in);
			Read<float>(t_frames[i].Translate.z, in);
			out << "\t" << t_frames[i].Translate.x << " " << t_frames[i].Translate.y << " " << t_frames[i].Translate.z << "\n";

			in.read(&t_frames[i].Name[0], sizeof(char)*16);
			out << t_frames[i].Name << "\n";

			in.read(reinterpret_cast<char*>(&t_frames[i].Vertices[0]), sizeof(MD2Vertex)* header.numVertices);
		}

		Vertex t;
		frames.resize(numFrames);
		for(int i = 0; i < t_frames.size(); i++) {
			frames[i].Name = t_frames[i].Name;
			for(int j = 0; j < triangles.size(); j++) {
				for(int k = 0; k < 3; k++) {
					t.x = (t_frames[i].Scale.x * t_frames[i].Vertices[triangles[j].v[k]].v[0]) + t_frames[i].Translate.x;
					t.y = (t_frames[i].Scale.y * t_frames[i].Vertices[triangles[j].v[k]].v[1]) + t_frames[i].Translate.y;
					t.z = (t_frames[i].Scale.z * t_frames[i].Vertices[triangles[j].v[k]].v[2]) + t_frames[i].Translate.z;

					if(skin_width > 0) t.u = tex_coords[triangles[j].uv[k]].u / skin_width; else t.u = 0;
					if(skin_height > 0)	t.v = tex_coords[triangles[j].uv[k]].v / skin_height; else t.v = 0;
					frames[i].vertices.push_back(t);
				}
			}
		}

		D3DXVECTOR3 normal;
		for(int i = 0; i < frames.size(); i++) {
			for(int j = 0; j < frames[i].vertices.size(); j += 3) {
				normal = FaceNormal(frames[i].vertices[j], frames[i].vertices[j+1], frames[i].vertices[j+2]);
				frames[i].vertices[j].normal += normal;
				frames[i].vertices[j+1].normal += normal;
				frames[i].vertices[j+2].normal += normal;
			}
		}

		for(int j = 0; j < frames.size(); j++) {
			for(int i = 0; i < frames[j].vertices.size(); i++) {
				D3DXVec3Normalize(&frames[j].vertices[i].normal, &frames[j].vertices[i].normal);
			}
		}

		d3dd->CreateVertexBuffer(header.numVertices*sizeof(Vertex), 0, FVF_FORMAT, D3DPOOL_MANAGED, &vb, NULL);
		cur_frame = 0;
	}
Here is some output I am getting:
//First 3 numbers are the vertices
//The 2 after are the UV coords
//The last is the normal, this looks wrong? All the same yet they're different faces?
0: -0.992157, 0.992157, 1
0, 0
-0.57735, -0.57735, -0.57735
1: -0.992157, -1, -1
0, 0
-0.57735, -0.57735, -0.57735
2: -0.992157, 0.992157, -1
0, 0
-0.57735, -0.57735, -0.57735
3: -0.992157, 0.992157, 1
0, 0
-0.57735, -0.57735, -0.57735
4: -0.992157, -1, 1
0, 0
-0.57735, -0.57735, -0.57735
5: -0.992157, -1, -1
0, 0
-0.57735, -0.57735, -0.57735
6: 1, -1, -1
0, 0
-0.57735, -0.57735, -0.57735
7: -0.992157, -1, 1
0, 0
-0.57735, -0.57735, -0.57735
8: 1, -1, 1
0, 0
-0.57735, -0.57735, -0.57735
9: 1, -1, -1
0, 0
-0.57735, -0.57735, -0.57735
10: -0.992157, -1, -1
0, 0
-0.57735, -0.57735, -0.57735
11: -0.992157, -1, 1
0, 0
-0.57735, -0.57735, -0.57735
12: 1, 0.992157, -1
0, 0
-0.57735, -0.57735, -0.57735
13: 1, -1, -1
0, 0
-0.57735, -0.57735, -0.57735
14: 1, 0.992157, 1
0, 0
-0.57735, -0.57735, -0.57735
15: 1, 0.992157, 1
0, 0
-0.57735, -0.57735, -0.57735
16: 1, -1, -1
0, 0
-0.57735, -0.57735, -0.57735
17: 1, -1, 1
0, 0
-0.57735, -0.57735, -0.57735
18: -0.992157, 0.992157, -1
0, 0
-0.57735, -0.57735, -0.57735
19: 1, 0.992157, -1
0, 0
-0.57735, -0.57735, -0.57735
20: -0.992157, 0.992157, 1
0, 0
-0.57735, -0.57735, -0.57735
21: -0.992157, 0.992157, 1
0, 0
-0.57735, -0.57735, -0.57735
22: 1, 0.992157, -1
0, 0
-0.57735, -0.57735, -0.57735
23: 1, 0.992157, 1
0, 0
-0.57735, -0.57735, -0.57735
24: -0.992157, 0.992157, 1
0, 0
-0.57735, -0.57735, -0.57735
25: 1, 0.992157, 1
0, 0
-0.57735, -0.57735, -0.57735
26: -0.992157, -1, 1
0, 0
-0.57735, -0.57735, -0.57735
27: -0.992157, -1, 1
0, 0
-0.57735, -0.57735, -0.57735
28: 1, 0.992157, 1
0, 0
-0.57735, -0.57735, -0.57735
29: 1, -1, 1
0, 0
-0.57735, -0.57735, -0.57735
30: -0.992157, 0.992157, -1
0, 0
-0.57735, -0.57735, -0.57735
31: 1, -1, -1
0, 0
-0.57735, -0.57735, -0.57735
32: 1, 0.992157, -1
0, 0
-0.57735, -0.57735, -0.57735
33: -0.992157, 0.992157, -1
0, 0
-0.57735, -0.57735, -0.57735
34: -0.992157, -1, -1
0, 0
-0.57735, -0.57735, -0.57735
35: 1, -1, -1
0, 0
-0.57735, -0.57735, -0.57735

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this