|
1 /* |
|
2 * Copyright (c) 2003 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 |
|
18 |
|
19 package javax.microedition.m3g; |
|
20 |
|
21 public class Mesh extends Node |
|
22 { |
|
23 //------------------------------------------------------------------ |
|
24 // Instance data |
|
25 //------------------------------------------------------------------ |
|
26 |
|
27 private VertexBuffer vertices; |
|
28 private Appearance[] appearances; |
|
29 private IndexBuffer[] triangles; |
|
30 |
|
31 static private IndexBuffer[] tempTrianglesArray; |
|
32 static private Appearance[] tempAppearanceArray; |
|
33 |
|
34 static private IndexBuffer tempTriangles; |
|
35 static private Appearance tempAppearance; |
|
36 |
|
37 //------------------------------------------------------------------ |
|
38 // Constructor(s) |
|
39 //------------------------------------------------------------------ |
|
40 |
|
41 Mesh(int handle) |
|
42 { |
|
43 super(handle); |
|
44 updateReferences(); |
|
45 } |
|
46 |
|
47 public Mesh(VertexBuffer vertices, |
|
48 IndexBuffer[] triangles, |
|
49 Appearance[] appearances) |
|
50 { |
|
51 super(createHandle(vertices, triangles, appearances)); |
|
52 updateReferences(); |
|
53 } |
|
54 |
|
55 public Mesh(VertexBuffer vertices, |
|
56 IndexBuffer triangles, |
|
57 Appearance appearance) |
|
58 { |
|
59 super(createHandle(vertices, triangles, appearance)); |
|
60 updateReferences(); |
|
61 } |
|
62 |
|
63 //------------------------------------------------------------------ |
|
64 // Public methods |
|
65 //------------------------------------------------------------------ |
|
66 |
|
67 public void setAppearance(int index, Appearance appearance) |
|
68 { |
|
69 _setAppearance(handle, index, appearance != null ? appearance.handle : 0); |
|
70 appearances[index] = appearance; |
|
71 } |
|
72 |
|
73 public Appearance getAppearance(int index) |
|
74 { |
|
75 return appearances[index]; |
|
76 } |
|
77 |
|
78 public IndexBuffer getIndexBuffer(int index) |
|
79 { |
|
80 return triangles[index]; |
|
81 } |
|
82 |
|
83 public VertexBuffer getVertexBuffer() |
|
84 { |
|
85 return vertices; |
|
86 } |
|
87 |
|
88 public int getSubmeshCount() |
|
89 { |
|
90 return _getSubmeshCount(handle); |
|
91 } |
|
92 |
|
93 //------------------------------------------------------------------ |
|
94 // Private methods |
|
95 //------------------------------------------------------------------ |
|
96 |
|
97 static void verifyParams(VertexBuffer vertices, |
|
98 IndexBuffer[] triangles, |
|
99 Appearance[] appearances) |
|
100 { |
|
101 if (vertices == null || triangles == null) |
|
102 { |
|
103 throw new NullPointerException(); |
|
104 } |
|
105 if (triangles.length == 0 |
|
106 || appearances != null && appearances.length < triangles.length) |
|
107 { |
|
108 throw new IllegalArgumentException(); |
|
109 } |
|
110 if (triangles.length == 0) |
|
111 { |
|
112 throw new IllegalArgumentException(); |
|
113 } |
|
114 for (int i = 0; i < triangles.length; ++i) |
|
115 { |
|
116 if (triangles[i] == null) |
|
117 { |
|
118 throw new NullPointerException(); |
|
119 } |
|
120 } |
|
121 } |
|
122 |
|
123 static void verifyParams(VertexBuffer vertices, |
|
124 IndexBuffer triangles) |
|
125 { |
|
126 if (vertices == null || triangles == null) |
|
127 { |
|
128 throw new NullPointerException(); |
|
129 } |
|
130 } |
|
131 |
|
132 void updateReferences() |
|
133 { |
|
134 triangles = new IndexBuffer[_getSubmeshCount(handle)]; |
|
135 appearances = new Appearance[triangles.length]; |
|
136 |
|
137 vertices = (VertexBuffer)getInstance(_getVertexBuffer(handle)); |
|
138 |
|
139 for (int i = 0; i < triangles.length; i++) |
|
140 { |
|
141 triangles[i] = (IndexBuffer)getInstance(_getIndexBuffer(handle, i)); |
|
142 appearances[i] = (Appearance)getInstance(_getAppearance(handle, i)); |
|
143 } |
|
144 } |
|
145 |
|
146 static int createHandle(VertexBuffer vertices, |
|
147 IndexBuffer[] triangles, |
|
148 Appearance[] appearances) |
|
149 { |
|
150 |
|
151 tempTrianglesArray = triangles; |
|
152 tempAppearanceArray = appearances; |
|
153 |
|
154 // Verify parameters |
|
155 verifyParams(vertices, triangles, appearances); |
|
156 |
|
157 // Init the native side |
|
158 int[] hTriangles = new int[triangles.length]; |
|
159 int[] hAppearances = null; |
|
160 |
|
161 if (appearances != null) |
|
162 { |
|
163 hAppearances = new int[appearances.length]; |
|
164 } |
|
165 |
|
166 for (int i = 0; i < triangles.length; i++) |
|
167 { |
|
168 hTriangles[i] = triangles[i].handle; |
|
169 |
|
170 if (appearances != null) |
|
171 { |
|
172 hAppearances[i] = appearances[i] != null ? appearances[i].handle : 0; |
|
173 } |
|
174 } |
|
175 |
|
176 int ret = _ctor(Interface.getHandle(), |
|
177 vertices.handle, |
|
178 hTriangles, |
|
179 hAppearances); |
|
180 |
|
181 |
|
182 tempTrianglesArray = null; |
|
183 tempAppearanceArray = null; |
|
184 |
|
185 return ret; |
|
186 |
|
187 } |
|
188 |
|
189 static int createHandle(VertexBuffer vertices, |
|
190 IndexBuffer triangles, |
|
191 Appearance appearance) |
|
192 { |
|
193 |
|
194 tempTriangles = triangles; |
|
195 tempAppearance = appearance; |
|
196 |
|
197 verifyParams(vertices, triangles); |
|
198 |
|
199 // Init the native side |
|
200 int[] hTriangles = new int[1]; |
|
201 int[] hAppearances = null; |
|
202 |
|
203 hTriangles[0] = triangles.handle; |
|
204 |
|
205 if (appearance != null) |
|
206 { |
|
207 hAppearances = new int[1]; |
|
208 hAppearances[0] = appearance.handle; |
|
209 } |
|
210 |
|
211 int ret = _ctor(Interface.getHandle(), |
|
212 vertices.handle, |
|
213 hTriangles, |
|
214 hAppearances); |
|
215 |
|
216 |
|
217 tempTriangles = null; |
|
218 tempAppearance = null; |
|
219 |
|
220 |
|
221 return ret; |
|
222 |
|
223 } |
|
224 |
|
225 // Native methods |
|
226 private static native int _ctor(int hInstance, |
|
227 int hVertices, |
|
228 int[] hTriangles, |
|
229 int[] hAppearances); |
|
230 private static native void _setAppearance(int handle, int index, int hAppearance); |
|
231 private static native int _getAppearance(int handle, int index); |
|
232 private static native int _getIndexBuffer(int handle, int index); |
|
233 private static native int _getVertexBuffer(int handle); |
|
234 private static native int _getSubmeshCount(int handle); |
|
235 } |