Archived

This topic is now archived and is closed to further replies.

some help with ms3d files

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

Ok I got the following code:
MS3D::MS3D()
{
	
}

MS3D::~MS3D()
{
	if(vertices)
	{
		delete vertices;
		vertices=NULL;
	}
	if(triangles)
	{
		delete triangles;
		triangles=NULL;
	}
	if(groups)
	{
		delete groups;
		groups=NULL;
	}
}


bool MS3D::Load(char* path)
{
	FILE *f;
	MS3D_HEADER header;
	int loop;

	if(!path) return false;

	f =fopen(path,"rb");

	if(!f)
	{
		MessageBox(NULL,"Failed to open file","...",MB_ICONINFORMATION|MB_OK);
		return false;
	}

	fread(&header.id,sizeof(char),10,f);
	fread(&header.version,1,sizeof(int),f);

	if(strncmp( header.id, "MS3D000000",10)!=0)
	{
		MessageBox(NULL,"Not a valid ms3d file","...",
		MB_ICONINFORMATION|MB_OK);
		return false;
	}

	if(header.version != 3 && header.version != 4)
	{
		MessageBox(NULL,"Not a valid ms3d file","...",
			MB_ICONINFORMATION|MB_OK);
		return false;
	}

	fread(&numVertices,sizeof(unsigned short),1,f);
	vertices = new MS3D_VERTEX[numVertices];

	for( loop = 0; loop < numVertices; loop++ )
	{
		fread( &vertices[loop].flags,sizeof(BYTE), 1,f   );
		fread( &vertices[loop].vertex,sizeof(float),3,f  );
		fread( &vertices[loop].boneID,sizeof(char),1,f   );
		fread( &vertices[loop].refCount,sizeof(BYTE),1,f );
	}

	fread( &numTriangles,sizeof(unsigned short),1,f );
	triangles = new MS3D_TRIANGLE[numTriangles];

	for( loop = 0;loop < numTriangles;loop++ )
	{
		fread( &triangles[loop].flags,sizeof(unsigned short),1,f );
		fread( triangles[loop].vertexIndices,sizeof(unsigned short),3,f );
		fread( triangles[loop].vertexNormals[0],sizeof(float),3,f );
		fread( triangles[loop].vertexNormals[1],sizeof(float),3,f );
		fread( triangles[loop].vertexNormals[2],sizeof(float),3,f );
		fread( triangles[loop].u,sizeof(float),3,f );
		fread( triangles[loop].v,sizeof(float),3,f );
		fread( &triangles[loop].smoothingGroup,sizeof(unsigned char),1,f );
		fread( &triangles[loop].groupIndex,sizeof(unsigned char),1,f );
	}

	fread(&numGroups,sizeof(unsigned short),1,f);
	groups = new MS3D_GROUP[numGroups];

	for( loop = 0;loop < numGroups; loop++ )
	{
		fread(&groups[loop].flags,sizeof(unsigned char),1,f);
		fread( groups[loop].name,sizeof(char),32,f);
		fread(&groups[loop].numTriangles,sizeof(unsigned short),1,f);

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

		fread( groups[loop].triangleIndices, 
			   sizeof(unsigned short),
			   groups[loop].numTriangles, f);

		fread(&groups[loop].materialIndex,sizeof(char),1,f);
	}
	return true;
}

void MS3D::Render()
{
	int loop1;
	int loop2;
	int loop3;

	for( loop1 = 0; loop1 < numTriangles;loop1++ )
	{
		glBegin(GL_TRIANGLES);
		 for( loop2 = 0;loop2 < groups[loop1].numTriangles;loop2++ )
		 {
			 int triangleIndex = 
				 groups[loop1].triangleIndices[loop2];
			 
			 const MS3D_TRIANGLE* tri = &triangles[triangleIndex];

			 for( loop3 = 0; loop3 < 3;loop3++ )
			 {
				 int index = tri->vertexIndices[ loop3 ];
				 glTexCoord2f( tri->u[ loop3 ], tri->v[ loop3 ] );
				 glVertex3fv( vertices[ index ].vertex );
			 }
		 }
		glEnd();
	}
}
Here's my class definition
#ifndef _ms3dLoader_h_
#define _ms3dLoader_h_

#include <windows.h>

typedef struct MS3D_HEADER_TYP
{
	char id[10];
	int version;
}MS3D_HEADER,*MS3D_HEADER_PTR;

typedef struct MS3D_VERTEX_TYP
{
	unsigned char flags;
	unsigned char refCount;
	char boneID;
	float vertex[3];
}MS3D_VERTEX,*MS3D_VERTEX_PTR;

typedef struct MS3D_TRIANGLE_TYP
{
	unsigned short flags;
	unsigned short vertexIndices[3];
	float vertexNormals[3][3];
	float u[3];
	float v[3];
	unsigned char smoothingGroup;
	unsigned char groupIndex;
}MS3D_TRIANGLE,*MS3D_TRIANGLE_PTR;

typedef struct MS3D_GROUP_TYPE
{
	unsigned char flags;
	char name[32];
	unsigned short numTriangles;
	unsigned short *triangleIndices;
	char materialIndex;
}MS3D_GROUP,*MS3D_GROUP_PTR;

class MS3D
{
public:
	MS3D();
	~MS3D();
	unsigned short numVertices;
	MS3D_VERTEX* vertices;
	unsigned short numTriangles;
	MS3D_TRIANGLE* triangles;
	unsigned short numGroups;
	MS3D_GROUP* groups;

	bool Load(char*);
	void Render();
};

#endif
It compiles ok but when I run it I get unhandled exception saying this line: int triangleIndex = groups[loop1].triangleIndices[loop2]; ..is at fault and I already checked the function is allocating the memory correctly and everything, so...any ideas why this happens? Thanks in advance. [edited by - JohnyB on January 3, 2004 6:52:00 PM]

Share this post


Link to post
Share on other sites
Seems like the conditional of your outer loop should be
loop1 < numGroups
instead of
loop1 < numTriangles
, since you seem to be iterating the groups, then iterating the triangles of each group, and then 3 verts per tri. That works, except you''re testing against the number of triangles, not groups, in the outer loop.

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by ZealousElixir
What type of exception? Null pointer, invalid pointer, other? Have you tried debugging?

What exactly is debugging. I know that in general it''s a way of finding bugs in a program (obviously), but how do you actually debug? With output files that show results? I''ve heard the term "run it through the debugger", but what does this mean? When you run through the debugger, is there like an extra output window that shows all the variable values or something?

Please, can someone explain debugging and what it exactly is.

Share this post


Link to post
Share on other sites
You really don''t know what debugging is? It''s a very handy tool to see if your code is doing what you want it to do.

You generally work yourself through your lines of code and check what the values of the parameters in the code are.

So, you have your function "foo()" and tell the debugger to run to the first line of foo(). Then you can step through each line of the function, see what the value of each variable is as you continue. You can step into other function, etc etc, see what the result is from certain calculations.

And you can do all this while your program is running. Basically execution stops when you are looking at the code, and it will execute line by line as you walk through the code.

I use it quite a lot.
But I''m now working on some networking code that is a BIATCH to debug because it''s happening on two machines at the same time and I''m having syncronisation issues.

Share this post


Link to post
Share on other sites