SHOW:
|
|
- or go back to the newest paste.
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 | - | |
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 | - | |
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 | ||
90 | private void applyRot(float[] result, float[] data, float t) | |
91 | { | |
92 | if (Math.abs(data[3]) > 1.0e-7 && Math.hypot(Math.hypot(data[0], data[1]), data[2]) > 1.0e-7) | |
93 | Matrix.rotateM(result, 0, (float) (data[3] * t * 180 / Math.PI), data[0], data[1], data[2]); | |
94 | } | |
95 | ||
96 | public void Compute(float time) | |
97 | { | |
98 | int i, n = animations.size(); | |
99 | for (i = 0; i < n; i++) { | |
100 | Animation anim = animations.get(i); | |
101 | Object3D obj = anim.object; | |
102 | float[] result = new float[16]; | |
103 | ||
104 | Matrix.setIdentityM(result, 0); | |
105 | ||
106 | if (anim.position != null && anim.position.length > 0) { | |
107 | AnimKey key = findVec(anim.position, time); | |
108 | float[] pos = key.data; | |
109 | Matrix.translateM(result, 0, pos[0], pos[1], pos[2]); | |
110 | } | |
111 | ||
112 | if (anim.rotation != null && anim.rotation.length > 0) { | |
113 | // All rotations that are prior to the target time should be applied sequentially | |
114 | for (int j = anim.rotation.length - 1; j > 0; j--) { | |
115 | if (time >= anim.rotation[j].time) // rotation in the past, apply as is | |
116 | applyRot(result, anim.rotation[j].data, 1); | |
117 | else if (time > anim.rotation[j - 1].time) { | |
118 | // rotation between key frames, apply part of it | |
119 | float local = (time - anim.rotation[j - 1].time) / | |
120 | (anim.rotation[j].time - anim.rotation[j - 1].time); | |
121 | applyRot(result, anim.rotation[j].data, local); | |
122 | } | |
123 | // otherwise, it's a rotation in the future, skip it | |
124 | } | |
125 | ||
126 | // Always apply the first rotation | |
127 | applyRot(result, anim.rotation[0].data, 1); | |
128 | } | |
129 | ||
130 | if (anim.scaling != null && anim.scaling.length > 0) { | |
131 | AnimKey key = findVec(anim.scaling, time); | |
132 | float[] scale = key.data; | |
133 | Matrix.scaleM(result, 0, scale[0], scale[1], scale[2]); | |
134 | } | |
135 | ||
136 | if (anim.parent != null) | |
137 | Matrix.multiplyMM(anim.result, 0, anim.parent.result, 0, result, 0); | |
138 | else | |
139 | Matrix.translateM(anim.result, 0, result, 0, 0, 0, 0); | |
140 | ||
141 | if (obj != null && obj.trMatrix != null) { | |
142 | float[] pivot = new float[16]; | |
143 | Matrix.setIdentityM(pivot, 0); | |
144 | Matrix.translateM(pivot, 0, -anim.pivot[0], -anim.pivot[1], -anim.pivot[2]); | |
145 | Matrix.multiplyMM(result, 0, pivot, 0, obj.trMatrix, 0); | |
146 | } | |
147 | else { | |
148 | Matrix.setIdentityM(result, 0); | |
149 | Matrix.translateM(result, 0, -anim.pivot[0], -anim.pivot[1], -anim.pivot[2]); | |
150 | } | |
151 | Matrix.multiplyMM(anim.world, 0, anim.result, 0, result, 0); | |
152 | } | |
153 | } | |
154 | } | |
155 | ||
156 | class Object3D { | |
157 | public String name; | |
158 | public int vertCount; | |
159 | public int indCount; | |
160 | public ArrayList<FaceMat> faceMats; | |
161 | public int glVertices; | |
162 | public int glIndices; | |
163 | public float[] vertexBuffer; | |
164 | public float[] trMatrix; | |
165 | } | |
166 | ||
167 | class FaceMat { | |
168 | public Material3D material; | |
169 | public short[] indexBuffer; | |
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 int idParent; |
196 | + | |
197 | public Object3D object; | |
198 | public Light3D light; | |
199 | public Animation parent; | |
200 | public float[] pivot; | |
201 | ||
202 | - | |
202 | + | |
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 | } | |
214 | |