svn-gvsig-desktop / trunk / libraries / libGPE-OSG / src / main / java / org / gvsig / gpe / osg / OSGParser.java @ 23296
History | View | Annotate | Download (21.2 KB)
1 | 21503 | jcampos | /* gvSIG. Geographic Information System of the Valencian Government
|
---|---|---|---|
2 | * osgVP. OSG Virtual Planets.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2008 Infrastructures and Transports Department
|
||
5 | * of the Valencian Government (CIT)
|
||
6 | *
|
||
7 | * This program is free software; you can redistribute it and/or
|
||
8 | * modify it under the terms of the GNU General Public License
|
||
9 | * as published by the Free Software Foundation; either version 2
|
||
10 | * of the License, or (at your option) any later version.
|
||
11 | *
|
||
12 | * This program is distributed in the hope that it will be useful,
|
||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
15 | * GNU General Public License for more details.
|
||
16 | *
|
||
17 | * You should have received a copy of the GNU General Public License
|
||
18 | * along with this program; if not, write to the Free Software
|
||
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
20 | * MA 02110-1301, USA.
|
||
21 | *
|
||
22 | */
|
||
23 | /*
|
||
24 | * AUTHORS (In addition to CIT):
|
||
25 | * 2008 Instituto de Automática e Informática Industrial, UPV.
|
||
26 | */
|
||
27 | |||
28 | |||
29 | package org.gvsig.gpe.osg; |
||
30 | |||
31 | import java.awt.Color; |
||
32 | import java.awt.image.BufferedImage; |
||
33 | import java.io.FileNotFoundException; |
||
34 | import java.lang.reflect.Constructor; |
||
35 | import java.net.URI; |
||
36 | import java.util.NoSuchElementException; |
||
37 | import java.util.Stack; |
||
38 | import java.util.Vector; |
||
39 | import java.util.logging.Level; |
||
40 | |||
41 | import org.gvsig.gpe.IGPEContentHandler3D; |
||
42 | 21782 | jcampos | import org.gvsig.gpe.parser.GPEParser; |
43 | 21503 | jcampos | import org.gvsig.osgvp.AutoTransform; |
44 | import org.gvsig.osgvp.DrawArrayLengths; |
||
45 | import org.gvsig.osgvp.Drawable; |
||
46 | import org.gvsig.osgvp.Geode; |
||
47 | import org.gvsig.osgvp.Geometry; |
||
48 | import org.gvsig.osgvp.Group; |
||
49 | import org.gvsig.osgvp.Material; |
||
50 | import org.gvsig.osgvp.Matrix; |
||
51 | import org.gvsig.osgvp.MatrixTransform; |
||
52 | import org.gvsig.osgvp.Node; |
||
53 | import org.gvsig.osgvp.PositionAttitudeTransform; |
||
54 | import org.gvsig.osgvp.PrimitiveSet; |
||
55 | import org.gvsig.osgvp.Quat; |
||
56 | import org.gvsig.osgvp.Texture2D; |
||
57 | import org.gvsig.osgvp.Vec2; |
||
58 | import org.gvsig.osgvp.Vec3; |
||
59 | import org.gvsig.osgvp.Vec4; |
||
60 | import org.gvsig.osgvp.osgDB; |
||
61 | import org.gvsig.osgvp.exceptions.node.ChildIndexOutOfBoundsExceptions; |
||
62 | import org.gvsig.osgvp.exceptions.node.LoadNodeException; |
||
63 | import org.gvsig.osgvp.exceptions.node.NodeException; |
||
64 | import org.gvsig.osgvp.exceptions.texture.TextureStageException; |
||
65 | import org.gvsig.osgvp.util.Util; |
||
66 | |||
67 | public class OSGParser extends GPEParser { |
||
68 | |||
69 | Stack<Matrix> _transforms = new Stack<Matrix>(); |
||
70 | Vector<Stack<Texture2D>> _textures = new Vector<Stack<Texture2D>>(); |
||
71 | Stack<Material> _materials = new Stack<Material>(); |
||
72 | Stack<Boolean> _blendings = new Stack<Boolean>(); |
||
73 | private IGPEContentHandler3D _content;
|
||
74 | 21782 | jcampos | private String description; |
75 | private String name; |
||
76 | 21503 | jcampos | |
77 | public OSGParser(String name, String description) { |
||
78 | 21782 | jcampos | // super(name, description);
|
79 | this.name = name;
|
||
80 | this.description = description;
|
||
81 | 21503 | jcampos | } |
82 | 21782 | jcampos | |
83 | @Override
|
||
84 | public String getDescription() { |
||
85 | // TODO Auto-generated method stub
|
||
86 | return this.description; |
||
87 | } |
||
88 | 21503 | jcampos | |
89 | @Override
|
||
90 | 21782 | jcampos | public String getName() { |
91 | // TODO Auto-generated method stub
|
||
92 | return this.name; |
||
93 | } |
||
94 | |||
95 | @Override
|
||
96 | 21503 | jcampos | public boolean accept(URI uri) { |
97 | if ((uri.getPath().toUpperCase().endsWith("OSG")) |
||
98 | || (uri.getPath().toUpperCase().endsWith("3DS"))
|
||
99 | || (uri.getPath().toUpperCase().endsWith("IVE"))
|
||
100 | || (uri.getPath().toUpperCase().endsWith("OBJ"))) {
|
||
101 | return true; |
||
102 | } |
||
103 | return false; |
||
104 | } |
||
105 | |||
106 | public String[] getFormats() { |
||
107 | String[] formats = new String[4]; |
||
108 | formats[0] = "OSG"; |
||
109 | formats[1] = "3DS"; |
||
110 | formats[2] = "OBJ"; |
||
111 | formats[3] = "IVE"; |
||
112 | return formats;
|
||
113 | } |
||
114 | |||
115 | public String[] getVersions() { |
||
116 | String[] versions = new String[1]; |
||
117 | versions[0] = "All"; |
||
118 | return versions;
|
||
119 | } |
||
120 | |||
121 | @Override
|
||
122 | protected void parseStream() { |
||
123 | 22587 | jcampos | |
124 | |||
125 | 21503 | jcampos | } |
126 | |||
127 | @Override
|
||
128 | protected void parseURI() { |
||
129 | |||
130 | // This is a very bad hack to avoid testing hangs in Mac OS Lepard
|
||
131 | Color color = new Color(0, 0, 0, 255); |
||
132 | // TODO remove this hack
|
||
133 | |||
134 | Vector<Integer> addedTextures = new Vector<Integer>(); |
||
135 | boolean addedMaterial = false; |
||
136 | |||
137 | _content = (IGPEContentHandler3D) getContentHandler(); |
||
138 | 22231 | jcampos | // Util.logger.log(Level.FINEST, getMainFile().getPath());
|
139 | 21503 | jcampos | Node root=null;
|
140 | |||
141 | try {
|
||
142 | 22231 | jcampos | // root = osgDB.readNodeFile(getMainFile().getPath());
|
143 | 22587 | jcampos | root = osgDB.readNodeFile(getMainFile().getPath().substring(1, getMainFile().getPath().length()));
|
144 | 22231 | jcampos | |
145 | 21503 | jcampos | } catch (LoadNodeException e) {
|
146 | // TODO Auto-generated catch block
|
||
147 | e.printStackTrace(); |
||
148 | return;
|
||
149 | 21782 | jcampos | } catch (FileNotFoundException e) { |
150 | // TODO Auto-generated catch block
|
||
151 | e.printStackTrace(); |
||
152 | return;
|
||
153 | 21503 | jcampos | } |
154 | if (root == null) |
||
155 | 22231 | jcampos | // Util.logger.log(Level.FINEST, "Cannot open"
|
156 | // + getMainFile().getPath());
|
||
157 | System.out.println("error"); |
||
158 | 21503 | jcampos | else {
|
159 | |||
160 | Object multiGeometry;
|
||
161 | multiGeometry = _content.startMultiGeometry("", ""); |
||
162 | |||
163 | if (root.className().equals("Geode")) { |
||
164 | |||
165 | Geode rootGeode = new Geode(root.getCPtr());
|
||
166 | Object multiSolid;
|
||
167 | |||
168 | multiSolid = _content.startMultiSolid("", ""); |
||
169 | |||
170 | _content.addGeometryToMultiGeometry(multiSolid, multiGeometry); |
||
171 | |||
172 | try {
|
||
173 | addedTextures = pushTextures(rootGeode); |
||
174 | } catch (NodeException e) {
|
||
175 | } |
||
176 | try {
|
||
177 | addedMaterial = pushMaterial(rootGeode); |
||
178 | } catch (NodeException e) {
|
||
179 | } |
||
180 | |||
181 | _blendings.push(rootGeode.getOrCreateStateSet().getEnabledBlending()); |
||
182 | |||
183 | for (int i = 0; i < rootGeode.getNumDrawables(); i++) { |
||
184 | |||
185 | try {
|
||
186 | parseDrawable(rootGeode.getDrawable(i), multiSolid); |
||
187 | } catch (ChildIndexOutOfBoundsExceptions e) {
|
||
188 | e.printStackTrace(); |
||
189 | return;
|
||
190 | } catch (NodeException e) {
|
||
191 | } |
||
192 | |||
193 | } |
||
194 | |||
195 | _blendings.pop(); |
||
196 | _content.endMultiSolid(multiSolid); |
||
197 | |||
198 | } |
||
199 | |||
200 | else if (root.className().equals("Group")) { |
||
201 | Group rootGroup;
|
||
202 | |||
203 | try {
|
||
204 | rootGroup = new Group(root.getCPtr()); |
||
205 | } catch (NodeException e) {
|
||
206 | // TODO Auto-generated catch block
|
||
207 | e.printStackTrace(); |
||
208 | return;
|
||
209 | } |
||
210 | try {
|
||
211 | addedTextures = pushTextures(rootGroup); |
||
212 | } catch (NodeException e) {
|
||
213 | } |
||
214 | try {
|
||
215 | addedMaterial = pushMaterial(rootGroup); |
||
216 | } catch (NodeException e) {
|
||
217 | } |
||
218 | _blendings.push(rootGroup.getOrCreateStateSet().getEnabledBlending()); |
||
219 | |||
220 | 23296 | jcampos | //System.out.println("CHILDREN: " + rootGroup.getNumChildren());
|
221 | |||
222 | 21503 | jcampos | for (int i = 0; i < rootGroup.getNumChildren(); i++) { |
223 | |||
224 | try {
|
||
225 | parseNode(rootGroup.getChild(i), multiGeometry); |
||
226 | } catch (ChildIndexOutOfBoundsExceptions e) {
|
||
227 | e.printStackTrace(); |
||
228 | return;
|
||
229 | } |
||
230 | 22231 | jcampos | // Util.logger.log(Level.FINEST, "Node parsed");
|
231 | 21503 | jcampos | } |
232 | |||
233 | _blendings.pop(); |
||
234 | |||
235 | } |
||
236 | |||
237 | else {
|
||
238 | |||
239 | 22231 | jcampos | // Util.logger.log(Level.FINEST,
|
240 | // "First parsed object must be Group or Geode");
|
||
241 | 21503 | jcampos | |
242 | } |
||
243 | |||
244 | popTextures(addedTextures); |
||
245 | if (addedMaterial)
|
||
246 | _materials.pop(); |
||
247 | _content.endMultiGeometry(multiGeometry); |
||
248 | |||
249 | } |
||
250 | |||
251 | } |
||
252 | |||
253 | protected void parseNode(Node node, Object parent) { |
||
254 | 22231 | jcampos | // Util.logger.log(Level.FINEST, "node init");
|
255 | 21503 | jcampos | Node instance; |
256 | boolean isTransform = false; |
||
257 | boolean addedMaterial = false; |
||
258 | Vector<Integer> addedTextures = new Vector<Integer>(); |
||
259 | |||
260 | try {
|
||
261 | |||
262 | Class<?> reflect = Class.forName("org.gvsig.osgvp." |
||
263 | + node.className()); |
||
264 | Constructor<?> builder = reflect
|
||
265 | .getConstructor(new Class[] { long.class }); |
||
266 | instance = (Node) builder |
||
267 | .newInstance(new Object[] { node.getCPtr() }); |
||
268 | |||
269 | } |
||
270 | |||
271 | catch (Exception e) { |
||
272 | instance = new Node(node.getCPtr());
|
||
273 | } |
||
274 | |||
275 | if (instance instanceof Group) { |
||
276 | |||
277 | Object multiGeometry;
|
||
278 | multiGeometry = _content.startMultiGeometry("", ""); |
||
279 | _content.addGeometryToMultiGeometry(multiGeometry, parent); |
||
280 | isTransform = pushTransform((Group) instance);
|
||
281 | try {
|
||
282 | addedTextures = pushTextures(instance); |
||
283 | } catch (NodeException e) {
|
||
284 | } |
||
285 | try {
|
||
286 | addedMaterial = pushMaterial(instance); |
||
287 | } catch (NodeException e) {
|
||
288 | } |
||
289 | if (_blendings.lastElement())
|
||
290 | _blendings.push(true);
|
||
291 | else
|
||
292 | _blendings.push(instance.getOrCreateStateSet().getEnabledBlending()); |
||
293 | for (int i = 0; i < ((Group) instance).getNumChildren(); i++) { |
||
294 | |||
295 | try {
|
||
296 | parseNode(((Group) instance).getChild(i), multiGeometry);
|
||
297 | } catch (ChildIndexOutOfBoundsExceptions e) {
|
||
298 | e.printStackTrace(); |
||
299 | return;
|
||
300 | } |
||
301 | |||
302 | } |
||
303 | if (isTransform)
|
||
304 | _transforms.pop(); |
||
305 | if (addedMaterial)
|
||
306 | _materials.pop(); |
||
307 | _blendings.pop(); |
||
308 | popTextures(addedTextures); |
||
309 | _content.endMultiGeometry(multiGeometry); |
||
310 | |||
311 | } |
||
312 | |||
313 | else if (instance instanceof Geode) { |
||
314 | |||
315 | Object multiSolid;
|
||
316 | multiSolid = _content.startMultiSolid("", ""); |
||
317 | _content.addGeometryToMultiGeometry(multiSolid, parent); |
||
318 | try {
|
||
319 | addedTextures = pushTextures(instance); |
||
320 | } catch (NodeException e) {
|
||
321 | } |
||
322 | try {
|
||
323 | addedMaterial = pushMaterial(instance); |
||
324 | } catch (NodeException e) {
|
||
325 | } |
||
326 | if (_blendings.lastElement())
|
||
327 | _blendings.push(true);
|
||
328 | else
|
||
329 | _blendings.push(instance.getOrCreateStateSet().getEnabledBlending()); |
||
330 | for (int i = 0; i < ((Geode) instance).getNumDrawables(); i++) { |
||
331 | |||
332 | try {
|
||
333 | parseDrawable(((Geode) instance).getDrawable(i), multiSolid); |
||
334 | } catch (ChildIndexOutOfBoundsExceptions e) {
|
||
335 | e.printStackTrace(); |
||
336 | return;
|
||
337 | } catch (NodeException e) {
|
||
338 | } |
||
339 | |||
340 | } |
||
341 | if (addedMaterial)
|
||
342 | _materials.pop(); |
||
343 | _blendings.pop(); |
||
344 | popTextures(addedTextures); |
||
345 | _content.endMultiSolid(multiSolid); |
||
346 | |||
347 | } |
||
348 | 22231 | jcampos | // Util.logger.log(Level.FINEST, "Node parsed-0");
|
349 | 21503 | jcampos | // else Util.logger.log(Level.FINEST,instance.className() + " is not
|
350 | // instanceof
|
||
351 | // Group or Geode");
|
||
352 | |||
353 | } |
||
354 | |||
355 | protected void parseDrawable(Drawable drawable, Object feature) |
||
356 | throws NodeException {
|
||
357 | Util.logger.log(Level.FINEST, "drawable init"); |
||
358 | int t = 0; |
||
359 | if (drawable instanceof Geometry) { |
||
360 | |||
361 | int j;
|
||
362 | Object solid;
|
||
363 | solid = _content.startSolid("", ""); |
||
364 | _content.addSolidToMultiSolid(solid, feature); |
||
365 | |||
366 | Matrix matrix = new Matrix();
|
||
367 | for (int i = 0; i < _transforms.size(); i++) { |
||
368 | |||
369 | matrix.mult(_transforms.get(i), matrix); |
||
370 | |||
371 | } |
||
372 | |||
373 | 22231 | jcampos | // Util.logger.log(Level.FINEST, "transforms");
|
374 | 21503 | jcampos | |
375 | Vector<Vec3> vertices = ((Geometry) drawable).getVertexArray();
|
||
376 | if (vertices == null) |
||
377 | vertices = new Vector<Vec3>(); |
||
378 | Vec3 vertex; |
||
379 | int i;
|
||
380 | 22231 | jcampos | // Util.logger.log(Level.FINEST, "vertex starts");
|
381 | 21503 | jcampos | _content.startSolidVertexArray(solid); |
382 | for (i = 0; i < vertices.size(); i++) { |
||
383 | |||
384 | vertex = vertices.get(i); |
||
385 | |||
386 | // Util.logger.log(Level.FINEST,"Vertice sin
|
||
387 | // transformar"+vertex.x()+";"+vertex.y()+";"+vertex.z()+";");
|
||
388 | vertex = matrix.prod(vertex, matrix); |
||
389 | |||
390 | // Util.logger.log(Level.FINEST,"Vertice
|
||
391 | // transformado"+vertex.x()+";"+vertex.y()+";"+vertex.z()+";");
|
||
392 | _content.addVertexToSolid(solid, vertex.x(), vertex.y(), vertex |
||
393 | .z()); |
||
394 | |||
395 | } |
||
396 | _content.endSolidVertexArray(); |
||
397 | |||
398 | Vector<Vec3> normals = ((Geometry) drawable).getNormalArray();
|
||
399 | 22231 | jcampos | // Util.logger.log(Level.FINEST, "talla normals" + normals.size());
|
400 | 21503 | jcampos | |
401 | if (normals == null) |
||
402 | normals = new Vector<Vec3>(); |
||
403 | |||
404 | Vec3 normal; |
||
405 | _content.startSolidNormalArray(solid); |
||
406 | |||
407 | for (i = 0; i < normals.size(); i++) { |
||
408 | |||
409 | normal = normals.get(i); |
||
410 | 22231 | jcampos | // Util.logger.log(Level.FINEST, " normal bucleee" + normal);
|
411 | 21503 | jcampos | normal = matrix.prod(normal, matrix); |
412 | normal.normalize(); |
||
413 | _content.addNormalToSolid(solid, normal.x(), normal.y(), normal |
||
414 | .z()); |
||
415 | |||
416 | } |
||
417 | _content.endSolidNormalArray(); |
||
418 | |||
419 | _content.setNormalBindingToSolid(solid, ((Geometry) drawable) |
||
420 | .getNormalBinding()); |
||
421 | |||
422 | try {
|
||
423 | Vector<Vec4> colors = ((Geometry) drawable).getColorArray();
|
||
424 | if (colors == null) |
||
425 | colors = new Vector<Vec4>(); |
||
426 | Vec4 color; |
||
427 | 22231 | jcampos | // Util.logger.log(Level.FINEST, "color starts" + colors.size());
|
428 | 21503 | jcampos | _content.startSolidColorArray(solid); |
429 | for (i = 0; i < colors.size(); i++) { |
||
430 | 22231 | jcampos | // Util.logger.log(Level.FINEST, "bucleeeeee 0");
|
431 | 21503 | jcampos | color = colors.get(i); |
432 | _content |
||
433 | .addColorToSolid(solid, (float) color.x(),
|
||
434 | (float) color.y(), (float) color.z(), |
||
435 | (float) color.w());
|
||
436 | 22231 | jcampos | //Util.logger.log(Level.FINEST, "bucleeeeee");
|
437 | 21503 | jcampos | } |
438 | |||
439 | _content.endSolidColorArray(); |
||
440 | |||
441 | } catch (NullPointerException e) { |
||
442 | |||
443 | |||
444 | |||
445 | } |
||
446 | |||
447 | 22231 | jcampos | //Util.logger.log(Level.FINEST, "primitiveSets: "
|
448 | // + ((Geometry) drawable).getNumPrimitiveSets());
|
||
449 | 21503 | jcampos | |
450 | for (int k = 0; k < ((Geometry) drawable).getNumPrimitiveSets(); k++) { |
||
451 | int mode = ((Geometry) drawable).getPrimitiveSet(k).getMode();
|
||
452 | int type = ((Geometry) drawable).getPrimitiveSet(k).getType();
|
||
453 | |||
454 | if (type == PrimitiveSet.Type.DrawArrayLengthsPrimitiveType) {
|
||
455 | |||
456 | int offset = 0; |
||
457 | int accum = 0; |
||
458 | DrawArrayLengths lengths = new DrawArrayLengths(
|
||
459 | ((PrimitiveSet) ((Geometry) drawable) |
||
460 | .getPrimitiveSet(k)).getCPtr()); |
||
461 | Vector<Integer> arrayLengths = lengths |
||
462 | .getElementLengthsVector(); |
||
463 | |||
464 | if (arrayLengths == null) |
||
465 | arrayLengths = new Vector<Integer>(); |
||
466 | |||
467 | for (int h = 0; h < arrayLengths.size(); h++) { |
||
468 | |||
469 | Object primitiveSet = _content.startPrimitiveSet(mode,
|
||
470 | PrimitiveSet.Type.DrawArraysPrimitiveType); |
||
471 | _content.addPrimitiveSetToSolid(solid, primitiveSet); |
||
472 | |||
473 | accum = arrayLengths.get(h); |
||
474 | |||
475 | for (int l = 0; l < accum; l++) { |
||
476 | |||
477 | _content.addIndexToPrimitiveSet(primitiveSet, |
||
478 | ((Geometry) drawable).getPrimitiveSet(k) |
||
479 | .index(offset + l)); |
||
480 | |||
481 | } |
||
482 | |||
483 | offset = offset + accum; |
||
484 | _content.endPrimitiveSetIndexArray(); |
||
485 | _content.endPrimitiveSet(primitiveSet); |
||
486 | |||
487 | } |
||
488 | |||
489 | } else {
|
||
490 | Object primitiveSet = _content
|
||
491 | .startPrimitiveSet(mode, type); |
||
492 | |||
493 | _content.addPrimitiveSetToSolid(solid, primitiveSet); |
||
494 | _content.startPrimitiveSetIndexArray(primitiveSet, |
||
495 | ((Geometry) drawable).getPrimitiveSet(k) |
||
496 | .getNumIndices()); |
||
497 | 22231 | jcampos | //Util.logger.log(Level.FINEST, "Num Indices: "
|
498 | // + String.valueOf(((Geometry) drawable)
|
||
499 | // .getPrimitiveSet(k).getNumIndices()));
|
||
500 | 21503 | jcampos | for (int ps = 0; ps < ((Geometry) drawable) |
501 | .getPrimitiveSet(k).getNumIndices(); ps++) { |
||
502 | |||
503 | 22231 | jcampos | //Util.logger.log(Level.FINEST, "adding index");
|
504 | 21503 | jcampos | _content.addIndexToPrimitiveSet(primitiveSet, |
505 | ((Geometry) drawable).getPrimitiveSet(k).index( |
||
506 | ps)); |
||
507 | 22231 | jcampos | //Util.logger.log(Level.FINEST, String.valueOf(t));
|
508 | // t++;
|
||
509 | 21503 | jcampos | } |
510 | _content.endPrimitiveSetIndexArray(); |
||
511 | _content.endPrimitiveSet(primitiveSet); |
||
512 | } |
||
513 | |||
514 | } |
||
515 | |||
516 | Vector<Integer> addedTextures = new Vector<Integer>(); |
||
517 | boolean addedMaterial = false; |
||
518 | |||
519 | addedTextures = pushTexturesDrawable(drawable); |
||
520 | |||
521 | 22231 | jcampos | //Util.logger.log(Level.FINEST, "Added textures: " + addedTextures);
|
522 | 21503 | jcampos | |
523 | try {
|
||
524 | addTexturesToGeometry(solid); |
||
525 | } catch (Exception e) { |
||
526 | |||
527 | e.printStackTrace(); |
||
528 | } |
||
529 | |||
530 | 22231 | jcampos | //Util.logger.log(Level.FINEST, "getNumTextures"
|
531 | // + drawable.getOrCreateStateSet().getNumTextureStages());
|
||
532 | 21503 | jcampos | for (j = 0; j < drawable.getOrCreateStateSet().getNumTextureStages(); j++) { |
533 | |||
534 | try {
|
||
535 | if (drawable.getOrCreateStateSet().getTextureAttribute(j) != null) { |
||
536 | Vector<Vec2> texCoords = ((Geometry) drawable)
|
||
537 | .getTexCoordArray(j); |
||
538 | if (texCoords == null) |
||
539 | texCoords = new Vector<Vec2>(); |
||
540 | Vec2 texCoord; |
||
541 | |||
542 | _content |
||
543 | .startSolidTexCoordArray(solid, texCoords.size(), j); |
||
544 | |||
545 | for (i = 0; i < texCoords.size(); i++) { |
||
546 | |||
547 | texCoord = texCoords.get(i); |
||
548 | _content.addTextureCoordinateToSolid(solid, texCoord |
||
549 | .x(), texCoord.y(), j); |
||
550 | |||
551 | } |
||
552 | _content.endSolidTexCoordArray(); |
||
553 | } |
||
554 | } catch (TextureStageException e) {
|
||
555 | // TODO Auto-generated catch block
|
||
556 | e.printStackTrace(); |
||
557 | } |
||
558 | } |
||
559 | |||
560 | addedMaterial = pushMaterialDrawable(drawable); |
||
561 | try {
|
||
562 | |||
563 | Material osgMaterial = _materials.lastElement(); |
||
564 | |||
565 | float x, y, z, w;
|
||
566 | |||
567 | Vec4 vectorAmb = new Vec4();
|
||
568 | Vec4 vectorDif = new Vec4();
|
||
569 | Vec4 vectorEmi = new Vec4();
|
||
570 | Vec4 vectorSpe = new Vec4();
|
||
571 | |||
572 | vectorAmb = osgMaterial.getAmbient(Material.Face.FRONT); |
||
573 | vectorDif = osgMaterial.getDiffuse(Material.Face.FRONT); |
||
574 | vectorEmi = osgMaterial.getEmission(Material.Face.FRONT); |
||
575 | vectorSpe = osgMaterial.getSpecular(Material.Face.FRONT); |
||
576 | |||
577 | Object material = _content.startMaterial();
|
||
578 | x = (float) vectorAmb.x();
|
||
579 | y = (float) vectorAmb.y();
|
||
580 | z = (float) vectorAmb.z();
|
||
581 | w = (float) vectorAmb.w();
|
||
582 | |||
583 | _content.addAmbientToMaterial(material, x, y, z, w); |
||
584 | |||
585 | x = (float) vectorDif.x();
|
||
586 | y = (float) vectorDif.y();
|
||
587 | z = (float) vectorDif.z();
|
||
588 | w = (float) vectorDif.w();
|
||
589 | |||
590 | _content.addDiffuseToMaterial(material, x, y, z, w); |
||
591 | |||
592 | x = (float) vectorSpe.x();
|
||
593 | y = (float) vectorSpe.y();
|
||
594 | z = (float) vectorSpe.z();
|
||
595 | w = (float) vectorSpe.w();
|
||
596 | |||
597 | _content.addSpecularToMaterial(material, x, y, z, w); |
||
598 | |||
599 | x = (float) vectorEmi.x();
|
||
600 | y = (float) vectorEmi.y();
|
||
601 | z = (float) vectorEmi.z();
|
||
602 | w = (float) vectorEmi.w();
|
||
603 | |||
604 | _content.addEmissionToMaterial(material, x, y, z, w); |
||
605 | |||
606 | _content.addShininessToMaterial(material, osgMaterial |
||
607 | .getShininess(Material.Face.FRONT)); |
||
608 | |||
609 | _content.addMaterialToSolid(solid, material); |
||
610 | |||
611 | _content.endMaterial(material); |
||
612 | |||
613 | } catch (ArrayIndexOutOfBoundsException ex) { |
||
614 | |||
615 | } |
||
616 | |||
617 | if (_blendings.lastElement())
|
||
618 | _content.addBlendingToSolid(solid, true);
|
||
619 | else
|
||
620 | _content.addBlendingToSolid(solid, drawable.getOrCreateStateSet() |
||
621 | .getEnabledBlending()); |
||
622 | |||
623 | popTextures(addedTextures); |
||
624 | |||
625 | _content.endSolid(solid); |
||
626 | |||
627 | } |
||
628 | 22231 | jcampos | //Util.logger.log(Level.FINEST, "drawable parsed");
|
629 | 21503 | jcampos | } |
630 | |||
631 | protected boolean pushTransform(Group group) { |
||
632 | |||
633 | if (group instanceof MatrixTransform) { |
||
634 | |||
635 | // Util.logger.log(Level.FINEST,"Push a MatrixTransform");
|
||
636 | _transforms.push(((MatrixTransform) group).getMatrix()); |
||
637 | |||
638 | return true; |
||
639 | |||
640 | } |
||
641 | |||
642 | if (group instanceof PositionAttitudeTransform) { |
||
643 | |||
644 | // Util.logger.log(Level.FINEST,"Push a PAT");
|
||
645 | Matrix scaleMatrix = Matrix |
||
646 | .scale(((PositionAttitudeTransform) group).getScale()); |
||
647 | |||
648 | Quat quat = new Quat((((PositionAttitudeTransform) group)
|
||
649 | .getAttitudeAngle()), (((PositionAttitudeTransform) group) |
||
650 | .getAttitudeAxis())); |
||
651 | Matrix rotateMatrix = Matrix.rotate(quat); |
||
652 | |||
653 | Matrix transMatrix = Matrix |
||
654 | .translate(((PositionAttitudeTransform) group) |
||
655 | .getPosition()); |
||
656 | |||
657 | 23296 | jcampos | // rotateMatrix.postMult(scaleMatrix);
|
658 | // transMatrix.postMult(rotateMatrix);
|
||
659 | rotateMatrix.preMult(scaleMatrix); |
||
660 | transMatrix.preMult(rotateMatrix); |
||
661 | 21503 | jcampos | |
662 | _transforms.push(transMatrix); |
||
663 | |||
664 | return true; |
||
665 | |||
666 | } |
||
667 | |||
668 | if (group instanceof AutoTransform) { |
||
669 | |||
670 | // Util.logger.log(Level.FINEST,"Push a AutoTransform");
|
||
671 | Matrix scaleMatrix = Matrix.scale(((AutoTransform) group) |
||
672 | .getScale()); |
||
673 | Quat quat = new Quat((((AutoTransform) group).getRotationAngle()),
|
||
674 | (((AutoTransform) group).getRotationAxis())); |
||
675 | Matrix rotateMatrix = Matrix.rotate(quat); |
||
676 | Matrix transMatrix = Matrix.translate(((AutoTransform) group) |
||
677 | .getPosition()); |
||
678 | |||
679 | 23296 | jcampos | // rotateMatrix.postMult(scaleMatrix);
|
680 | // transMatrix.postMult(rotateMatrix);
|
||
681 | |||
682 | rotateMatrix.preMult(scaleMatrix); |
||
683 | transMatrix.preMult(rotateMatrix); |
||
684 | 21503 | jcampos | |
685 | _transforms.push(transMatrix); |
||
686 | |||
687 | return true; |
||
688 | |||
689 | } |
||
690 | |||
691 | return false; |
||
692 | |||
693 | } |
||
694 | |||
695 | protected Vector<Integer> pushTextures(Node node) throws NodeException { |
||
696 | |||
697 | int numTextures;
|
||
698 | numTextures = node.getOrCreateStateSet().getNumTextureStages(); |
||
699 | Vector<Texture2D> vector = node.getOrCreateStateSet().getTextureAttributeVector();
|
||
700 | Vector<Integer> stages = new Vector<Integer>(); |
||
701 | |||
702 | for (int i = 0; i < numTextures; i++) { |
||
703 | |||
704 | _textures.get(i); |
||
705 | |||
706 | _textures.add(i, new Stack<Texture2D>()); |
||
707 | |||
708 | _textures.get(i).push(vector.get(i)); |
||
709 | // Util.logger.log(Level.FINEST,"PUSH");
|
||
710 | stages.add(i); |
||
711 | |||
712 | } |
||
713 | |||
714 | return stages;
|
||
715 | } |
||
716 | |||
717 | protected boolean pushMaterial(Node node) throws NodeException { |
||
718 | |||
719 | boolean hasMaterial = false; |
||
720 | |||
721 | Material material; |
||
722 | material = node.getOrCreateStateSet().getMaterial(); |
||
723 | _materials.push(material); |
||
724 | hasMaterial = true;
|
||
725 | |||
726 | return hasMaterial;
|
||
727 | |||
728 | } |
||
729 | |||
730 | protected Vector<Integer> pushTexturesDrawable(Drawable drawable) |
||
731 | throws NodeException {
|
||
732 | |||
733 | int numTextures;
|
||
734 | numTextures = drawable.getOrCreateStateSet().getNumTextureStages(); |
||
735 | Vector<Texture2D> vector = drawable.getOrCreateStateSet().getTextureAttributeVector();
|
||
736 | Vector<Integer> stages = new Vector<Integer>(); |
||
737 | |||
738 | 22231 | jcampos | //Util.logger.log(Level.FINEST, "NumTextures: " + numTextures);
|
739 | 21503 | jcampos | for (int i = 0; i < numTextures; i++) { |
740 | |||
741 | try {
|
||
742 | |||
743 | _textures.get(i); |
||
744 | |||
745 | } catch (ArrayIndexOutOfBoundsException ex) { |
||
746 | |||
747 | _textures.add(i, new Stack<Texture2D>()); |
||
748 | |||
749 | } |
||
750 | |||
751 | try {
|
||
752 | |||
753 | _textures.get(i).push(vector.get(i)); |
||
754 | // Util.logger.log(Level.FINEST,"PUSH");
|
||
755 | stages.add(i); |
||
756 | Util.logger.log(Level.FINEST, "Added textures: " + stages); |
||
757 | } catch (ArrayIndexOutOfBoundsException ex) { |
||
758 | |||
759 | } |
||
760 | } |
||
761 | |||
762 | return stages;
|
||
763 | } |
||
764 | |||
765 | protected boolean pushMaterialDrawable(Drawable draw) throws NodeException { |
||
766 | |||
767 | boolean hasMaterial = false; |
||
768 | |||
769 | try {
|
||
770 | |||
771 | Material material; |
||
772 | material = draw.getOrCreateStateSet().getMaterial(); |
||
773 | _materials.push(material); |
||
774 | hasMaterial = true;
|
||
775 | |||
776 | } catch (ArrayIndexOutOfBoundsException ex) { |
||
777 | |||
778 | hasMaterial = false;
|
||
779 | |||
780 | } catch (NullPointerException e) { |
||
781 | |||
782 | } |
||
783 | |||
784 | return hasMaterial;
|
||
785 | |||
786 | } |
||
787 | |||
788 | protected void popTextures(Vector<Integer> stages) { |
||
789 | |||
790 | for (int i = 0; i < stages.size(); i++) { |
||
791 | |||
792 | // Util.logger.log(Level.FINEST,"POP");
|
||
793 | _textures.get(stages.get(i)).pop(); |
||
794 | |||
795 | } |
||
796 | |||
797 | } |
||
798 | |||
799 | protected void addTexturesToGeometry(Object solid) throws Exception { |
||
800 | |||
801 | BufferedImage image;
|
||
802 | for (int i = 0; i < _textures.size(); i++) { |
||
803 | |||
804 | try {
|
||
805 | 22231 | jcampos | //Util.logger.log(Level.FINEST, "Num textures in stack: "
|
806 | // + _textures.get(i).size());
|
||
807 | 21503 | jcampos | _textures.get(i).lastElement(); |
808 | try {
|
||
809 | 22231 | jcampos | //Util.logger.log(Level.FINEST, "TRYING STAGE: " + i);
|
810 | 21503 | jcampos | image = _textures.get(i).lastElement().getImage() |
811 | .getBufferedImage(); |
||
812 | _content.addTextureToSolid(solid, i, image); |
||
813 | |||
814 | } catch (ArrayIndexOutOfBoundsException ex) { |
||
815 | |||
816 | } |
||
817 | } |
||
818 | |||
819 | catch (NoSuchElementException ex) { |
||
820 | |||
821 | } |
||
822 | } |
||
823 | |||
824 | } |
||
825 | 21782 | jcampos | |
826 | 22177 | jcampos | @Override
|
827 | public String getFormat() { |
||
828 | // TODO Auto-generated method stub
|
||
829 | return null; |
||
830 | } |
||
831 | |||
832 | 21782 | jcampos | |
833 | 21503 | jcampos | } |