Hi all
I`m trying to create sort of simple model loading with XML . My idea is to fetch the objects from the xml file (identified by specific tag (in my case 'object')) and than load them as an array of models. I get it to work with one model , however array objects just wont work … dunno why
This is work done by my so far (Sorry for messy code , still need to work on it :P):
Can i somehow fetch this array from the xml file to loader ? and does it have any influence on game performance ?
Any help is welcome
XML Model reader :
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class ModelReader {
private static String MODEL_PATH = "";
private static URL absoluteModelPath;
private static String TEXTURE_URL = "";
private static int xCoordinates;
private static int yCoordinates;
private static float modelScale;
private static int zCoordinates;
private static URL [] array = new URL[2];
public ModelReader(){
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new File("buildingsList.xml"));
NodeList listOfElements = doc.getElementsByTagName("object"); // this is practically array of the elements
int listLength = listOfElements.getLength(); // need to somehow display these together with the
System.out.println("Total no. of objects : " + listLength);
for(int s=0; s<listLength ; s++){
// this are the list of all elements in node format
// simply mean that all data is formed into one node
// specific elements can be retrived by getchild etc
Node firstObjectNode = listOfElements.item(s);
if(firstObjectNode.getNodeType() == Node.ELEMENT_NODE){
Element firstElement = (Element)firstObjectNode; // this is object that need to be put in the array i think
//
model (object) path
NodeList modelPath = firstElement.getElementsByTagName("modelLocation");
Element modelPathElement = (Element)modelPath.item(0);
setModelPath(modelPathElement.getTextContent());
NodeList textModelPath = modelPathElement.getChildNodes();
System.out.println("Model Location : " +
((Node)textModelPath.item(0)).getNodeValue().trim());
//
texture path
NodeList textureURL = firstElement.getElementsByTagName("TextureURL");
Element textureURLElement = (Element)textureURL.item(0);
setTexturePath(textureURLElement.getTextContent());
NodeList textURL = textureURLElement.getChildNodes();
System.out.println("Texture location : " +
((Node)textURL.item(0)).getNodeValue().trim());
//
x coordinates for the object
NodeList locationX = firstElement.getElementsByTagName("LocationX");
Element locationXElement = (Element)locationX.item(0);
setXCoordinates(Integer.parseInt(locationXElement.getTextContent()));
NodeList textLocationX = locationXElement.getChildNodes();
System.out.println("X coordinates : " +
((Node)textLocationX.item(0)).getNodeValue().trim());
//
y coordinates for the object
NodeList locationY = firstElement.getElementsByTagName("LocationY");
Element locationYElement = (Element)locationY.item(0);
setYCoordinates(Integer.parseInt(locationYElement.getTextContent()));
NodeList textLocationY = locationYElement.getChildNodes();
System.out.println("Y coordinates : " +
((Node)textLocationY.item(0)).getNodeValue().trim());
//
z coordinates for the object
NodeList locationZ = firstElement.getElementsByTagName("LocationZ");
Element locationZElement = (Element)locationZ.item(0);
setZCoordinates(Integer.parseInt(locationZElement.getTextContent()));
NodeList textLocationZ = locationZElement.getChildNodes();
System.out.println("Z coordinates : " +
((Node)textLocationZ.item(0)).getNodeValue().trim());
//---- size for the building
NodeList modelScale = firstElement.getElementsByTagName("Scale");
Element scaleElement = (Element)modelScale.item(0);
setModelScale(Float.parseFloat(scaleElement.getTextContent()));
NodeList textScale = scaleElement.getChildNodes();
System.out.println("Model Scale : " +
((Node)textScale.item(0)).getNodeValue().trim());
//
retrieve absolute URL path to the model
URL absoluteURL = new URL(getModelPath());
setAbsoluteModelPath(absoluteURL);
array[s] = getAbsoluteModelPath();
}//end of if
System.out.println();
}//end of for loop through the file
}catch (SAXParseException err) {
System.out.println("OK , you fucked up" + ", line : "
+ err.getLineNumber() +" problem with file URL : " + err.getSystemId());
System.out.println(" " + err.getMessage());
// actually very usefull excpetion , showing exactly where is the problem
}catch (SAXException e) {
Exception x = e.getException();
((x == null) ? e : x).printStackTrace();
}catch (Throwable t) {
t.printStackTrace();
}
}
//
getter and setter method for MODEL_PATH
public void setModelPath(String MODEL_PATH) {
this.MODEL_PATH = MODEL_PATH;
}
public String getModelPath() {
return MODEL_PATH;
}
//
getter and setter method for Texture
public String getTexturePath() {
return TEXTURE_URL;
}
public void setTexturePath(String TEXTURE_URL) {
this.TEXTURE_URL = TEXTURE_URL;
}
//
getter and setter method for X coordiantes
public void setXCoordinates(int xCoordinates) {
this.xCoordinates = xCoordinates;
}
public int getXCoordinates() {
return xCoordinates;
}
//
getter and setter method for Y coordinates
public int getYCoordinates() {
return yCoordinates;
}
public void setYCoordinates(int yCoordinates) {
this.yCoordinates = yCoordinates;
}
//
getter and setter for Z coordinates
public int getZCoordinates() {
return zCoordinates;
}
public void setZCoordinates(int zCoordinates) {
this.zCoordinates = zCoordinates;
}
//
getter and setter method for scale of the model
public void setModelScale(float modelScale) {
this.modelScale = modelScale;
}
public float getModelScale() {
return modelScale;
}
//
getter and setter for model absolute path
public URL getAbsoluteModelPath() {
return absoluteModelPath;
}
public void setAbsoluteModelPath(URL absoluteModelPath) {
this.absoluteModelPath = absoluteModelPath;
}
public URL[] getArray() {
return array;
}
public static void main(String argv []) {
ModelReader initialize = new ModelReader();
}
}
Standard model loading ( this code is unfinished , still working on it ):
import com.jme.math.Vector3f;
import com.jmex.model.converters.AseToJme;
import com.jmex.model.converters.MaxToJme;
import com.jmex.model.converters.Md2ToJme;
import com.jmex.model.converters.Md3ToJme;
import com.jmex.model.converters.MilkToJme;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.jme.app.AbstractGame;
import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingSphere;
import com.jme.scene.Node;
import com.jme.util.export.binary.BinaryImporter;
import com.jmex.model.converters.FormatConverter;
import com.jmex.model.converters.ObjToJme;
public class ModelLoader extends SimpleGame {
private static final Logger logger = Logger.getLogger(ModelLoader.class.getName());
private static ModelReader readModelData = new ModelReader();
private static URL [] model;
public static void main(String[] args) {
ModelLoader app = new ModelLoader();
// app.setDialogBehaviour(AbstractGame.ALWAYS_SHOW_PROPS_DIALOG);
// // Turn the logger off so we can see the XML later on
app.start();
}
protected void simpleInitGame() {
for(int i=0;i<2;i++){
model = readModelData.getArray();
System.out.println(model[i]);
}
Node [] loadedModel = null;
FormatConverter converter = new ObjToJme();
ByteArrayOutputStream BO = new ByteArrayOutputStream();
converter.setProperty("mtllib",model);
try {
for(int z = 0 ; z <model.length;z++)
loadedModel[z] = (Node)BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
loadedModel[1].setLocalScale(readModelData.getModelScale());
loadedModel[1].setModelBound(new BoundingSphere());
loadedModel[1].updateModelBound();
loadedModel[2].setLocalScale(readModelData.getModelScale());
loadedModel[2].setModelBound(new BoundingSphere());
loadedModel[2].updateModelBound();
rootNode.attachChild(loadedModel[2]);
}catch(Exception x ){
System.err.print("something aint work");
}
}
} // end of class
And finally XML file (well at least simplified version) :
<scene>
<object>
<modelLocation>file:///D:netbeans projectsjmesrcjmetestdatamodelmaggie.obj</modelLocation>
<LocationX>0</LocationX>
<LocationY>0</LocationY>
<LocationZ>0</LocationZ>
<Scale>.1f</Scale>
</object>
<object>
<modelLocation>file:///D:netbeans projectsjmesrcjmetestdatamodelmesh.obj</modelLocation>
<LocationX>20</LocationX>
<LocationY>20</LocationY>
<LocationZ>20</LocationZ>
<Scale>.1f</Scale>
</object>
</scene>