Jumat, 12 Juni 2015

Garden 3ds max

Pagi rekan-rekan sekalian...pagi ini cerah sekali...sangat tepat untuk jalan-jalan di taman (garden)...tapi gardennya kita buat dulu ya...

Kali ini aku akan memberi contoh objek 3D yang aku buat dengan menggunakan  3ds max, lalu objek 3D tersebut aku tampilkan dengan program grafik OpenGL. Teori tentang face telah aku bahas di artikel sebelumnya, sehingga aku tidak akan mengulangnya lagi.


Gambar di atas adalah gambar garden yang aku buat dengan 3ds max.



Setelah dikonversi menggunakan script 3ds max, maka garden bisa ditampilkan dengan menggunakan OpenGL, hasilnya seperti yang rekan-rekan lihat pada gambar di atas.


Dengan menggunakan 3ds max maka rekan-rekan bisa membuat objek 3D game apa saja, tetapi harus diperhitungankan jumlah face yang diperlukan, karena objek 3D game biasanya menggunakan poligon yang sedikit (low) face agar proses render ke layar monitor menjadi cepat.

Berikut ini adalah proses load objek garden 3D.

bool Load_All_Data()
{
    if (!Load_3_Daftar("garden_Mesh/BungaDahan.Meshtxt", 2.7f, BungaDahan))
        return false;
    if (!Load_3_Daftar("garden_Mesh/BungaDaun.Meshtxt", 2.7f, BungaDaun))
        return false;

    if (!Load_3_Daftar("garden_Mesh/Dahan.Meshtxt", 2.7f, Dahan))
        return false;
    if (!Load_3_Daftar("garden_Mesh/Daun.Meshtxt", 2.7f, Daun))
        return false;

    if (!Load_3_Daftar("garden_Mesh/Rumput.Meshtxt", 2.7f, Rumput))
        return false;

    if (!Load_3_Daftar("garden_Mesh/PagarX.Meshtxt", 2.7f, PagarX))
        return false;

    if (!Load_3_Daftar("garden_Mesh/Pagar.Meshtxt", 2.7f, Pagar))
        return false;

    if (!Load_3_Daftar("garden_Mesh/ground.Meshtxt", 2.7f, Ground))
        return false;

    return true;
}

bool Load_3_Daftar(char *FileName, float skala, Mesh3D_Struct& Object_Mesh3D)
{
    FILE *file;
    errno_t err;
    int i;

   
    if ((err = fopen_s(&file, FileName, "r")) != 0)
    {
        MessageBox(NULL, _T("Mesh3D load error"), _T(" .MeshTxt File tidak ditemukan!"), MB_OK);
        return false;
    }

    fscanf_s(file, "%d", &Object_Mesh3D.TotalF_Mesh3D);
    fscanf_s(file, "%d", &Object_Mesh3D.TotalTK_Mesh3D);
    fscanf_s(file, "%d", &Object_Mesh3D.TotalV_Mesh3D);
   
    //siapkan tempat untuk data mesh
    Object_Mesh3D.Face_Mesh3D = new Face[Object_Mesh3D.TotalF_Mesh3D];
    Object_Mesh3D.TK_Mesh3D = new TK[Object_Mesh3D.TotalTK_Mesh3D];
    Object_Mesh3D.V_Mesh3D = new Vertex[Object_Mesh3D.TotalV_Mesh3D];

   
    //load Face
    for (i = 0; i < Object_Mesh3D.TotalF_Mesh3D; i++)
    {
        fscanf_s(file, "%d", &Object_Mesh3D.Face_Mesh3D[i].indexV1);
        fscanf_s(file, "%d", &Object_Mesh3D.Face_Mesh3D[i].indexV2);
        fscanf_s(file, "%d", &Object_Mesh3D.Face_Mesh3D[i].indexV3);

        fscanf_s(file, "%d", &Object_Mesh3D.Face_Mesh3D[i].indexTK1);
        fscanf_s(file, "%d", &Object_Mesh3D.Face_Mesh3D[i].indexTK2);
        fscanf_s(file, "%d", &Object_Mesh3D.Face_Mesh3D[i].indexTK3);

        Object_Mesh3D.Face_Mesh3D[i].indexV1--;
        Object_Mesh3D.Face_Mesh3D[i].indexV2--;
        Object_Mesh3D.Face_Mesh3D[i].indexV3--;

        Object_Mesh3D.Face_Mesh3D[i].indexTK1--;
        Object_Mesh3D.Face_Mesh3D[i].indexTK2--;
        Object_Mesh3D.Face_Mesh3D[i].indexTK3--;
    }


    //load TK
    for (i = 0; i < Object_Mesh3D.TotalTK_Mesh3D; i++)
    {
        fscanf_s(file, "%f", &Object_Mesh3D.TK_Mesh3D[i].s);
        fscanf_s(file, "%f", &Object_Mesh3D.TK_Mesh3D[i].t);
    }

    //load Vertex
    for (i = 0; i < Object_Mesh3D.TotalV_Mesh3D; i++)
    {
        fscanf_s(file, "%f", &Object_Mesh3D.V_Mesh3D[i].X);
        fscanf_s(file, "%f", &Object_Mesh3D.V_Mesh3D[i].Y);
        fscanf_s(file, "%f", &Object_Mesh3D.V_Mesh3D[i].Z);

        //skala Mesh3D
        Object_Mesh3D.V_Mesh3D[i].X = Object_Mesh3D.V_Mesh3D[i].X * skala;
        Object_Mesh3D.V_Mesh3D[i].Y = Object_Mesh3D.V_Mesh3D[i].Y * skala;
        Object_Mesh3D.V_Mesh3D[i].Z = Object_Mesh3D.V_Mesh3D[i].Z * skala;

        ///RotasiX(-180.0, Object_Mesh3D.V_Mesh3D[i].X,
               Object_Mesh3D.V_Mesh3D[i].Y,
               Object_Mesh3D.V_Mesh3D[i].Z);
    }

    fclose(file);

    return true;
}

Menampilkan garden 3D.

void Display_3_Daftar(Mesh3D_Struct Object_Mesh3D, int iDisplay_Polygon)
{
    int i;

    for (i = 0; i < Object_Mesh3D.TotalF_Mesh3D; i++)
    {
        if (iDisplay_Polygon == 1)
            glBegin(GL_TRIANGLES);
        else
            glBegin(GL_LINE_LOOP);

        glTexCoord2d(Object_Mesh3D.TK_Mesh3D[Object_Mesh3D.Face_Mesh3D[i].indexTK1].s,
                             Object_Mesh3D.TK_Mesh3D[Object_Mesh3D.Face_Mesh3D[i].indexTK1].t);
        glVertex3d(Object_Mesh3D.V_Mesh3D[Object_Mesh3D.Face_Mesh3D[i].indexV1].X,
                           Object_Mesh3D.V_Mesh3D[Object_Mesh3D.Face_Mesh3D[i].indexV1].Y,
                           Object_Mesh3D.V_Mesh3D[Object_Mesh3D.Face_Mesh3D[i].indexV1].Z);

        glTexCoord2d(Object_Mesh3D.TK_Mesh3D[Object_Mesh3D.Face_Mesh3D[i].indexTK2].s,
                             Object_Mesh3D.TK_Mesh3D[Object_Mesh3D.Face_Mesh3D[i].indexTK2].t);
        glVertex3d(Object_Mesh3D.V_Mesh3D[Object_Mesh3D.Face_Mesh3D[i].indexV2].X,
                           Object_Mesh3D.V_Mesh3D[Object_Mesh3D.Face_Mesh3D[i].indexV2].Y,
                           Object_Mesh3D.V_Mesh3D[Object_Mesh3D.Face_Mesh3D[i].indexV2].Z);

        glTexCoord2d(Object_Mesh3D.TK_Mesh3D[Object_Mesh3D.Face_Mesh3D[i].indexTK3].s,
                             Object_Mesh3D.TK_Mesh3D[Object_Mesh3D.Face_Mesh3D[i].indexTK3].t);
        glVertex3d(Object_Mesh3D.V_Mesh3D[Object_Mesh3D.Face_Mesh3D[i].indexV3].X,
                           Object_Mesh3D.V_Mesh3D[Object_Mesh3D.Face_Mesh3D[i].indexV3].Y,
                           Object_Mesh3D.V_Mesh3D[Object_Mesh3D.Face_Mesh3D[i].indexV3].Z);
        glEnd();
    }

    return;
}

Selesai deh pembahasan tentang objek 3D yang dibuat dengan 3ds max di artikel lain ogut akan memberi contoh cara untuk membuat object 3D dengan 3ds max lalu mengkonversinya, semoga bermanfaat dan selamat belajar.

Salam


Heriady
heriady.yoh@gmail.com


Tidak ada komentar:

Posting Komentar