Qualcomm Vuforia 教學 (5) – 替換茶壺 – Android JNI 版本

前面的文章提到為什麼要用 Android NDK 來開發,既然已經有了 Android 版本替換茶壺的文章,這邊記錄 Android NDK 版本的替換茶壺。

2016-07-07 更新:本篇為 3.x 版本,目前 Vuforia 以更新到 5.5.9,新版本使用方式請參考 

使用 Android NDK 來做的好處在上一篇文章已提過,C++ 在處理複雜模型上有不錯的表現,也不會出現 Java 宣告大小上限的問題,直接進入步驟。

1. 下載範例檔案 – cube.hcube.png (這個跟前面範例使用的是一樣的)

2. 將範例檔案 cube.h 放到  ImageTargetsNative 專案下的 jni 目錄

3. 將範例檔案 cube.png 放到 ImageTargetsNative 專案下的 assets 目錄

4. 開啟 ImageTargetsNative 專案下 jni 下的 ImageTargets.cpp

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
// 在上方加入
#include "cube.h"
// static const float kObjectScale = 3.f;
// 改成
static const float kObjectScale = 120.f;
// glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &teapotVertices[0]);
// 改成
glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &cubeVerts[0]);
// glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &teapotNormals[0]);
// 改成
glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &cubeNormals[0]);
// glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &teapotTexCoords[0]);
// 改成
glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &cubeTexCoords[0]);
// glDrawElements(GL_TRIANGLES, NUM_TEAPOT_OBJECT_INDEX, GL_UNSIGNED_SHORT, (const GLvoid*) &teapotIndices[0]);
// 改成
glDrawArrays(GL_TRIANGLES, 0, cubeNumVerts);

5. 開啟 ImageTargetsNative 專案下 jni 下的 ImageTargets.java

1
2
3
4
5
6
7
8
private void loadTextures()
{
        //mTextures.add(Texture.loadTextureFromApk("TextureTeapotBrass.png", getAssets()));
        mTextures.add(Texture.loadTextureFromApk("TextureTeapotBlue.png", getAssets()));
        mTextures.add(Texture.loadTextureFromApk("cube.png", getAssets()));
        mTextures.add(Texture.loadTextureFromApk("TextureTeapotRed.png", getAssets()));
        mTextures.add(Texture.loadTextureFromApk("Buildings.jpeg", getAssets()));
}

6. 執行 ndk-build (如果忘記可查看上一篇文章),重新整理 Eclipse 專案後執行。下圖是 cube.h 透過 OpenGL 畫出來的模型,上方是 cube.png 當材質繪製出來的,下方是原本範例中茶壺的貼圖。
018.png

發佈留言

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