Android Open Source - min3d A Parser






From Project

Back to project page min3d.

License

The source code is released under:

MIT License

If you think the Android project min3d listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.min3d.lib;
//from   w w  w.  j a v a 2s.co  m
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.util.Log;

import com.min3d.lib.animation.AnimationObject3d;
import com.min3d.lib.core.Object3dContainer;
import com.min3d.lib.vos.Color4;
import com.min3d.lib.vos.Number3d;
import com.min3d.lib.vos.Uv;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

/**
 * Abstract parser class with basic parsing functionality.
 * 
 * @author dennis.ippel
 *
 */
public abstract class AParser implements IParser {
  protected Resources resources;
  protected int resourceID;
  protected String packageID;
  protected String currentMaterialKey;
  protected ArrayList<ParseObjectData> parseObjects;
  protected ParseObjectData co;
  protected boolean firstObject;
  protected TextureAtlas textureAtlas;
  protected ArrayList<Number3d> vertices;
  protected ArrayList<Uv> texCoords;
  protected ArrayList<Number3d> normals;
  protected boolean generateMipMap;
  protected HashMap<String, Material> materialMap;
  
  public AParser()
  {
    vertices = new ArrayList<Number3d>();
    texCoords = new ArrayList<Uv>();
    normals = new ArrayList<Number3d>();
    parseObjects = new ArrayList<ParseObjectData>();
    textureAtlas = new TextureAtlas();
    firstObject = true;
    materialMap = new HashMap<String, Material>();
  }
  
  public AParser(Resources resources, int resourceID, String packageID, Boolean generateMipMap)
  {
    this();
    this.resources = resources;
    this.resourceID = resourceID;
      this.packageID = packageID;
    this.generateMipMap = generateMipMap;
  }
  
  protected void cleanup()
  {
    parseObjects.clear();
    textureAtlas.cleanup();
    vertices.clear();
    texCoords.clear();
    normals.clear();
  }
  
  /**
   * Override this in the concrete parser
   */
  public Object3dContainer getParsedObject() {
    return null;
  }
  
  /**
   * Override this in the concrete parser if applicable 
   */
  public AnimationObject3d getParsedAnimationObject() {
    return null;
  }

  protected String readString(InputStream stream) throws IOException {
    String result = new String();
    byte inByte;
    while ((inByte = (byte) stream.read()) != 0)
      result += (char) inByte;
    return result;
  }

  protected int readInt(InputStream stream) throws IOException {
    return stream.read() | (stream.read() << 8) | (stream.read() << 16)
        | (stream.read() << 24);
  }

  protected int readShort(InputStream stream) throws IOException {
    return (stream.read() | (stream.read() << 8));
  }

  protected float readFloat(InputStream stream) throws IOException {
    return Float.intBitsToFloat(readInt(stream));
  }

  /**
   * Override this in the concrete parser
   */
  public void parse() {
  }
  

  /**
   * Contains texture information. UV offsets and scaling is stored here.
   * This is used with texture atlases.
   * 
   * @author dennis.ippel
   *
   */
  protected class BitmapAsset
  {
    /**
     * The texture bitmap
     */
    public Bitmap bitmap;
    /**
     * The texture identifier
     */
    public String key;
    /**
     * Resource ID
     */
    public String resourceID;
    /**
     * U-coordinate offset
     */
    public float uOffset;
    /**
     * V-coordinate offset
     */
    public float vOffset;
    /**
     * U-coordinate scaling value
     */
    public float uScale;
    /**
     * V-coordinate scaling value
     */
    public float vScale;
    public boolean useForAtlasDimensions;
    
    /**
     * Creates a new BitmapAsset object
     */
    public BitmapAsset(String key, String resourceID)
    {
      this.key = key;
      this.resourceID = resourceID;
      useForAtlasDimensions = false;
    }
  }
  
  /**
   * When a model contains per-face textures a texture atlas is created. This
   * combines multiple textures into one and re-calculates the UV coordinates.
   * 
   * @author dennis.ippel
   * 
   */
  protected class TextureAtlas {
    /**
     * The texture bitmaps that should be combined into one.
     */
    private ArrayList<BitmapAsset> bitmaps;
    /**
     * The texture atlas bitmap
     */
    private Bitmap atlas;

    /**
     * Creates a new texture atlas instance.
     */
    public TextureAtlas() {
      bitmaps = new ArrayList<BitmapAsset>();
    }
    private String atlasId;

    /**
     * Adds a bitmap to the atlas
     */
    public void addBitmapAsset(BitmapAsset ba) {
      BitmapAsset existingBA = getBitmapAssetByResourceID(ba.resourceID);

      if(existingBA == null)
      {
        int bmResourceID = resources.getIdentifier(ba.resourceID, null, null);
        if(bmResourceID == 0)
        {
          Log.d(Min3d.TAG, "Texture not found: " + ba.resourceID);
          return;
        }

        Log.d(Min3d.TAG, "Adding texture " + ba.resourceID);
        
        Bitmap b = Utils.makeBitmapFromResourceId(bmResourceID);
        ba.useForAtlasDimensions = true;
        ba.bitmap = b;
      }
      else
      {
        ba.bitmap = existingBA.bitmap;
      }

      bitmaps.add(ba);
    }
    
    public BitmapAsset getBitmapAssetByResourceID(String resourceID)
    {
      int numBitmaps = bitmaps.size();
      
      for(int i=0; i<numBitmaps; i++)
      {
        if(bitmaps.get(i).resourceID.equals(resourceID))
          return bitmaps.get(i);
      }
      
      return null;
    }

    /**
     * Generates a new texture atlas
     */
    public void generate() {
      Collections.sort(bitmaps, new BitmapHeightComparer());

      if(bitmaps.size() == 0) return;
      
      BitmapAsset largestBitmap = bitmaps.get(0);
      int totalWidth = 0;
      int numBitmaps = bitmaps.size();
      int uOffset = 0;
      int vOffset = 0;

      for (int i = 0; i < numBitmaps; i++) {
        if(bitmaps.get(i).useForAtlasDimensions)
          totalWidth += bitmaps.get(i).bitmap.getWidth();
      }

      atlas = Bitmap.createBitmap(totalWidth, largestBitmap.bitmap
          .getHeight(), Config.ARGB_8888);

      for (int i = 0; i < numBitmaps; i++) {
        BitmapAsset ba = bitmaps.get(i);
        BitmapAsset existingBA = getBitmapAssetByResourceID(ba.resourceID);        
        
        if(ba.useForAtlasDimensions)
        {
          Bitmap b = ba.bitmap;
          int w = b.getWidth();
          int h = b.getHeight();
          int[] pixels = new int[w * h];
          
          b.getPixels(pixels, 0, w, 0, 0, w, h);
          atlas.setPixels(pixels, 0, w, uOffset, vOffset, w, h);
          
          ba.uOffset = (float) uOffset / totalWidth;
          ba.vOffset = 0;
          ba.uScale = (float) w / (float) totalWidth;
          ba.vScale = (float) h / (float) largestBitmap.bitmap.getHeight();
          
          uOffset += w;
          b.recycle();
        }
        else
        {
          ba.uOffset = existingBA.uOffset;
          ba.vOffset = existingBA.vOffset;
          ba.uScale = existingBA.uScale;
          ba.vScale = existingBA.vScale;
        }
      }
      /*
      FileOutputStream fos;
      try {
        fos = new FileOutputStream("/data/screenshot.png");
        atlas.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
      } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      */
      setId(Shared.textureManager().getNewAtlasId());
    }

    /**
     * Returns the generated texture atlas bitmap
     * 
     * @return
     */
    public Bitmap getBitmap() {
      return atlas;
    }

    /**
     * Indicates whether bitmaps have been added to the atlas.
     * 
     * @return
     */
    public boolean hasBitmaps() {
      return bitmaps.size() > 0;
    }

    /**
     * Compares the height of two BitmapAsset objects.
     * 
     * @author dennis.ippel
     * 
     */
    private class BitmapHeightComparer implements Comparator<BitmapAsset> {
      public int compare(BitmapAsset b1, BitmapAsset b2) {
        int height1 = b1.bitmap.getHeight();
        int height2 = b2.bitmap.getHeight();

        if (height1 < height2) {
          return 1;
        } else if (height1 == height2) {
          return 0;
        } else {
          return -1;
        }
      }
    }
    
    /**
     * Returns a bitmap asset with a specified name.
     * 
     * @param materialKey
     * @return
     */
    public BitmapAsset getBitmapAssetByName(String materialKey) {
      int numBitmaps = bitmaps.size();

      for (int i = 0; i < numBitmaps; i++) {
        if (bitmaps.get(i).key.equals(materialKey))
          return bitmaps.get(i);
      }

      return null;
    }
    
    public void cleanup()
    {
      int numBitmaps = bitmaps.size();

      for (int i = 0; i < numBitmaps; i++) {
        bitmaps.get(i).bitmap.recycle();
      }
      
      if(atlas != null) atlas.recycle();
      bitmaps.clear();
      vertices.clear();
      texCoords.clear();
      normals.clear();
    }

    public void setId(String newAtlasId) {
      atlasId = newAtlasId;      
    }

    public String getId() {
      return atlasId;
    }
  }
  
  protected class Material {
    public String name;
    public String diffuseTextureMap;
    public Color4 diffuseColor;

    public Material(String name) {
      this.name = name;
    }
  }
}




Java Source Code List

com.min3d.ApplicationTest.java
com.min3d.ExampleAccelerometer.java
com.min3d.ExampleAnimatedTexture.java
com.min3d.ExampleAnimatingVertices.java
com.min3d.ExampleAssigningTexturesDynamically.java
com.min3d.ExampleCamera.java
com.min3d.ExampleFog.java
com.min3d.ExampleFromScratch.java
com.min3d.ExampleInsideLayout.java
com.min3d.ExampleKeyframeAnimation.java
com.min3d.ExampleLightProperties.java
com.min3d.ExampleLoad3DSFile.java
com.min3d.ExampleLoadMD2File.java
com.min3d.ExampleLoadObjFileMultiple.java
com.min3d.ExampleLoadObjFile.java
com.min3d.ExampleMipMap.java
com.min3d.ExampleMostMinimal.java
com.min3d.ExampleMultiTexture.java
com.min3d.ExampleMultipleLights.java
com.min3d.ExampleRenderType.java
com.min3d.ExampleRotatingPlanets.java
com.min3d.ExampleSubsetOfFaces.java
com.min3d.ExampleTextureOffset.java
com.min3d.ExampleTextureWrap.java
com.min3d.ExampleTextures.java
com.min3d.ExampleTransparentGlSurface.java
com.min3d.ExampleVertexColors.java
com.min3d.ExampleVerticesVariations.java
com.min3d.ScratchActivity.java
com.min3d.SplashActivity.java
com.min3d.lib.AParser.java
com.min3d.lib.ApplicationTest.java
com.min3d.lib.IParser.java
com.min3d.lib.LittleEndianDataInputStream.java
com.min3d.lib.MD2Parser.java
com.min3d.lib.Max3DSParser.java
com.min3d.lib.Min3d.java
com.min3d.lib.ObjParser.java
com.min3d.lib.ParseObjectData.java
com.min3d.lib.ParseObjectFace.java
com.min3d.lib.Parser.java
com.min3d.lib.Shared.java
com.min3d.lib.Utils.java
com.min3d.lib.animation.AnimationObject3d.java
com.min3d.lib.animation.KeyFrame.java
com.min3d.lib.core.Color4BufferList.java
com.min3d.lib.core.FacesBufferedList.java
com.min3d.lib.core.ManagedLightList.java
com.min3d.lib.core.Number3dBufferList.java
com.min3d.lib.core.Object3dContainer.java
com.min3d.lib.core.Object3d.java
com.min3d.lib.core.RenderCaps.java
com.min3d.lib.core.RendererActivity.java
com.min3d.lib.core.Renderer.java
com.min3d.lib.core.Scene.java
com.min3d.lib.core.TextureList.java
com.min3d.lib.core.TextureManager.java
com.min3d.lib.core.UvBufferList.java
com.min3d.lib.core.Vertices.java
com.min3d.lib.interfaces.IDirtyManaged.java
com.min3d.lib.interfaces.IDirtyParent.java
com.min3d.lib.interfaces.IObject3dContainer.java
com.min3d.lib.interfaces.ISceneController.java
com.min3d.lib.objectPrimitives.Box.java
com.min3d.lib.objectPrimitives.HollowCylinder.java
com.min3d.lib.objectPrimitives.Rectangle.java
com.min3d.lib.objectPrimitives.SkyBox.java
com.min3d.lib.objectPrimitives.Sphere.java
com.min3d.lib.objectPrimitives.Torus.java
com.min3d.lib.vos.AbstractDirtyManaged.java
com.min3d.lib.vos.BooleanManaged.java
com.min3d.lib.vos.CameraVo.java
com.min3d.lib.vos.Color4Managed.java
com.min3d.lib.vos.Color4.java
com.min3d.lib.vos.Face.java
com.min3d.lib.vos.FloatManaged.java
com.min3d.lib.vos.FogType.java
com.min3d.lib.vos.FrustumManaged.java
com.min3d.lib.vos.LightType.java
com.min3d.lib.vos.Light.java
com.min3d.lib.vos.Number3dManaged.java
com.min3d.lib.vos.Number3d.java
com.min3d.lib.vos.RenderType.java
com.min3d.lib.vos.ShadeModelManaged.java
com.min3d.lib.vos.ShadeModel.java
com.min3d.lib.vos.TexEnvxVo.java
com.min3d.lib.vos.TextureVo.java
com.min3d.lib.vos.Uv.java
com.min3d.lib.vos.Vertex3d.java