Skip to content
Browse files

Parsers now also take a String that specifies the path to the file on…

… the SD Card. Fixes issue #30
  • Loading branch information...
1 parent 6286a14 commit a791e20a107f6f52259f76063fa204aac3cc9536 MasDennis committed May 23, 2012
View
BIN build/rajawalilib.zip
Binary file not shown.
View
9 src/rajawali/BaseObject3D.java
@@ -602,13 +602,18 @@ public SerializedObject3D toSerializedObject3D() {
return ser;
}
- public BaseObject3D clone() {
+ public BaseObject3D clone(boolean copyMaterial) {
BaseObject3D clone = new BaseObject3D();
clone.getGeometry().copyFromGeometry3D(mGeometry);
clone.isContainer(mIsContainerOnly);
- clone.setMaterial(mMaterial, false);
+ if(copyMaterial) clone.setMaterial(mMaterial, false);
+ clone.mElementsBufferType = mGeometry.areOnlyShortBuffersSupported() ? GLES20.GL_UNSIGNED_SHORT : GLES20.GL_UNSIGNED_INT;
return clone;
}
+
+ public BaseObject3D clone() {
+ return clone(true);
+ }
public void setVisible(boolean visible) {
mIsVisible = visible;
View
3 src/rajawali/materials/AMaterial.java
@@ -5,6 +5,7 @@
import rajawali.Camera;
import rajawali.lights.ALight;
+import rajawali.materials.TextureManager.TextureType;
import rajawali.math.Number3D;
import rajawali.renderer.RajawaliRenderer;
import rajawali.util.RajLog;
@@ -235,7 +236,7 @@ public void addTexture(TextureInfo textureInfo, boolean isExistingTexture) {
+ textureName + ", " + textureInfo.getTextureType());
}
textureInfo.setUniformHandle(textureHandle);
- mUseColor = false;
+ if(textureInfo.getTextureType() != TextureType.SPHERE_MAP) mUseColor = false;
if(!isExistingTexture) {
mTextureInfoList.add(textureInfo);
mNumTextures++;
View
25 src/rajawali/parser/AParser.java
@@ -1,21 +1,31 @@
package rajawali.parser;
+import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import rajawali.BaseObject3D;
import rajawali.animation.mesh.AAnimationObject3D;
import rajawali.materials.TextureManager;
+import rajawali.renderer.RajawaliRenderer;
import android.content.res.Resources;
+import android.os.Environment;
public abstract class AParser implements IParser {
protected TextureManager mTextureManager;
protected Resources mResources;
protected int mResourceId;
+ protected String mFileOnSDCard;
+ protected File mFile;
protected BaseObject3D mRootObject;
+ public AParser(RajawaliRenderer renderer, String fileOnSDCard) {
+ this(renderer.getContext().getResources(), renderer.getTextureManager(), 0);
+ mFileOnSDCard = fileOnSDCard;
+ }
+
public AParser(Resources resources, TextureManager textureManager, int resourceId) {
mTextureManager = textureManager;
mResources = resources;
@@ -24,6 +34,10 @@ public AParser(Resources resources, TextureManager textureManager, int resourceI
}
public void parse() {
+ if(mFileOnSDCard != null) {
+ File sdcard = Environment.getExternalStorageDirectory();
+ mFile = new File(sdcard, mFileOnSDCard);
+ }
}
public BaseObject3D getParsedObject() {
@@ -70,6 +84,17 @@ protected float readFloat(InputStream stream) throws IOException {
public String bumpTexture;
}
+ protected String getOnlyFileName(String fileName) {
+ String fName = new String(fileName);
+ int indexOf = fName.lastIndexOf("\\");
+ if(indexOf > -1)
+ fName = fName.substring(indexOf + 1, fName.length());
+ indexOf = fName.lastIndexOf("/");
+ if(indexOf > -1)
+ fName = fName.substring(indexOf + 1, fName.length());
+ return fName.toLowerCase().replaceAll("\\s", "_");
+ }
+
protected String getFileNameWithoutExtension(String fileName) {
String fName = fileName.substring(0, fileName.lastIndexOf("."));
int indexOf = fName.lastIndexOf("\\");
View
43 src/rajawali/parser/MD2Parser.java
@@ -2,6 +2,9 @@
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Stack;
@@ -12,7 +15,9 @@
import rajawali.animation.mesh.VertexAnimationObject3D;
import rajawali.materials.DiffuseMaterial;
import rajawali.materials.TextureManager;
+import rajawali.renderer.RajawaliRenderer;
import rajawali.util.LittleEndianDataInputStream;
+import rajawali.util.RajLog;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
@@ -27,6 +32,10 @@
private int[] mIndices;
private float[] mTextureCoords;
+ public MD2Parser(RajawaliRenderer renderer, String fileOnSDCard) {
+ super(renderer, fileOnSDCard);
+ }
+
public MD2Parser(Resources resources, TextureManager textureManager, int resourceId) {
super(resources, textureManager, resourceId);
}
@@ -38,8 +47,20 @@ public AAnimationObject3D getParsedAnimationObject() {
@Override
public void parse() {
- InputStream fileIn = mResources.openRawResource(mResourceId);
- BufferedInputStream stream = new BufferedInputStream(fileIn);
+ super.parse();
+ BufferedInputStream stream = null;
+ if(mFile == null) {
+ InputStream fileIn = mResources.openRawResource(mResourceId);
+ stream = new BufferedInputStream(fileIn);
+ } else {
+ try {
+ stream = new BufferedInputStream(new FileInputStream(mFile));
+ } catch (FileNotFoundException e) {
+ RajLog.e("["+getClass().getCanonicalName()+"] Could not find file.");
+ e.printStackTrace();
+ }
+ }
+
mObject = new VertexAnimationObject3D();
mObject.setFps(10);
@@ -88,15 +109,29 @@ private void getMaterials(BufferedInputStream stream, byte[] bytes)
skinPath = skinPath.substring(skinPath.lastIndexOf("/") + 1,
skinPath.length());
StringBuffer textureName = new StringBuffer(skinPath.toLowerCase());
+ mCurrentTextureName = textureName.toString().trim();
+ if(mFile != null) continue;
int dotIndex = textureName.lastIndexOf(".");
if (dotIndex > -1)
textureName = new StringBuffer(textureName.substring(0, dotIndex));
mCurrentTextureName = textureName.toString();
}
- int identifier = mResources.getIdentifier(mCurrentTextureName, "drawable", mResources.getResourcePackageName(mResourceId));
- mTexture = BitmapFactory.decodeResource(mResources, identifier);
+ if(mFile == null) {
+ int identifier = mResources.getIdentifier(mCurrentTextureName, "drawable", mResources.getResourcePackageName(mResourceId));
+ mTexture = BitmapFactory.decodeResource(mResources, identifier);
+ } else {
+ try {
+ String filePath = mFile.getParent() + File.separatorChar + mCurrentTextureName;
+ RajLog.i(filePath);
+ mTexture = BitmapFactory.decodeFile(filePath);
+ } catch (Exception e) {
+ RajLog.e("["+getClass().getCanonicalName()+"] Could not find file " + mCurrentTextureName);
+ e.printStackTrace();
+ return;
+ }
+ }
}
private float[] getTexCoords(BufferedInputStream stream, byte[] bytes)
View
90 src/rajawali/parser/ObjParser.java
@@ -1,6 +1,9 @@
package rajawali.parser;
import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -15,6 +18,7 @@
import rajawali.materials.PhongMaterial;
import rajawali.materials.TextureManager;
import rajawali.materials.TextureManager.TextureType;
+import rajawali.renderer.RajawaliRenderer;
import rajawali.util.RajLog;
import rajawali.wallpaper.Wallpaper;
import android.content.res.Resources;
@@ -72,14 +76,29 @@
protected final String DIFFUSE_COLOR = "Kd";
protected final String DIFFUSE_TEX_MAP = "map_Kd";
+ public ObjParser(RajawaliRenderer renderer, String fileOnSDCard) {
+ super(renderer, fileOnSDCard);
+ }
+
public ObjParser(Resources resources, TextureManager textureManager, int resourceId) {
super(resources, textureManager, resourceId);
}
@Override
public void parse() {
- InputStream fileIn = mResources.openRawResource(mResourceId);
- BufferedReader buffer = new BufferedReader(new InputStreamReader(fileIn));
+ super.parse();
+ BufferedReader buffer = null;
+ if(mFile == null) {
+ InputStream fileIn = mResources.openRawResource(mResourceId);
+ buffer = new BufferedReader(new InputStreamReader(fileIn));
+ } else {
+ try {
+ buffer = new BufferedReader(new FileReader(mFile));
+ } catch (FileNotFoundException e) {
+ RajLog.e("["+getClass().getCanonicalName()+"] Could not find file.");
+ e.printStackTrace();
+ }
+ }
String line;
ObjIndexData currObjIndexData = new ObjIndexData(new BaseObject3D());
ArrayList<ObjIndexData> objIndices = new ArrayList<ObjIndexData>();
@@ -183,7 +202,10 @@ public void parse() {
if(!parts.hasMoreTokens()) continue;
String materialLibPath = parts.nextToken().replace(".", "_");
Log.d(Wallpaper.TAG, "Found Material Lib: " + materialLibPath);
- matLib.parse(materialLibPath, mResources.getResourceTypeName(mResourceId), mResources.getResourcePackageName(mResourceId));
+ if(mFile != null)
+ matLib.parse(materialLibPath, null, null);
+ else
+ matLib.parse(materialLibPath, mResources.getResourceTypeName(mResourceId), mResources.getResourcePackageName(mResourceId));
} else if(type.equals(USE_MATERIAL)) {
currObjIndexData.materialName = parts.nextToken();
}
@@ -300,11 +322,23 @@ public MaterialLib() {
}
public void parse(String materialLibPath, String resourceType, String resourcePackage) {
- mResourcePackage = resourcePackage;
- int identifier = mResources.getIdentifier(materialLibPath, resourceType, resourcePackage);
+ BufferedReader buffer = null;
+ if(mFile == null) {
+ mResourcePackage = resourcePackage;
+ int identifier = mResources.getIdentifier(materialLibPath, resourceType, resourcePackage);
+ InputStream fileIn = mResources.openRawResource(identifier);
+ buffer = new BufferedReader(new InputStreamReader(fileIn));
+ } else {
+ try {
+ File materialFile = new File(mFile.getParent() + File.separatorChar + materialLibPath);
+ buffer = new BufferedReader(new FileReader(materialFile));
+ } catch (Exception e) {
+ RajLog.e("["+getClass().getCanonicalName()+"] Could not find file.");
+ e.printStackTrace();
+ return;
+ }
+ }
- InputStream fileIn = mResources.openRawResource(identifier);
- BufferedReader buffer = new BufferedReader(new InputStreamReader(fileIn));
String line;
MaterialDef matDef = null;
@@ -335,17 +369,17 @@ public void parse(String materialLibPath, String resourceType, String resourcePa
} else if(type.equals(ALPHA_1) || type.equals(ALPHA_2)) {
matDef.alpha = Float.parseFloat(parts.nextToken());
} else if(type.equals(AMBIENT_TEXTURE)) {
- matDef.ambientTexture = getFileNameWithoutExtension(parts.nextToken());
+ matDef.ambientTexture = parts.nextToken();
} else if(type.equals(DIFFUSE_TEXTURE)) {
- matDef.diffuseTexture = getFileNameWithoutExtension(parts.nextToken());
+ matDef.diffuseTexture = parts.nextToken();
} else if(type.equals(SPECULAR_COLOR_TEXTURE)) {
- matDef.specularColorTexture = getFileNameWithoutExtension(parts.nextToken());
+ matDef.specularColorTexture = parts.nextToken();
} else if(type.equals(SPECULAR_HIGHLIGHT_TEXTURE)) {
- matDef.specularHightlightTexture = getFileNameWithoutExtension(parts.nextToken());
+ matDef.specularHightlightTexture = parts.nextToken();
} else if(type.equals(ALPHA_TEXTURE_1) || type.equals(ALPHA_TEXTURE_2)) {
- matDef.alphaTexture = getFileNameWithoutExtension(parts.nextToken());
+ matDef.alphaTexture = parts.nextToken();
} else if(type.equals(BUMP_TEXTURE)) {
- matDef.bumpTexture = getFileNameWithoutExtension(parts.nextToken());
+ matDef.bumpTexture = parts.nextToken();
}
}
if(matDef != null) mMaterials.add(matDef);
@@ -389,12 +423,34 @@ else if(hasBump)
}
if(hasTexture) {
- int identifier = mResources.getIdentifier(matDef.diffuseTexture, "drawable", mResourcePackage);
- object.addTexture(mTextureManager.addTexture(BitmapFactory.decodeResource(mResources, identifier)));
+ if(mFile == null) {
+ int identifier = mResources.getIdentifier(getFileNameWithoutExtension(matDef.diffuseTexture), "drawable", mResourcePackage);
+ object.addTexture(mTextureManager.addTexture(BitmapFactory.decodeResource(mResources, identifier)));
+ } else {
+ try {
+ String filePath = mFile.getParent() + File.separatorChar + getOnlyFileName(matDef.diffuseTexture);
+ object.addTexture(mTextureManager.addTexture(BitmapFactory.decodeFile(filePath), TextureType.BUMP));
+ } catch (Exception e) {
+ RajLog.e("["+getClass().getCanonicalName()+"] Could not find file " + matDef.diffuseTexture);
+ e.printStackTrace();
+ return;
+ }
+ }
}
if(hasBump) {
- int identifier = mResources.getIdentifier(matDef.bumpTexture, "drawable", mResourcePackage);
- object.addTexture(mTextureManager.addTexture(BitmapFactory.decodeResource(mResources, identifier), TextureType.BUMP));
+ if(mFile == null) {
+ int identifier = mResources.getIdentifier(getFileNameWithoutExtension(matDef.bumpTexture), "drawable", mResourcePackage);
+ object.addTexture(mTextureManager.addTexture(BitmapFactory.decodeResource(mResources, identifier), TextureType.BUMP));
+ } else {
+ try {
+ String filePath = mFile.getParent() + File.separatorChar + getOnlyFileName(matDef.bumpTexture);
+ object.addTexture(mTextureManager.addTexture(BitmapFactory.decodeFile(filePath), TextureType.BUMP));
+ } catch (Exception e) {
+ RajLog.e("["+getClass().getCanonicalName()+"] Could not find file " + matDef.bumpTexture);
+ e.printStackTrace();
+ return;
+ }
+ }
}
}
View
45 src/rajawali/parser/fbx/FBXParser.java
@@ -1,6 +1,9 @@
package rajawali.parser.fbx;
import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -18,6 +21,7 @@
import rajawali.materials.DiffuseMaterial;
import rajawali.materials.PhongMaterial;
import rajawali.materials.SimpleMaterial;
+import rajawali.materials.TextureManager.TextureType;
import rajawali.math.Number3D;
import rajawali.math.Vector2D;
import rajawali.parser.AParser;
@@ -68,6 +72,14 @@
private Stack<Object> mObjStack;
private RajawaliRenderer mRenderer;
+ public FBXParser(RajawaliRenderer renderer, String fileOnSDCard) {
+ super(renderer, fileOnSDCard);
+ mRenderer = renderer;
+ mObjStack = new Stack<Object>();
+ mFbx = new FBXValues();
+ mObjStack.add(mFbx);
+ }
+
public FBXParser(RajawaliRenderer renderer, int resourceId) {
super(renderer.getContext().getResources(), renderer.getTextureManager(), resourceId);
mRenderer = renderer;
@@ -78,8 +90,19 @@ public FBXParser(RajawaliRenderer renderer, int resourceId) {
@Override
public void parse() {
- InputStream fileIn = mResources.openRawResource(mResourceId);
- BufferedReader buffer = new BufferedReader(new InputStreamReader(fileIn));
+ super.parse();
+ BufferedReader buffer = null;
+ if(mFile == null) {
+ InputStream fileIn = mResources.openRawResource(mResourceId);
+ buffer = new BufferedReader(new InputStreamReader(fileIn));
+ } else {
+ try {
+ buffer = new BufferedReader(new FileReader(mFile));
+ } catch (FileNotFoundException e) {
+ RajLog.e("["+getClass().getCanonicalName()+"] Could not find file.");
+ e.printStackTrace();
+ }
+ }
String line;
try {
while((line = buffer.readLine()) != null) {
@@ -358,10 +381,22 @@ private void setMeshTextures(BaseObject3D o, String name) {
if(conn.object2.equals(name) && conn.object1.equals(tex.textureName))
{
// -- one texture for now
- String textureName = getFileNameWithoutExtension(tex.fileName).toLowerCase();
+ String textureName = tex.fileName;
try {
- int identifier = mResources.getIdentifier(textureName, "drawable", mResources.getResourcePackageName(mResourceId));
- Bitmap texture = BitmapFactory.decodeResource(mResources, identifier);
+ Bitmap texture = null;
+ if(mFile == null) {
+ int identifier = mResources.getIdentifier(getFileNameWithoutExtension(textureName).toLowerCase(), "drawable", mResources.getResourcePackageName(mResourceId));
+ texture = BitmapFactory.decodeResource(mResources, identifier);
+ } else {
+ try {
+ String filePath = mFile.getParent() + File.separatorChar + getOnlyFileName(textureName);
+ texture = BitmapFactory.decodeFile(filePath);
+ } catch (Exception e) {
+ RajLog.e("["+getClass().getCanonicalName()+"] Could not find file " + getOnlyFileName(textureName));
+ e.printStackTrace();
+ return;
+ }
+ }
o.addTexture(mTextureManager.addTexture(texture));
} catch(Exception e) {
RajLog.e("Could not load texture [" + textureName + "]: " + e.getMessage() );

0 comments on commit a791e20

Please sign in to comment.
Something went wrong with that request. Please try again.