Revision 249
1.10/tags/gvSIG_3D_Animation_1_10_build_21_FINAL/libraries/libGPE-OSG/src/test/java/org/gvsig/gpe/osg/TestOSGParser.java | ||
---|---|---|
1 |
/* 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.io.FileNotFoundException; |
|
32 |
import java.net.URI; |
|
33 |
import java.net.URISyntaxException; |
|
34 |
|
|
35 |
import junit.framework.TestCase; |
|
36 |
|
|
37 |
import org.gvsig.driver.OSGDriver; |
|
38 |
|
|
39 |
import org.gvsig.osgvp.util.Util; |
|
40 |
|
|
41 |
public class TestOSGParser extends TestCase { |
|
42 |
|
|
43 |
public void testParseURI() { |
|
44 |
//fail("Not yet implemented"); |
|
45 |
} |
|
46 |
|
|
47 |
public void testParseStream() { |
|
48 |
// TODO: This is the real work. |
|
49 |
OSGDriver osgDriver = new OSGDriver(); |
|
50 |
OSGParser parser = new OSGParser("OSG","OSG File Formats Parser"); |
|
51 |
try { |
|
52 |
parser.parse(osgDriver, null, Util.extractResource("/cessna.osg").toURI()); |
|
53 |
} catch (FileNotFoundException e) { |
|
54 |
fail("Resource not found"); |
|
55 |
} |
|
56 |
// osgDriver.getRootFeature(); |
|
57 |
} |
|
58 |
|
|
59 |
public void testOSGParser() { |
|
60 |
// TODO: This is the constructor, test only for creation |
|
61 |
// or something similar. |
|
62 |
// fail("Not yet implemented"); |
|
63 |
} |
|
64 |
|
|
65 |
public void testAcceptURI() { |
|
66 |
OSGParser parser = new OSGParser("OSG","OSG File Formats Parser"); |
|
67 |
URI uri; |
|
68 |
try { |
|
69 |
uri = Util.extractResource("/cessna.osg").toURI(); |
|
70 |
assertEquals(parser.accept(uri),true); |
|
71 |
uri = new URI("pepe.com"); |
|
72 |
assertEquals(parser.accept(uri),false); |
|
73 |
|
|
74 |
} catch (FileNotFoundException e) { |
|
75 |
// TODO Auto-generated catch block |
|
76 |
fail(); |
|
77 |
} catch (URISyntaxException e) { |
|
78 |
// TODO Auto-generated catch block |
|
79 |
fail(); |
|
80 |
} |
|
81 |
|
|
82 |
} |
|
83 |
|
|
84 |
public void testGetFormats() { |
|
85 |
String[] formats = new String[4]; |
|
86 |
OSGParser parser = new OSGParser("OSG","OSG File Formats Parser"); |
|
87 |
formats[0] = "OSG"; |
|
88 |
formats[1] = "3DS"; |
|
89 |
formats[2] = "OBJ"; |
|
90 |
formats[3] = "IVE"; |
|
91 |
|
|
92 |
for(int i=0; i<formats.length; i++){ |
|
93 |
|
|
94 |
assertEquals(formats[i].equals(parser.getFormats()[i]),true); |
|
95 |
|
|
96 |
|
|
97 |
} |
|
98 |
} |
|
99 |
|
|
100 |
public void testGetVersions() { |
|
101 |
String[] versions = new String[1]; |
|
102 |
versions[0] = "All"; |
|
103 |
OSGParser parser = new OSGParser("OSG","OSG File Formats Parser"); |
|
104 |
|
|
105 |
for(int i=0; i<versions.length; i++){ |
|
106 |
|
|
107 |
assertEquals(versions[i].equals(parser.getVersions()[i]),true); |
|
108 |
|
|
109 |
|
|
110 |
} |
|
111 |
} |
|
112 |
|
|
113 |
} |
1.10/tags/gvSIG_3D_Animation_1_10_build_21_FINAL/libraries/libGPE-OSG/src/main/java/org/gvsig/driver/OSGDriver.java | ||
---|---|---|
1 |
/* 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 |
* |
|
30 |
*/ |
|
31 |
package org.gvsig.driver; |
|
32 |
|
|
33 |
import java.awt.Color; |
|
34 |
import java.awt.geom.Point2D; |
|
35 |
import java.awt.image.BufferedImage; |
|
36 |
import java.util.Vector; |
|
37 |
import java.util.logging.Level; |
|
38 |
|
|
39 |
import org.gvsig.GeometryFactory3D; |
|
40 |
import org.gvsig.fmap.geom.GeometryManager; |
|
41 |
import org.gvsig.fmap.geom.primitive.AbstractPrimitive; |
|
42 |
import org.gvsig.geometries3D.Material; |
|
43 |
import org.gvsig.geometries3D.MultiGeometry; |
|
44 |
import org.gvsig.geometries3D.MultiSolid; |
|
45 |
import org.gvsig.geometries3D.Point3D; |
|
46 |
import org.gvsig.geometries3D.PrimitiveSet; |
|
47 |
import org.gvsig.geometries3D.Solid; |
|
48 |
import org.gvsig.gpe.IGPEContentHandler3D; |
|
49 |
import org.gvsig.gpe.parser.GPEContentHandler; |
|
50 |
import org.gvsig.osgvp.util.Util; |
|
51 |
|
|
52 |
/** |
|
53 |
* @author rgaitan |
|
54 |
* |
|
55 |
*/ |
|
56 |
public class OSGDriver extends GPEContentHandler implements |
|
57 |
IGPEContentHandler3D { |
|
58 |
|
|
59 |
private MultiGeometry _root; |
|
60 |
|
|
61 |
@Override |
|
62 |
public Object startMultiGeometry(String id, String srs) { |
|
63 |
Util.logger.log(Level.FINEST, "Starting MultiGeometry"); |
|
64 |
MultiGeometry feature = (MultiGeometry) ((GeometryFactory3D) GeometryManager |
|
65 |
.getInstance().getGeometryFactory()).createMultiGeometry(id); |
|
66 |
if (_root == null) |
|
67 |
_root = feature; |
|
68 |
return feature; |
|
69 |
} |
|
70 |
|
|
71 |
public Object startMultiSolid(String id, String srs) { |
|
72 |
Util.logger.log(Level.FINEST, "Starting MultiSolid"); |
|
73 |
return (MultiSolid) ((GeometryFactory3D) GeometryManager.getInstance() |
|
74 |
.getGeometryFactory()).createMultiSolid(id); |
|
75 |
} |
|
76 |
|
|
77 |
public void endMultiSolid(Object solid) { |
|
78 |
} |
|
79 |
|
|
80 |
public Object startSolid(String id, String srs) { |
|
81 |
// Util.logger.log(Level.FINEST,"Starting Geometry"); |
|
82 |
Solid geometry = (Solid) ((GeometryFactory3D) GeometryManager |
|
83 |
.getInstance().getGeometryFactory()).createSolid(id); |
|
84 |
return geometry; |
|
85 |
} |
|
86 |
|
|
87 |
public void endSolid(Object geometry) { |
|
88 |
// Util.logger.log(Level.FINEST,"Ending Geometry"); |
|
89 |
|
|
90 |
} |
|
91 |
|
|
92 |
@Override |
|
93 |
public void addGeometryToMultiGeometry(Object geometry, Object multiGeometry) { |
|
94 |
// Util.logger.log(Level.FINEST,"Adding Feature to Feature"); |
|
95 |
((MultiGeometry) multiGeometry) |
|
96 |
.addGeometry((AbstractPrimitive) geometry); |
|
97 |
} |
|
98 |
|
|
99 |
public void addSolidToMultiSolid(Object solid, Object multiSolid) { |
|
100 |
// Util.logger.log(Level.FINEST,"Adding Geometry to Feature"); |
|
101 |
((MultiSolid) multiSolid).addSolid((Solid) solid); |
|
102 |
} |
|
103 |
|
|
104 |
public void startSolidVertexArray(Object geometry) { |
|
105 |
} |
|
106 |
|
|
107 |
public void endSolidVertexArray() { |
|
108 |
} |
|
109 |
|
|
110 |
public void startSolidNormalArray(Object geometry) { |
|
111 |
} |
|
112 |
|
|
113 |
public void endSolidNormalArray() { |
|
114 |
} |
|
115 |
|
|
116 |
public void startSolidColorArray(Object geometry) { |
|
117 |
|
|
118 |
} |
|
119 |
|
|
120 |
public void endSolidColorArray() { |
|
121 |
} |
|
122 |
|
|
123 |
public void startSolidTexCoordArray(Object geometry, int nTexCoords, |
|
124 |
int stage) { |
|
125 |
} |
|
126 |
|
|
127 |
public void endSolidTexCoordArray() { |
|
128 |
} |
|
129 |
|
|
130 |
public void addVertexToSolid(Object geometry, double x, double y, double z) { |
|
131 |
|
|
132 |
Point3D point = (Point3D) ((GeometryFactory3D) GeometryManager |
|
133 |
.getInstance().getGeometryFactory()).createPoint3D(""); |
|
134 |
point.setX(x); |
|
135 |
point.setY(y); |
|
136 |
point.setZ(z); |
|
137 |
((Solid) geometry).getVertices().add(point); |
|
138 |
// Util.logger.log(Level.FINEST,"Adding Vertex"); |
|
139 |
} |
|
140 |
|
|
141 |
public void addNormalToSolid(Object geometry, double x, double y, double z) { |
|
142 |
Point3D point = (Point3D) ((GeometryFactory3D) GeometryManager |
|
143 |
.getInstance().getGeometryFactory()).createPoint3D(""); |
|
144 |
point.setX(x); |
|
145 |
point.setY(y); |
|
146 |
point.setZ(z); |
|
147 |
((Solid) geometry).getNormals().add(point); |
|
148 |
Util.logger.log(Level.FINEST, "Adding Normal" + point.getX()); |
|
149 |
} |
|
150 |
|
|
151 |
public void addTextureToSolid(Object geometry, int stage, |
|
152 |
BufferedImage image) { |
|
153 |
|
|
154 |
((Solid) geometry).getTextures().add(stage, image); |
|
155 |
|
|
156 |
} |
|
157 |
|
|
158 |
public void setNormalBindingToSolid(Object geometry, int mode) { |
|
159 |
|
|
160 |
((Solid) geometry).setNormalBinding(mode); |
|
161 |
|
|
162 |
} |
|
163 |
|
|
164 |
public void setColorBindingToSolid(Object geometry, int mode) { |
|
165 |
|
|
166 |
((Solid) geometry).setColorBinding(mode); |
|
167 |
|
|
168 |
} |
|
169 |
|
|
170 |
public void addColorToSolid(Object geometry, float r, float g, float b, |
|
171 |
float a) { |
|
172 |
|
|
173 |
((Solid) geometry).getColors().add(convertColor(r, g, b, a)); |
|
174 |
|
|
175 |
} |
|
176 |
|
|
177 |
public void addTextureCoordinateToSolid(Object geometry, double x, |
|
178 |
double y, int stage) { |
|
179 |
try { |
|
180 |
((Solid) geometry).getTexcoord().get(stage); |
|
181 |
} catch (ArrayIndexOutOfBoundsException ex) { |
|
182 |
((Solid) geometry).getTexcoord().add(stage, new Vector<Point2D>()); |
|
183 |
} |
|
184 |
Point2D point = new Point2D.Double(); |
|
185 |
point.setLocation(x, y); |
|
186 |
((Solid) geometry).getTexcoord().get(stage).add(point); |
|
187 |
|
|
188 |
} |
|
189 |
|
|
190 |
public MultiGeometry getRootFeature() { |
|
191 |
return _root; |
|
192 |
} |
|
193 |
|
|
194 |
public void addPrimitiveSetToSolid(Object geometry, Object primitiveSet) { |
|
195 |
((Solid) geometry).getPrimitiveSets().add((PrimitiveSet) primitiveSet); |
|
196 |
} |
|
197 |
|
|
198 |
public void addIndexToPrimitiveSet(Object primitiveSet, int i) { |
|
199 |
((PrimitiveSet) primitiveSet).getIndices().add(i); |
|
200 |
} |
|
201 |
|
|
202 |
public void endPrimitiveSet(Object primitiveSet) { |
|
203 |
} |
|
204 |
|
|
205 |
public void endPrimitiveSetIndexArray() { |
|
206 |
} |
|
207 |
|
|
208 |
public Object startPrimitiveSet(int mode, int type) { |
|
209 |
return new PrimitiveSet(mode, type); |
|
210 |
} |
|
211 |
|
|
212 |
public void startPrimitiveSetIndexArray(Object primitiveSet, int indices) { |
|
213 |
((PrimitiveSet) primitiveSet).setIndices(new Vector<Integer>()); |
|
214 |
} |
|
215 |
|
|
216 |
public void addMaterialToSolid(Object solid, Object material) { |
|
217 |
((Solid) solid).setMaterial((Material) material); |
|
218 |
|
|
219 |
} |
|
220 |
|
|
221 |
public void endMaterial(Object material) { |
|
222 |
|
|
223 |
} |
|
224 |
|
|
225 |
public Object startMaterial() { |
|
226 |
Material material = new Material(); |
|
227 |
return material; |
|
228 |
} |
|
229 |
|
|
230 |
public void addAmbientToMaterial(Object material, float r, float g, |
|
231 |
float b, float a) { |
|
232 |
|
|
233 |
((Material) material).setAmbient(convertColor(r, g, b, a)); |
|
234 |
|
|
235 |
} |
|
236 |
|
|
237 |
public void addDiffuseToMaterial(Object material, float r, float g, |
|
238 |
float b, float a) { |
|
239 |
|
|
240 |
((Material) material).setDiffuse(convertColor(r, g, b, a)); |
|
241 |
|
|
242 |
} |
|
243 |
|
|
244 |
public void addEmissionToMaterial(Object material, float r, float g, |
|
245 |
float b, float a) { |
|
246 |
|
|
247 |
((Material) material).setEmission(convertColor(r, g, b, a)); |
|
248 |
|
|
249 |
} |
|
250 |
|
|
251 |
public void addShininessToMaterial(Object material, float s) { |
|
252 |
|
|
253 |
((Material) material).setShininess(s); |
|
254 |
|
|
255 |
} |
|
256 |
|
|
257 |
public void addSpecularToMaterial(Object material, float r, float g, |
|
258 |
float b, float a) { |
|
259 |
|
|
260 |
((Material) material).setSpecular(convertColor(r, g, b, a)); |
|
261 |
|
|
262 |
} |
|
263 |
|
|
264 |
protected Color convertColor(float r, float g, float b, float a) { |
|
265 |
|
|
266 |
int _r, _g, _b, _a; |
|
267 |
|
|
268 |
_r = Math.min((int) (r * 255), 255); |
|
269 |
_g = Math.min((int) (g * 255), 255); |
|
270 |
_b = Math.min((int) (b * 255), 255); |
|
271 |
_a = Math.min((int) (a * 255), 255); |
|
272 |
|
|
273 |
Color color = new Color(_r, _g, _b, _a); |
|
274 |
|
|
275 |
return color; |
|
276 |
|
|
277 |
} |
|
278 |
|
|
279 |
public void addBlendingToSolid(Object solid, boolean blending) { |
|
280 |
|
|
281 |
((Solid) solid).setBlending(blending); |
|
282 |
|
|
283 |
} |
|
284 |
|
|
285 |
} |
1.10/tags/gvSIG_3D_Animation_1_10_build_21_FINAL/libraries/libGPE-OSG/src/main/java/org/gvsig/gpe/IGPEContentHandler3D.java | ||
---|---|---|
1 |
/* 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; |
|
30 |
|
|
31 |
import java.awt.image.BufferedImage; |
|
32 |
|
|
33 |
import org.gvsig.gpe.parser.IGPEContentHandler; |
|
34 |
|
|
35 |
public interface IGPEContentHandler3D extends IGPEContentHandler { |
|
36 |
|
|
37 |
public Object startSolid(String id, String srs); |
|
38 |
|
|
39 |
public void endSolid(Object solid); |
|
40 |
|
|
41 |
public Object startMultiSolid(String id, String srs); |
|
42 |
|
|
43 |
public void endMultiSolid(Object solid); |
|
44 |
|
|
45 |
public void addSolidToMultiSolid(Object solid, Object feature); |
|
46 |
|
|
47 |
public void startSolidVertexArray(Object solid); |
|
48 |
|
|
49 |
public void endSolidVertexArray(); |
|
50 |
|
|
51 |
public void startSolidNormalArray(Object solid); |
|
52 |
|
|
53 |
public void endSolidNormalArray(); |
|
54 |
|
|
55 |
public void startSolidColorArray(Object solid); |
|
56 |
|
|
57 |
public void endSolidColorArray(); |
|
58 |
|
|
59 |
public void startSolidTexCoordArray(Object solid, int nTexCoords, int stage); |
|
60 |
|
|
61 |
public void endSolidTexCoordArray(); |
|
62 |
|
|
63 |
public void addVertexToSolid(Object solid, double x, double y, double z); |
|
64 |
|
|
65 |
public void addNormalToSolid(Object solid, double x, double y, double z); |
|
66 |
|
|
67 |
public void addTextureToSolid(Object geometry, int stage, BufferedImage image); |
|
68 |
|
|
69 |
public void setNormalBindingToSolid(Object solid, int mode ); |
|
70 |
|
|
71 |
public void setColorBindingToSolid(Object solid, int mode ); |
|
72 |
|
|
73 |
public void addColorToSolid(Object solid, float r, float g, float b, float a); |
|
74 |
|
|
75 |
public void addTextureCoordinateToSolid(Object solid, double x, double y, int stage); |
|
76 |
|
|
77 |
public Object startPrimitiveSet(int mode, int type); |
|
78 |
|
|
79 |
public void endPrimitiveSet(Object primitiveSet); |
|
80 |
|
|
81 |
public void addPrimitiveSetToSolid(Object solid, Object primitiveSet); |
|
82 |
|
|
83 |
public void startPrimitiveSetIndexArray(Object primitiveSet, int nIndices); |
|
84 |
|
|
85 |
public void endPrimitiveSetIndexArray(); |
|
86 |
|
|
87 |
public void addIndexToPrimitiveSet(Object primitiveSet, int i); |
|
88 |
|
|
89 |
public Object startMaterial(); |
|
90 |
|
|
91 |
public void addMaterialToSolid(Object solid, Object material); |
|
92 |
|
|
93 |
public void endMaterial(Object material); |
|
94 |
|
|
95 |
public void addAmbientToMaterial(Object material, float r, float g, float b, float a); |
|
96 |
|
|
97 |
public void addDiffuseToMaterial(Object material, float r, float g, float b, float a); |
|
98 |
|
|
99 |
public void addSpecularToMaterial(Object material, float r, float g, float b, float a); |
|
100 |
|
|
101 |
public void addEmissionToMaterial(Object material, float r, float g, float b, float a); |
|
102 |
|
|
103 |
public void addShininessToMaterial(Object material, float s); |
|
104 |
|
|
105 |
public void addBlendingToSolid(Object solid, boolean blending); |
|
106 |
|
|
107 |
} |
1.10/tags/gvSIG_3D_Animation_1_10_build_21_FINAL/libraries/libGPE-OSG/src/main/java/org/gvsig/gpe/osg/OSGParser.java | ||
---|---|---|
1 |
/* 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 |
package org.gvsig.gpe.osg; |
|
29 |
|
|
30 |
import java.awt.Color; |
|
31 |
import java.awt.image.BufferedImage; |
|
32 |
import java.io.FileNotFoundException; |
|
33 |
import java.lang.reflect.Constructor; |
|
34 |
import java.net.URI; |
|
35 |
import java.util.NoSuchElementException; |
|
36 |
import java.util.Stack; |
|
37 |
import java.util.Vector; |
|
38 |
import java.util.logging.Level; |
|
39 |
|
|
40 |
|
|
41 |
import org.gvsig.gpe.IGPEContentHandler3D; |
|
42 |
import org.gvsig.gpe.parser.GPEParser; |
|
43 |
import org.gvsig.osgvp.core.osg.AutoTransform; |
|
44 |
import org.gvsig.osgvp.core.osg.DrawArrayLengths; |
|
45 |
import org.gvsig.osgvp.core.osg.Drawable; |
|
46 |
import org.gvsig.osgvp.core.osg.Geode; |
|
47 |
import org.gvsig.osgvp.core.osg.Geometry; |
|
48 |
import org.gvsig.osgvp.core.osg.Group; |
|
49 |
import org.gvsig.osgvp.core.osg.Material; |
|
50 |
import org.gvsig.osgvp.core.osg.Matrix; |
|
51 |
import org.gvsig.osgvp.core.osg.MatrixTransform; |
|
52 |
import org.gvsig.osgvp.core.osg.Node; |
|
53 |
import org.gvsig.osgvp.core.osg.PositionAttitudeTransform; |
|
54 |
import org.gvsig.osgvp.core.osg.PrimitiveSet; |
|
55 |
import org.gvsig.osgvp.core.osg.Quat; |
|
56 |
import org.gvsig.osgvp.core.osg.Texture2D; |
|
57 |
import org.gvsig.osgvp.core.osg.Vec2; |
|
58 |
import org.gvsig.osgvp.core.osg.Vec3; |
|
59 |
import org.gvsig.osgvp.core.osg.Vec4; |
|
60 |
import org.gvsig.osgvp.core.osgdb.osgDB; |
|
61 |
import org.gvsig.osgvp.exceptions.image.ImageConversionException; |
|
62 |
import org.gvsig.osgvp.exceptions.image.ImageException; |
|
63 |
import org.gvsig.osgvp.exceptions.node.ChildIndexOutOfBoundsExceptions; |
|
64 |
import org.gvsig.osgvp.exceptions.node.LoadNodeException; |
|
65 |
import org.gvsig.osgvp.exceptions.node.NodeException; |
|
66 |
import org.gvsig.osgvp.exceptions.texture.TextureStageException; |
|
67 |
import org.gvsig.osgvp.util.Util; |
|
68 |
|
|
69 |
public class OSGParser extends GPEParser { |
|
70 |
|
|
71 |
Stack<Matrix> _transforms = new Stack<Matrix>(); |
|
72 |
Vector<Stack<Texture2D>> _textures = new Vector<Stack<Texture2D>>(); |
|
73 |
Stack<Material> _materials = new Stack<Material>(); |
|
74 |
Stack<Boolean> _blendings = new Stack<Boolean>(); |
|
75 |
private IGPEContentHandler3D _content; |
|
76 |
private String description; |
|
77 |
private String name; |
|
78 |
|
|
79 |
public OSGParser(String name, String description) { |
|
80 |
// super(name, description); |
|
81 |
this.name = name; |
|
82 |
this.description = description; |
|
83 |
} |
|
84 |
|
|
85 |
@Override |
|
86 |
public String getDescription() { |
|
87 |
// TODO Auto-generated method stub |
|
88 |
return this.description; |
|
89 |
} |
|
90 |
|
|
91 |
@Override |
|
92 |
public String getName() { |
|
93 |
// TODO Auto-generated method stub |
|
94 |
return this.name; |
|
95 |
} |
|
96 |
|
|
97 |
@Override |
|
98 |
public boolean accept(URI uri) { |
|
99 |
if ((uri.getPath().toUpperCase().endsWith("OSG")) |
|
100 |
|| (uri.getPath().toUpperCase().endsWith("3DS")) |
|
101 |
|| (uri.getPath().toUpperCase().endsWith("IVE")) |
|
102 |
|| (uri.getPath().toUpperCase().endsWith("OBJ"))) { |
|
103 |
return true; |
|
104 |
} |
|
105 |
return false; |
|
106 |
} |
|
107 |
|
|
108 |
public String[] getFormats() { |
|
109 |
String[] formats = new String[4]; |
|
110 |
formats[0] = "OSG"; |
|
111 |
formats[1] = "3DS"; |
|
112 |
formats[2] = "OBJ"; |
|
113 |
formats[3] = "IVE"; |
|
114 |
return formats; |
|
115 |
} |
|
116 |
|
|
117 |
public String[] getVersions() { |
|
118 |
String[] versions = new String[1]; |
|
119 |
versions[0] = "All"; |
|
120 |
return versions; |
|
121 |
} |
|
122 |
|
|
123 |
@Override |
|
124 |
protected void parseStream() { |
|
125 |
|
|
126 |
} |
|
127 |
|
|
128 |
@Override |
|
129 |
protected void parseURI() { |
|
130 |
|
|
131 |
// This is a very bad hack to avoid testing hangs in Mac OS Lepard |
|
132 |
Color color = new Color(0, 0, 0, 255); |
|
133 |
// TODO remove this hack |
|
134 |
|
|
135 |
Vector<Integer> addedTextures = new Vector<Integer>(); |
|
136 |
boolean addedMaterial = false; |
|
137 |
|
|
138 |
_content = (IGPEContentHandler3D) getContentHandler(); |
|
139 |
// Util.logger.log(Level.FINEST, getMainFile().getPath()); |
|
140 |
Node root=null; |
|
141 |
|
|
142 |
try { |
|
143 |
String os = System.getProperty("os.name"); |
|
144 |
String path; |
|
145 |
if (os.toLowerCase().startsWith("windows")) { |
|
146 |
|
|
147 |
path = getMainFile().getPath().substring(1, |
|
148 |
getMainFile().getPath().length()); |
|
149 |
} else { |
|
150 |
path = getMainFile().getPath(); |
|
151 |
} |
|
152 |
|
|
153 |
root = osgDB.readNodeFile(path); |
|
154 |
// root = osgDB.readNodeFile(getMainFile().getPath().substring(1, getMainFile().getPath().length())); |
|
155 |
|
|
156 |
} catch (LoadNodeException e) { |
|
157 |
// TODO Auto-generated catch block |
|
158 |
e.printStackTrace(); |
|
159 |
return; |
|
160 |
} catch (FileNotFoundException e) { |
|
161 |
// TODO Auto-generated catch block |
|
162 |
e.printStackTrace(); |
|
163 |
return; |
|
164 |
} |
|
165 |
if (root == null) |
|
166 |
// Util.logger.log(Level.FINEST, "Cannot open" |
|
167 |
// + getMainFile().getPath()); |
|
168 |
System.out.println("error"); |
|
169 |
else { |
|
170 |
|
|
171 |
Object multiGeometry; |
|
172 |
multiGeometry = _content.startMultiGeometry("", ""); |
|
173 |
|
|
174 |
if (root.className().equals("Geode")) { |
|
175 |
|
|
176 |
Geode rootGeode = new Geode(root.getCPtr()); |
|
177 |
Object multiSolid; |
|
178 |
|
|
179 |
multiSolid = _content.startMultiSolid("", ""); |
|
180 |
|
|
181 |
_content.addGeometryToMultiGeometry(multiSolid, multiGeometry); |
|
182 |
|
|
183 |
try { |
|
184 |
addedTextures = pushTextures(rootGeode); |
|
185 |
} catch (NodeException e) { |
|
186 |
} |
|
187 |
try { |
|
188 |
addedMaterial = pushMaterial(rootGeode); |
|
189 |
} catch (NodeException e) { |
|
190 |
} |
|
191 |
|
|
192 |
_blendings.push(rootGeode.getOrCreateStateSet().getEnabledBlending()); |
|
193 |
|
|
194 |
for (int i = 0; i < rootGeode.getNumDrawables(); i++) { |
|
195 |
|
|
196 |
try { |
|
197 |
parseDrawable(rootGeode.getDrawable(i), multiSolid); |
|
198 |
} catch (ChildIndexOutOfBoundsExceptions e) { |
|
199 |
e.printStackTrace(); |
|
200 |
return; |
|
201 |
} catch (NodeException e) { |
|
202 |
} |
|
203 |
|
|
204 |
} |
|
205 |
|
|
206 |
_blendings.pop(); |
|
207 |
_content.endMultiSolid(multiSolid); |
|
208 |
|
|
209 |
} |
|
210 |
|
|
211 |
else if (root.className().equals("Group")) { |
|
212 |
Group rootGroup; |
|
213 |
|
|
214 |
try { |
|
215 |
rootGroup = new Group(root.getCPtr()); |
|
216 |
} catch (NodeException e) { |
|
217 |
// TODO Auto-generated catch block |
|
218 |
e.printStackTrace(); |
|
219 |
return; |
|
220 |
} |
|
221 |
try { |
|
222 |
addedTextures = pushTextures(rootGroup); |
|
223 |
} catch (NodeException e) { |
|
224 |
} |
|
225 |
try { |
|
226 |
addedMaterial = pushMaterial(rootGroup); |
|
227 |
} catch (NodeException e) { |
|
228 |
} |
|
229 |
_blendings.push(rootGroup.getOrCreateStateSet().getEnabledBlending()); |
|
230 |
|
|
231 |
//System.out.println("CHILDREN: " + rootGroup.getNumChildren()); |
|
232 |
|
|
233 |
for (int i = 0; i < rootGroup.getNumChildren(); i++) { |
|
234 |
|
|
235 |
try { |
|
236 |
parseNode(rootGroup.getChild(i), multiGeometry); |
|
237 |
} catch (ChildIndexOutOfBoundsExceptions e) { |
|
238 |
e.printStackTrace(); |
|
239 |
return; |
|
240 |
} |
|
241 |
// Util.logger.log(Level.FINEST, "Node parsed"); |
|
242 |
} |
|
243 |
|
|
244 |
_blendings.pop(); |
|
245 |
|
|
246 |
} |
|
247 |
|
|
248 |
else { |
|
249 |
|
|
250 |
// Util.logger.log(Level.FINEST, |
|
251 |
// "First parsed object must be Group or Geode"); |
|
252 |
|
|
253 |
} |
|
254 |
|
|
255 |
popTextures(addedTextures); |
|
256 |
if (addedMaterial) |
|
257 |
_materials.pop(); |
|
258 |
_content.endMultiGeometry(multiGeometry); |
|
259 |
|
|
260 |
} |
|
261 |
|
|
262 |
} |
|
263 |
|
|
264 |
protected void parseNode(Node node, Object parent) { |
|
265 |
// Util.logger.log(Level.FINEST, "node init"); |
|
266 |
Node instance; |
|
267 |
boolean isTransform = false; |
|
268 |
boolean addedMaterial = false; |
|
269 |
Vector<Integer> addedTextures = new Vector<Integer>(); |
|
270 |
|
|
271 |
try { |
|
272 |
|
|
273 |
Class<?> reflect = Class.forName("org.gvsig.osgvp.core.osg." |
|
274 |
+ node.className()); |
|
275 |
Constructor<?> builder = reflect |
|
276 |
.getConstructor(new Class[] { long.class }); |
|
277 |
instance = (Node) builder |
|
278 |
.newInstance(new Object[] { node.getCPtr() }); |
|
279 |
|
|
280 |
} |
|
281 |
|
|
282 |
catch (Exception e) { |
|
283 |
instance = new Node(node.getCPtr()); |
|
284 |
} |
|
285 |
|
|
286 |
if (instance instanceof Group) { |
|
287 |
|
|
288 |
Object multiGeometry; |
|
289 |
multiGeometry = _content.startMultiGeometry("", ""); |
|
290 |
_content.addGeometryToMultiGeometry(multiGeometry, parent); |
|
291 |
isTransform = pushTransform((Group) instance); |
|
292 |
try { |
|
293 |
addedTextures = pushTextures(instance); |
|
294 |
} catch (NodeException e) { |
|
295 |
} |
|
296 |
try { |
|
297 |
addedMaterial = pushMaterial(instance); |
|
298 |
} catch (NodeException e) { |
|
299 |
} |
|
300 |
if (_blendings.lastElement()) |
|
301 |
_blendings.push(true); |
|
302 |
else |
|
303 |
_blendings.push(instance.getOrCreateStateSet() |
|
304 |
.getEnabledBlending()); |
|
305 |
for (int i = 0; i < ((Group) instance).getNumChildren(); i++) { |
|
306 |
|
|
307 |
try { |
|
308 |
parseNode(((Group) instance).getChild(i), multiGeometry); |
|
309 |
} catch (ChildIndexOutOfBoundsExceptions e) { |
|
310 |
e.printStackTrace(); |
|
311 |
return; |
|
312 |
} |
|
313 |
|
|
314 |
} |
|
315 |
if (isTransform) |
|
316 |
_transforms.pop(); |
|
317 |
if (addedMaterial) |
|
318 |
_materials.pop(); |
|
319 |
_blendings.pop(); |
|
320 |
popTextures(addedTextures); |
|
321 |
_content.endMultiGeometry(multiGeometry); |
|
322 |
|
|
323 |
} |
|
324 |
|
|
325 |
else if (instance instanceof Geode) { |
|
326 |
|
|
327 |
Object multiSolid; |
|
328 |
multiSolid = _content.startMultiSolid("", ""); |
|
329 |
_content.addGeometryToMultiGeometry(multiSolid, parent); |
|
330 |
try { |
|
331 |
addedTextures = pushTextures(instance); |
|
332 |
} catch (NodeException e) { |
|
333 |
} |
|
334 |
try { |
|
335 |
addedMaterial = pushMaterial(instance); |
|
336 |
} catch (NodeException e) { |
|
337 |
} |
|
338 |
if (_blendings.lastElement()) |
|
339 |
_blendings.push(true); |
|
340 |
else |
|
341 |
_blendings.push(instance.getOrCreateStateSet() |
|
342 |
.getEnabledBlending()); |
|
343 |
for (int i = 0; i < ((Geode) instance).getNumDrawables(); i++) { |
|
344 |
|
|
345 |
try { |
|
346 |
parseDrawable(((Geode) instance).getDrawable(i), multiSolid); |
|
347 |
} catch (ChildIndexOutOfBoundsExceptions e) { |
|
348 |
e.printStackTrace(); |
|
349 |
return; |
|
350 |
} catch (NodeException e) { |
|
351 |
} |
|
352 |
|
|
353 |
} |
|
354 |
if (addedMaterial) |
|
355 |
_materials.pop(); |
|
356 |
_blendings.pop(); |
|
357 |
popTextures(addedTextures); |
|
358 |
_content.endMultiSolid(multiSolid); |
|
359 |
|
|
360 |
} |
|
361 |
// Util.logger.log(Level.FINEST, "Node parsed-0"); |
|
362 |
// else Util.logger.log(Level.FINEST,instance.className() + " is not |
|
363 |
// instanceof |
|
364 |
// Group or Geode"); |
|
365 |
|
|
366 |
} |
|
367 |
|
|
368 |
protected void parseDrawable(Drawable drawable, Object feature) |
|
369 |
throws NodeException { |
|
370 |
Util.logger.log(Level.FINEST, "drawable init"); |
|
371 |
int t = 0; |
|
372 |
if (drawable instanceof Geometry) { |
|
373 |
|
|
374 |
int j; |
|
375 |
Object solid; |
|
376 |
solid = _content.startSolid("", ""); |
|
377 |
_content.addSolidToMultiSolid(solid, feature); |
|
378 |
|
|
379 |
Matrix matrix = new Matrix(); |
|
380 |
for (int i = 0; i < _transforms.size(); i++) { |
|
381 |
|
|
382 |
matrix.mult(_transforms.get(i), matrix); |
|
383 |
|
|
384 |
} |
|
385 |
|
|
386 |
// Util.logger.log(Level.FINEST, "transforms"); |
|
387 |
|
|
388 |
Vector<Vec3> vertices = ((Geometry) drawable).getVertexArray(); |
|
389 |
if (vertices == null) |
|
390 |
vertices = new Vector<Vec3>(); |
|
391 |
Vec3 vertex; |
|
392 |
int i; |
|
393 |
// Util.logger.log(Level.FINEST, "vertex starts"); |
|
394 |
_content.startSolidVertexArray(solid); |
|
395 |
for (i = 0; i < vertices.size(); i++) { |
|
396 |
|
|
397 |
vertex = vertices.get(i); |
|
398 |
|
|
399 |
// Util.logger.log(Level.FINEST,"Vertice sin |
|
400 |
// transformar"+vertex.x()+";"+vertex.y()+";"+vertex.z()+";"); |
|
401 |
vertex = matrix.prod(vertex, matrix); |
|
402 |
|
|
403 |
// Util.logger.log(Level.FINEST,"Vertice |
|
404 |
// transformado"+vertex.x()+";"+vertex.y()+";"+vertex.z()+";"); |
|
405 |
_content.addVertexToSolid(solid, vertex.x(), vertex.y(), vertex |
|
406 |
.z()); |
|
407 |
|
|
408 |
} |
|
409 |
_content.endSolidVertexArray(); |
|
410 |
|
|
411 |
Vector<Vec3> normals = ((Geometry) drawable).getNormalArray(); |
|
412 |
// Util.logger.log(Level.FINEST, "talla normals" + normals.size()); |
|
413 |
|
|
414 |
if (normals == null) |
|
415 |
normals = new Vector<Vec3>(); |
|
416 |
|
|
417 |
Vec3 normal; |
|
418 |
_content.startSolidNormalArray(solid); |
|
419 |
|
|
420 |
for (i = 0; i < normals.size(); i++) { |
|
421 |
|
|
422 |
normal = normals.get(i); |
|
423 |
// Util.logger.log(Level.FINEST, " normal bucleee" + normal); |
|
424 |
normal = matrix.prod(normal, matrix); |
|
425 |
normal.normalize(); |
|
426 |
_content.addNormalToSolid(solid, normal.x(), normal.y(), normal |
|
427 |
.z()); |
|
428 |
|
|
429 |
} |
|
430 |
_content.endSolidNormalArray(); |
|
431 |
|
|
432 |
_content.setNormalBindingToSolid(solid, ((Geometry) drawable) |
|
433 |
.getNormalBinding()); |
|
434 |
|
|
435 |
try { |
|
436 |
Vector<Vec4> colors = ((Geometry) drawable).getColorArray(); |
|
437 |
if (colors == null) |
|
438 |
colors = new Vector<Vec4>(); |
|
439 |
Vec4 color; |
|
440 |
// Util.logger.log(Level.FINEST, "color starts" + |
|
441 |
// colors.size()); |
|
442 |
_content.startSolidColorArray(solid); |
|
443 |
for (i = 0; i < colors.size(); i++) { |
|
444 |
// Util.logger.log(Level.FINEST, "bucleeeeee 0"); |
|
445 |
color = colors.get(i); |
|
446 |
_content.addColorToSolid(solid, (float) color.x(), |
|
447 |
(float) color.y(), (float) color.z(), (float) color |
|
448 |
.w()); |
|
449 |
// Util.logger.log(Level.FINEST, "bucleeeeee"); |
|
450 |
} |
|
451 |
|
|
452 |
_content.endSolidColorArray(); |
|
453 |
|
|
454 |
} catch (NullPointerException e) { |
|
455 |
|
|
456 |
} |
|
457 |
|
|
458 |
// Util.logger.log(Level.FINEST, "primitiveSets: " |
|
459 |
// + ((Geometry) drawable).getNumPrimitiveSets()); |
|
460 |
|
|
461 |
for (int k = 0; k < ((Geometry) drawable).getNumPrimitiveSets(); k++) { |
|
462 |
int mode = ((Geometry) drawable).getPrimitiveSet(k).getMode(); |
|
463 |
int type = ((Geometry) drawable).getPrimitiveSet(k).getType(); |
|
464 |
|
|
465 |
if (type == PrimitiveSet.Type.DrawArrayLengthsPrimitiveType) { |
|
466 |
|
|
467 |
int offset = 0; |
|
468 |
int accum = 0; |
|
469 |
DrawArrayLengths lengths = new DrawArrayLengths( |
|
470 |
((PrimitiveSet) ((Geometry) drawable) |
|
471 |
.getPrimitiveSet(k)).getCPtr()); |
|
472 |
Vector<Integer> arrayLengths = lengths |
|
473 |
.getElementLengthsVector(); |
|
474 |
|
|
475 |
if (arrayLengths == null) |
|
476 |
arrayLengths = new Vector<Integer>(); |
|
477 |
|
|
478 |
for (int h = 0; h < arrayLengths.size(); h++) { |
|
479 |
|
|
480 |
Object primitiveSet = _content.startPrimitiveSet(mode, |
|
481 |
PrimitiveSet.Type.DrawArraysPrimitiveType); |
|
482 |
_content.addPrimitiveSetToSolid(solid, primitiveSet); |
|
483 |
|
|
484 |
accum = arrayLengths.get(h); |
|
485 |
|
|
486 |
for (int l = 0; l < accum; l++) { |
|
487 |
|
|
488 |
_content.addIndexToPrimitiveSet(primitiveSet, |
|
489 |
((Geometry) drawable).getPrimitiveSet(k) |
|
490 |
.index(offset + l)); |
|
491 |
|
|
492 |
} |
|
493 |
|
|
494 |
offset = offset + accum; |
|
495 |
_content.endPrimitiveSetIndexArray(); |
|
496 |
_content.endPrimitiveSet(primitiveSet); |
|
497 |
|
|
498 |
} |
|
499 |
|
|
500 |
} else { |
|
501 |
Object primitiveSet = _content |
|
502 |
.startPrimitiveSet(mode, type); |
|
503 |
|
|
504 |
_content.addPrimitiveSetToSolid(solid, primitiveSet); |
|
505 |
_content.startPrimitiveSetIndexArray(primitiveSet, |
|
506 |
((Geometry) drawable).getPrimitiveSet(k) |
|
507 |
.getNumIndices()); |
|
508 |
// Util.logger.log(Level.FINEST, "Num Indices: " |
|
509 |
// + String.valueOf(((Geometry) drawable) |
|
510 |
// .getPrimitiveSet(k).getNumIndices())); |
|
511 |
for (int ps = 0; ps < ((Geometry) drawable) |
|
512 |
.getPrimitiveSet(k).getNumIndices(); ps++) { |
|
513 |
|
|
514 |
// Util.logger.log(Level.FINEST, "adding index"); |
|
515 |
_content.addIndexToPrimitiveSet(primitiveSet, |
|
516 |
((Geometry) drawable).getPrimitiveSet(k).index( |
|
517 |
ps)); |
|
518 |
// Util.logger.log(Level.FINEST, String.valueOf(t)); |
|
519 |
// t++; |
|
520 |
} |
|
521 |
_content.endPrimitiveSetIndexArray(); |
|
522 |
_content.endPrimitiveSet(primitiveSet); |
|
523 |
} |
|
524 |
|
|
525 |
} |
|
526 |
|
|
527 |
Vector<Integer> addedTextures = new Vector<Integer>(); |
|
528 |
boolean addedMaterial = false; |
|
529 |
|
|
530 |
addedTextures = pushTexturesDrawable(drawable); |
|
531 |
|
|
532 |
// Util.logger.log(Level.FINEST, "Added textures: " + |
|
533 |
// addedTextures); |
|
534 |
|
|
535 |
try { |
|
536 |
addTexturesToGeometry(solid); |
|
537 |
} catch (Exception e) { |
|
538 |
|
|
539 |
e.printStackTrace(); |
|
540 |
} |
|
541 |
|
|
542 |
// Util.logger.log(Level.FINEST, "getNumTextures" |
|
543 |
// + drawable.getOrCreateStateSet().getNumTextureStages()); |
|
544 |
for (j = 0; j < drawable.getOrCreateStateSet() |
|
545 |
.getNumTextureStages(); j++) { |
|
546 |
|
|
547 |
try { |
|
548 |
if (drawable.getOrCreateStateSet().getTextureAttribute(j) != null) { |
|
549 |
Vector<Vec2> texCoords = ((Geometry) drawable) |
|
550 |
.getTexCoordArray(j); |
|
551 |
if (texCoords == null) |
|
552 |
texCoords = new Vector<Vec2>(); |
|
553 |
Vec2 texCoord; |
|
554 |
|
|
555 |
_content.startSolidTexCoordArray(solid, texCoords |
|
556 |
.size(), j); |
|
557 |
|
|
558 |
for (i = 0; i < texCoords.size(); i++) { |
|
559 |
|
|
560 |
texCoord = texCoords.get(i); |
|
561 |
_content.addTextureCoordinateToSolid(solid, |
|
562 |
texCoord.x(), texCoord.y(), j); |
|
563 |
|
|
564 |
} |
|
565 |
_content.endSolidTexCoordArray(); |
|
566 |
} |
|
567 |
} catch (TextureStageException e) { |
|
568 |
// TODO Auto-generated catch block |
|
569 |
e.printStackTrace(); |
|
570 |
} |
|
571 |
} |
|
572 |
|
|
573 |
addedMaterial = pushMaterialDrawable(drawable); |
|
574 |
try { |
|
575 |
|
|
576 |
Material osgMaterial = _materials.lastElement(); |
|
577 |
|
|
578 |
float x, y, z, w; |
|
579 |
|
|
580 |
Vec4 vectorAmb = new Vec4(); |
|
581 |
Vec4 vectorDif = new Vec4(); |
|
582 |
Vec4 vectorEmi = new Vec4(); |
|
583 |
Vec4 vectorSpe = new Vec4(); |
|
584 |
|
|
585 |
vectorAmb = osgMaterial.getAmbient(Material.Face.FRONT); |
|
586 |
vectorDif = osgMaterial.getDiffuse(Material.Face.FRONT); |
|
587 |
vectorEmi = osgMaterial.getEmission(Material.Face.FRONT); |
|
588 |
vectorSpe = osgMaterial.getSpecular(Material.Face.FRONT); |
|
589 |
|
|
590 |
Object material = _content.startMaterial(); |
|
591 |
x = (float) vectorAmb.x(); |
|
592 |
y = (float) vectorAmb.y(); |
|
593 |
z = (float) vectorAmb.z(); |
|
594 |
w = (float) vectorAmb.w(); |
|
595 |
|
|
596 |
_content.addAmbientToMaterial(material, x, y, z, w); |
|
597 |
|
|
598 |
x = (float) vectorDif.x(); |
|
599 |
y = (float) vectorDif.y(); |
|
600 |
z = (float) vectorDif.z(); |
|
601 |
w = (float) vectorDif.w(); |
|
602 |
|
|
603 |
_content.addDiffuseToMaterial(material, x, y, z, w); |
|
604 |
|
|
605 |
x = (float) vectorSpe.x(); |
|
606 |
y = (float) vectorSpe.y(); |
|
607 |
z = (float) vectorSpe.z(); |
|
608 |
w = (float) vectorSpe.w(); |
|
609 |
|
|
610 |
_content.addSpecularToMaterial(material, x, y, z, w); |
|
611 |
|
|
612 |
x = (float) vectorEmi.x(); |
|
613 |
y = (float) vectorEmi.y(); |
|
614 |
z = (float) vectorEmi.z(); |
|
615 |
w = (float) vectorEmi.w(); |
|
616 |
|
|
617 |
_content.addEmissionToMaterial(material, x, y, z, w); |
|
618 |
|
|
619 |
_content.addShininessToMaterial(material, osgMaterial |
|
620 |
.getShininess(Material.Face.FRONT)); |
|
621 |
|
|
622 |
_content.addMaterialToSolid(solid, material); |
|
623 |
|
|
624 |
_content.endMaterial(material); |
|
625 |
|
|
626 |
} catch (ArrayIndexOutOfBoundsException ex) { |
|
627 |
|
|
628 |
} |
|
629 |
|
|
630 |
if (_blendings.lastElement()) |
|
631 |
_content.addBlendingToSolid(solid, true); |
|
632 |
else |
|
633 |
_content.addBlendingToSolid(solid, drawable |
|
634 |
.getOrCreateStateSet().getEnabledBlending()); |
|
635 |
|
|
636 |
popTextures(addedTextures); |
|
637 |
|
|
638 |
_content.endSolid(solid); |
|
639 |
|
|
640 |
} |
|
641 |
// Util.logger.log(Level.FINEST, "drawable parsed"); |
|
642 |
} |
|
643 |
|
|
644 |
protected boolean pushTransform(Group group) { |
|
645 |
|
|
646 |
if (group instanceof MatrixTransform) { |
|
647 |
|
|
648 |
// Util.logger.log(Level.FINEST,"Push a MatrixTransform"); |
|
649 |
_transforms.push(((MatrixTransform) group).getMatrix()); |
|
650 |
|
|
651 |
return true; |
|
652 |
|
|
653 |
} |
|
654 |
|
|
655 |
if (group instanceof PositionAttitudeTransform) { |
|
656 |
|
|
657 |
// Util.logger.log(Level.FINEST,"Push a PAT"); |
|
658 |
Matrix scaleMatrix = Matrix |
|
659 |
.scale(((PositionAttitudeTransform) group).getScale()); |
|
660 |
|
|
661 |
Quat quat = new Quat((((PositionAttitudeTransform) group) |
|
662 |
.getAttitudeAngle()), (((PositionAttitudeTransform) group) |
|
663 |
.getAttitudeAxis())); |
|
664 |
Matrix rotateMatrix = Matrix.rotate(quat); |
|
665 |
|
|
666 |
Matrix transMatrix = Matrix |
|
667 |
.translate(((PositionAttitudeTransform) group) |
|
668 |
.getPosition()); |
|
669 |
|
|
670 |
// rotateMatrix.postMult(scaleMatrix); |
|
671 |
// transMatrix.postMult(rotateMatrix); |
|
672 |
rotateMatrix.preMult(scaleMatrix); |
|
673 |
transMatrix.preMult(rotateMatrix); |
|
674 |
|
|
675 |
_transforms.push(transMatrix); |
|
676 |
|
|
677 |
return true; |
|
678 |
|
|
679 |
} |
|
680 |
|
|
681 |
if (group instanceof AutoTransform) { |
|
682 |
|
|
683 |
// Util.logger.log(Level.FINEST,"Push a AutoTransform"); |
|
684 |
Matrix scaleMatrix = Matrix.scale(((AutoTransform) group) |
|
685 |
.getScale()); |
|
686 |
Quat quat = new Quat((((AutoTransform) group).getRotationAngle()), |
|
687 |
(((AutoTransform) group).getRotationAxis())); |
|
688 |
Matrix rotateMatrix = Matrix.rotate(quat); |
|
689 |
Matrix transMatrix = Matrix.translate(((AutoTransform) group) |
|
690 |
.getPosition()); |
|
691 |
|
|
692 |
// rotateMatrix.postMult(scaleMatrix); |
|
693 |
// transMatrix.postMult(rotateMatrix); |
|
694 |
|
|
695 |
rotateMatrix.preMult(scaleMatrix); |
|
696 |
transMatrix.preMult(rotateMatrix); |
|
697 |
|
|
698 |
_transforms.push(transMatrix); |
|
699 |
|
|
700 |
return true; |
|
701 |
|
|
702 |
} |
|
703 |
|
|
704 |
return false; |
|
705 |
|
|
706 |
} |
|
707 |
|
|
708 |
protected Vector<Integer> pushTextures(Node node) throws NodeException { |
|
709 |
|
|
710 |
int numTextures; |
|
711 |
numTextures = node.getOrCreateStateSet().getNumTextureStages(); |
|
712 |
Vector<Texture2D> vector = node.getOrCreateStateSet() |
|
713 |
.getTextureAttributeVector(); |
|
714 |
Vector<Integer> stages = new Vector<Integer>(); |
|
715 |
|
|
716 |
for (int i = 0; i < numTextures; i++) { |
|
717 |
|
|
718 |
_textures.get(i); |
|
719 |
|
|
720 |
_textures.add(i, new Stack<Texture2D>()); |
|
721 |
|
|
722 |
_textures.get(i).push(vector.get(i)); |
|
723 |
// Util.logger.log(Level.FINEST,"PUSH"); |
|
724 |
stages.add(i); |
|
725 |
|
|
726 |
} |
|
727 |
|
|
728 |
return stages; |
|
729 |
} |
|
730 |
|
|
731 |
protected boolean pushMaterial(Node node) throws NodeException { |
|
732 |
|
|
733 |
boolean hasMaterial = false; |
|
734 |
|
|
735 |
Material material; |
|
736 |
material = node.getOrCreateStateSet().getMaterial(); |
|
737 |
_materials.push(material); |
|
738 |
hasMaterial = true; |
|
739 |
|
|
740 |
return hasMaterial; |
|
741 |
|
|
742 |
} |
|
743 |
|
|
744 |
protected Vector<Integer> pushTexturesDrawable(Drawable drawable) |
|
745 |
throws NodeException { |
|
746 |
|
|
747 |
int numTextures; |
|
748 |
numTextures = drawable.getOrCreateStateSet().getNumTextureStages(); |
|
749 |
Vector<Texture2D> vector = drawable.getOrCreateStateSet() |
|
750 |
.getTextureAttributeVector(); |
|
751 |
Vector<Integer> stages = new Vector<Integer>(); |
|
752 |
|
|
753 |
// Util.logger.log(Level.FINEST, "NumTextures: " + numTextures); |
|
754 |
for (int i = 0; i < numTextures; i++) { |
|
755 |
|
|
756 |
try { |
|
757 |
|
|
758 |
_textures.get(i); |
|
759 |
|
|
760 |
} catch (ArrayIndexOutOfBoundsException ex) { |
|
761 |
|
|
762 |
_textures.add(i, new Stack<Texture2D>()); |
|
763 |
|
|
764 |
} |
|
765 |
|
|
766 |
try { |
|
767 |
|
|
768 |
_textures.get(i).push(vector.get(i)); |
|
769 |
// Util.logger.log(Level.FINEST,"PUSH"); |
|
770 |
stages.add(i); |
|
771 |
Util.logger.log(Level.FINEST, "Added textures: " + stages); |
|
772 |
} catch (ArrayIndexOutOfBoundsException ex) { |
|
773 |
|
|
774 |
} |
|
775 |
} |
|
776 |
|
|
777 |
return stages; |
|
778 |
} |
|
779 |
|
|
780 |
protected boolean pushMaterialDrawable(Drawable draw) throws NodeException { |
|
781 |
|
|
782 |
boolean hasMaterial = false; |
|
783 |
|
|
784 |
try { |
|
785 |
|
|
786 |
Material material; |
|
787 |
material = draw.getOrCreateStateSet().getMaterial(); |
|
788 |
_materials.push(material); |
|
789 |
hasMaterial = true; |
|
790 |
|
|
791 |
} catch (ArrayIndexOutOfBoundsException ex) { |
|
792 |
|
|
793 |
hasMaterial = false; |
|
794 |
|
|
795 |
} catch (NullPointerException e) { |
|
796 |
|
|
797 |
} |
|
798 |
|
|
799 |
return hasMaterial; |
|
800 |
|
|
801 |
} |
|
802 |
|
|
803 |
protected void popTextures(Vector<Integer> stages) { |
|
804 |
|
|
805 |
for (int i = 0; i < stages.size(); i++) { |
|
806 |
|
|
807 |
// Util.logger.log(Level.FINEST,"POP"); |
|
808 |
_textures.get(stages.get(i)).pop(); |
|
809 |
|
|
810 |
} |
|
811 |
|
|
812 |
} |
|
813 |
|
|
814 |
protected void addTexturesToGeometry(Object solid) throws Exception { |
|
815 |
|
|
816 |
BufferedImage image; |
|
817 |
for (int i = 0; i < _textures.size(); i++) { |
|
818 |
|
|
819 |
try { |
|
820 |
// Util.logger.log(Level.FINEST, "Num textures in stack: " |
|
821 |
// + _textures.get(i).size()); |
|
822 |
_textures.get(i).lastElement(); |
|
823 |
try { |
|
824 |
// Util.logger.log(Level.FINEST, "TRYING STAGE: " + i); |
|
825 |
image = _textures.get(i).lastElement().getImage() |
|
826 |
.getBufferedImage(); |
|
827 |
_content.addTextureToSolid(solid, i, image); |
|
828 |
|
|
829 |
} catch (ArrayIndexOutOfBoundsException ex) { |
|
830 |
|
|
831 |
} catch (ImageConversionException e) { |
|
832 |
// TODO Auto-generated catch block |
|
833 |
e.printStackTrace(); |
|
834 |
} catch (ImageException e) { |
|
835 |
// TODO Auto-generated catch block |
|
836 |
e.printStackTrace(); |
|
837 |
} |
|
838 |
} |
|
839 |
|
|
840 |
catch (NoSuchElementException ex) { |
|
841 |
|
|
842 |
} |
|
843 |
} |
|
844 |
|
|
845 |
} |
|
846 |
|
|
847 |
@Override |
|
848 |
public String getFormat() { |
|
849 |
// TODO Auto-generated method stub |
|
850 |
return null; |
|
851 |
} |
|
852 |
|
|
853 |
} |
1.10/tags/gvSIG_3D_Animation_1_10_build_21_FINAL/libraries/libGPE-OSG/src/main/resources/box.osg | ||
---|---|---|
1 |
Group { |
|
2 |
name "box.3ds" |
|
3 |
nodeMask 0xffffffff |
|
4 |
cullingActive TRUE |
|
5 |
num_children 1 |
|
6 |
Geode { |
|
7 |
name "Cube" |
|
8 |
nodeMask 0xffffffff |
|
9 |
cullingActive TRUE |
|
10 |
num_drawables 1 |
|
11 |
Geometry { |
|
12 |
DataVariance STATIC |
|
13 |
StateSet { |
|
14 |
DataVariance STATIC |
|
15 |
rendering_hint DEFAULT_BIN |
|
16 |
renderBinMode INHERIT |
|
17 |
Material { |
|
18 |
ColorMode OFF |
|
19 |
ambientColor 0.4 0.4 0.4 1 |
|
20 |
diffuseColor 0.8 0.8 0.8 1 |
|
21 |
specularColor 0 0 0 0 |
|
22 |
emissionColor 0 0 0 1 |
|
23 |
shininess 0 |
|
24 |
} |
|
25 |
} |
|
26 |
useDisplayList TRUE |
|
27 |
useVertexBufferObjects FALSE |
|
28 |
PrimitiveSets 1 |
|
29 |
{ |
|
30 |
DrawElementsUShort TRIANGLES 36 |
|
31 |
{ |
|
32 |
0 1 2 0 2 3 4 5 6 4 |
|
33 |
6 7 0 4 7 0 7 1 1 7 |
|
34 |
6 1 6 2 2 6 5 2 5 3 |
|
35 |
4 0 3 4 3 5 |
|
36 |
} |
|
37 |
} |
|
38 |
VertexArray Vec3Array 8 |
|
39 |
{ |
|
40 |
1 1 -1 |
|
41 |
1 -1 -1 |
|
42 |
-1 -1 -1 |
|
43 |
-1 1 -1 |
|
44 |
1 0.999999 1 |
|
45 |
-1 1 1 |
|
46 |
-1 -1 1 |
|
47 |
0.999999 -1 1 |
|
48 |
|
|
49 |
} |
|
50 |
NormalBinding PER_VERTEX |
|
51 |
NormalArray Vec3Array 8 |
|
52 |
{ |
|
53 |
0.666667 0.333333 -0.666667 |
|
54 |
0.408248 -0.816497 -0.408248 |
|
55 |
-0.666667 -0.333333 -0.666667 |
|
56 |
-0.408248 0.816497 -0.408248 |
|
57 |
0.333333 0.666667 0.666667 |
|
58 |
-0.816496 0.408248 0.408248 |
|
59 |
-0.333334 -0.666667 0.666667 |
|
60 |
0.816496 -0.408249 0.408248 |
|
61 |
|
|
62 |
} |
|
63 |
ColorBinding OVERALL |
|
64 |
ColorArray Vec4ubArray 1 { |
|
65 |
255 255 255 255 |
|
66 |
} |
|
67 |
} |
|
68 |
} |
|
69 |
} |
1.10/tags/gvSIG_3D_Animation_1_10_build_21_FINAL/libraries/libGPE-OSG/src/main/resources/dumptruck.osg | ||
---|---|---|
1 |
Group { |
|
2 |
UniqueID Group_0 |
|
3 |
DataVariance STATIC |
|
4 |
cullingActive TRUE |
|
5 |
num_children 1 |
|
6 |
Geode { |
|
7 |
DataVariance DYNAMIC |
|
8 |
name "dumptruck.osg" |
|
9 |
cullingActive TRUE |
|
10 |
num_drawables 3 |
|
11 |
Geometry { |
|
12 |
DataVariance DYNAMIC |
|
13 |
StateSet { |
|
14 |
UniqueID StateSet_1 |
|
15 |
DataVariance STATIC |
|
16 |
rendering_hint OPAQUE_BIN |
|
17 |
renderBinMode INHERIT |
|
18 |
GL_CULL_FACE OFF |
|
19 |
GL_LIGHTING ON |
|
20 |
Material { |
|
21 |
DataVariance STATIC |
|
22 |
ColorMode OFF |
|
23 |
ambientColor 0.2 0.2 0.2 1 |
|
24 |
diffuseColor 0.8 0.8 0.8 1 |
|
25 |
specularColor 0 0 0 1 |
|
26 |
emissionColor 0 0 0 1 |
|
27 |
shininess 0 |
|
28 |
} |
|
29 |
} |
|
30 |
useDisplayList TRUE |
|
31 |
Primitives 1 |
|
32 |
{ |
|
33 |
DrawArrayLengths TRIANGLE_STRIP 0 907 |
|
34 |
{ |
Also available in: Unified diff