Sign in to follow this  

3ds-loader has me stumped big time [SOLVED]

This topic is 3739 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

Hey there, I've come across a really strange problem while trying to load a 3ds-file in my OpenGL-application. I'm using MSVC++ 6. Take a look at the function that is responsible for loading a model:
void cmesh::init ( const char* filename )
// loads a 3ds model file
{
	FILE			*file;
	unsigned short	l_chunk_id;
	unsigned int	l_chunk_lenght;
	unsigned char	l_char;
	unsigned short	l_qty;
	unsigned short	l_face_flags;

	if ( ( file = fopen ( filename, "rb" ) ) == NULL )
		return_error ( "Failed to load model..." );

	printf ( "function called\n" );

	while ( ftell ( file ) < filelength ( fileno ( file ) ) ) 
	{
		fread ( &l_chunk_id, 2, 1, file );
		fread ( &l_chunk_lenght, 4, 1, file );
		
		int i;

		switch ( l_chunk_id )
        {
			case 0x4d4d:
				
			break;    
			case 0x3d3d:

			break;
			case 0x4000:
				printf ( "name called\n" );
				i = 0;
				do
				{
					fread ( &l_char, 1, 1, file );
					name[i] = l_char;
					i++;
                }
				while ( l_char != '\0' && i < 20 );
				printf ( "name: %s\n", name );
			break;
			case 0x4100:

			break;
			case 0x4110: 
				printf ( "vertices called\n" );
				fread ( &l_qty, sizeof ( unsigned short ), 1, file );
                num_vertices = l_qty;
				printf ( "vertices: %d", num_vertices );
                for ( i = 0; i < l_qty; i++ )
                {
					fread ( &vertex[i].x, sizeof ( unsigned short ), 1, file );
 					fread ( &vertex[i].y, sizeof ( unsigned short ), 1, file );
 					fread ( &vertex[i].z, sizeof ( unsigned short ), 1, file );
				}
			break;
			case 0x4120:
				printf ( "polygons called\n" );
				fread ( &l_qty, sizeof ( unsigned short ), 1, file );
                num_polygons = l_qty;
				printf ( "polygons: %d", num_polygons );
                for ( i = 0; i < l_qty; i++ )
                {
					fread ( &triangle_index[i].a, sizeof ( int ), 1, file );
					fread ( &triangle_index[i].b, sizeof ( int ), 1, file );
					fread ( &triangle_index[i].c, sizeof ( int ), 1, file );
					fread ( &l_face_flags, sizeof ( unsigned short ), 1, file );
				}
			break;
			case 0x4140:
				printf ( "texture-coordinates called\n" );
				fread ( &l_qty, sizeof ( unsigned short ), 1, file );
				for ( i = 0; i < l_qty; i++ )
				{
					//fread ( &p_object->mapcoord[i].u, sizeof ( float ), 1, file );
					//fread ( &p_object->mapcoord[i].v, sizeof ( float ), 1, file );
				}
			break;
			default:
				 fseek ( file, l_chunk_lenght - 6, SEEK_CUR );
        }
	}

	fclose ( file );
}








Looks simple enough to me (texture coordinates aren't yet implemented) - anyways, this code gives some very weird and unexpected results. I'm using the printf-calls to write debug-lines into SDL's stdout.txt. Running the function like this in order to load an example mesh results in a debug-file looking like this:
function called
name called
name: Testmesh
vertices called
vertices: 2664
The chunks for polygon- and texture-coordinate-data aren't even touched. Upon inspecting the problem further I then come across the following behavior: as soon as I comment any case x: - break; portion out, it seems to mess with the complete rest of the loading-process, which is, frankly, quite strange to my understanding. An example: when I comment the first case 0x4d4d: - break; out (although this chunk doesn't even do anything), the debug-file will only read:
function called
How can this be the case? Am I missing something obvious here or have I somehow misunderstood the switch-case-system (and used it wrongly since years)? Any clear-ups would be very appreciated - I might haven't got enough sleep last night or something, but this has me stumped to the bone. Edit: Hmmm... Don't know why I haven't realized this before: obviously, the default-statement keeps on checking for other chunks - so when a case (even an empty one) is commented out, it does make a difference in loading the file because the default case is NOT called. However, the question why the file isn't loaded all the way remains. Edit 2: Figured it out. Note to self: remember to keep the variable-types correct when parsing files. Note to self: you are stupid. [Edited by - d h k on September 17, 2007 7:10:54 PM]

Share this post


Link to post
Share on other sites

This topic is 3739 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.

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