3DS Vertex List Chunk

Started by
3 comments, last by MrPickle 15 years, 4 months ago
I'm having problems reading the Vertex List chunk of the 3DS model format. I keep getting numbers like: -1.#QNAN which I understand means "quiet not a number" and then numbers like 2.0345E-041, I think that's standard form? I followed this tutorial, http://www.spacesimulator.net/tut4_3dsloader.html, but I'm using C++ and i'm converting everything to little endian. Here's my code:
template <typename T> union Buffer {
	unsigned char bytes[sizeof(T)];
	T value;
};

bool isLittleEndian() {
	Buffer<unsigned short> b;
	b.value = 1;
	return (b.bytes[0] == 1);
}

template<typename T> void Read(T& value, std::ifstream& in) {
	Buffer<T> e;
	in.read(reinterpret_cast<char*>(&e.bytes[0]), sizeof(T));

	if(isLittleEndian()) {
		value = e.value;
		return;
	}
	
	char t;
	for(int i = 0, j = sizeof(T); i < j--; i++) {
		t = e.bytes;
		e.bytes = e.bytes[j];
		e.bytes[j] = t;
	}

	value = e.value;
}
while(in.tellg() < size) {
	Read<unsigned short>(info.ID, in);
	Read<int>(info.Size, in);
	info.Offset = in.tellg();

	switch(info.ID) {
		//...

		case VERTEX_LIST:
			int numVertices;
			Read<int>(numVertices, in);
			for(i = 0; i < numVertices; i++) {
				Read<FLOAT>(v.x, in);
				Read<FLOAT>(v.y, in);
				Read<FLOAT>(v.z, in);
				
				mesh.Vertices.push_back(v);
				v.x = v.y = v.z = 0.0f;
			}
			in.seekg(info.Offset + info.Size - 6);
			break;
		//...

		default:
			in.seekg(info.Size -6, std::ios_base::cur);
			break;
	}
}
Here to learn :)
Advertisement
Any help is appreciated.
Here to learn :)
I have fixed the problem. The number of vertices is a short int, not a int thus making it read too many bytes.
Here to learn :)
It's worth noting that 3DS is a pretty horrid and antiquated format, so you should avoid it if you can get your data in any other more modern form.
Quote:Original post by jpetrie
It's worth noting that 3DS is a pretty horrid and antiquated format, so you should avoid it if you can get your data in any other more modern form.


It was more of a test on loading files that aren't in human readable format. Thanks for the concern though.
Here to learn :)

This topic is closed to new replies.

Advertisement