• Advertisement
Sign in to follow this  

MD2 Loader

This topic is 3357 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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.u, in);
			Read<short>(tex_coords.v, in);
			out << tex_coords.u << " " << tex_coords.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.v[j], in);
				out << "\t" << triangles.v[j] << " ";
			}
			out << "\n";

			for(int j = 0; j < 3; j++) {
				Read<unsigned short>(triangles.uv[j], in);
				out << "\t" << triangles.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.Vertices.resize(header.numVertices);
			
			Read<float>(t_frames.Scale.x, in);
			Read<float>(t_frames.Scale.y, in);
			Read<float>(t_frames.Scale.z, in);
			out << "\t" << t_frames.Scale.x << " " << t_frames.Scale.y << " " << t_frames.Scale.z << "\n";
			Read<float>(t_frames.Translate.x, in);
			Read<float>(t_frames.Translate.y, in);
			Read<float>(t_frames.Translate.z, in);
			out << "\t" << t_frames.Translate.x << " " << t_frames.Translate.y << " " << t_frames.Translate.z << "\n";

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

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

		Vertex t;
		frames.resize(numFrames);
		for(int i = 0; i < t_frames.size(); i++) {
			frames.Name = t_frames.Name;
			for(int j = 0; j < triangles.size(); j++) {
				for(int k = 0; k < 3; k++) {
					t.x = (t_frames.Scale.x * t_frames.Vertices[triangles[j].v[k]].v[0]) + t_frames.Translate.x;
					t.y = (t_frames.Scale.y * t_frames.Vertices[triangles[j].v[k]].v[1]) + t_frames.Translate.y;
					t.z = (t_frames.Scale.z * t_frames.Vertices[triangles[j].v[k]].v[2]) + t_frames.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.vertices.push_back(t);
				}
			}
		}

		D3DXVECTOR3 normal;
		for(int i = 0; i < frames.size(); i++) {
			for(int j = 0; j < frames.vertices.size(); j += 3) {
				normal = FaceNormal(frames.vertices[j], frames.vertices[j+1], frames.vertices[j+2]);
				frames.vertices[j].normal += normal;
				frames.vertices[j+1].normal += normal;
				frames.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.normal, &frames[j].vertices.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
Advertisement
Sign in to follow this  

  • Advertisement