Advertisement

Milkshape model loader not loading

Started by July 13, 2005 05:39 AM
8 comments, last by relpats_eht 19 years, 2 months ago
well, I just wrote a class to load a ms3d model based on the specs from whatever milkshapes site is (I never spell it correct, sowhy butcher it here?) but its not loading correctly. most of the values dont get filled out. I was wondering if anyone would be so kind as to point out any major things im doing wrong (other than the horrid code)
bool MSmodel::loadModel(char* filename){
    long fileSize;
    byte* buffer;
    byte* ptr;
    int i,l;
    ifstream inputFile(filename, ios::in | ios::binary);
    
    if(!inputFile.is_open()){
        return false;
    }
    
    inputFile.seekg(0, ios::end);
    fileSize = inputFile.tellg();
    inputFile.seekg(0, ios::beg);
    
    buffer = new byte[fileSize];
    inputFile.read((char*)buffer, fileSize);
    
    if(inputFile.bad()){
        return false;
    }
    
    inputFile.close();
	
    ptr = buffer+sizeof(MS3Dheader);
	
    numVertices = *(word*)ptr;
    ptr += sizeof(word);
    
    vertex = new MS3Dvertex [numVertices];
	for(i=0; i<numVertices; i++){
        vertex = *(MS3Dvertex*)ptr;
        ptr += sizeof(MS3Dvertex);
    }
    
    numTriangles = *(word*)ptr;
    ptr += sizeof(word);
    
    triangle = new MS3Dtriangle [numTriangles];
    for(i=0; i<numTriangles; i++){
        triangle = *(MS3Dtriangle*)ptr;
        ptr += sizeof(MS3Dtriangle);
    }
    
    numMeshes = *(word*)ptr;
    ptr += sizeof(word);
    
    mesh = new MS3Dmesh[numMeshes];
    for(i=0; i<numMeshes; i++){
        mesh.flags = *(byte*)ptr;
        ptr += sizeof(byte);
        
        strncpy(mesh.name, (char*)ptr, 32);
        ptr += sizeof(char)*32;
        
        mesh.numTriangles = *(word*)ptr;
        ptr += sizeof(word);
        
        mesh.triangleIndices = new word[mesh.numTriangles];
        for(l=0; l<mesh.numTriangles; l++){
            mesh.triangleIndices = (word*)ptr;
            ptr += sizeof(word);
        }
        
        mesh.materialIndex = *(char*)ptr;
        ptr += sizeof(char);
    }
    
    numMaterials = *(word*)ptr;
    ptr += sizeof(word);
    
    material = new MS3Dmaterial[numMaterials];
    for(i=0; i<numMaterials; i++){
        material = *(MS3Dmaterial*)ptr;
        ptr += sizeof(MS3Dmaterial);
    }
    
    animationFPS = *(word*)ptr;
    ptr += sizeof(word);
    
    currentTime = *(word*)ptr;
    ptr += sizeof(word);
    
    totalFrames = *(word*)ptr;
    ptr += sizeof(word);
    
    numJoints = *(word*)ptr;
    ptr += sizeof(word);
    
    joint = new MS3Djoint[numJoints];
    for(i=0; i<numMaterials; i++){
        joint.flags = *(byte*)ptr;
        ptr += sizeof(byte);
        
        strncpy(joint.name, (char*)ptr, 32);
        ptr += sizeof(char)*32;
        
        strncpy(joint.parentName, (char*)ptr, 32);
        ptr += sizeof(char)*32;
        
        strncpy((char*)joint.rotation, (char*)ptr, sizeof(float)*3);
        ptr += sizeof(float)*3;
        
        strncpy((char*)joint.position, (char*)ptr, sizeof(float)*3);
        ptr += sizeof(float)*3;
        
        joint.numKeyFramesRot = *(word*)ptr;
        ptr += sizeof(word);
        
        joint.numKeyFramesTrans = *(word*)ptr;
        ptr += sizeof(word);
        
        joint.keyFramesRot = new MS3DkeyframeRot[joint.numKeyFramesRot];
        for(l=0; l<joint.numKeyFramesRot; l++){
            joint.keyFramesRot[l] = *(MS3DkeyframeRot*)ptr;
            ptr += sizeof(MS3DkeyframeRot);
        }
        
        joint.keyFramesTrans = new MS3DkeyframePos[joint.numKeyFramesTrans];
        for(l=0; l<joint.numKeyFramesTrans; l++){
            joint.keyFramesTrans[l] = *(MS3DkeyframePos*)ptr;
            ptr += sizeof(MS3DkeyframePos);
        }
    }
    
    delete [] buffer;
    
    return true;
}
- relpats_eht
Where did you got the struct you use from????
If you made your own it could help to pack the
structures (i had this problem with a bitmap)
i don'tknow if this is usefull be you could
ateast try it. Just define your structs like this:

#pragma pack(1)struct MY_STRUCT{  int MyInt;  int MyInt2;};#pragma pack()


I hope this helps.

Tjaalie,
if (*pYou == ASSHOLE) { pYou->Die(); delete pYou; };
Advertisement
that was in the spec, but i removed it because when it was there, the program wouldnt run.
- relpats_eht
Personally I use #include <pshpack1.h> and #include <poppack.h> to force byte-alignment and restore it repectively, I'm not sure if #pragma is well supported so that may have been why it didn't run.

As far as I can see the code looks fine, the only thing is reading in the buffer, I use a reinterpret_case<char*> to read with but that's about all that's different. I do have some code here that loads in files and works, if you would like a look at it then just shout.
--
Cheers,
Darren Clark
i also tried the include thing, forgot to mention it.

sure, ill take a look, you can post it here or send it to relpatseht [at] gmail [dot] com

[Edited by - relpats_eht on July 13, 2005 8:29:07 AM]
- relpats_eht
Mailed. Let me know if you have any problems :)
--
Cheers,
Darren Clark
Advertisement
thnx, ill see if i can get that up.
- relpats_eht
i decided to go with fixing mine, and finally found all the problems, so mine works now. of course, it doesnt have skeletal animation yet, but it will soon enough. when im done, i may post it up here or something...
- relpats_eht
Glad to hear you've got it working :) What was the problem if you don't mind me asking?
--
Cheers,
Darren Clark
mainly a lack of [l] while setting triangleIndices in the meshes, and there were a few varialbes i had set to the wrong type.
- relpats_eht

This topic is closed to new replies.

Advertisement