Skip to content

Commit

Permalink
(#10) Add outline support to .obj export
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasstarsz committed Aug 29, 2021
1 parent e30d566 commit f8dab19
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
29 changes: 28 additions & 1 deletion src/main/java/tech/fastj/resources/models/MtlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,27 @@ public static void write(Path destinationPath, Model2D model) {
}

private static void writeMaterial(StringBuilder fileContents, Polygon2D polygon, Path destinationPath, int materialIndex) {
switch (polygon.getRenderStyle()) {
case Fill: {
writeFillMaterial(fileContents, polygon, destinationPath, materialIndex);
break;
}
case Outline: {
writeOutlineMaterial(fileContents, polygon, materialIndex);
break;
}
case FillAndOutline: {
writeFillMaterial(fileContents, polygon, destinationPath, materialIndex);
writeOutlineMaterial(fileContents, polygon, materialIndex);
break;
}
}
}

private static void writeFillMaterial(StringBuilder fileContents, Polygon2D polygon, Path destinationPath, int materialIndex) {
fileContents.append(ParsingKeys.NewMaterial)
.append(' ')
.append("Polygon2D_material_")
.append("Polygon2D_material_fill_")
.append(materialIndex)
.append(LineSeparator);

Expand All @@ -77,6 +95,15 @@ private static void writeMaterial(StringBuilder fileContents, Polygon2D polygon,
}
}

private static void writeOutlineMaterial(StringBuilder fileContents, Polygon2D polygon, int materialIndex) {
fileContents.append(ParsingKeys.NewMaterial)
.append(' ')
.append("Polygon2D_material_outline_")
.append(materialIndex)
.append(LineSeparator);
writeColorMaterial(fileContents, polygon.getOutlineColor());
}

private static void writeGradientMaterial(StringBuilder fileContents, Polygon2D polygon, Path destinationPath, int materialIndex) {
writeDefaultColorValues(fileContents);

Expand Down
49 changes: 44 additions & 5 deletions src/main/java/tech/fastj/resources/models/ObjUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ public static void write(Path destinationPath, Model2D model) {
for (int i = 0; i < model.getPolygons().length; i++) {
Polygon2D polygon = model.getPolygons()[i];
writeObject(fileContents, i + 1);
writeMaterialUsage(fileContents, i + 1);
writeFaces(fileContents, polygon, vertexCount);
writeMaterial(fileContents, polygon, i + 1, vertexCount);

vertexCount += polygon.getPoints().length;
}
Expand Down Expand Up @@ -127,10 +126,41 @@ private static void writeObject(StringBuilder fileContents, int polygonIndex) {
.append(LineSeparator);
}

private static void writeMaterialUsage(StringBuilder fileContents, int polygonIndex) {
private static void writeMaterial(StringBuilder fileContents, Polygon2D polygon, int polygonIndex, int vertexCount) {
switch (polygon.getRenderStyle()) {
case Fill: {
writeFillMaterialUsage(fileContents, polygonIndex);
writeFaces(fileContents, polygon, vertexCount);
break;
}
case Outline: {
writeOutlineMaterialUsage(fileContents, polygonIndex);
writeLines(fileContents, polygon, vertexCount);
break;
}
case FillAndOutline: {
writeFillMaterialUsage(fileContents, polygonIndex);
writeFaces(fileContents, polygon, vertexCount);
writeOutlineMaterialUsage(fileContents, polygonIndex);
writeLines(fileContents, polygon, vertexCount);
break;
}
}
fileContents.append(LineSeparator);
}

private static void writeFillMaterialUsage(StringBuilder fileContents, int polygonIndex) {
fileContents.append(ParsingKeys.UseMaterial)
.append(' ')
.append("Polygon2D_material_fill_")
.append(polygonIndex)
.append(LineSeparator);
}

private static void writeOutlineMaterialUsage(StringBuilder fileContents, int polygonIndex) {
fileContents.append(ParsingKeys.UseMaterial)
.append(' ')
.append("Polygon2D_material_")
.append("Polygon2D_material_outline_")
.append(polygonIndex)
.append(LineSeparator);
}
Expand All @@ -143,7 +173,15 @@ private static void writeFaces(StringBuilder fileContents, Polygon2D polygon, in
.append('/')
.append(vertexCount + i);
}
fileContents.append(LineSeparator).append(LineSeparator);
fileContents.append(LineSeparator);
}

private static void writeLines(StringBuilder fileContents, Polygon2D polygon, int vertexCount) {
fileContents.append(ParsingKeys.ObjectLine);
for (int i = 1; i <= polygon.getPoints().length; i++) {
fileContents.append(' ').append(vertexCount + i);
}
fileContents.append(LineSeparator);
}

public static class ParsingKeys {
Expand All @@ -158,5 +196,6 @@ private ParsingKeys() {
public static final String ObjectName = "g";
public static final String UseMaterial = "usemtl";
public static final String ObjectFace = "f";
public static final String ObjectLine = "l";
}
}

0 comments on commit f8dab19

Please sign in to comment.