Skip to content

Commit

Permalink
(#10) Added .obj writer (implementation not complete)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasstarsz committed Aug 28, 2021
1 parent 3c23d10 commit 81245a1
Showing 1 changed file with 134 additions and 0 deletions.
134 changes: 134 additions & 0 deletions src/main/java/tech/fastj/resources/models/ObjUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package tech.fastj.resources.models;

import tech.fastj.engine.CrashMessages;
import tech.fastj.math.Pointf;
import tech.fastj.graphics.game.Model2D;
import tech.fastj.graphics.game.Polygon2D;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ObjUtil {

private static final String LineSeparator = System.lineSeparator();

private ObjUtil() {
throw new java.lang.IllegalStateException();
}

public static Polygon2D[] parse(List<String> lines) {
return null;
}

public static void write(Path destinationPath, Model2D model) {
StringBuilder fileContents = new StringBuilder();
writeTimestamp(fileContents);

int vertexCount = 0;

for (int i = 0; i < model.getPolygons().length; i++) {
writeVertexes(fileContents, model.getPolygons()[i]);
}

writeVertexTextures(fileContents);

for (int i = 0; i < model.getPolygons().length; i++) {
Polygon2D polygon = model.getPolygons()[i];
writeObject(fileContents, polygon, i + 1);
writeFaces(fileContents, polygon, vertexCount);

vertexCount += polygon.getPoints().length;
}

try {
Files.writeString(destinationPath, fileContents, StandardCharsets.US_ASCII);
} catch (IOException exception) {
throw new IllegalStateException(CrashMessages.theGameCrashed("a ." + SupportedModelFormats.Obj + " file writing error."), exception);
}
}

private static void writeTimestamp(StringBuilder fileContents) {
fileContents.append("# Generated by the FastJ Game Engine https://github.com/fastjengine/FastJ")
.append(LineSeparator)
.append("# Timestamp: ")
.append(new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(new Date()))
.append(LineSeparator)
.append(LineSeparator);
}

private static void writeVertexes(StringBuilder fileContents, Polygon2D polygon) {
for (int j = 0; j < polygon.getPoints().length; j++) {
Pointf vertex = polygon.getPoints()[j];
fileContents.append(ParsingKeys.Vertex)
.append(' ')
.append(String.format("%4f", vertex.x))
.append(' ')
.append(String.format("%4f", vertex.y))
.append(' ')
.append(String.format("%4f", 0f))
.append(LineSeparator);
}
}

private static void writeVertexTextures(StringBuilder fileContents) {
fileContents.append(LineSeparator);
fileContents.append(ParsingKeys.VertexTexture)
.append(' ')
.append(0)
.append(' ')
.append(0)
.append(LineSeparator);
fileContents.append(ParsingKeys.VertexTexture)
.append(' ')
.append(1)
.append(' ')
.append(0)
.append(LineSeparator);
fileContents.append(ParsingKeys.VertexTexture)
.append(' ')
.append(1)
.append(' ')
.append(1)
.append(LineSeparator);
fileContents.append(ParsingKeys.VertexTexture)
.append(' ')
.append(0)
.append(' ')
.append(1)
.append(LineSeparator)
.append(LineSeparator);
}

private static void writeObject(StringBuilder fileContents, Polygon2D polygon, int polygonIndex) {
fileContents.append(ParsingKeys.ObjectName)
.append(' ')
.append("Polygon2D_")
.append(polygonIndex)
.append(LineSeparator);
}

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

public static class ParsingKeys {
private ParsingKeys() {
throw new java.lang.IllegalStateException();
}

public static final String Empty = "";
public static final String Vertex = "v";
public static final String VertexTexture = "vt";
public static final String ObjectName = "g";
public static final String ObjectFace = "f";
}
}

0 comments on commit 81245a1

Please sign in to comment.