Advertisement
ginkage

Scene3D

Jun 1st, 2012
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.ArrayList;
  2. import android.opengl.Matrix;
  3.  
  4. public class Scene3D {
  5.     public ArrayList<Material3D> materials;
  6.     public ArrayList<Object3D> objects;
  7.     public ArrayList<Light3D> lights;
  8.     public ArrayList<Animation> animations;
  9.     public float[] background;
  10.     public float[] ambient;
  11.  
  12.     public Material3D FindMaterial(String name)
  13.     {
  14.         if (materials == null || name == null) return null;
  15.         int i, n = materials.size();
  16.         for (i = 0; i < n; i++) {
  17.             Material3D mat = materials.get(i);
  18.             if (mat.name.equals(name))
  19.                 return mat;
  20.         }
  21.         return null;
  22.     }
  23.  
  24.     public Object3D FindObject(String name)
  25.     {
  26.         if (objects == null || name == null) return null;
  27.         int i, n = objects.size();
  28.         for (i = 0; i < n; i++) {
  29.             Object3D obj = objects.get(i);
  30.             if (obj.name.equals(name))
  31.                 return obj;
  32.         }
  33.         return null;
  34.     }
  35.  
  36.     public Light3D FindLight(String name)
  37.     {
  38.         if (lights == null || name == null) return null;
  39.         int i, n = lights.size();
  40.         for (i = 0; i < n; i++) {
  41.             Light3D light = lights.get(i);
  42.             if (light.name.equals(name))
  43.                 return light;
  44.         }
  45.         return null;
  46.     }
  47.  
  48.     public Animation FindAnimation(int id)
  49.     {
  50.         if (animations == null || id == 0xffff) return null;
  51.         int i, n = animations.size();
  52.         for (i = 0; i < n; i++) {
  53.             Animation anim = animations.get(i);
  54.             if (anim.id == id)
  55.                 return anim;
  56.         }
  57.         return null;
  58.     }
  59.  
  60.     private void lerp3(float[] out, float[] from, float[] to, float t)
  61.     {
  62.         for (int i = 0; i < 3; i++)
  63.             out[i] = from[i] + (to[i] - from[i]) * t;
  64.     }
  65.  
  66.     private AnimKey findVec(AnimKey[] keys, float time)
  67.     {
  68.         AnimKey key = keys[keys.length - 1];
  69.  
  70.         // We'll use either first, or last, or interpolated key
  71.         for (int j = 0; j < keys.length; j++) {
  72.             if (keys[j].time >= time) {
  73.                 if (j > 0) {
  74.                     float local = (time - keys[j - 1].time) /
  75.                         (keys[j].time - keys[j - 1].time);
  76.                     key = new AnimKey();
  77.                     key.time = time;
  78.                     key.data = new float[3];
  79.                     lerp3(key.data, keys[j - 1].data, keys[j].data, local);
  80.                 }
  81.                 else
  82.                     key = keys[j];
  83.                 break;
  84.             }
  85.         }
  86.  
  87.         return key;
  88.     }
  89.     private void applyRot(float[] result, float[] data, float t)
  90.     {
  91.         if (Math.abs(data[3]) > 1.0e-7 && Math.hypot(Math.hypot(data[0], data[1]), data[2]) > 1.0e-7)
  92.             Matrix.rotateM(result, 0, (float) (data[3] * t * 180 / Math.PI), data[0], data[1], data[2]);
  93.     }
  94.  
  95.     public void Compute(float time)
  96.     {
  97.         int i, n = animations.size();
  98.         for (i = 0; i < n; i++) {
  99.             Animation anim = animations.get(i);
  100.             Object3D obj = anim.object;
  101.             float[] result = new float[16];
  102.  
  103.             Matrix.setIdentityM(result, 0);
  104.  
  105.             if (anim.position != null && anim.position.length > 0) {
  106.                 AnimKey key = findVec(anim.position, time);
  107.                 float[] pos = key.data;
  108.                 Matrix.translateM(result, 0, pos[0], pos[1], pos[2]);
  109.             }
  110.  
  111.             if (anim.rotation != null && anim.rotation.length > 0) {
  112.                 // All rotations that are prior to the target time should be applied sequentially
  113.                 for (int j = anim.rotation.length - 1; j > 0; j--) {
  114.                     if (time >= anim.rotation[j].time) // rotation in the past, apply as is
  115.                         applyRot(result, anim.rotation[j].data, 1);
  116.                     else if (time > anim.rotation[j - 1].time) {
  117.                         // rotation between key frames, apply part of it
  118.                         float local = (time - anim.rotation[j - 1].time) /
  119.                                 (anim.rotation[j].time - anim.rotation[j - 1].time);
  120.                         applyRot(result, anim.rotation[j].data, local);
  121.                     }
  122.                     // otherwise, it's a rotation in the future, skip it
  123.                 }
  124.  
  125.                 // Always apply the first rotation
  126.                 applyRot(result, anim.rotation[0].data, 1);
  127.             }
  128.  
  129.             if (anim.scaling != null && anim.scaling.length > 0) {
  130.                 AnimKey key = findVec(anim.scaling, time);
  131.                 float[] scale = key.data;
  132.                 Matrix.scaleM(result, 0, scale[0], scale[1], scale[2]);
  133.             }
  134.  
  135.             if (anim.parent != null)
  136.                 Matrix.multiplyMM(anim.result, 0, anim.parent.result, 0, result, 0);
  137.             else
  138.                 Matrix.translateM(anim.result, 0, result, 0, 0, 0, 0);
  139.  
  140.             if (obj != null && obj.trMatrix != null) {
  141.                 float[] pivot = new float[16];
  142.                 Matrix.setIdentityM(pivot, 0);
  143.                 Matrix.translateM(pivot, 0, -anim.pivot[0], -anim.pivot[1], -anim.pivot[2]);
  144.                 Matrix.multiplyMM(result, 0, pivot, 0, obj.trMatrix, 0);
  145.             }
  146.             else {
  147.                 Matrix.setIdentityM(result, 0);
  148.                 Matrix.translateM(result, 0, -anim.pivot[0], -anim.pivot[1], -anim.pivot[2]);
  149.             }
  150.             Matrix.multiplyMM(anim.world, 0, anim.result, 0, result, 0);
  151.         }
  152.     }
  153. }
  154.  
  155. class Object3D {
  156.     public String name;
  157.     public int vertCount;
  158.     public int indCount;
  159.     public ArrayList<FaceMat> faceMats;
  160.     public int glVertices;
  161.     public int glIndices;
  162.     public float[] vertexBuffer;
  163.     public float[] trMatrix;
  164. }
  165.  
  166. class FaceMat {
  167.     public Material3D material;
  168.     public int[] indexBuffer;
  169.     public int indCount;
  170.     public int bufOffset;
  171. }
  172.  
  173. class Light3D {
  174.     public String name;
  175.     public float[] pos;
  176.     public float[] color;
  177.     public float[] dir;
  178.     public float theta, phi;
  179. }
  180.  
  181. class Material3D {
  182.     public String name;
  183.     public float[] ambient;
  184.     public float[] diffuse;
  185.     public float[] specular;
  186.     public String texture;
  187.     public float shininess;
  188.     public float shinStren;
  189.     public float transparency;
  190.     public float selfIllum;
  191.     public int type;
  192. }
  193.  
  194. class Animation {
  195.     public int id;
  196.     public String name;
  197.     public Object3D object;
  198.     public Light3D light;
  199.     public Animation parent;
  200.     public float[] pivot;
  201.  
  202.     public AnimKey[] position;
  203.     public AnimKey[] rotation;
  204.     public AnimKey[] scaling;
  205.  
  206.     public float[] result;
  207.     public float[] world;
  208. }
  209.  
  210. class AnimKey {
  211.     public float time;
  212.     public float[] data;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement