1
2
3
4
5
6
7 package org.sourceforge.jvb3d.Loader;
8
9 import java.io.File;
10 import java.util.logging.Logger;
11
12 import javax.media.j3d.*;
13 import com.sun.j3d.utils.geometry.*;
14
15 import javax.vecmath.*;
16 import javax.xml.parsers.DocumentBuilder;
17 import javax.xml.parsers.DocumentBuilderFactory;
18 import org.w3c.dom.Document;
19 import org.w3c.dom.NodeList;
20 /***
21 * @author Michał Listwan
22 *
23 * klasa wczytuje scenę podaną jako parametr, a następnie umożliwia jej pobranie przez model
24 */
25 public class SceneLoader {
26 protected Document sceneData = null;
27 protected BranchGroup staticTree = new BranchGroup();
28
29 static final Logger loaderLogger = Logger.getLogger("SceneLoader");
30 /***
31 * Wczytuje scene zadaną w parametrze
32 * @param scene plik ze sceną
33 */
34 public void loadScene(String scene) {
35 try {
36 DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
37 sceneData = docBuilder.parse(new File(scene));
38 NodeList rootList = sceneData.getElementsByTagName("Surface");
39 for(int i=0; i<rootList.getLength(); i++) {
40 staticTree.addChild(GraphObjectFactory.getInstance().newGraphObject(rootList.item(i)));
41 }
42
43 staticTree.addChild(ModelManager.getInstance().getObjects());
44
45
46
47 }
48 catch (Exception e) {
49 e.printStackTrace();
50 }
51 System.gc();
52 }
53 /***
54 * Zwraca statyczne poddrzewo z aktualnie wczytanej sceny
55 * @return root statycznego poddrzewa
56 */
57 public BranchGroup getStaticScene(){
58
59 TransformGroup tgg= new TransformGroup();
60
61
62 Transform3D trans = new Transform3D();
63
64
65
66
67
68
69 BranchGroup bg = new BranchGroup();
70
71 tgg.addChild(staticTree);
72 trans.set(new Vector3f(-6,-1.6f,0f));
73 tgg.setTransform(trans);
74 bg.addChild(tgg);
75
76 AmbientLight ambient = new AmbientLight();
77 ambient.setInfluencingBounds(staticTree.getBounds());
78 ambient.setColor(new Color3f(1.0f, 1.0f, 1.0f));
79
80 bg.compile();
81 return bg;
82
83
84
85
86
87
88
89 }
90
91 /***
92 * zwraca scenę uzywaną do testowania
93 * @return root sceny
94 */
95 public BranchGroup getCubeScene(){
96 int groupCount=50;
97 int cubeCount=200;
98 BranchGroup bgs[]= new BranchGroup[groupCount];
99 for(int i=0;i<groupCount;++i){
100 bgs[i]=new BranchGroup();
101 }
102
103 BranchGroup bg = new BranchGroup();
104 TransformGroup tgg;
105 Transform3D trans = new Transform3D();
106 Vector3f vector= new Vector3f();
107 Vector3f vector2= new Vector3f();
108 vector.set(0.3f,0,0);
109
110
111 tgg=new TransformGroup();
112 tgg.setTransform(trans);
113 bg.addChild(tgg);
114 Group g;
115 g=tgg;
116 for(int j=0;j<groupCount;++j)
117 {
118 if(j>0)
119 {
120 bgs[j-1].compile();
121 bg.addChild(bgs[j-1]);
122 }
123 vector2.z-=0.5f;
124 trans.set(vector2);
125 tgg=new TransformGroup();
126 tgg.setTransform(trans);
127 bgs[j].addChild(tgg);
128 g=tgg;
129 for(int i=0;i<cubeCount;++i)
130 {
131 tgg= new TransformGroup();
132 trans.set(vector);
133 tgg.setTransform(trans);
134 tgg.addChild(new ColorCube(0.1));
135 g.addChild(tgg);
136 g=tgg;
137 }
138 }
139 bgs[groupCount-1].compile();
140 bg.addChild(bgs[groupCount-1]);
141
142
143
144
145
146 bg.compile();
147 return bg;
148 }
149 /***
150 * zwraca drzewo obiektów które można używać.
151 * w obecnej wersji niezaimplementowane
152 */
153 public void getUsableScene(){
154
155 }
156 }