Roi Atalla avatar Roi Atalla committed 5fec735

Changed Mesh to take a URL instead.

Comments (0)

Files changed (2)

src/main/java/com/ra4king/opengl/arcsynthesis/gl33/chapter7/example1/Example7_1.java

 		perspectiveMatrix = new Matrix4();
 		
 		try {
-			coneMesh = new Mesh(getClass().getResourceAsStream("example7.1.UnitConeTint.xml"));
-			cylinderMesh = new Mesh(getClass().getResourceAsStream("example7.1.UnitCylinderTint.xml"));
-			cubeTintMesh = new Mesh(getClass().getResourceAsStream("example7.1.UnitCubeTint.xml"));
-			cubeColorMesh = new Mesh(getClass().getResourceAsStream("example7.1.UnitCubeColor.xml"));
-			planeMesh = new Mesh(getClass().getResourceAsStream("example7.1.UnitPlane.xml"));
+			coneMesh = new Mesh(getClass().getResource("example7.1.UnitConeTint.xml"));
+			cylinderMesh = new Mesh(getClass().getResource("example7.1.UnitCylinderTint.xml"));
+			cubeTintMesh = new Mesh(getClass().getResource("example7.1.UnitCubeTint.xml"));
+			cubeColorMesh = new Mesh(getClass().getResource("example7.1.UnitCubeColor.xml"));
+			planeMesh = new Mesh(getClass().getResource("example7.1.UnitPlane.xml"));
 		}
 		catch(Exception exc) {
 			exc.printStackTrace();

src/main/java/com/ra4king/opengl/util/Mesh.java

 import static org.lwjgl.opengl.GL30.*;
 
 import java.io.InputStream;
+import java.net.URL;
 import java.nio.ByteBuffer;
 import java.nio.FloatBuffer;
 import java.nio.IntBuffer;
 	private int vao;
 	private ArrayList<RenderCmd> renderCommands;
 	
-	public Mesh(InputStream file) throws Exception {
+	public Mesh(URL url) throws Exception {
 		renderCommands = new ArrayList<RenderCmd>();
 		
-		XmlPullParser xml = XmlPullParserFactory.newInstance().newPullParser();
-		xml.setInput(file, "UTF-8");
-		
-		xml.next();
-		
-		xml.require(XmlPullParser.START_TAG, null, "mesh");
-		
 		ArrayList<Attribute> attributes = new ArrayList<Attribute>();
 		
 		ByteBuffer attributeData = BufferUtils.createByteBuffer(0), indexData = BufferUtils.createByteBuffer(0);
 		
-		do {
-			xml.nextTag();
+		try(InputStream is = url.openStream()) {
+			XmlPullParser xml = XmlPullParserFactory.newInstance().newPullParser();
+			xml.setInput(is, "UTF-8");
 			
-			switch(xml.getName()) {
-				case "attribute": {
-					int index = Integer.parseInt(xml.getAttributeValue(null, "index"));
-					String type = xml.getAttributeValue(null, "type");
-					int size = Integer.parseInt(xml.getAttributeValue(null, "size"));
-					
-					Attribute attrib = new Attribute(index,type,size);
-					attributes.add(attrib);
-					
-					xml.next();
-					xml.require(XmlPullParser.TEXT, null, null);
-					
-					attributeData = attrib.storeData(attributeData, clean(xml.getText().trim().replace("\r\n", " ").replace('\n', ' ').split(" ")));
-					
-					xml.next();
-					xml.require(XmlPullParser.END_TAG, null, "attribute");
-					
-					break;
+			xml.next();
+			
+			xml.require(XmlPullParser.START_TAG, null, "mesh");
+			
+			do {
+				switch(xml.nextTag()) {
+					case XmlPullParser.START_TAG:
+						switch(xml.getName()) {
+							case "attribute": {
+								int index = Integer.parseInt(xml.getAttributeValue(null, "index"));
+								String type = xml.getAttributeValue(null, "type");
+								int size = Integer.parseInt(xml.getAttributeValue(null, "size"));
+								
+								Attribute attrib = new Attribute(index,type,size);
+								attributes.add(attrib);
+								
+								xml.next();
+								xml.require(XmlPullParser.TEXT, null, null);
+								
+								attributeData = attrib.storeData(attributeData, clean(xml.getText().trim().replace("\r\n", " ").replace('\n', ' ').split(" ")));
+								
+								xml.next();
+								xml.require(XmlPullParser.END_TAG, null, "attribute");
+								
+								break;
+							}
+							case "indices": {
+								String primitive = xml.getAttributeValue(null, "cmd");
+								String type = xml.getAttributeValue(null, "type");
+								
+								RenderCmd cmd = new RenderCmd(primitive,type);
+								renderCommands.add(cmd);
+								
+								xml.next();
+								xml.require(XmlPullParser.TEXT, null, null);
+								
+								indexData = cmd.storeData(indexData, clean(xml.getText().trim().replace("\r\n", " ").replace('\n', ' ').split(" ")));
+								
+								xml.next();
+								xml.require(XmlPullParser.END_TAG, null, "indices");
+								
+								break;
+							}
+							case "arrays": {
+								String primitive = xml.getAttributeValue(null, "cmd");
+								int start = Integer.parseInt(xml.getAttributeValue(null, "start"));
+								int count = Integer.parseInt(xml.getAttributeValue(null, "count"));
+								
+								RenderCmd cmd = new RenderCmd(primitive,start,count);
+								renderCommands.add(cmd);
+								
+								xml.next();
+								xml.require(XmlPullParser.END_TAG, null, "arrays");
+								
+								break;
+							}
+							case "mesh":
+								break;
+							default:
+								throw new IllegalArgumentException("Invalid TAG name: " + xml.getName());
+						}
+						
+						break;
+					case XmlPullParser.END_TAG:
+						continue;
 				}
-				case "indices": {
-					String primitive = xml.getAttributeValue(null, "cmd");
-					String type = xml.getAttributeValue(null, "type");
-					
-					RenderCmd cmd = new RenderCmd(primitive,type);
-					renderCommands.add(cmd);
-					
-					xml.next();
-					xml.require(XmlPullParser.TEXT, null, null);
-					
-					indexData = cmd.storeData(indexData, clean(xml.getText().trim().replace("\r\n", " ").replace('\n', ' ').split(" ")));
-					
-					xml.next();
-					xml.require(XmlPullParser.END_TAG, null, "indices");
-					
-					break;
-				}
-				case "arrays": {
-					String primitive = xml.getAttributeValue(null, "cmd");
-					int start = Integer.parseInt(xml.getAttributeValue(null, "start"));
-					int count = Integer.parseInt(xml.getAttributeValue(null, "count"));
-					
-					RenderCmd cmd = new RenderCmd(primitive,start,count);
-					renderCommands.add(cmd);
-					
-					xml.next();
-					xml.require(XmlPullParser.END_TAG, null, "arrays");
-					
-					break;
-				}
-				case "mesh":
-					break;
-				default:
-					throw new IllegalArgumentException("Invalid TAG name: " + xml.getName());
-			}
-		} while(xml.next() != XmlPullParser.END_DOCUMENT);
+			} while(xml.next() != XmlPullParser.END_DOCUMENT);
+		}
+		catch(Exception exc) {
+			throw exc;
+		}
 		
 		if(attributes.size() == 0)
 			throw new IllegalArgumentException("There must be at least 1 set of attributes.");
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.