export/import C/C++ Source examples

Discussion in 'Models, Animations, and Particle Effects' started by XVicious, Jul 12, 2013.

Remove all ads!
  1. XVicious

    XVicious Established Member

    Joined:
    Jun 21, 2013
    Messages:
    427
    Likes Received:
    8

    This thread is dedicated to those interested in seeing source
    code examples of my import/export tools I am using for TOEE
    model modifications


    I am going to post examples of source code in C/C++ here for future reference and development
    of tool creations or updates


    [​IMG]
     
  2. XVicious

    XVicious Established Member

    Joined:
    Jun 21, 2013
    Messages:
    427
    Likes Received:
    8
    my first topic will be SKM data structure design
    this means I am defining what kind of memory space I want to use
    for the functionality of the tool.

    file: SKM.cpp

    #include <iostream.h>
    #include <stddef.h>

    typedef struct XYZi_
    {
    unsigned short i;
    }XYZi_;

    typedef struct XYZ_
    {
    float x,y,z,w;
    }XYZ_;

    typedef struct Texture_
    {
    float x,y,z,u;
    }Normal_;

    typedef struct ModelData_
    {
    XYZ_ Vect;
    XYZ_ Norm;
    Texture_ Texel;
    }ModelData_;



    namespace vect
    {
    typedef float DataT_;
    typedef int DataT_Key;
    #include <list_.h>
    };

    namespace vect3
    {
    typedef XYZ_ DataT_;
    typedef int DataT_Key;
    #include <list_.h>
    };

    namespace vect3i
    {
    typedef XYZi_ DataT_;
    typedef int DataT_Key;
    #include <list_.h>
    };

    namespace vect2
    {
    typedef Texture_ DataT_;
    typedef int DataT_Key;
    #include <list_.h>
    };

    namespace ModelSD
    {
    typedef ModelData_ DataT_;
    typedef int DataT_Key;
    #include <list_.h>
    };

    typedef struct SKMHeader_T
    {
    unsigned long count;
    unsigned long offset;
    }SKMHeader_T;


    typedef struct SKMData
    {
    SKMHeader_T HData[4];
    //WORD 2 bytes
    //DWORD 4 bytes
    vect3::List_ Vertdata_; //Vertex data size: DWORD
    vect3i::List_ Facedata_; //index data size: Word
    vect3::List_ Normdata_;
    vect2::List_ TexelCoords_;

    ModelSD::List_ Model_;

    }SKMData;

    #include <fstream.h>

    //global data or memory space//
    SKMData GData;
    SKMData Bones;


    @foot note:
    notice the structure data is defined, then used in a container class name space for
    list, queue's, or stacks defined by < List_.h >
    in all the conversion parse tools I used the Queue feature, but I had designed the queue
    to allow foward or backward itoration access.
    List_.h allows dynamic memory allocation using Link List format using DataT_ def as
    the data type used in the list.
    This basically means that I dont start with a determined
    or set memory Size or array, as the file is opened and parsed, the memory is allocated step by step, instead of all at once.

    the use is similar to vector.h standard C++, but in this case the data type is defined
    in a namespace instead of using a template type.

    file: SKM.cpp: main():

    #ifndef MAIN__1.0


    int main(int argv, char * argc[])
    {

    if(argv == 2){
    char buffn[255];
    strcpy(buffn, argc[1]);
    LoadRaDATA(buffn);
    cout<<"\nfile converted!";
    cout<<endl;

    system("pause");
    return 0;
    }
    else{
    printf("skm file input parameter needed\ninput: <exe> <filname.skm>\nOutput:<filename.skm.an8>\n");
    system("pause");
    return 1;}

    }

    #endif

    @footnote2:
    after the main function is terminated and returns, all memory is freed from all the queue's
    via class default de-construction
     
    Last edited: Jul 12, 2013
  3. XVicious

    XVicious Established Member

    Joined:
    Jun 21, 2013
    Messages:
    427
    Likes Received:
    8
    Next topic is the function body definition of Load random access data for the SKM
    file. this function opens / reads the file input SKM via file path parameter. Parsing
    through the file saving the global data step by step, line by line.
    the next step in this function is to save and output a converted SKM file in the format
    of anim8or ".an8"
    next step is; saving bone data in memory to a back up file extension ".SKB" (used later
    to be restored in an SKM file)

    int LoadRaDATA(char *filen)
    {
    int tempf[2];
    fstream dfile;
    fstream ofile;
    dfile.open(filen, ios::in | ios::eek:ut | ios::ate | ios::binary );
    dfile.seekg(0);

    char Buff[255];
    string fname1;
    fname1.assign(filen);
    fname1.append(".an8");
    ofile.open((char*)fname1.data(), ios::in | ios::eek:ut | ios::trunc | ios::ate );
    ofile.seekg(0);

    XYZ_ tempxyz;
    int count;

    dfile.read(reinterpret_cast<char *>(&GData.HData),4*8);

    printf("\nbones: %d materials: %d points: %d faces: %d \n", GData.HData[0].count,GData.HData[1].count,GData.HData[2].count,GData.HData[3].count);
    printf("\n@bit : %d @bit : %d @bit : %d @bit : %d \n", GData.HData[0].offset,GData.HData[1].offset,GData.HData[2].offset,GData.HData[3].offset);

    dfile.seekg(GData.HData[2].offset);
    int pos = dfile.tellg(); float tempfl;
    Texture_ TempTexel;

    count = 0;
    while( pos < GData.HData[3].offset && count/2 < GData.HData[2].count){


    ++count;

    dfile.read(reinterpret_cast<char *>(&tempxyz),4*4);
    if(count % 2 == 1)
    GData.Vertdata_.enQueue(tempxyz);

    if(count % 2 == 0)
    Bones.Vertdata_.enQueue(tempxyz);

    dfile.read(reinterpret_cast<char *>(&tempxyz),4*4);
    if(count % 2 == 1)
    GData.Normdata_.enQueue(tempxyz);

    if(count % 2 == 0)
    Bones.Normdata_.enQueue(tempxyz);

    dfile.read(reinterpret_cast<char *>(&TempTexel),4*2);
    if(count % 2 == 1)
    GData.TexelCoords_.enQueue(TempTexel);

    if(count % 2 == 0)
    Bones.TexelCoords_.enQueue(TempTexel);



    pos = dfile.tellg();
    }

    printf("\ncount: %d\n",count/2);
    //system("pause");

    XYZi_ tempI; count = 0;
    dfile.clear();
    dfile.seekg(GData.HData[3].offset, ios::beg);
    while(!dfile.fail() )
    {
    ++count;

    //id
    short id;
    dfile.read(reinterpret_cast<char *>(&id),2);


    dfile.read(reinterpret_cast<char *>(&tempI.i),2);
    GData.Facedata_.enQueue(tempI);

    dfile.read(reinterpret_cast<char *>(&tempI.i),2);
    if(!dfile.eof())
    GData.Facedata_.enQueue(tempI);

    dfile.read(reinterpret_cast<char *>(&tempI.i),2);
    if(!dfile.eof())
    GData.Facedata_.enQueue(tempI);


    //pos = dfile.tellg();
    //printf("id:%d :",id);

    }

    //.AN8 file format heading inits
    ofile<<"header {\n version { \"0.95\" }\n build { \"2007.04.02\" }\n}\nenvironment {\n grid { 0 5 10 50 }\n";
    ofile<<" framerate { 24 }\n}\nobject { \"Convert_1.0\"\n mesh {\n name { \"mesh01\" }\n base {\n";
    ofile<<" origin { (0 0 0) }\n }\n material { \" -- default --\" }\n smoothangle { 45 }\n";
    ofile<<" /* "<<GData.HData[2].count<<" points, "<<GData.HData[3].count<<" faces, "<<GData.HData[2].count<<" uvCoords */\n";
    ofile<<" materiallist {\n materialname { \" -- default --\" }\n }";


    ofile<<"\n points {";

    GData.Vertdata_.iEnd(); count = 0;
    do{

    if(count != 0) GData.Vertdata_.gPrev();


    ++count; if(count%3==1) ofile<<endl<<" ";
    ofile<<"("
    <<GData.Vertdata_.ShowData(GData.Vertdata_.gItorator()).x<<" "
    <<GData.Vertdata_.ShowData(GData.Vertdata_.gItorator()).y<<" "
    <<GData.Vertdata_.ShowData(GData.Vertdata_.gItorator()).z
    <<") ";


    //if(count % 50 == 0)
    //system("pause");
    }while(GData.Vertdata_.HasPrev());


    ofile<<"\n }";
    ofile<<"\n texcoords {";

    GData.TexelCoords_.iEnd(); count = 0;
    do
    {
    if(count != 0) GData.TexelCoords_.gPrev();

    ++count; if(count%3==1) ofile<<"\n ";
    ofile<<"("
    <<GData.TexelCoords_.ShowData(GData.TexelCoords_.gItorator()).x<<" "
    <<GData.TexelCoords_.ShowData(GData.TexelCoords_.gItorator()).y<<")"
    <<" ";

    }while(GData.TexelCoords_.HasPrev());


    ofile<<"\n }";
    ofile<<"\n faces {";

    dfile.clear();
    GData.Facedata_.iEnd(); count = 0;
    do{

    ++count; if(count%3 == 1 && count != 1) ofile<<")\n 4 4 0 -1 ( "; if(count == 1) ofile<<"\n 3 4 0 -1 ( ";
    ofile<<"("<<GData.Facedata_.ShowData(GData.Facedata_.gItorator()).i<<" "<<GData.Facedata_.ShowData(GData.Facedata_.gItorator()).i<<") ";
    GData.Facedata_.gPrev();
    }while(GData.Facedata_.HasPrev());


    ofile<<")\n }\n }\n}";

    printf("\ncount: %d\n",count);
    //system("pause");

    int L; GData.Facedata_.iEnd();
    do
    {

    L=0; GData.Vertdata_.iEnd(); GData.Normdata_.iEnd(); GData.TexelCoords_.iEnd();
    while(GData.Vertdata_.HasPrev() && (L < GData.Facedata_.ShowData(GData.Facedata_.gItorator()).i) ){++L;
    GData.Vertdata_.gPrev(); GData.Normdata_.gPrev(); GData.TexelCoords_.gPrev();}

    ModelData_ TempModel;
    TempModel.Vect = GData.Vertdata_.ShowData(GData.Vertdata_.gItorator());
    TempModel.Norm = GData.Normdata_.ShowData(GData.Normdata_.gItorator());
    TempModel.Texel = GData.TexelCoords_.ShowData(GData.TexelCoords_.gItorator());

    GData.Model_.enQueue(TempModel);
    GData.Facedata_.gPrev();
    }while(GData.Facedata_.HasPrev());

    ofile.close();
    dfile.close();

    string bonestr;
    bonestr.assign(filen);
    bonestr.append(".SKB");
    ofile.open((char*)bonestr.data(),ios::in | ios::eek:ut | ios::trunc | ios::binary);
    ofile.seekp(0,ios::beg);

    Bones.Vertdata_.iEnd(); Bones.Normdata_.iEnd(); Bones.TexelCoords_.iEnd();
    count = 0;
    do{

    if(count != 0){
    Bones.Vertdata_.gPrev();Bones.Normdata_.gPrev();Bones.TexelCoords_.gPrev();
    }


    ofile.write(reinterpret_cast<char*>(&Bones.Vertdata_.ShowData(Bones.Vertdata_.gItorator()) ), 4*4);
    ofile.write(reinterpret_cast<char*>(&Bones.Normdata_.ShowData(Bones.Normdata_.gItorator()) ), 4*4);
    ofile.write(reinterpret_cast<char*>(&Bones.TexelCoords_.ShowData(Bones.TexelCoords_.gItorator()) ), 4*2);
    ++count;

    }while(Bones.Vertdata_.HasPrev());

    ofile.close();

    return 1;
    }
     
    Last edited: Jul 12, 2013
Our Host!