Home | History | Annotate | Download | only in scenegraph
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.scenegraph;
     18 
     19 import java.io.File;
     20 import java.io.IOException;
     21 import java.io.InputStream;
     22 import java.io.FileInputStream;
     23 import java.io.BufferedInputStream;
     24 import java.io.Writer;
     25 import java.util.ArrayList;
     26 import java.util.HashMap;
     27 import java.util.Map;
     28 import java.util.Vector;
     29 
     30 import android.content.res.Resources;
     31 import android.graphics.Bitmap;
     32 import android.graphics.BitmapFactory;
     33 import android.os.AsyncTask;
     34 import android.renderscript.*;
     35 import android.renderscript.Allocation.MipmapControl;
     36 import android.renderscript.Element.Builder;
     37 import android.renderscript.Font.Style;
     38 import android.renderscript.Program.TextureType;
     39 import android.renderscript.ProgramStore.DepthFunc;
     40 import android.util.Log;
     41 import com.android.scenegraph.SceneManager.SceneLoadedCallback;
     42 
     43 
     44 public class ColladaScene {
     45 
     46     private String modelName;
     47     private static String TAG = "ColladaScene";
     48     private final int STATE_LAST_FOCUS = 1;
     49     boolean mLoadFromSD = false;
     50 
     51     SceneLoadedCallback mCallback;
     52 
     53     public ColladaScene(String name, SceneLoadedCallback cb) {
     54         modelName = name;
     55         mCallback = cb;
     56     }
     57 
     58     public void init(RenderScriptGL rs, Resources res) {
     59         mRS = rs;
     60         mRes = res;
     61 
     62         mLoadFromSD = SceneManager.isSDCardPath(modelName);
     63 
     64         new ColladaLoaderTask().execute(modelName);
     65     }
     66 
     67     private Resources mRes;
     68     private RenderScriptGL mRS;
     69     Scene mActiveScene;
     70 
     71     private class ColladaLoaderTask extends AsyncTask<String, Void, Boolean> {
     72         ColladaParser sceneSource;
     73         protected Boolean doInBackground(String... names) {
     74             String rootDir = names[0].substring(0, names[0].lastIndexOf('/') + 1);
     75             long start = System.currentTimeMillis();
     76             sceneSource = new ColladaParser();
     77             InputStream is = null;
     78             try {
     79                 if (!mLoadFromSD) {
     80                     is = mRes.getAssets().open(names[0]);
     81                 } else {
     82                     File f = new File(names[0]);
     83                     is = new BufferedInputStream(new FileInputStream(f));
     84                 }
     85             } catch (IOException e) {
     86                 Log.e(TAG, "Could not open collada file");
     87                 return new Boolean(false);
     88             }
     89             long end = System.currentTimeMillis();
     90             Log.v("TIMER", "Stream load time: " + (end - start));
     91 
     92             start = System.currentTimeMillis();
     93             sceneSource.init(is, rootDir);
     94             end = System.currentTimeMillis();
     95             Log.v("TIMER", "Collada parse time: " + (end - start));
     96             return new Boolean(true);
     97         }
     98 
     99         protected void onPostExecute(Boolean result) {
    100             mActiveScene = sceneSource.getScene();
    101             if (mCallback != null) {
    102                 mCallback.mLoadedScene = mActiveScene;
    103                 mCallback.run();
    104             }
    105 
    106             String shortName = modelName.substring(0, modelName.lastIndexOf('.'));
    107             new A3DLoaderTask().execute(shortName + ".a3d");
    108         }
    109     }
    110 
    111     private class A3DLoaderTask extends AsyncTask<String, Void, Boolean> {
    112         protected Boolean doInBackground(String... names) {
    113             long start = System.currentTimeMillis();
    114             FileA3D model;
    115             if (!mLoadFromSD) {
    116                 model = FileA3D.createFromAsset(mRS, mRes.getAssets(), names[0]);
    117             } else {
    118                 model = FileA3D.createFromFile(mRS, names[0]);
    119             }
    120             int numModels = model.getIndexEntryCount();
    121             for (int i = 0; i < numModels; i ++) {
    122                 FileA3D.IndexEntry entry = model.getIndexEntry(i);
    123                 if (entry != null && entry.getEntryType() == FileA3D.EntryType.MESH) {
    124                     mActiveScene.meshLoaded(entry.getMesh());
    125                 }
    126             }
    127             long end = System.currentTimeMillis();
    128             Log.v("TIMER", "A3D load time: " + (end - start));
    129             return new Boolean(true);
    130         }
    131 
    132         protected void onPostExecute(Boolean result) {
    133         }
    134     }
    135 
    136 }
    137 
    138 
    139 
    140