Advertisement
ginkage

Scene3D

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