Vuforia如何添加纹理到原生android中

huang0316 2013-12-20 11:37:24
本文介绍如何纹理添加到原生的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().
...全文
1764 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

2,852

社区成员

发帖
与我相关
我的任务
社区描述
本论坛以AI、WoS 、XR、IoT、Auto、生成式AI等核心板块组成,为开发者提供便捷及高效的学习和交流平台。 高通开发者专区主页:https://qualcomm.csdn.net/
人工智能物联网机器学习 技术论坛(原bbs) 北京·东城区
社区管理员
  • csdnsqst0050
  • chipseeker
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧