Skip to content

Commit f3bffe9

Browse files
author
Oğuzhan Çevik
committed
first commit
0 parents  commit f3bffe9

File tree

21 files changed

+112508
-0
lines changed

21 files changed

+112508
-0
lines changed

.gitignore

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
2+
# Created by https://www.toptal.com/developers/gitignore/api/java,intellij+all
3+
# Edit at https://www.toptal.com/developers/gitignore?templates=java,intellij+all
4+
5+
### Intellij+all ###
6+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
7+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
8+
9+
# User-specific stuff
10+
.idea/**/workspace.xml
11+
.idea/**/tasks.xml
12+
.idea/**/usage.statistics.xml
13+
.idea/**/dictionaries
14+
.idea/**/shelf
15+
16+
# Generated files
17+
.idea/**/contentModel.xml
18+
19+
# Sensitive or high-churn files
20+
.idea/**/dataSources/
21+
.idea/**/dataSources.ids
22+
.idea/**/dataSources.local.xml
23+
.idea/**/sqlDataSources.xml
24+
.idea/**/dynamic.xml
25+
.idea/**/uiDesigner.xml
26+
.idea/**/dbnavigator.xml
27+
28+
# Gradle
29+
.idea/**/gradle.xml
30+
.idea/**/libraries
31+
32+
# Gradle and Maven with auto-import
33+
# When using Gradle or Maven with auto-import, you should exclude module files,
34+
# since they will be recreated, and may cause churn. Uncomment if using
35+
# auto-import.
36+
# .idea/artifacts
37+
# .idea/compiler.xml
38+
# .idea/jarRepositories.xml
39+
# .idea/modules.xml
40+
# .idea/*.iml
41+
# .idea/modules
42+
# *.iml
43+
# *.ipr
44+
45+
# CMake
46+
cmake-build-*/
47+
48+
# Mongo Explorer plugin
49+
.idea/**/mongoSettings.xml
50+
51+
# File-based project format
52+
*.iws
53+
54+
# IntelliJ
55+
out/
56+
57+
# mpeltonen/sbt-idea plugin
58+
.idea_modules/
59+
60+
# JIRA plugin
61+
atlassian-ide-plugin.xml
62+
63+
# Cursive Clojure plugin
64+
.idea/replstate.xml
65+
66+
# Crashlytics plugin (for Android Studio and IntelliJ)
67+
com_crashlytics_export_strings.xml
68+
crashlytics.properties
69+
crashlytics-build.properties
70+
fabric.properties
71+
72+
# Editor-based Rest Client
73+
.idea/httpRequests
74+
75+
# Android studio 3.1+ serialized cache file
76+
.idea/caches/build_file_checksums.ser
77+
78+
### Intellij+all Patch ###
79+
# Ignores the whole .idea folder and all .iml files
80+
# See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360
81+
82+
.idea/
83+
84+
# Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023
85+
86+
*.iml
87+
modules.xml
88+
.idea/misc.xml
89+
*.ipr
90+
91+
# Sonarlint plugin
92+
.idea/sonarlint
93+
94+
### Java ###
95+
# Compiled class file
96+
*.class
97+
98+
# Log file
99+
*.log
100+
101+
# BlueJ files
102+
*.ctxt
103+
104+
# Mobile Tools for Java (J2ME)
105+
.mtj.tmp/
106+
107+
# Package Files #
108+
*.jar
109+
*.war
110+
*.nar
111+
*.ear
112+
*.zip
113+
*.tar.gz
114+
*.rar
115+
116+
# Target Directory #
117+
*/target/**
118+
119+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
120+
hs_err_pid*
121+
122+
# End of https://www.toptal.com/developers/gitignore/api/java,intellij+all

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
### OBJ to glTF converter for Java :arrows_counterclockwise:
2+
3+
This is a library for converting OBJ files into glTF assets. You can convert OBJ file embedded or binary.
4+
5+
Usage examples :heavy_check_mark:
6+
7+
* Convert OBJ file binary
8+
9+
`
10+
11+
String inputObjFilePath = "src/main/resources/model/model.obj";
12+
String inputMtlFileName = "model";
13+
String outputFilePath = "src/main/resources/model/output_binary/";
14+
String outputFileName = "model";
15+
BufferStrategy bufferStrategy = BufferStrategy.BUFFER_PER_FILE;
16+
IndicesComponentType indicesComponentType = IndicesComponentType.GL_UNSIGNED_SHORT;
17+
18+
ConvertObjToGltf convertObjToGltf = new ConvertObjToGltf.Builder().inputObjFilePath(inputObjFilePath)
19+
.inputMtlFileName(inputMtlFileName).outputFilePath(outputFilePath).outputFileName(outputFileName)
20+
.bufferStrategy(bufferStrategy).indicesComponentType(indicesComponentType).build();
21+
22+
convertObjToGltf.convert();
23+
`
24+
25+
* Convert OBJ file embedded
26+
27+
`
28+
29+
String inputObjFilePath = "src/main/resources/model/model.obj";
30+
String inputMtlFileName = "model";
31+
String outputFilePath = "src/main/resources/model/output_binary/";
32+
String outputFileName = "model";
33+
BufferStrategy bufferStrategy = BufferStrategy.BUFFER_PER_FILE;
34+
IndicesComponentType indicesComponentType = IndicesComponentType.GL_UNSIGNED_SHORT;
35+
GltfWriteType gltfWriteType = GltfWriteType.EMBEDDED;
36+
37+
ConvertObjToGltf convertObjToGltf = new ConvertObjToGltf.Builder().inputObjFilePath(inputObjFilePath)
38+
.inputMtlFileName(inputMtlFileName).outputFilePath(outputFilePath).outputFileName(outputFileName)
39+
.bufferStrategy(bufferStrategy).indicesComponentType(indicesComponentType).gltfWriteType(gltfWriteType).build();
40+
41+
convertObjToGltf.convert();
42+
`
43+
44+
You can see all of usage on ConvertObjToGltfTest.java
45+
46+
You can see available input OBJ files and output files on src/main/resources/model/
47+
48+
Thanks https://github.com/javagl/JglTF/tree/master/jgltf-obj

pom.xml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>io.github.oguzhancevik</groupId>
8+
<artifactId>obj2gltf</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<build>
11+
<plugins>
12+
<plugin>
13+
<groupId>org.apache.maven.plugins</groupId>
14+
<artifactId>maven-compiler-plugin</artifactId>
15+
<configuration>
16+
<source>8</source>
17+
<target>8</target>
18+
</configuration>
19+
</plugin>
20+
</plugins>
21+
</build>
22+
23+
<dependencies>
24+
25+
<!-- https://mvnrepository.com/artifact/de.javagl/jgltf-model -->
26+
<dependency>
27+
<groupId>de.javagl</groupId>
28+
<artifactId>jgltf-model</artifactId>
29+
<version>2.0.0</version>
30+
</dependency>
31+
32+
<!-- https://mvnrepository.com/artifact/de.javagl/obj -->
33+
<dependency>
34+
<groupId>de.javagl</groupId>
35+
<artifactId>obj</artifactId>
36+
<version>0.3.0</version>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>junit</groupId>
41+
<artifactId>junit</artifactId>
42+
<version>4.13</version>
43+
<scope>test</scope>
44+
</dependency>
45+
46+
</dependencies>
47+
48+
49+
</project>
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package io.github.oguzhancevik.obj2gltf;
2+
3+
import de.javagl.jgltf.model.GltfConstants;
4+
import de.javagl.jgltf.model.GltfModel;
5+
import de.javagl.jgltf.model.GltfModels;
6+
import de.javagl.jgltf.model.io.GltfAsset;
7+
import de.javagl.jgltf.model.io.GltfModelWriter;
8+
import io.github.oguzhancevik.obj2gltf.creator.ObjGltfAssetCreatorV2;
9+
import io.github.oguzhancevik.obj2gltf.obj.BufferStrategy;
10+
import io.github.oguzhancevik.obj2gltf.obj.GltfWriteType;
11+
import io.github.oguzhancevik.obj2gltf.obj.IndicesComponentType;
12+
13+
import java.io.File;
14+
import java.net.URI;
15+
import java.nio.file.Paths;
16+
import java.util.concurrent.TimeUnit;
17+
18+
public class ConvertObjToGltf {
19+
20+
private final String inputObjFilePath, inputMtlFileName, outputFilePath, outputFileName;
21+
private final BufferStrategy bufferStrategy;
22+
private final IndicesComponentType indicesComponentType;
23+
private final GltfWriteType gltfWriteType;
24+
25+
26+
public ConvertObjToGltf(Builder builder) {
27+
this.inputObjFilePath = builder.inputObjFilePath;
28+
this.inputMtlFileName = builder.inputMtlFileName;
29+
this.outputFilePath = builder.outputFilePath;
30+
this.outputFileName = builder.outputFileName;
31+
this.bufferStrategy = builder.bufferStrategy;
32+
this.indicesComponentType = builder.indicesComponentType;
33+
this.gltfWriteType = builder.gltfWriteType;
34+
}
35+
36+
public static class Builder {
37+
private String inputObjFilePath, inputMtlFileName, outputFilePath, outputFileName;
38+
private BufferStrategy bufferStrategy;
39+
private IndicesComponentType indicesComponentType;
40+
private GltfWriteType gltfWriteType;
41+
42+
public Builder inputObjFilePath(String inputObjFilePath) {
43+
this.inputObjFilePath = inputObjFilePath;
44+
return this;
45+
}
46+
47+
public Builder inputMtlFileName(String inputMtlFileName) {
48+
this.inputMtlFileName = inputMtlFileName;
49+
return this;
50+
}
51+
52+
public Builder outputFilePath(String outputFilePath) {
53+
this.outputFilePath = outputFilePath;
54+
return this;
55+
}
56+
57+
public Builder outputFileName(String outputFileName) {
58+
this.outputFileName = outputFileName;
59+
return this;
60+
}
61+
62+
public Builder bufferStrategy(BufferStrategy bufferStrategy) {
63+
this.bufferStrategy = bufferStrategy;
64+
return this;
65+
}
66+
67+
public Builder indicesComponentType(IndicesComponentType indicesComponentType) {
68+
this.indicesComponentType = indicesComponentType;
69+
return this;
70+
}
71+
72+
public Builder gltfWriteType(GltfWriteType gltfWriteType) {
73+
this.gltfWriteType = gltfWriteType;
74+
return this;
75+
}
76+
77+
public ConvertObjToGltf build() {
78+
return new ConvertObjToGltf(this);
79+
}
80+
81+
}
82+
83+
public void convert() {
84+
try {
85+
86+
long startTime = System.nanoTime();
87+
88+
if (inputObjFilePath == null || inputObjFilePath.isEmpty())
89+
throw new NullPointerException("inputObjFilePath cannot be empty!");
90+
URI objUri = Paths.get(inputObjFilePath).toUri();
91+
92+
if (bufferStrategy == null) throw new NullPointerException("bufferStrategy cannot be empty!");
93+
ObjGltfAssetCreatorV2 gltfAssetCreator = new ObjGltfAssetCreatorV2(bufferStrategy);
94+
95+
if (indicesComponentType == null) throw new NullPointerException("indicesComponentType cannot be empty!");
96+
else if (indicesComponentType == IndicesComponentType.GL_UNSIGNED_BYTE)
97+
gltfAssetCreator.setIndicesComponentType(GltfConstants.GL_UNSIGNED_BYTE);
98+
else if (indicesComponentType == IndicesComponentType.GL_UNSIGNED_SHORT)
99+
gltfAssetCreator.setIndicesComponentType(GltfConstants.GL_UNSIGNED_SHORT);
100+
else if (indicesComponentType == IndicesComponentType.GL_UNSIGNED_INT)
101+
gltfAssetCreator.setIndicesComponentType(GltfConstants.GL_UNSIGNED_INT);
102+
103+
GltfAsset gltfAsset = gltfAssetCreator.create(objUri, inputMtlFileName);
104+
105+
GltfModel gltfModel = GltfModels.create(gltfAsset);
106+
107+
GltfModelWriter gltfModelWriter = new GltfModelWriter();
108+
109+
if (outputFilePath == null || outputFilePath.isEmpty())
110+
throw new NullPointerException("outputFilePath cannot be empty!");
111+
if (outputFileName == null || outputFileName.isEmpty())
112+
throw new NullPointerException("outputFileName cannot be empty!");
113+
File outputFile = new File(outputFilePath + "/" + outputFileName + ".gltf");
114+
File parentFile = outputFile.getParentFile();
115+
116+
if (parentFile != null) parentFile.mkdirs();
117+
118+
if (gltfWriteType == GltfWriteType.BINARY) gltfModelWriter.writeBinary(gltfModel, outputFile);
119+
else if (gltfWriteType == GltfWriteType.EMBEDDED) gltfModelWriter.writeEmbedded(gltfModel, outputFile);
120+
else gltfModelWriter.write(gltfModel, outputFile);
121+
122+
long finishTime = System.nanoTime();
123+
long duration = finishTime - startTime;
124+
long seconds = TimeUnit.SECONDS.convert(duration, TimeUnit.NANOSECONDS);
125+
System.out.println(seconds + " seconds");
126+
} catch (Exception e) {
127+
e.printStackTrace();
128+
}
129+
}
130+
131+
}

0 commit comments

Comments
 (0)