2011年3月5日 星期六

BoneEdit - 模型

c:我說明一下,這邊使用到的模型檔為MMD
路人:哦,有什麼意義嗎?
c:沒什麼,就只是一個副檔名
路人:之前你有提到,是從warcraft III提取出來的,
     我記得是叫MDX的模型檔,那從MDX轉MMD是怎麼弄啊?
c:就...一個小工具,然後.....你先看看吧.
路人:怎麼回事啊...
c:你看了就明白了.

路人:這是在做啥?為什麼不是那種按一個鈕就完成的
c:呃,手工的,要自己開檔找出檔案內有VRTX,PUTX,UVBX等標籤,後面會跟著資料的位址.
路人:哇,真是辛苦你了啊,但也太爛了吧,不用介紹了.
C:所以重點不在這裡,還是來看看BoneEdit的模型類別吧.

class CMmd
{
public:
    CMmd();
    virtual ~CMmd();
    bool Load( char *filename );
    void Destroy( void );
    int MeshCount; //頂點資料數量
    int UVCount; //貼圖資料數量
    Vector *Mesh;      //頂點資料
    Vector *MeshCopy;      //頂點資料
    gUV *UV;   //貼圖資料
    Vector *Normal;//向量資料
    char FileName[256];//檔案名稱
}

最主要的功能其實就是把模型載入,附上Load的程式碼

bool CMmd::Load( char *filename ){
    FILE *f;
    strcpy(FileName, filename);
    f = fopen( filename, "rb" );
    char *p;

    if( f )
    {
        int i;
        if( Mesh )
            free( Mesh );
        if( MeshCopy )
            free( MeshCopy );
        if( UV )
            free( UV );
        if( Normal )
            free( Normal );
        fread( &MeshCount, sizeof(int), 1, f );
        Mesh = (Vector *)malloc(sizeof(Vector) * MeshCount );
        MeshCopy = (Vector *)malloc(sizeof(Vector) * MeshCount );
        for( i=0;i<MeshCount;i++)
            fread( Mesh[i], sizeof(Vector), 1, f );
        memcpy( MeshCopy, Mesh, sizeof(Vector) * MeshCount );
        fread( &UVCount, sizeof(int), 1, f );
        UV = (gUV *)malloc(sizeof(gUV) * UVCount );
        for( i=0;i<UVCount;i++)
        {
            fread( &UV[i].u, sizeof(float), 1, f );
            fread( &UV[i].v, sizeof(float), 1, f );
        }
        fclose( f );
        Normal = (Vector *)malloc(sizeof(Vector) * MeshCount );
        memcpy(Normal,Mesh,sizeof(Vector) * MeshCount);
        for( i=0;i<MeshCount;i++)
        {
            Normal[i].Normalize();
        }
    }
    return true;
}

沒有留言:

張貼留言