Vuforia如何添加纹理到原生android中
本文介绍如何纹理添加到原生的Android中和如何在运行时换纹理。
添加纹理的步骤
添加一个新的纹理是非常简单的。试用任何的示例都可以用一下步骤:
1、添加纹理图片到 assets 文件夹.
2、In the main activity class (e.g. ImageTargets.java) find the loadTextures() method. Add additional calls to mTextures.add for each of your image files. Note that the order in which the textures are added determines their order in the native texture array.
3、In the native renderFrame() method (e.g. in ImageTargets.cpp) find the point at which the texture object is obtained (textures[textureIndex]). Change the texture index to select the desired texture. The indices start at 0, where index 0 is the first texture added in the loadTextures() Java method.
在运行时添加纹理
有时,例如当该纹理图片从服务器下载,这时最好是在运行时添加。首先,我们需要一个方法来从位图加载,而不是从APK加载纹理。
The loadTextureFromApk() method in Texture.java can be easily modified for this purpose:
public static Texture loadTextureFromBitmap(Bitmap bitMap)
{
int[] data = new int[bitMap.getWidth() * bitMap.getHeight()];
bitMap.getPixels(data, 0, bitMap.getWidth(), 0, 0,
bitMap.getWidth(), bitMap.getHeight());
// Convert:
byte[] dataBytes = new byte[bitMap.getWidth() *
bitMap.getHeight() * 4];
for (int p = 0; p < bitMap.getWidth() * bitMap.getHeight(); ++p)
{
int colour = data[p];
dataBytes[p * 4] = (byte)(colour >>> 16); // R
dataBytes[p * 4 + 1] = (byte)(colour >>> 8); // G
dataBytes[p * 4 + 2] = (byte) colour; // B
dataBytes[p * 4 + 3] = (byte)(colour >>> 24); // A
}
Texture texture = new Texture();
texture.mWidth = bitMap.getWidth();
texture.mHeight = bitMap.getHeight();
texture.mChannels = 4;
texture.mData = dataBytes;
return texture;
}
Next, we need a native method for creating the OpenGL texture. Here is a version that can be added to ImageTargets.cpp:
Texture* myTexture = NULL;
JNIEXPORT void JNICALL
Java_com_qualcomm_QCARSamples_ImageTargets_ImageTargetsRenderer_createGLTextureNative(
JNIEnv* env, jobject obj, jobject textureObject)
{
if (textureObject != NULL)
{
myTexture = Texture::create(env, textureObject);
glGenTextures(1, &(myTexture->mTextureID));
glBindTexture(GL_TEXTURE_2D, myTexture->mTextureID);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, myTexture->mWidth,
myTexture->mHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE,
(GLvoid*) myTexture->mData);
}
}
When you are done with the native texture, be sure to call glDeleteTextures to delete the GL texture. Also delete the Texture object. You may also need to free the Android Bitmap using Bitmap.recycle().