Android Vuforia with jPCT-AE (3) – 載入 md2 測試

前兩篇介紹如何直接透過 jPCT-AE 直接繪圖以及載入 .obj 檔案,這邊繼續介紹 jPCT-AE 載入 md2 的方法,載入動作大致上類似,唯一不同的只有 texture 設定部份。

圖片來源:http://tw.freeimages.com/photo/3d-maze-2-1208096
圖片來源:http://tw.freeimages.com/photo/3d-maze-2-1208096

囉哩巴唆

這次沒有什麼好囉哩巴唆了,小蛙對於這些 3D model 的檔案類型是一知半解,只是看到 jPCT-AE 有這幾種 loader,一個一個測試過後把程式碼留下來。

下載需要的檔案

找了半天,要找到 md2 檔案真是有點困難,後來終於在 這裡 找到可以下載的 md2 模型。

載入 md2 模型

附上以下程式碼

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public ImageTargetRenderer(ImageTargets activity, SampleApplicationSession session) {
    mActivity = activity;
    vuforiaAppSession = session;
    world = new World();
    world.setAmbientLight(150, 150, 150);
    // set the following value according to your need, so the object won't be disappeared.
    world.setClippingPlanes(2.0f, 3000.0f);
    sun = new Light(world);
    sun.setIntensity(250, 250, 250);
    try{
        // loadMD2(檔案, 縮放);
        cylinder = Loader.loadMD2(mActivity.getAssets().open("Blade.md2"), 10);
        // 載入材質,材質名稱直接設定為檔案名稱,也可以修改
        TextureManager.getInstance().addTexture("Blade.jpg",
                new Texture(mActivity.getAssets().open("Blade.jpg")));
        // 設定使用名為 Blade.jpg 的材質 (就是上面設定的名字)
        cylinder.setTexture("Blade.jpg");
        cylinder.strip();
        cylinder.build();
        // X 軸方向翻轉
        cylinder.rotateX(1.5f);
        // 位移 x, y, z
        cylinder.translate(-100, 0, -200);
        world.addObject(cylinder);
        cam = world.getCamera();
        SimpleVector sv = new SimpleVector();
        sv.set(cylinder.getTransformedCenter());
        sv.y -= 100;
        sv.z -= 100;
        sun.setPosition(sv);
    }catch(Exception e){
        e.printStackTrace();
    }
    MemoryHelper.compact();
}

快點試試看,是不是能如下圖載入刀鋒戰士呢?

載入 md2 測試
載入 md2 測試

參考資料

2 則留言

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *