Skip to content

Commit

Permalink
Parsers now also take a String that specifies the path to the file on…
Browse files Browse the repository at this point in the history
… the SD Card. Fixes issue #30
  • Loading branch information
MasDennis committed May 23, 2012
1 parent 6286a14 commit a791e20
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 29 deletions.
Binary file modified build/rajawalilib.zip
Binary file not shown.
9 changes: 7 additions & 2 deletions src/rajawali/BaseObject3D.java
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/rajawali/materials/AMaterial.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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++;
Expand Down
25 changes: 25 additions & 0 deletions 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;
Expand All @@ -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() {
Expand Down Expand Up @@ -70,6 +84,17 @@ protected class MaterialDef {
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("\\");
Expand Down
43 changes: 39 additions & 4 deletions src/rajawali/parser/MD2Parser.java
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -27,6 +32,10 @@ public class MD2Parser extends AParser implements IParser {
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);
}
Expand All @@ -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);

Expand Down Expand Up @@ -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)
Expand Down
90 changes: 73 additions & 17 deletions 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;
Expand All @@ -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;
Expand Down Expand Up @@ -72,14 +76,29 @@ public class ObjParser extends AParser {
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>();
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
}
}

Expand Down

0 comments on commit a791e20

Please sign in to comment.