Learning Material: A Simple B3D Parser

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply

Should I spend time learning B3D?

Yes
8
89%
No
1
11%
 
Total votes: 9

dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Learning Material: A Simple B3D Parser

Post by dlangdev »

Hi,

I'd like to share this with anyone interested in learning mesh loaders, for example loading B3D files.

B3D works pretty well with Irrlicht, that's one reason why you need to learn the innards of it. And this sample B3D parser will get you there, learning how to read from and probably write to B3D files later. The code will also provide a good understanding of Irrlicht B3D loader as well.

The state of the of code is incomplete, though some of the code are reading chunks just fine. Though it contains interesting stuff about the internals of B3D.

For those who are interested to look around, the code pretty much relies on stdio and stdlib, nothing fancy was added to make it complicated. So, you won't be stumped with code jargon.

https://sourceforge.net/project/showfil ... _id=265015

By the way, please post any changes you made, I'd appreciate any fix/addition you will make into the code.

The B3D spec is right here...
http://www.blitzbasic.com/sdkspecs/sdks ... _specs.txt

Have fun.
Image
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Post by dlangdev »

Here's another interesting code snippet for dumping the children of BB3D...

Code: Select all

int CB3DLoader::load(char* fname) 
{
	size_t items_read;
	char buf[4];
	int chunk_length;
	int version;
	int c = 0;

	FILE *file;
	file = fopen(fname, "r"); 

	char* p_chunk;

	printf("file: %s\n", fname);
	items_read = fread(buf, sizeof(buf), 1, file);
	if ( (buf[0]=='B') && (buf[1]=='B') && (buf[2]=='3') && (buf[3]=='D')  ) {
		items_read = fread(&chunk_length, sizeof(int), 1, file);
		printf("\n%c%c%c%c chunk_length: %d\n",buf[0],buf[1],buf[2],buf[3], chunk_length);
		items_read = fread(&version, sizeof(version), 1, file);
		printf("\tversion: %d\n", version);
		if (feof(file)) {printf("load 0.error: eof\n");}
		getChildren(file);
	}
	fclose(file);
	return 0;
}

int CB3DLoader::getChildren(FILE *file) 
{
	size_t items_read;
	char buf[4];
	int chunk_length=0;
	int version=0;
	char c = '\0';
	char* p_chunk=0;

	while ( !feof(file) ) {
		items_read = fread(buf, sizeof(buf), 1, file);
		items_read = fread(&chunk_length, sizeof(int), 1, file);
		printf("\n%c%c%c%c chunk_length: %d\n",buf[0],buf[1],buf[2],buf[3], chunk_length);
		p_chunk = (char*)malloc(chunk_length);
		for (int i=0; i<chunk_length; i++) {
			c = fgetc(file);
			p_chunk[i] = c;
		}
		free (p_chunk);
	}
	return 0;
}
Image
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Post by dlangdev »

Update:

Finally found the nasty bug running wild in the code.

The Bug:
file = fopen(fname, "r");

The Fix:
file = fopen(fname, "rb");

It's now walking through the binary file collecting the chunk values.

Once that is squared away, I'll run parallel test against Irrlicht B3D loader and compare the numbers.

That should be interesting, figuring out what parts of the code need improvement.
Image
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Post by dlangdev »

Update:

1) Reader is now walking the file stream, reading both headers and chunks.

2) The walker uses recursion to collect child elements.

3) Next step is continue writing the parse tree.

4) After that, collect performance data and compare it to Irr's B3D loader.

5) Then probably, code a B3D writer program for exporting scene from Irrlicht.
Image
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

dlangdev wrote: 5) Then probably, code a B3D writer program for exporting scene from Irrlicht.
Now that would be very useful :)
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Post by dlangdev »

bitplane wrote:
dlangdev wrote: 5) Then probably, code a B3D writer program for exporting scene from Irrlicht.
Now that would be very useful :)
Yup, I'm planning to use the B3D writer for the Procedural Building/City which is currently in-development. With that, I'll be able to load it on Irrlicht and animate from there.
Image
Post Reply