MD2 Model Format Issues

This is a discussion on MD2 Model Format Issues within the Game Programming forums, part of the General Programming Boards category; Basically I'm loading the first frames verticies to start out with (Gotta start somewhere), then scale and translate them, swap ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    13

    MD2 Model Format Issues

    Basically I'm loading the first frames verticies to start out with (Gotta start somewhere), then scale and translate them, swap Y-Z values, all that stuff. Now when I render it, I get this:
    Image: http://img3.imageshack.us/img3/9662/model1.jpg

    Basically if you;re to lazy to look all the triangles and such are disorirented, although you can make out the basic shape if u look hard enough. Anyone run into this problem and manage to fix it? Thanks.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You're problem could be anywhere. Maybe post up your code or samples from it. Have you tried stepping through the debugging and making sure you're getting the correct values from the file? Are you using OpenGL or Direct3D. Make sure you are using the correct vertex information with the correct parameters passed to the rendering method.

    Sorry if that doesn't help much but it would be nice to have more information.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Looks to me like you are not loading the mesh correctly. This could be from using the wrong vertex format. Whatever FVF they are using is what you must also use when loading it or D3D will parse the info incorrectly - actually it will process it how you tell it to it's just that the info will not be in the right spot.

    Most likely your FVF is simply off somewhere which is causing the distorted triangles, but like Mr. Wizard said it could be anything and w/o any code to go by...your guess is as good as ours.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    13
    Code:
    struct md2Header {
       int magic;					
       int version;					
       int skinWidth;				
       int skinHeight;				
       int frameSize;				
       int numSkins;				
       int numVertices;				
       int numTexCoords;			
       int numTriangles;			
       int numGlCommands;			
       int numFrames;				
       int offsetSkins;				
       int offsetTexCoords;			
       int offsetTriangles;			
       int offsetFrames;			
       int offsetGlCommands;		
       int offsetEnd;				
    };
    
    
    
    
    struct md2cVert {
    	unsigned char Verts[3];
    	unsigned char LightNormalIndex;
    };
    
    struct md2Vert {
    	float Verts[3];
    };
    
    struct cTexCoords {
    	short s, t;
    };
    
    struct rTexCoords {
    	float s, t;
    };
    
    struct md2TrigInd {
     short VertexIndicies[3];
     short TextureIndicies[3];
    
    };
    
    struct md2Frame {
    	float Scale[3];
    	float Translate[3];
    	char Name[16];
    	md2cVert Verts[1];
    };
    
    struct md2Frame_t {
    	md2Vert *pVerts;
    	char Name[16];
    };
    
    struct md2Triangle {
    	float Verts[3];
    	float Normals[3];
    };
    There are my structures, perhaps somethign is wrong with them perhaps not.

    Code:
    	FILE *pFile = fopen("tris.md2","rb");
    
    
    	md2Header Header;
    	md2Triangle *Triangles;
    	md2Frame *Frames;
    	cTexCoords *TexCoords;
    
    
    	fread(&Header,1,sizeof(md2Header),pFile);
    
    	Triangles = new md2Triangle[Header.numTriangles];
    	
    	Frames = new md2Frame[Header.numFrames];
    	TexCoords = new cTexCoords[Header.numTexCoords];
    
    
    	fseek(pFile,Header.offsetTexCoords,SEEK_SET);
    	fread(TexCoords,sizeof(cTexCoords),Header.numTexCoords,pFile);
    
    
    	fseek(pFile,Header.offsetTriangles,SEEK_SET);
    	fread(Triangles,sizeof(md2Triangle),Header.numTriangles,pFile);
    My loading Code (its in the winmain function as of now).

    Now my rendering code:

    Code:
    	glPushMatrix();
        
    	glScalef(.05f,.05,.05);
    	glBegin(GL_TRIANGLES);
    	
    
    	
    	for(int i = 0; i < Header.numVertices; i++) {
    
    			glTexCoord2f(TexCoords[i].s / Header.skinWidth,TexCoords[i].t / Header.skinHeight);
     glVertex3f(((Frames[0].Verts[i].Verts[0] * Frames[0].Scale[0]) + Frames[0].Translate[0]),
    			((Frames[0].Verts[i].Verts[2] * Frames[0].Scale[2]) + Frames[2].Translate[2]),
    			((-1 * Frames[0].Verts[i].Verts[1] * Frames[0].Scale[1]) + Frames[1].Translate[1]));
    	
    	}
    	
    
    	glEnd();
    	glPopMatrix();
    Edit: Sorry that I didn't post this before, but its a fair bit to look at, but if you see somethign wrong, please let me know.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    13
    YOu should knwo that the Frame index was changed because I was messing around with it, that doesn't make the difference, I just realized I posted it like that, sorry!

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    I don't personally have any experience with the MD2 file format. However, you could try the tutorial on MD2 models over at GameTutorials and compare your values with theirs. That is what I would do.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    13
    Thats what I did to get my code in the first place. Turns out I was rendering all the verticies from the array, I kinda assumed that you could render the verticies in order, but you need to use hte triangles you read in for that as well. I feel kinda stupid, but it works

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problems with MD2 file format
    By scwizzo in forum Game Programming
    Replies: 3
    Last Post: 12-27-2008, 09:11 PM
  2. 3d Model file format
    By glo in forum Game Programming
    Replies: 4
    Last Post: 09-15-2008, 04:33 PM
  3. Help with file reading/dynamic memory allocation
    By Quasar in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2004, 03:36 PM
  4. What are the differences between MD2 and MD3 format?
    By Shadow12345 in forum Game Programming
    Replies: 1
    Last Post: 10-17-2002, 01:34 PM
  5. model format and editor
    By Hershlag in forum Game Programming
    Replies: 3
    Last Post: 07-30-2002, 08:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21