Commits

Michael Ludwig  committed 5dc1fbe

Add geometry loader interface in style of texture loader and a PLY file loader.

  • Participants
  • Parent commits 8bf2259

Comments (0)

Files changed (3)

File ferox-renderer/src/main/java/com/ferox/renderer/loader/ASCIIPLYFileLoader.java

 import com.ferox.renderer.geom.Tangents;
 import com.ferox.renderer.geom.TriangleIterator;
 
+import java.io.BufferedInputStream;
 import java.io.BufferedReader;
 import java.io.IOException;
-import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.util.*;
 
  */
 public class ASCIIPLYFileLoader implements GeometryFileLoader {
     @Override
-    public Geometry read(Framework framework, InputStream input) throws IOException {
+    public Geometry read(Framework framework, BufferedInputStream input) throws IOException {
         try (BufferedReader in = new BufferedReader(new InputStreamReader(input))) {
             VertexElementDefinition vertices = null;
             FaceElementDefinition faces = null;
             int currentElement = 0;
             int elementCount = 0;
 
+            // FIXME if this file is total hocum, then a new line could read way more than 32 bytes
+            // and then mark is hosed, should handle the whole parsing more like how it's done in Radiance or DDS loaders
+            input.mark(32);
             String line = in.readLine();
             if (!"ply".equalsIgnoreCase(line)) {
+                input.reset();
                 return null;
             }
+            line = in.readLine();
             if (!"format ascii 1.0".equalsIgnoreCase(line)) {
+                input.reset();
                 return null;
             }
 
 
             // compute additional vector attributes
             // FIXME compute normals here as well, if they're not provided
-            TriangleIterator ti = TriangleIterator.Builder.newBuilder().vertices(v.pos).normals(v.norm)
-                                                          .textureCoordinates(v.tc).tangents(v.tan)
-                                                          .fromElements(i, 0, i.length).build();
-            Tangents.compute(ti);
+            if (v.hasTexCoords()) {
+                TriangleIterator ti = TriangleIterator.Builder.newBuilder().vertices(v.pos).normals(v.norm)
+                                                              .textureCoordinates(v.tc).tangents(v.tan)
+                                                              .fromElements(i, 0, i.length).build();
+                Tangents.compute(ti);
+            }
 
             vertices = new VertexAttribute(framework.newVertexBuffer().from(v.pos).build(), 3);
             normals = new VertexAttribute(framework.newVertexBuffer().from(v.norm).build(), 3);
-            texCoords = new VertexAttribute(framework.newVertexBuffer().from(v.tc).build(), 2);
-            tangents = new VertexAttribute(framework.newVertexBuffer().from(v.tan).build(), 4);
+            if (v.hasTexCoords()) {
+                texCoords = new VertexAttribute(framework.newVertexBuffer().from(v.tc).build(), 2);
+                tangents = new VertexAttribute(framework.newVertexBuffer().from(v.tan).build(), 4);
+            } else {
+                texCoords = null;
+                tangents = null;
+            }
 
 
             indices = framework.newElementBuffer().fromUnsigned(i).build();
     }
 
     private static class VertexElementDefinition extends ElementDefinition {
-        private static final List<String> SPECIAL = Arrays.asList("x", "y", "z", "nx", "ny", "nz", "u", "v");
+        private static final List<String> SPECIAL = Arrays.asList("x", "y", "z", "nx", "ny", "nz", "u", "v",
+                                                                  "s", "t");
 
         final float[] pos;
         final float[] norm;
         }
 
         boolean hasTexCoords() {
-            return properties.containsKey("u") && properties.containsKey("v");
+            return (properties.containsKey("u") && properties.containsKey("v")) ||
+                   (properties.containsKey("s") && properties.containsKey("t"));
         }
 
         // TODO add support for vertex colors and the red, green, blue properties
                 norm[i * 3 + 2] = Float.parseFloat(line[properties.get("nz")]);
             }
             if (hasTexCoords()) {
-                pos[i * 2] = Float.parseFloat(line[properties.get("u")]);
-                pos[i * 2 + 1] = Float.parseFloat(line[properties.get("v")]);
+                if (properties.containsKey("u") && properties.containsKey("v")) {
+                    tc[i * 2] = Float.parseFloat(line[properties.get("u")]);
+                    tc[i * 2 + 1] = Float.parseFloat(line[properties.get("v")]);
+                } else {
+                    tc[i * 2] = Float.parseFloat(line[properties.get("s")]);
+                    tc[i * 2 + 1] = Float.parseFloat(line[properties.get("t")]);
+                }
             }
         }
     }
 
         @Override
         void addProperty(String[] line) throws IOException {
-            if (line[line.length - 1].equalsIgnoreCase("vertex_index")) {
-                if (line.length != 5 || line[1].equalsIgnoreCase("list") ||
-                    !line[2].equalsIgnoreCase("uchar") || !line[3].equalsIgnoreCase("int")) {
-                    throw new IOException("Face vertex_index property must be list uchar int, any other type is unsupported");
+            if (line[line.length - 1].equalsIgnoreCase("vertex_index") ||
+                line[line.length - 1].equalsIgnoreCase("vertex_indices")) {
+                if (line.length != 5 || !line[1].equalsIgnoreCase("list") ||
+                    line[2].equalsIgnoreCase("float") || line[3].equalsIgnoreCase("float")) {
+                    throw new IOException("Face vertex_index property must be list with integer params, any other type is unsupported");
                 }
             }
             super.addProperty(line);
         @Override
         void processElement(String[] line, int i) {
             // skip if we don't have the conventional vertex_index list defined
-            if (!properties.containsKey("vertex_index")) {
+            int base;
+            if (properties.containsKey("vertex_index")) {
+                base = properties.get("vertex_index");
+            } else if (properties.containsKey("vertex_indices")) {
+                base = properties.get("vertex_indices");
+            } else {
                 return;
             }
 
-            int base = properties.get("vertex_index");
             int ct = Integer.parseInt(line[base]);
             if (ct == 3) {
                 // plain triangle

File ferox-renderer/src/main/java/com/ferox/renderer/loader/GeometryFileLoader.java

 import com.ferox.renderer.Framework;
 import com.ferox.renderer.geom.Geometry;
 
+import java.io.BufferedInputStream;
 import java.io.IOException;
-import java.io.InputStream;
 
 /**
  *
  */
 public interface GeometryFileLoader {
-    public Geometry read(Framework framework, InputStream in) throws IOException;
+    public Geometry read(Framework framework, BufferedInputStream in) throws IOException;
 }

File ferox-renderer/src/main/java/com/ferox/renderer/loader/GeometryLoader.java

      */
     public static Geometry readGeometry(Framework framework, InputStream stream) throws IOException {
         // make sure we're buffered
-        if (!(stream instanceof BufferedInputStream)) {
-            stream = new BufferedInputStream(stream);
+        BufferedInputStream in;
+        if (stream instanceof BufferedInputStream) {
+            in = (BufferedInputStream) stream;
+        } else {
+            in = new BufferedInputStream(stream);
         }
 
         // load the file
 
         synchronized (loaders) {
             for (int i = loaders.size() - 1; i >= 0; i--) {
-                stream.reset();
-                t = loaders.get(i).read(framework, stream);
+                t = loaders.get(i).read(framework, in);
                 if (t != null) {
                     return t; // we've loaded it
                 }