Jump to content
  • Advertisement
Sign in to follow this  
Lenggo

ms3d loader

This topic is 4886 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 have been working on a milkshape3d model loader for a while. For some reason when I load the joint data, the number of joints is zero and not the actual amount.
bool MS3D::
	Load(char* filename)
	{
	FILE* file;
	header header;
	int loop;

	//Open the .ms3d model file
	if((file= fopen(filename, "rb"))==NULL)
		{

        extern BOOL stop;
		stop = TRUE;
		MessageBox(NULL,"File Could Not Be Loaded.","ERROR",MB_OK|MB_ICONSTOP);	

		return true;
		
		}

	//Read the header data in
	fread(&header.id, sizeof(char), 10, file);
	fread(&header.version, 1, sizeof(int), file);





	if(strncmp(header.id, "MS3D000000", 10)!=0)
		{
		MessageBox( NULL, "Not Valid File", "Error", MB_OK | MB_ICONERROR );
		return 0;//false;	
		}
	if(header.version!=3 && header.version!=4)
		{
		MessageBox( NULL, "Not Valid File", "Error", MB_OK | MB_ICONERROR );
		return 0;//false;
		}


	//Read the vertex data in
	fread(&numVertices, sizeof(unsigned short), 1, file);
	vertices= new vertex [numVertices];
	for(loop=0; loop<numVertices; loop++)
		{
		fread(&vertices[loop].flags,	sizeof(BYTE),  1, file);
		fread( vertices[loop].vertex,	sizeof(float), 3, file);
		fread(&vertices[loop].boneID,	sizeof(char),  1, file);
		fread(&vertices[loop].refCount, sizeof(BYTE),  1, file);
		}

	//Read the triangle data in
	fread(&numTriangles, sizeof(unsigned short), 1, file);
	triangles= new triangle [numTriangles];
	for(loop=0; loop<numTriangles; loop++)
		{
		float mt[3]={1.0f-triangles->t[0],1.0f-triangles->t[1],	1.0f-triangles->t[2]};
		fread(&triangles[loop].flags,			 sizeof(unsigned short), 1, file);
		fread( triangles[loop].vertexIndices,	 sizeof(unsigned short), 3, file);
		fread( triangles[loop].vertexNormals[0], sizeof(float),			 3, file);
		fread( triangles[loop].vertexNormals[1], sizeof(float),		 	 3, file);
		fread( triangles[loop].vertexNormals[2], sizeof(float),			 3, file);
		fread( triangles[loop].s,				 sizeof(float),			 3, file);
		fread( triangles[loop].t,				 sizeof(float),			 3, file);
		fread(&triangles[loop].smoothingGroup,	 sizeof(unsigned char),  1, file);
		fread(&triangles[loop].groupIndex,		 sizeof(unsigned char),  1, file);

	
		}

	//Read the group data in
	fread(&numGroups, sizeof(unsigned short), 1, file);
	groups= new group [numGroups];
	for(loop=0; loop<numGroups; loop++)
		{
		fread(&groups[loop].flags,			 sizeof(unsigned char),  1,	 file);
		fread( groups[loop].name,			 sizeof(char),			 32, file);
		fread(&groups[loop].numTriangles,	 sizeof(unsigned short), 1,	 file);

		groups[loop].triangleIndices=new unsigned short [groups[loop].numTriangles];

		fread( groups[loop].triangleIndices, sizeof(unsigned short), groups[loop].numTriangles,file);
		fread(&groups[loop].materialIndex,	 sizeof(char), 1, file);
		}

	
	//read in material data
	fread(&numMaterials,sizeof(unsigned short), 1, file);
	materials= new material[numMaterials];

	for(loop=0; loop<numMaterials;loop++)
		{

		fread( materials[loop].name,    sizeof(char) ,32,file);
		fread( materials[loop].ambient, sizeof(float),4,file);
		fread( materials[loop].diffuse, sizeof(float),4,file);
		fread( materials[loop].specular,sizeof(float),4,file);
		fread( materials[loop].emissive,sizeof(float),4,file);
		fread(&materials[loop].shininess,sizeof(float),1,file);
		fread(&materials[loop].transparency,sizeof(float),1,file);
		fread(&materials[loop].mode,	sizeof(char),1,file);
		fread( materials[loop].texture,	sizeof(char),128,file);
		fread( materials[loop].alphamap,sizeof(char),128,file);
	
		}

	//Read in Joint data
        fread(&numJoints, sizeof(unsigned short), 1, file);
	joints= new joint[numJoints];
	
	for(loop=0;loop<numJoints;loop++)
	{
		fread(&joints[loop].flags, sizeof(unsigned char),1 ,file);
		fread( joints[loop].name, sizeof(char)          ,32,file);
		fread( joints[loop].parentName, sizeof(char)    ,32,file);
		fread( joints[loop].rotation,sizeof(float)      ,3 ,file);
		fread( joints[loop].position,sizeof(float)      ,3 ,file);
		fread(&joints[loop].numKeyFramesRot,sizeof(unsigned short),1,file);
		fread(&joints[loop].numKeyFramesTrans,sizeof(unsigned short),1,file);

		joints[loop].keyFramesRot=new keyframe_rot [joints[loop].numKeyFramesRot];
		joints[loop].keyFramesTrans=new keyframe_pos [joints[loop].numKeyFramesTrans];
	}
	fclose(file);		




//Find the parent joint array indices
	for(int x1=0;x1<numJoints;x1++)
	{
		//If the bone has a parent
		if(joints[x1].parentName != '\0')
		{
			//Compare names of theparent bone of x with the names of all bones
			for(int x2=0;x2<numJoints;x2++)
			{
				//A match has been found
				if(strcmp(joints[x2].name, joints[x1].parentName) == 0)
				{
					joints[x1].parent = x2;
				}
			}
		}
		//The bone has no parent
		else
		{
			joints[x1].parent = -1;
		}
	
	}
//end of loop to find parents

	Setup();





	return true;
	}

Theres the main part of the loading section. If anyone knows why the number of joints (numJoints) is alway zero it would be really helpful. Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!