private static FbxElement readFBXElement(ByteBuffer byteBuffer) throws IOException { long endOffset = getUInt(byteBuffer); if(endOffset == 0) return null; long propCount = getUInt(byteBuffer); getUInt(byteBuffer); // Properties length unused FbxElement element = new FbxElement((int) propCount); element.id = new String(getBytes(byteBuffer, getUByte(byteBuffer))); for(int i = 0; i < propCount; ++i) { char dataType = readDataType(byteBuffer); element.properties.add(readData(byteBuffer, dataType)); element.propertiesTypes[i] = dataType; } if(byteBuffer.position() < endOffset) { while(byteBuffer.position() < (endOffset - BLOCK_SENTINEL_LENGTH)) element.children.add(readFBXElement(byteBuffer)); if(!Arrays.equals(BLOCK_SENTINEL_DATA, getBytes(byteBuffer, BLOCK_SENTINEL_LENGTH))) throw new IOException("Failed to read block sentinel, expected 13 zero bytes"); } if(byteBuffer.position() != endOffset) throw new IOException("Data length not equal to expected"); return element; }
private static Object readArray(ByteBuffer byteBuffer, char type, int bytes) throws IOException { int count = (int) getUInt(byteBuffer); int encoding = (int) getUInt(byteBuffer); int length = (int) getUInt(byteBuffer); byte[] data = getBytes(byteBuffer, length); if(encoding == 1) data = inflate(data); if(data.length != count * bytes) throw new IOException("Wrong data lenght. Expected: " + count * bytes + ", got: " + data.length);
return byteBuffer.getLong(); case 'R': return getBytes(byteBuffer, (int) getUInt(byteBuffer)); case 'S': return new String(getBytes(byteBuffer, (int) getUInt(byteBuffer))); case 'f': return readArray(byteBuffer, 'f', 4); case 'i': return readArray(byteBuffer, 'i', 4); case 'd': return readArray(byteBuffer, 'd', 8); case 'l': return readArray(byteBuffer, 'l', 8); case 'b': return readArray(byteBuffer, 'b', 1); case 'c': return readArray(byteBuffer, 'c', 1);
public static FbxFile readFBX(InputStream stream) throws IOException { FbxFile fbxFile = new FbxFile(); // Read file to byte buffer so we can know current position in file ByteBuffer byteBuffer = readToByteBuffer(stream); try { stream.close(); } catch(IOException e) { } // Check majic header byte[] majic = getBytes(byteBuffer, HEAD_MAGIC.length); if(!Arrays.equals(HEAD_MAGIC, majic)) throw new IOException("Either ASCII FBX or corrupt file. " + "Only binary FBX files are supported"); // Read version fbxFile.version = getUInt(byteBuffer); // Read root elements while(true) { FbxElement e = readFBXElement(byteBuffer); if(e == null) break; fbxFile.rootElements.add(e); } return fbxFile; }
private void loadScene(InputStream stream) throws IOException { logger.log(Level.FINE, "Loading scene {0}", sceneFilename); long startTime = System.currentTimeMillis(); FbxFile scene = FbxReader.readFBX(stream); for(FbxElement e : scene.rootElements) { // Is it possible for elements to be in wrong order? switch(e.id) { case "GlobalSettings": loadGlobalSettings(e); break; case "Objects": loadObjects(e); break; case "Connections": loadConnections(e); break; } } long estimatedTime = System.currentTimeMillis() - startTime; logger.log(Level.FINE, "Loading done in {0} ms", estimatedTime); }
public static FbxFile readFBX(InputStream stream) throws IOException { FbxFile fbxFile = new FbxFile(); // Read file to byte buffer so we can know current position in file ByteBuffer byteBuffer = readToByteBuffer(stream); try { stream.close(); } catch(IOException e) { } // Check majic header byte[] majic = getBytes(byteBuffer, HEAD_MAGIC.length); if(!Arrays.equals(HEAD_MAGIC, majic)) throw new IOException("Either ASCII FBX or corrupt file. " + "Only binary FBX files are supported"); // Read version fbxFile.version = getUInt(byteBuffer); // Read root elements while(true) { FbxElement e = readFBXElement(byteBuffer); if(e == null) break; fbxFile.rootElements.add(e); } return fbxFile; }
private void loadData(InputStream stream) throws IOException { FbxFile scene = FbxReader.readFBX(stream); FbxDump.dumpFile(scene); // TODO: Load FBX object templates for (FbxElement e : scene.rootElements) { if (e.id.equals("FBXHeaderExtension")) { loadHeader(e); } else if (e.id.equals("GlobalSettings")) { loadGlobalSettings(e); } else if (e.id.equals("Objects")) { loadObjects(e); } else if (e.id.equals("Connections")) { connectObjects(e); } } }
private static FbxElement readFBXElement(ByteBuffer byteBuffer) throws IOException { long endOffset = getUInt(byteBuffer); if(endOffset == 0) return null; long propCount = getUInt(byteBuffer); getUInt(byteBuffer); // Properties length unused FbxElement element = new FbxElement((int) propCount); element.id = new String(getBytes(byteBuffer, getUByte(byteBuffer))); for(int i = 0; i < propCount; ++i) { char dataType = readDataType(byteBuffer); element.properties.add(readData(byteBuffer, dataType)); element.propertiesTypes[i] = dataType; } if(byteBuffer.position() < endOffset) { while(byteBuffer.position() < (endOffset - BLOCK_SENTINEL_LENGTH)) element.children.add(readFBXElement(byteBuffer)); if(!Arrays.equals(BLOCK_SENTINEL_DATA, getBytes(byteBuffer, BLOCK_SENTINEL_LENGTH))) throw new IOException("Failed to read block sentinel, expected 13 zero bytes"); } if(byteBuffer.position() != endOffset) throw new IOException("Data length not equal to expected"); return element; }
return byteBuffer.getLong(); case 'R': return getBytes(byteBuffer, (int) getUInt(byteBuffer)); case 'S': return new String(getBytes(byteBuffer, (int) getUInt(byteBuffer))); case 'f': return readArray(byteBuffer, 'f', 4); case 'i': return readArray(byteBuffer, 'i', 4); case 'd': return readArray(byteBuffer, 'd', 8); case 'l': return readArray(byteBuffer, 'l', 8); case 'b': return readArray(byteBuffer, 'b', 1); case 'c': return readArray(byteBuffer, 'c', 1);
private static Object readArray(ByteBuffer byteBuffer, char type, int bytes) throws IOException { int count = (int) getUInt(byteBuffer); int encoding = (int) getUInt(byteBuffer); int length = (int) getUInt(byteBuffer); byte[] data = getBytes(byteBuffer, length); if(encoding == 1) data = inflate(data); if(data.length != count * bytes) throw new IOException("Wrong data lenght. Expected: " + count * bytes + ", got: " + data.length);
private void loadScene(InputStream stream) throws IOException { logger.log(Level.FINE, "Loading scene {0}", sceneFilename); long startTime = System.currentTimeMillis(); FbxFile scene = FbxReader.readFBX(stream); for(FbxElement e : scene.rootElements) { // Is it possible for elements to be in wrong order? switch(e.id) { case "GlobalSettings": loadGlobalSettings(e); break; case "Objects": loadObjects(e); break; case "Connections": loadConnections(e); break; } } long estimatedTime = System.currentTimeMillis() - startTime; logger.log(Level.FINE, "Loading done in {0} ms", estimatedTime); }
private void loadData(InputStream stream) throws IOException { FbxFile scene = FbxReader.readFBX(stream); FbxDump.dumpFile(scene); // TODO: Load FBX object templates for (FbxElement e : scene.rootElements) { if (e.id.equals("FBXHeaderExtension")) { loadHeader(e); } else if (e.id.equals("GlobalSettings")) { loadGlobalSettings(e); } else if (e.id.equals("Objects")) { loadObjects(e); } else if (e.id.equals("Connections")) { connectObjects(e); } } }