Babysteps Towards Model Loading

Sage
Posts: 1,066
Joined: 2004.07
Post: #1
I've been working on rolling a loader for .OBJ models for a couple days but am not familiar yet with file and stream functions. I'm getting there. So while creating my mesh class, to see if it worked, I decided to quickly make an XML model file. As it turns out, the XML model isn't that bad. I'm sure it'd suck for anything large scale. If only I could convert .OBJ files to XML, but I'd have to know file and stream functions and then I might as well not use XML.

The focus of this post is simply to see if anyone can help me take what I've done with my XML loader and shift it to loading .OBJ files directly. A side point of this post is to see if anyone would actually want the code to the loader. I guess I'll just throw it here so that someone might be able to help me.

Also, is (or rather was) this worth my time to check the mesh class rather than pushing forward into learning stream and file functions? I still plan to as XML files are hell to write and are quite large. Just wondering.

Code:
int LoadXML(Mesh &theMesh, const char *filename, bool output)
{
    TiXmlDocument    modelDoc;
    TiXmlElement    *txmesh;
    TiXmlElement    *txvertex;
    TiXmlElement    *txface;
    int                CurrentVertex = 0;
    int                CurrentFace = 0;
    int                x,y,z,a,b,c,d,e,f,g,h;
    Vertex            tempVertex;
    Face            tempFace;
    
    if (!modelDoc.LoadFile(filename))
    {
        cout << "Failed to open " << filename << ". Model not loaded." << endl;
        return -1;
    }
    
    txmesh = modelDoc.RootElement();
    txvertex=txmesh->FirstChildElement("Vertex");
    
    while (txvertex)
    {
        txvertex->Attribute("x", &x);
        txvertex->Attribute("y", &y);
        txvertex->Attribute("z", &z);
        tempVertex.SetX(x);
        tempVertex.SetY(y);
        tempVertex.SetZ(z);
        theMesh.SetV(CurrentVertex,tempVertex);
        if(output)
        {
            cout << "Vertex " << CurrentVertex << ":" << endl;
            cout << "     Should Be   Is" << endl;
            cout << "       x: " << x << "  ~~  " << theMesh.GetV(CurrentVertex).GetX() << endl
                << "       y: "  << y << "  ~~  " << theMesh.GetV(CurrentVertex).GetY() << endl
                << "       z: "  << z << "  ~~  " << theMesh.GetV(CurrentVertex).GetZ() << endl;
        }
        CurrentVertex++;
        txvertex=txvertex->NextSiblingElement();
    }
    
    theMesh.SetNV(CurrentVertex);
    
    txface=txmesh->FirstChildElement("Face");
    
    while (txface)
    {
        txface->Attribute("v1", &a);
        txface->Attribute("v2", &b);
        txface->Attribute("v3", &c);
        txface->Attribute("v4", &d);
        txface->Attribute("vt1", &e);
        txface->Attribute("vt2", &f);
        txface->Attribute("vt3", &g);
        txface->Attribute("vt4", &h);
        tempFace.SetVI(0,a-1);
        tempFace.SetVI(1,b-1);
        tempFace.SetVI(2,c-1);
        tempFace.SetVI(3,d-1);
        tempFace.SetVTI(0,e-1);
        tempFace.SetVTI(1,f-1);
        tempFace.SetVTI(2,g-1);
        tempFace.SetVTI(3,h-1);
        theMesh.SetF(CurrentFace,tempFace);
        if(output)
        {
            cout << endl << endl;
            cout << "Face " << CurrentFace << ":" << endl;
            cout << "       v1: " << theMesh.GetF(CurrentFace).GetVI(0) << endl
                << "       v2: " << theMesh.GetF(CurrentFace).GetVI(1) << endl
                << "       v3: " << theMesh.GetF(CurrentFace).GetVI(2) << endl
                << "       v4: " << theMesh.GetF(CurrentFace).GetVI(3) << endl;
            cout << "      vt1: " << theMesh.GetF(CurrentFace).GetVTI(0) << endl
                << "      vt2: " << theMesh.GetF(CurrentFace).GetVTI(1) << endl
                << "      vt3: " << theMesh.GetF(CurrentFace).GetVTI(2) << endl
                << "      vt4: " << theMesh.GetF(CurrentFace).GetVTI(3) << endl;
        }
        CurrentFace++;
        txface=txface->NextSiblingElement();
    }
    
    theMesh.SetNF(CurrentFace);
    
    if(output)
    {
        cout << endl << endl;
        cout << "Mesh:" << endl
        << "       Number of Vertices: " << theMesh.GetNV() << endl
        << "          Number of Faces: " << theMesh.GetNF() << endl;
    }
    
    return 0;    
}
Code:
<Mesh>
    <Vertex x="-3" y="7" z="3"/>
    <Vertex x="-3" y="13" z="3"/>
    <Vertex x="3" y="13" z="3"/>
    <Vertex x="3" y="7" z="3"/>
    <Vertex x="-3" y="7" z="-3"/>
    <Vertex x="-3" y="13" z="-3"/>
    <Vertex x="3" y="13" z="-3"/>
    <Vertex x="3" y="7" z="-3"/>
    
    <Face v1="3" vt1="0"
            v2="2" vt2="0"
            v3="1" vt3="0"
            v4="4" vt4="0"/>
    <Face v1="5" vt1="0"
            v2="1" vt2="0"
            v3="2" vt3="0"
            v4="6" vt4="0"/>
    <Face v1="6" vt1="0"
            v2="2" vt2="0"
            v3="3" vt3="0"
            v4="7" vt4="0"/>
    <Face v1="7" vt1="0"
            v2="3" vt2="0"
            v3="4" vt3="0"
            v4="8" vt4="0"/>
    <Face v1="8" vt1="0"
            v2="4" vt2="0"
            v3="1" vt3="0"
            v4="5" vt4="0"/>
    <Face v1="8" vt1="0"
            v2="5" vt2="0"
            v3="6" vt3="0"
            v4="7" vt4="0"/>
</Mesh>
Quote this message in a reply
Post Reply