|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 #include "javax_microedition_m3g_Transform.h" |
|
18 |
|
19 JNIEXPORT void JNICALL Java_javax_microedition_m3g_Transform__1transformArray |
|
20 (JNIEnv* aEnv, jclass, jbyteArray aMatrix, jint aHArray, jfloatArray aOutArray, jboolean aW) |
|
21 { |
|
22 // null pointers are never passed |
|
23 Matrix* matrix = (Matrix *)(aEnv->GetByteArrayElements(aMatrix, NULL)); |
|
24 if (matrix == NULL) |
|
25 { |
|
26 M3G_RAISE_EXCEPTION(aEnv, "java/lang/OutOfMemoryError"); |
|
27 return; |
|
28 } |
|
29 |
|
30 jfloat* outArray = aEnv->GetFloatArrayElements(aOutArray, NULL); |
|
31 if (outArray == NULL) |
|
32 { |
|
33 aEnv->ReleaseByteArrayElements(aMatrix, (jbyte*)matrix, JNI_ABORT); |
|
34 |
|
35 M3G_RAISE_EXCEPTION(aEnv, "java/lang/OutOfMemoryError"); |
|
36 return; |
|
37 } |
|
38 |
|
39 int outArrayLen = aEnv->GetArrayLength(aOutArray); |
|
40 |
|
41 M3G_DO_LOCK |
|
42 m3gTransformArray((M3GVertexArray)aHArray, matrix, (M3Gfloat *)outArray, outArrayLen, (M3Gbool)aW); |
|
43 M3G_DO_UNLOCK(aEnv) |
|
44 |
|
45 aEnv->ReleaseByteArrayElements(aMatrix, (jbyte*)matrix, JNI_ABORT); |
|
46 aEnv->ReleaseFloatArrayElements(aOutArray, outArray, 0); |
|
47 } |
|
48 |
|
49 JNIEXPORT void JNICALL Java_javax_microedition_m3g_Transform__1rotateQuat |
|
50 (JNIEnv* aEnv, jclass, jbyteArray aMatrix, jfloat aQx, jfloat aQy, jfloat aQz, jfloat aQw) |
|
51 { |
|
52 M3GQuat quat; |
|
53 Matrix* matrix = NULL; |
|
54 |
|
55 if (aQx == 0 && aQy == 0 && aQz == 0 && aQw == 0) |
|
56 { |
|
57 M3G_RAISE_EXCEPTION(aEnv, "java/lang/IllegalArgumentException"); |
|
58 return; |
|
59 } |
|
60 |
|
61 matrix = (Matrix *)(aEnv->GetByteArrayElements(aMatrix, NULL)); |
|
62 if (matrix == NULL) |
|
63 { |
|
64 M3G_RAISE_EXCEPTION(aEnv, "java/lang/OutOfMemoryError"); |
|
65 return; |
|
66 } |
|
67 |
|
68 quat.x = aQx; |
|
69 quat.y = aQy; |
|
70 quat.z = aQz; |
|
71 quat.w = aQw; |
|
72 |
|
73 M3G_DO_LOCK |
|
74 m3gNormalizeQuat(&quat); |
|
75 m3gPostRotateMatrixQuat(matrix, (const Quat *)&quat); |
|
76 M3G_DO_UNLOCK(aEnv) |
|
77 |
|
78 aEnv->ReleaseByteArrayElements(aMatrix, (jbyte*)matrix, 0); |
|
79 } |
|
80 |
|
81 JNIEXPORT void JNICALL Java_javax_microedition_m3g_Transform__1setIdentity |
|
82 (JNIEnv* aEnv, jclass, jbyteArray aMatrix) |
|
83 { |
|
84 Matrix* matrix = (Matrix *)(aEnv->GetByteArrayElements(aMatrix, NULL)); |
|
85 if (matrix == NULL) |
|
86 { |
|
87 M3G_RAISE_EXCEPTION(aEnv, "java/lang/OutOfMemoryError"); |
|
88 return; |
|
89 } |
|
90 M3G_DO_LOCK |
|
91 m3gIdentityMatrix(matrix); |
|
92 M3G_DO_UNLOCK(aEnv) |
|
93 aEnv->ReleaseByteArrayElements(aMatrix, (jbyte*)matrix, 0); |
|
94 } |
|
95 |
|
96 JNIEXPORT void JNICALL Java_javax_microedition_m3g_Transform__1translate |
|
97 (JNIEnv* aEnv, jclass, jbyteArray aMatrix, jfloat aTx, jfloat aTy, jfloat aTz) |
|
98 { |
|
99 Matrix* matrix = (Matrix *)(aEnv->GetByteArrayElements(aMatrix, NULL)); |
|
100 if (matrix == NULL) |
|
101 { |
|
102 M3G_RAISE_EXCEPTION(aEnv, "java/lang/OutOfMemoryError"); |
|
103 return; |
|
104 } |
|
105 M3G_DO_LOCK |
|
106 m3gPostTranslateMatrix(matrix, aTx, aTy, aTz); |
|
107 M3G_DO_UNLOCK(aEnv) |
|
108 |
|
109 aEnv->ReleaseByteArrayElements(aMatrix, (jbyte*)matrix, 0); |
|
110 } |
|
111 |
|
112 JNIEXPORT void JNICALL Java_javax_microedition_m3g_Transform__1getMatrix |
|
113 (JNIEnv* aEnv, jclass, jbyteArray aMatrix, jfloatArray aDstArray) |
|
114 { |
|
115 Matrix *matrix = NULL; |
|
116 |
|
117 if (aDstArray == NULL) |
|
118 { |
|
119 M3G_RAISE_EXCEPTION(aEnv, "java/lang/NullPointerException"); |
|
120 return; |
|
121 } |
|
122 |
|
123 if (aEnv->GetArrayLength(aDstArray) < 16) |
|
124 { |
|
125 M3G_RAISE_EXCEPTION(aEnv, "java/lang/IllegalArgumentException"); |
|
126 return; |
|
127 } |
|
128 |
|
129 matrix = (Matrix *)(aEnv->GetByteArrayElements(aMatrix, NULL)); |
|
130 if (matrix == NULL) |
|
131 { |
|
132 M3G_RAISE_EXCEPTION(aEnv, "java/lang/OutOfMemoryError"); |
|
133 return; |
|
134 } |
|
135 |
|
136 float* dstArray = (float*)(aEnv->GetFloatArrayElements(aDstArray, NULL)); |
|
137 if (dstArray == NULL) |
|
138 { |
|
139 aEnv->ReleaseByteArrayElements(aMatrix, (jbyte*)matrix, JNI_ABORT); |
|
140 |
|
141 M3G_RAISE_EXCEPTION(aEnv, "java/lang/OutOfMemoryError"); |
|
142 return; |
|
143 } |
|
144 |
|
145 M3G_DO_LOCK |
|
146 m3gGetMatrixRows(matrix, dstArray); |
|
147 M3G_DO_UNLOCK(aEnv) |
|
148 |
|
149 aEnv->ReleaseByteArrayElements(aMatrix, (jbyte*)matrix, JNI_ABORT); |
|
150 aEnv->ReleaseFloatArrayElements(aDstArray, dstArray, 0); |
|
151 } |
|
152 |
|
153 JNIEXPORT void JNICALL Java_javax_microedition_m3g_Transform__1transformTable |
|
154 (JNIEnv* aEnv, jclass, jbyteArray aMatrix, jfloatArray aTableArray) |
|
155 { |
|
156 // null pointers are never passed |
|
157 M3Gfloat *v = (M3Gfloat *)(aEnv->GetFloatArrayElements(aTableArray, NULL)); |
|
158 if (v == NULL) |
|
159 { |
|
160 M3G_RAISE_EXCEPTION(aEnv, "java/lang/OutOfMemoryError"); |
|
161 return; |
|
162 } |
|
163 int tabelArrayLen = aEnv->GetArrayLength(aTableArray); |
|
164 Matrix* matrix = (Matrix *)(aEnv->GetByteArrayElements(aMatrix, NULL)); |
|
165 if (matrix == NULL) |
|
166 { |
|
167 aEnv->ReleaseFloatArrayElements(aTableArray, v, JNI_ABORT); |
|
168 |
|
169 M3G_RAISE_EXCEPTION(aEnv, "java/lang/OutOfMemoryError"); |
|
170 return; |
|
171 } |
|
172 |
|
173 { |
|
174 M3Gint i; |
|
175 M3GVec4 vec; |
|
176 |
|
177 M3G_DO_LOCK |
|
178 for (i = 0; i < tabelArrayLen; i += 4) |
|
179 { |
|
180 m3gSetVec4(&vec, v[i + 0], v[i + 1], v[i + 2], v[i + 3]); |
|
181 m3gTransformVec4(matrix, &vec); |
|
182 v[i + 0] = vec.x; |
|
183 v[i + 1] = vec.y; |
|
184 v[i + 2] = vec.z; |
|
185 v[i + 3] = vec.w; |
|
186 } |
|
187 M3G_DO_UNLOCK(aEnv) |
|
188 } |
|
189 |
|
190 aEnv->ReleaseByteArrayElements(aMatrix, (jbyte*)matrix, JNI_ABORT); |
|
191 aEnv->ReleaseFloatArrayElements(aTableArray, v, 0); |
|
192 } |
|
193 |
|
194 JNIEXPORT void JNICALL Java_javax_microedition_m3g_Transform__1scale |
|
195 (JNIEnv* aEnv, jclass, jbyteArray aMatrix, jfloat aSx, jfloat aSy, jfloat aSz) |
|
196 { |
|
197 Matrix* matrix = (Matrix *)(aEnv->GetByteArrayElements(aMatrix, NULL)); |
|
198 if (matrix == NULL) |
|
199 { |
|
200 M3G_RAISE_EXCEPTION(aEnv, "java/lang/OutOfMemoryError"); |
|
201 return; |
|
202 } |
|
203 |
|
204 M3G_DO_LOCK |
|
205 m3gPostScaleMatrix(matrix, aSx, aSy, aSz); |
|
206 M3G_DO_UNLOCK(aEnv) |
|
207 |
|
208 aEnv->ReleaseByteArrayElements(aMatrix, (jbyte*)matrix, 0); |
|
209 } |
|
210 |
|
211 JNIEXPORT void JNICALL Java_javax_microedition_m3g_Transform__1mul |
|
212 (JNIEnv* aEnv, jclass, jbyteArray aProdArray, jbyteArray aLeftArray, jbyteArray aRightArray) |
|
213 { |
|
214 |
|
215 if (aRightArray == NULL || aLeftArray == NULL || aProdArray == NULL) |
|
216 { |
|
217 M3G_RAISE_EXCEPTION(aEnv, "java/lang/NullPointerException"); |
|
218 return; |
|
219 } |
|
220 |
|
221 { |
|
222 const Matrix *right = (const Matrix *)(aEnv->GetByteArrayElements(aRightArray, NULL)); |
|
223 if (right == NULL) |
|
224 { |
|
225 M3G_RAISE_EXCEPTION(aEnv, "java/lang/OutOfMemoryError"); |
|
226 return; |
|
227 } |
|
228 |
|
229 const Matrix *left = (const Matrix *)(aEnv->GetByteArrayElements(aLeftArray, NULL)); |
|
230 if (left == NULL) |
|
231 { |
|
232 aEnv->ReleaseByteArrayElements(aRightArray, (jbyte*)right, JNI_ABORT); |
|
233 |
|
234 M3G_RAISE_EXCEPTION(aEnv, "java/lang/OutOfMemoryError"); |
|
235 return; |
|
236 } |
|
237 |
|
238 Matrix *prod = (Matrix *)(aEnv->GetByteArrayElements(aProdArray, NULL)); |
|
239 if (prod == NULL) |
|
240 { |
|
241 aEnv->ReleaseByteArrayElements(aRightArray, (jbyte*)right, JNI_ABORT); |
|
242 aEnv->ReleaseByteArrayElements(aLeftArray, (jbyte*)left, JNI_ABORT); |
|
243 |
|
244 M3G_RAISE_EXCEPTION(aEnv, "java/lang/OutOfMemoryError"); |
|
245 return; |
|
246 } |
|
247 |
|
248 Matrix temp; |
|
249 M3G_DO_LOCK |
|
250 m3gMatrixProduct(&temp, left, right); |
|
251 M3G_DO_UNLOCK(aEnv) |
|
252 |
|
253 *prod = temp; |
|
254 |
|
255 aEnv->ReleaseByteArrayElements(aRightArray, (jbyte*)right, JNI_ABORT); |
|
256 aEnv->ReleaseByteArrayElements(aLeftArray, (jbyte*)left, JNI_ABORT); |
|
257 aEnv->ReleaseByteArrayElements(aProdArray, (jbyte*)prod, 0); |
|
258 } |
|
259 } |
|
260 |
|
261 JNIEXPORT void JNICALL Java_javax_microedition_m3g_Transform__1setMatrix |
|
262 (JNIEnv* aEnv, jclass, jbyteArray aMatrix, jfloatArray aSrcArray) |
|
263 { |
|
264 Matrix *matrix = NULL; |
|
265 |
|
266 if (aSrcArray == NULL) |
|
267 { |
|
268 M3G_RAISE_EXCEPTION(aEnv, "java/lang/NullPointerException"); |
|
269 return; |
|
270 } |
|
271 if (aEnv->GetArrayLength(aSrcArray) < 16) |
|
272 { |
|
273 M3G_RAISE_EXCEPTION(aEnv, "java/lang/IllegalArgumentException"); |
|
274 return; |
|
275 } |
|
276 |
|
277 matrix = (Matrix *)(aEnv->GetByteArrayElements(aMatrix, NULL)); |
|
278 if (matrix == NULL) |
|
279 { |
|
280 M3G_RAISE_EXCEPTION(aEnv, "java/lang/OutOfMemoryError"); |
|
281 return; |
|
282 } |
|
283 |
|
284 float* srcArray = aEnv->GetFloatArrayElements(aSrcArray, NULL); |
|
285 if (srcArray == NULL) |
|
286 { |
|
287 aEnv->ReleaseByteArrayElements(aMatrix, (jbyte*)matrix, JNI_ABORT); |
|
288 |
|
289 M3G_RAISE_EXCEPTION(aEnv, "java/lang/OutOfMemoryError"); |
|
290 return; |
|
291 } |
|
292 |
|
293 M3G_DO_LOCK |
|
294 m3gSetMatrixRows(matrix, (const float *)srcArray); |
|
295 M3G_DO_UNLOCK(aEnv) |
|
296 |
|
297 aEnv->ReleaseFloatArrayElements(aSrcArray, srcArray, JNI_ABORT); |
|
298 aEnv->ReleaseByteArrayElements(aMatrix, (jbyte*)matrix, 0); |
|
299 } |
|
300 |
|
301 JNIEXPORT void JNICALL Java_javax_microedition_m3g_Transform__1transpose |
|
302 (JNIEnv* aEnv, jclass, jbyteArray aMatrix) |
|
303 { |
|
304 Matrix tpos; |
|
305 Matrix *matrix = (Matrix *)(aEnv->GetByteArrayElements(aMatrix, NULL)); |
|
306 if (matrix == NULL) |
|
307 { |
|
308 M3G_RAISE_EXCEPTION(aEnv, "java/lang/OutOfMemoryError"); |
|
309 return; |
|
310 } |
|
311 M3G_DO_LOCK |
|
312 m3gMatrixTranspose(&tpos, matrix); |
|
313 m3gCopyMatrix(matrix, &tpos); |
|
314 M3G_DO_UNLOCK(aEnv) |
|
315 |
|
316 aEnv->ReleaseByteArrayElements(aMatrix, (jbyte*)matrix, 0); |
|
317 } |
|
318 |
|
319 JNIEXPORT void JNICALL Java_javax_microedition_m3g_Transform__1invert |
|
320 (JNIEnv* aEnv, jclass, jbyteArray aMatrix) |
|
321 { |
|
322 Matrix *matrix = (Matrix *)(aEnv->GetByteArrayElements(aMatrix, NULL)); |
|
323 if (matrix == NULL) |
|
324 { |
|
325 M3G_RAISE_EXCEPTION(aEnv, "java/lang/OutOfMemoryError"); |
|
326 return; |
|
327 } |
|
328 |
|
329 M3G_BEGIN_PROFILE(M3G_PROFILE_TRANSFORM_INVERT); |
|
330 M3G_DO_LOCK |
|
331 if (!m3gInvertMatrix(matrix)) |
|
332 { |
|
333 aEnv->ReleaseByteArrayElements(aMatrix, (jbyte*)matrix, JNI_ABORT); |
|
334 M3G_RAISE_EXCEPTION(aEnv, "java/lang/ArithmeticException"); |
|
335 M3G_DO_UNLOCK(aEnv) |
|
336 return; |
|
337 } |
|
338 M3G_DO_UNLOCK(aEnv) |
|
339 M3G_END_PROFILE(M3G_PROFILE_TRANSFORM_INVERT); |
|
340 |
|
341 aEnv->ReleaseByteArrayElements(aMatrix, (jbyte*)matrix, 0); |
|
342 } |
|
343 |
|
344 JNIEXPORT void JNICALL Java_javax_microedition_m3g_Transform__1rotate |
|
345 (JNIEnv* aEnv, jclass, jbyteArray aMatrix, jfloat aAngle, jfloat aAx, jfloat aAy, jfloat aAz) |
|
346 { |
|
347 Matrix *matrix = NULL; |
|
348 |
|
349 if (aAx == 0 && aAy == 0 && aAz == 0 && aAngle != 0) |
|
350 { |
|
351 M3G_RAISE_EXCEPTION(aEnv, "java/lang/IllegalArgumentException"); |
|
352 return; |
|
353 } |
|
354 |
|
355 matrix = (Matrix *)(aEnv->GetByteArrayElements(aMatrix, NULL)); |
|
356 if (matrix == NULL) |
|
357 { |
|
358 M3G_RAISE_EXCEPTION(aEnv, "java/lang/OutOfMemoryError"); |
|
359 return; |
|
360 } |
|
361 |
|
362 M3G_DO_LOCK |
|
363 m3gPostRotateMatrix(matrix, aAngle, aAx, aAy, aAz); |
|
364 M3G_DO_UNLOCK(aEnv) |
|
365 |
|
366 aEnv->ReleaseByteArrayElements(aMatrix, (jbyte*)matrix, 0); |
|
367 } |