|
14 | 14 | import de.fabulousfox.libs.voxfileparser.*;
|
15 | 15 | import de.fabulousfox.libs.voxfileparser.chunk.VoxRGBAChunk;
|
16 | 16 | import org.joml.Vector3f;
|
17 |
| - |
| 17 | +import org.w3c.dom.Document; |
| 18 | +import org.w3c.dom.Element; |
| 19 | +import org.w3c.dom.Node; |
| 20 | +import org.w3c.dom.NodeList; |
| 21 | +import org.xml.sax.SAXException; |
| 22 | + |
| 23 | +import javax.xml.parsers.DocumentBuilder; |
| 24 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 25 | +import javax.xml.parsers.ParserConfigurationException; |
18 | 26 | import java.awt.*;
|
| 27 | +import java.io.File; |
19 | 28 | import java.io.FileInputStream;
|
20 | 29 | import java.io.IOException;
|
21 | 30 | import java.util.ArrayList;
|
| 31 | +import java.util.HashMap; |
22 | 32 | import java.util.List;
|
23 | 33 | import java.util.concurrent.ConcurrentLinkedQueue;
|
24 | 34 |
|
@@ -214,4 +224,93 @@ public static List<Model> loadGVOX(String path) {
|
214 | 224 |
|
215 | 225 | return List.of();
|
216 | 226 | }
|
| 227 | + |
| 228 | + private static Node firstNodeListElementOrNull(NodeList nodeList) { |
| 229 | + if (nodeList.getLength() > 0) { |
| 230 | + return nodeList.item(0); |
| 231 | + } else { |
| 232 | + return null; |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + private static int totalVoxBoxes = 0; |
| 237 | + private static int totalVoxBoxesCount = 0; |
| 238 | + |
| 239 | + private static void traverseNodes(Vector3f posOffset, Vector3f rotationOffset, Node thisNode, List<Model> models) throws NumberFormatException { |
| 240 | + if (thisNode.getNodeType() != Node.ELEMENT_NODE) return; |
| 241 | + Element thisElement = (Element) thisNode; |
| 242 | + |
| 243 | + Vector3f pOffset = new Vector3f(posOffset); |
| 244 | + Vector3f rOffset = new Vector3f(rotationOffset); |
| 245 | + int[] size = new int[3]; |
| 246 | + String[] pOffsetString = thisElement.getAttribute("pos").split(" "); |
| 247 | + String[] rOffsetString = thisElement.getAttribute("rot").split(" "); |
| 248 | + String[] sizeString = thisElement.getAttribute("size").split(" "); |
| 249 | + if(pOffsetString.length == 3) { |
| 250 | + pOffset.x += Float.parseFloat(pOffsetString[0]); |
| 251 | + pOffset.y += Float.parseFloat(pOffsetString[1]); |
| 252 | + pOffset.z += Float.parseFloat(pOffsetString[2]); |
| 253 | + } |
| 254 | + if(rOffsetString.length == 3) { |
| 255 | + rOffset.x += Float.parseFloat(rOffsetString[0]); |
| 256 | + rOffset.y += Float.parseFloat(rOffsetString[1]); |
| 257 | + rOffset.z += Float.parseFloat(rOffsetString[2]); |
| 258 | + } |
| 259 | + if(sizeString.length == 3) { |
| 260 | + size[0] = Integer.parseInt(sizeString[0]); |
| 261 | + size[1] = Integer.parseInt(sizeString[1]); |
| 262 | + size[2] = Integer.parseInt(sizeString[2]); |
| 263 | + } |
| 264 | + |
| 265 | + if(thisElement.getNodeName().equalsIgnoreCase("voxbox")){ |
| 266 | + Texture3D texture = new Texture3D(size[0], size[1], size[2]); |
| 267 | + texture.fill(); |
| 268 | + texture.create(); |
| 269 | + |
| 270 | + models.add(new Model(texture, pOffset.mul(Model.VOXEL_SIZE), rOffset, size[0], size[1], size[2])); |
| 271 | + |
| 272 | + totalVoxBoxesCount++; |
| 273 | + System.out.println("Progress: " + totalVoxBoxesCount + "/" + totalVoxBoxes + " -> " + 100f * ((float)totalVoxBoxesCount / (float)totalVoxBoxes) + "%"); |
| 274 | + } |
| 275 | + |
| 276 | + if(thisNode.getChildNodes().getLength() != 0) { |
| 277 | + for (int i = 0; i < thisNode.getChildNodes().getLength(); i++) { |
| 278 | + traverseNodes(pOffset, rOffset, thisNode.getChildNodes().item(i), models); |
| 279 | + } |
| 280 | + } |
| 281 | + } |
| 282 | + |
| 283 | + public static List<Model> loadTeardown(Vector3f spawnPoint, String pathToXML) throws ParserConfigurationException, IOException, SAXException, NumberFormatException { |
| 284 | + List<Model> models = new ArrayList<>(); |
| 285 | + |
| 286 | + String[] tempStringArray; |
| 287 | + Node tempNode; |
| 288 | + |
| 289 | + DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
| 290 | + Document doc = builder.parse(new File(pathToXML)); |
| 291 | + doc.getDocumentElement().normalize(); |
| 292 | + |
| 293 | + Node sceneNode = doc.getElementsByTagName("scene").item(0); |
| 294 | + |
| 295 | + Node spawnPointNode = firstNodeListElementOrNull(doc.getElementsByTagName("spawnpoint")); |
| 296 | + if(spawnPointNode != null) { |
| 297 | + tempNode = spawnPointNode.getAttributes().getNamedItem("pos"); |
| 298 | + tempStringArray = tempNode.getTextContent().split(" "); |
| 299 | + spawnPoint.x = Float.parseFloat(tempStringArray[0]); |
| 300 | + spawnPoint.y = Float.parseFloat(tempStringArray[1]); |
| 301 | + spawnPoint.z = Float.parseFloat(tempStringArray[2]); |
| 302 | + } |
| 303 | + |
| 304 | + Node worldNode = firstNodeListElementOrNull(doc.getElementsByTagName("group")); |
| 305 | + if(worldNode == null) throw new RuntimeException("No world node found"); |
| 306 | + |
| 307 | + totalVoxBoxes = doc.getElementsByTagName("voxbox").getLength(); |
| 308 | + totalVoxBoxesCount = 0; |
| 309 | + |
| 310 | + System.out.println("Traversing nodes... ("+totalVoxBoxes+")"); |
| 311 | + traverseNodes(new Vector3f(0, 0, 0), new Vector3f(0, 0, 0), worldNode, models); |
| 312 | + System.out.println("Done traversing nodes ("+totalVoxBoxes+")"); |
| 313 | + |
| 314 | + return models; |
| 315 | + } |
217 | 316 | }
|
0 commit comments