use of org.bimserver.plugins.serializers.SerializerException in project GltfSerializers by opensourceBIM.
the class BinaryGltfSerializer method addNormalsAccessor.
private String addNormalsAccessor(IfcProduct ifcProduct, String bufferViewName, int byteOffset, int count) throws SerializerException {
if (count <= 0) {
throw new SerializerException("Count <= 0");
}
String accessorName = "accessor_normal_" + (accessorCounter++);
ObjectNode accessor = OBJECT_MAPPER.createObjectNode();
accessor.put("bufferView", bufferViewName);
accessor.put("byteOffset", byteOffset);
accessor.put("byteStride", 12);
accessor.put("componentType", FLOAT);
accessor.put("count", count);
accessor.put("type", "VEC3");
ArrayNode min = OBJECT_MAPPER.createArrayNode();
min.add(-1d);
min.add(-1d);
min.add(-1d);
ArrayNode max = OBJECT_MAPPER.createArrayNode();
max.add(1);
max.add(1);
max.add(1);
accessor.set("min", min);
accessor.set("max", max);
accessors.set(accessorName, accessor);
return accessorName;
}
use of org.bimserver.plugins.serializers.SerializerException in project GltfSerializers by opensourceBIM.
the class BinaryGltfSerializer method write.
@Override
protected boolean write(OutputStream outputStream, ProgressReporter progressReporter) throws SerializerException {
gltfNode = OBJECT_MAPPER.createObjectNode();
buffers = OBJECT_MAPPER.createObjectNode();
meshes = OBJECT_MAPPER.createObjectNode();
buffersViews = OBJECT_MAPPER.createObjectNode();
scenesNode = OBJECT_MAPPER.createObjectNode();
accessors = OBJECT_MAPPER.createObjectNode();
nodes = OBJECT_MAPPER.createObjectNode();
materials = OBJECT_MAPPER.createObjectNode();
shaders = OBJECT_MAPPER.createObjectNode();
gltfNode.set("meshes", meshes);
gltfNode.set("bufferViews", buffersViews);
gltfNode.set("scenes", scenesNode);
gltfNode.set("accessors", accessors);
gltfNode.set("nodes", nodes);
gltfNode.set("buffers", buffers);
gltfNode.set("materials", materials);
gltfNode.set("shaders", shaders);
createVertexColorMaterial();
try {
LittleEndianDataOutputStream dataOutputStream = new LittleEndianDataOutputStream(outputStream);
generateSceneAndBody();
// StringWriter stringWriter = new StringWriter();
// OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValue(stringWriter, gltfNode);
// System.out.println(stringWriter);
byte[] sceneBytes = gltfNode.toString().getBytes(Charsets.UTF_8);
writeHeader(dataOutputStream, 20, sceneBytes.length, body.capacity());
writeScene(dataOutputStream, sceneBytes);
writeBody(dataOutputStream, body.array());
} catch (IOException e) {
throw new SerializerException(e);
}
return false;
}
use of org.bimserver.plugins.serializers.SerializerException in project GltfSerializers by opensourceBIM.
the class BinaryGltfSerializer2 method write.
@Override
protected boolean write(OutputStream outputStream, ProgressReporter progressReporter) throws SerializerException {
gltfNode = OBJECT_MAPPER.createObjectNode();
buffers = OBJECT_MAPPER.createArrayNode();
meshes = OBJECT_MAPPER.createArrayNode();
buffersViews = OBJECT_MAPPER.createArrayNode();
scenesNode = OBJECT_MAPPER.createArrayNode();
accessors = OBJECT_MAPPER.createArrayNode();
nodes = OBJECT_MAPPER.createArrayNode();
materials = OBJECT_MAPPER.createArrayNode();
shaders = OBJECT_MAPPER.createObjectNode();
gltfNode.set("meshes", meshes);
gltfNode.set("bufferViews", buffersViews);
gltfNode.set("scenes", scenesNode);
gltfNode.set("accessors", accessors);
gltfNode.set("nodes", nodes);
gltfNode.set("buffers", buffers);
gltfNode.set("materials", materials);
// gltfNode.set("shaders", shaders);
createVertexColorMaterial();
try {
LittleEndianDataOutputStream dataOutputStream = new LittleEndianDataOutputStream(outputStream);
generateSceneAndBody();
// StringWriter stringWriter = new StringWriter();
// OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValue(stringWriter, gltfNode);
// System.out.println(stringWriter);
byte[] sceneBytes = gltfNode.toString().getBytes(Charsets.UTF_8);
writeHeader(dataOutputStream, 12, 8 + sceneBytes.length + (sceneBytes.length % 4 == 0 ? 0 : 4 - sceneBytes.length % 4), 8 + body.capacity() + (body.capacity() % 4 == 4 ? 0 : 4 - body.capacity() % 4));
writeScene(dataOutputStream, sceneBytes);
writeBody(dataOutputStream, body.array());
dataOutputStream.flush();
} catch (IOException e) {
throw new SerializerException(e);
}
return false;
}
use of org.bimserver.plugins.serializers.SerializerException in project GltfSerializers by opensourceBIM.
the class BinaryGltfSerializer2 method addVerticesAccessor.
private int addVerticesAccessor(IfcProduct ifcProduct, int bufferViewIndex, int startPosition, int count) throws SerializerException {
if (count <= 0) {
throw new SerializerException("Count <= 0");
}
if (count * 12 == 9000) {
System.out.println();
}
GeometryData data = ifcProduct.getGeometry().getData();
ByteBuffer verticesBuffer = ByteBuffer.wrap(data.getVertices());
ObjectNode accessor = OBJECT_MAPPER.createObjectNode();
accessor.put("bufferView", bufferViewIndex);
accessor.put("byteOffset", startPosition);
// accessor.put("byteStride", 12);
accessor.put("componentType", FLOAT);
accessor.put("count", count);
accessor.put("type", "VEC3");
verticesBuffer.order(ByteOrder.LITTLE_ENDIAN);
float[] min = { Float.MAX_VALUE, Float.MAX_VALUE, Float.MAX_VALUE };
float[] max = { -Float.MAX_VALUE, -Float.MAX_VALUE, -Float.MAX_VALUE };
for (int i = 0; i < verticesBuffer.capacity(); i += 12) {
for (int j = 0; j < 3; j++) {
float val = verticesBuffer.getFloat(i + (j * 4));
if (val > max[j]) {
max[j] = val;
}
if (val < min[j]) {
min[j] = val;
}
}
}
ArrayNode minNode = OBJECT_MAPPER.createArrayNode();
minNode.add(min[0]);
minNode.add(min[1]);
minNode.add(min[2]);
ArrayNode maxNode = OBJECT_MAPPER.createArrayNode();
maxNode.add(max[0]);
maxNode.add(max[1]);
maxNode.add(max[2]);
accessor.set("min", minNode);
accessor.set("max", maxNode);
accessors.add(accessor);
return accessors.size() - 1;
}
use of org.bimserver.plugins.serializers.SerializerException in project GltfSerializers by opensourceBIM.
the class BinaryGltfSerializer2 method generateSceneAndBody.
private void generateSceneAndBody() throws SerializerException {
int totalBodyByteLength = 0;
int totalIndicesByteLength = 0;
int totalVerticesByteLength = 0;
int totalNormalsByteLength = 0;
int totalColorsByteLength = 0;
int maxIndexValues = 16389;
for (IfcProduct ifcProduct : model.getAllWithSubTypes(IfcProduct.class)) {
GeometryInfo geometryInfo = ifcProduct.getGeometry();
if (!ifcProduct.eClass().getName().equals("IfcOpeningElement") && geometryInfo != null && geometryInfo.getData().getVertices().length > 0) {
GeometryData data = geometryInfo.getData();
int nrIndicesBytes = data.getIndices().length;
totalIndicesByteLength += nrIndicesBytes / 2;
if (nrIndicesBytes > 4 * maxIndexValues) {
int nrIndices = nrIndicesBytes / 4;
totalVerticesByteLength += nrIndices * 3 * 4;
totalNormalsByteLength += nrIndices * 3 * 4;
if (data.getMaterials() != null) {
totalColorsByteLength += nrIndices * 4 * 4;
}
} else {
totalVerticesByteLength += data.getVertices().length;
totalNormalsByteLength += data.getNormals().length;
if (data.getMaterials() != null) {
totalColorsByteLength += data.getMaterials().length;
}
}
}
}
totalBodyByteLength = totalIndicesByteLength + totalVerticesByteLength + totalNormalsByteLength + totalColorsByteLength;
body = ByteBuffer.allocate(totalBodyByteLength + materialColorFragmentShaderBytes.length + materialColorVertexShaderBytes.length + vertexColorFragmentShaderBytes.length + vertexColorVertexShaderBytes.length);
body.order(ByteOrder.LITTLE_ENDIAN);
ByteBuffer newIndicesBuffer = ByteBuffer.allocate(totalIndicesByteLength);
newIndicesBuffer.order(ByteOrder.LITTLE_ENDIAN);
ByteBuffer newVerticesBuffer = ByteBuffer.allocate(totalVerticesByteLength);
newVerticesBuffer.order(ByteOrder.LITTLE_ENDIAN);
ByteBuffer newNormalsBuffer = ByteBuffer.allocate(totalNormalsByteLength);
newNormalsBuffer.order(ByteOrder.LITTLE_ENDIAN);
ByteBuffer newColorsBuffer = ByteBuffer.allocate(totalColorsByteLength);
newColorsBuffer.order(ByteOrder.LITTLE_ENDIAN);
int indicesBufferView = createBufferView(totalIndicesByteLength, 0, ELEMENT_ARRAY_BUFFER, -1);
int verticesBufferView = createBufferView(totalVerticesByteLength, totalIndicesByteLength, ARRAY_BUFFER, 12);
int normalsBufferView = createBufferView(totalNormalsByteLength, totalIndicesByteLength + totalVerticesByteLength, ARRAY_BUFFER, 12);
int colorsBufferView = -1;
scenesNode.add(createDefaultScene());
gltfNode.put("scene", 0);
createModelNode();
for (IfcProduct ifcProduct : model.getAllWithSubTypes(IfcProduct.class)) {
GeometryInfo geometryInfo = ifcProduct.getGeometry();
if (!ifcProduct.eClass().getName().equals("IfcOpeningElement") && geometryInfo != null) {
ByteBuffer matrixByteBuffer = ByteBuffer.wrap(ifcProduct.getGeometry().getTransformation());
matrixByteBuffer.order(ByteOrder.LITTLE_ENDIAN);
DoubleBuffer doubleBuffer = matrixByteBuffer.asDoubleBuffer();
float[] matrix = new float[16];
for (int i = 0; i < doubleBuffer.capacity(); i++) {
matrix[i] = (float) doubleBuffer.get();
}
updateExtends(geometryInfo, matrix);
}
}
float[] offsets = getOffsets();
// This will "normalize" the model by moving it's axis-aligned bounding box center to the 0-point. This will always be the wrong position, but at least the building will be close to the 0-point
modelTranslation.add(-offsets[0]);
modelTranslation.add(-offsets[1]);
modelTranslation.add(-offsets[2]);
for (IfcProduct ifcProduct : model.getAllWithSubTypes(IfcProduct.class)) {
GeometryInfo geometryInfo = ifcProduct.getGeometry();
if (!ifcProduct.eClass().getName().equals("IfcOpeningElement") && geometryInfo != null && geometryInfo.getData().getVertices().length > 0) {
int startPositionIndices = newIndicesBuffer.position();
int startPositionVertices = newVerticesBuffer.position();
int startPositionNormals = newNormalsBuffer.position();
int startPositionColors = newColorsBuffer.position();
GeometryData data = geometryInfo.getData();
ByteBuffer indicesBuffer = ByteBuffer.wrap(data.getIndices());
indicesBuffer.order(ByteOrder.LITTLE_ENDIAN);
IntBuffer indicesIntBuffer = indicesBuffer.asIntBuffer();
ByteBuffer verticesBuffer = ByteBuffer.wrap(data.getVertices());
verticesBuffer.order(ByteOrder.LITTLE_ENDIAN);
FloatBuffer verticesFloatBuffer = verticesBuffer.asFloatBuffer();
ByteBuffer normalsBuffer = ByteBuffer.wrap(data.getNormals());
normalsBuffer.order(ByteOrder.LITTLE_ENDIAN);
FloatBuffer normalsFloatBuffer = normalsBuffer.asFloatBuffer();
FloatBuffer materialsFloatBuffer = null;
if (data.getMaterials() != null) {
ByteBuffer materialsBuffer = ByteBuffer.wrap(data.getMaterials());
materialsBuffer.order(ByteOrder.LITTLE_ENDIAN);
materialsFloatBuffer = materialsBuffer.asFloatBuffer();
}
if (data.getIndices().length > 4 * maxIndexValues) {
int totalNrIndices = indicesIntBuffer.capacity();
int nrParts = (totalNrIndices + maxIndexValues - 1) / maxIndexValues;
ArrayNode primitivesNode = OBJECT_MAPPER.createArrayNode();
for (int part = 0; part < nrParts; part++) {
startPositionIndices = newIndicesBuffer.position();
startPositionVertices = newVerticesBuffer.position();
startPositionNormals = newNormalsBuffer.position();
startPositionColors = newColorsBuffer.position();
short indexCounter = 0;
int upto = Math.min((part + 1) * maxIndexValues, totalNrIndices);
for (int i = part * maxIndexValues; i < upto; i++) {
newIndicesBuffer.putShort(indexCounter++);
}
int[] min = new int[] { 0 };
int[] max = new int[] { upto };
int nrVertices = upto - part * maxIndexValues;
for (int i = part * maxIndexValues; i < upto; i += 3) {
int oldIndex1 = indicesIntBuffer.get(i);
int oldIndex2 = indicesIntBuffer.get(i + 1);
int oldIndex3 = indicesIntBuffer.get(i + 2);
newVerticesBuffer.putFloat(verticesFloatBuffer.get(oldIndex1 * 3));
newVerticesBuffer.putFloat(verticesFloatBuffer.get(oldIndex1 * 3 + 1));
newVerticesBuffer.putFloat(verticesFloatBuffer.get(oldIndex1 * 3 + 2));
newVerticesBuffer.putFloat(verticesFloatBuffer.get(oldIndex2 * 3));
newVerticesBuffer.putFloat(verticesFloatBuffer.get(oldIndex2 * 3 + 1));
newVerticesBuffer.putFloat(verticesFloatBuffer.get(oldIndex2 * 3 + 2));
newVerticesBuffer.putFloat(verticesFloatBuffer.get(oldIndex3 * 3));
newVerticesBuffer.putFloat(verticesFloatBuffer.get(oldIndex3 * 3 + 1));
newVerticesBuffer.putFloat(verticesFloatBuffer.get(oldIndex3 * 3 + 2));
}
for (int i = part * maxIndexValues; i < upto; i += 3) {
int oldIndex1 = indicesIntBuffer.get(i);
int oldIndex2 = indicesIntBuffer.get(i + 1);
int oldIndex3 = indicesIntBuffer.get(i + 2);
newNormalsBuffer.putFloat(normalsFloatBuffer.get(oldIndex1 * 3));
newNormalsBuffer.putFloat(normalsFloatBuffer.get(oldIndex1 * 3 + 1));
newNormalsBuffer.putFloat(normalsFloatBuffer.get(oldIndex1 * 3 + 2));
newNormalsBuffer.putFloat(normalsFloatBuffer.get(oldIndex2 * 3));
newNormalsBuffer.putFloat(normalsFloatBuffer.get(oldIndex2 * 3 + 1));
newNormalsBuffer.putFloat(normalsFloatBuffer.get(oldIndex2 * 3 + 2));
newNormalsBuffer.putFloat(normalsFloatBuffer.get(oldIndex3 * 3));
newNormalsBuffer.putFloat(normalsFloatBuffer.get(oldIndex3 * 3 + 1));
newNormalsBuffer.putFloat(normalsFloatBuffer.get(oldIndex3 * 3 + 2));
}
if (materialsFloatBuffer != null) {
for (int i = part * maxIndexValues; i < upto; i += 3) {
int oldIndex1 = indicesIntBuffer.get(i);
int oldIndex2 = indicesIntBuffer.get(i + 1);
int oldIndex3 = indicesIntBuffer.get(i + 2);
newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex1 * 4));
newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex1 * 4 + 1));
newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex1 * 4 + 2));
newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex1 * 4 + 3));
newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex2 * 4));
newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex2 * 4 + 1));
newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex2 * 4 + 2));
newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex2 * 4 + 3));
newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex3 * 4));
newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex3 * 4 + 1));
newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex3 * 4 + 2));
newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex3 * 4 + 3));
}
}
ObjectNode primitiveNode = OBJECT_MAPPER.createObjectNode();
int indicesAccessor = addIndicesAccessor(ifcProduct, indicesBufferView, startPositionIndices, nrVertices / 3, min, max);
int verticesAccessor = addVerticesAccessor(ifcProduct, verticesBufferView, startPositionVertices, nrVertices);
int normalsAccessor = addNormalsAccessor(ifcProduct, normalsBufferView, startPositionNormals, nrVertices);
int colorAccessor = -1;
if (data.getMaterials() != null) {
if (colorsBufferView == -1) {
colorsBufferView = createBufferView(totalColorsByteLength, totalIndicesByteLength + totalVerticesByteLength + totalNormalsByteLength, ARRAY_BUFFER, 16);
}
colorAccessor = addColorsAccessor(ifcProduct, colorsBufferView, startPositionColors, 16);
}
primitivesNode.add(primitiveNode);
primitiveNode.put("indices", indicesAccessor);
primitiveNode.put("mode", TRIANGLES);
ObjectNode attributesNode = OBJECT_MAPPER.createObjectNode();
primitiveNode.set("attributes", attributesNode);
attributesNode.put("NORMAL", normalsAccessor);
attributesNode.put("POSITION", verticesAccessor);
if (colorAccessor != -1) {
// attributesNode.put("COLOR_0", colorAccessor);
primitiveNode.put("material", vertexColorIndex);
} else {
primitiveNode.put("material", createOrGetMaterial(ifcProduct.eClass().getName(), IfcColors.getDefaultColor(ifcProduct.eClass().getName())));
}
}
int meshId = addMesh(ifcProduct, primitivesNode);
int nodeId = addNode(meshId, ifcProduct);
translationChildrenNode.add(nodeId);
} else {
int maxVal = 0;
for (int i = 0; i < indicesIntBuffer.capacity(); i++) {
int index = indicesIntBuffer.get(i);
if (index > Short.MAX_VALUE) {
throw new SerializerException("Index too large to store as short " + index);
}
newIndicesBuffer.putShort((short) (index));
if (index > maxVal) {
maxVal = index;
}
}
int[] min = new int[] { 0 };
int[] max = new int[] { maxVal };
newVerticesBuffer.put(data.getVertices());
newNormalsBuffer.put(data.getNormals());
if (data.getMaterials() != null) {
newColorsBuffer.put(data.getMaterials());
}
int totalNrIndices = indicesIntBuffer.capacity();
ArrayNode primitivesNode = OBJECT_MAPPER.createArrayNode();
ObjectNode primitiveNode = OBJECT_MAPPER.createObjectNode();
int indicesAccessor = addIndicesAccessor(ifcProduct, indicesBufferView, startPositionIndices, totalNrIndices, min, max);
int verticesAccessor = addVerticesAccessor(ifcProduct, verticesBufferView, startPositionVertices, data.getVertices().length / 12);
int normalsAccessor = addNormalsAccessor(ifcProduct, normalsBufferView, startPositionNormals, data.getNormals().length / 12);
int colorAccessor = -1;
if (data.getMaterials() != null) {
if (colorsBufferView == -1) {
colorsBufferView = createBufferView(totalColorsByteLength, totalIndicesByteLength + totalVerticesByteLength + totalNormalsByteLength, ARRAY_BUFFER, 16);
}
colorAccessor = addColorsAccessor(ifcProduct, colorsBufferView, startPositionColors, data.getVertices().length / 12);
}
primitivesNode.add(primitiveNode);
primitiveNode.put("indices", indicesAccessor);
primitiveNode.put("mode", TRIANGLES);
ObjectNode attributesNode = OBJECT_MAPPER.createObjectNode();
primitiveNode.set("attributes", attributesNode);
attributesNode.put("NORMAL", normalsAccessor);
attributesNode.put("POSITION", verticesAccessor);
if (colorAccessor != -1) {
attributesNode.put("COLOR_0", colorAccessor);
primitiveNode.put("material", vertexColorIndex);
} else {
primitiveNode.put("material", createOrGetMaterial(ifcProduct.eClass().getName(), IfcColors.getDefaultColor(ifcProduct.eClass().getName())));
}
int meshId = addMesh(ifcProduct, primitivesNode);
int nodeId = addNode(meshId, ifcProduct);
translationChildrenNode.add(nodeId);
}
}
}
if (newIndicesBuffer.position() != newIndicesBuffer.capacity()) {
throw new SerializerException("Not all space used");
}
if (newVerticesBuffer.position() != newVerticesBuffer.capacity()) {
throw new SerializerException("Not all space used");
}
if (newNormalsBuffer.position() != newNormalsBuffer.capacity()) {
throw new SerializerException("Not all space used");
}
if (newColorsBuffer.position() != newColorsBuffer.capacity()) {
throw new SerializerException("Not all space used");
}
newIndicesBuffer.position(0);
newVerticesBuffer.position(0);
newNormalsBuffer.position(0);
newColorsBuffer.position(0);
body.put(newIndicesBuffer);
body.put(newVerticesBuffer);
body.put(newNormalsBuffer);
body.put(newColorsBuffer);
int vertexColorFragmentShaderBufferViewName = createBufferView(vertexColorFragmentShaderBytes.length, body.position(), -1, -1);
body.put(vertexColorFragmentShaderBytes);
int vertexColorVertexShaderBufferViewName = createBufferView(vertexColorVertexShaderBytes.length, body.position(), -1, -1);
body.put(vertexColorVertexShaderBytes);
int materialColorFragmentShaderBufferViewName = createBufferView(materialColorFragmentShaderBytes.length, body.position(), -1, -1);
body.put(materialColorFragmentShaderBytes);
int materialColorVertexShaderBufferViewName = createBufferView(materialColorVertexShaderBytes.length, body.position(), -1, -1);
body.put(materialColorVertexShaderBytes);
// gltfNode.set("animations", createAnimations());
gltfNode.set("asset", createAsset());
// gltfNode.set("programs", createPrograms());
gltfNode.put("scene", 0);
// gltfNode.set("skins", createSkins());
// gltfNode.set("techniques", createTechniques());
// createVertexColorShaders(vertexColorFragmentShaderBufferViewName, vertexColorVertexShaderBufferViewName);
// createMaterialColorShaders(materialColorFragmentShaderBufferViewName, materialColorVertexShaderBufferViewName);
addBuffer(body.capacity());
// ArrayNode extensions = OBJECT_MAPPER.createArrayNode();
// extensions.add("KHR_binary_glTF");
// gltfNode.set("extensionsUsed", extensions);
}
Aggregations