1.获得一个obj模型文件(可以使用3dmax等建模软件制作,亦可直接百度下载)
2.
1)下载ActivePerl(百度下载)
http://download.csdn.net/detail/zhimou/8450071
2)下载obj2opengl(可以将.obj文件转化成.h)
http://download.csdn.net/detail/ggtaas/4998714
3.将obj2opengl.pl和你需要转换的obj放到C:\Perl\bin目录中
(这里我们以banana.obj为例)
4.运行命令提示符,定位到C:\Perl\bin目录下,输入命令:perl obj2opengl.pl banana.obj
这时候你会发现,bin文件夹中自动生成了banana.h文件。
5.修改代码
这部分有点繁,等哪天有空了再写教程,先放上代码…
1)仿照Teapot.java,新建一个Banana.java
主要任务是要替换模型数据,由于数据量大,这里使用了文件操作,我将verts, norms, texcoords三个模型数据放在文件中分别导入
同时要注意的是,TeaPot的实现有4个参数,而使用obj2opengl导出的.h数据中只有3个参数,缺少indices数据,不过不影响模型的显示,Sample中的Building也是只有3个参数,但具体原理我就不太懂了,跟opengl的知识有关,还望大神指教
/*===============================================================================
Copyright (c) 2012-2014 Qualcomm Connected Experiences, Inc. All Rights Reserved.
Vuforia is a trademark of QUALCOMM Incorporated, registered in the United States
and other countries. Trademarks of QUALCOMM Incorporated are used with permission.
===============================================================================*/
package com.qualcomm.vuforia.samples.SampleApplication.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import android.content.res.AssetManager;
import android.util.Log;
public class Banana extends MeshObject
{
private static final String LOGTAG = "Banana";
private Buffer mVertBuff;
private Buffer mTexCoordBuff;
private Buffer mNormBuff;
private Buffer mIndBuff;
private int indicesNumber = 0;
private int verticesNumber = 0;
private AssetManager assetManager;
public Banana(AssetManager inputassetManager)
{
this.assetManager = inputassetManager;
setVerts();
setTexCoords();
setNorms();
}
double[] banana_VERTS;
double[] banana_TEX_COORDS;
double[] banana_NORMS;
InputStream inputFile = null;
private int loadVertsFromModel(String fileName)
throws IOException
{
try
{
inputFile = assetManager.open(fileName);
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputFile));
String line = reader.readLine();
//ex.24168 3 floatsdata in 1 line
int floatsToRead = Integer.parseInt(line);
//int floatsToRead = 1000;
banana_VERTS = new double[3*floatsToRead];
for (int i = 0; i < floatsToRead; i++)
{
String curline = reader.readLine();
if( curline.indexOf('/') >= 0 ){
i--;
continue;
}
//split 1 line to 3 data
String floatStrs[] = curline.split(",");
banana_VERTS[3*i] = Float.parseFloat(floatStrs[0]);
banana_VERTS[3*i+1] = Float.parseFloat(floatStrs[1]);
banana_VERTS[3*i+2] = Float.parseFloat(floatStrs[2]);
Log.d(LOGTAG, ""+i);
}
return floatsToRead;
} finally
{
if (inputFile != null)
inputFile.close();
}
}
private int loadTexCoordsFromModel(String fileName)
throws IOException
{
try
{
inputFile = assetManager.open(fileName);
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputFile));
String line = reader.readLine();
//ex.24168 3 floatsdata in 1 line
int floatsToRead = Integer.parseInt(line);
//int floatsToRead = 1000;
banana_TEX_COORDS = new double[2*floatsToRead];
for (int i = 0; i < floatsToRead; i++)
{
String curline = reader.readLine();
if( curline.indexOf('/') >= 0 ){
i--;
continue;
}
//split 1 line to 2 data
String floatStrs[] = curline.split(",");
banana_TEX_COORDS[2*i] = Float.parseFloat(floatStrs[0]);
banana_TEX_COORDS[2*i+1] = Float.parseFloat(floatStrs[1]);
//banana_TEX_COORDS[3*i+2] = Float.parseFloat(floatStrs[2]);
}
return floatsToRead;
} finally
{
if (inputFile !=