Created
June 19, 2017 12:02
-
-
Save Viza74/53acf22944efeb45eceaaa9eff79faa6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import drop.*; | |
SDrop drop; | |
StringList vertices; | |
ArrayList<StringList> facegroups; | |
StringList colors; | |
void setup() { | |
size(400, 400); | |
frameRate(30); | |
drop = new SDrop(this); | |
} | |
void draw() { | |
background(0); | |
textAlign(CENTER, CENTER); | |
text("Drop an obj file here!", width/2, height/2); | |
} | |
void dropEvent(DropEvent theDropEvent) { | |
vertices = new StringList(); | |
colors = new StringList(); | |
facegroups = new ArrayList<StringList>(); | |
if (theDropEvent.isFile()) { | |
println("File dropped: "+theDropEvent.file().getName()); | |
String[] lines = loadStrings(theDropEvent.file()); | |
println("there are " + lines.length + " lines"); | |
StringList currFacegroup = null; | |
for (int i = 0; i < lines.length; i++) { | |
//println(lines[i]); | |
if (lines[i].startsWith("v")) { | |
String[] tokens = splitTokens(lines[i], " "); | |
println("Adding vertex: "+tokens[1]+", "+tokens[2]+", "+tokens[3]); | |
vertices.append("{"+tokens[1]+", "+tokens[2]+"}"); | |
} else if (lines[i].startsWith("f")) { | |
String[] tokens = splitTokens(lines[i], " "); | |
println("Adding face: "+tokens[1]+", "+tokens[2]+", "+tokens[3]); | |
if (currFacegroup!=null) { | |
currFacegroup.append("{"+tokens[1]+", "+tokens[2]+", "+tokens[3]+"}"); | |
} | |
} else if (lines[i].startsWith("usemtl")) { | |
String[] tokens = splitTokens(lines[i], " "); | |
println("Adding to color: "+tokens[1]); | |
colors.append(tokens[1]); | |
if (currFacegroup!=null) { | |
println("Adding previous facegroup to facegroups."); | |
facegroups.add(currFacegroup); | |
currFacegroup =null; | |
} | |
println("Creating new facegroup"); | |
currFacegroup = new StringList(); | |
} | |
} | |
if (currFacegroup!=null) { | |
println("Adding final facegroup to facegroups."); | |
facegroups.add(currFacegroup); | |
} | |
String fullFilename = theDropEvent.file().getName(); | |
String filename = fullFilename.substring(0, fullFilename.lastIndexOf('.')); | |
String exportfilename = theDropEvent.file().toString().substring(0, theDropEvent.file().toString().lastIndexOf('.')); | |
exportfilename += ".ticobj"; | |
println("Export filename: "+exportfilename); | |
PrintWriter export = createWriter(exportfilename); | |
export.append(filename+" = {\n"); | |
export.append("vertices = {"); | |
println("Num vertices: "+vertices.size()); | |
for (int i = 0; i <= vertices.size() - 1; i++) { | |
export.append(vertices.get(i)+(i==vertices.size()-1?"":",")); | |
} | |
export.append("},\n"); | |
export.append("faces = {\n"); | |
for (int i = 0; i <= facegroups.size() - 1; i++) { | |
StringList group = facegroups.get(i); | |
println("Facegroup "+i+" size: "+group.size()); | |
export.append("{"); | |
for (int o = 0; o <= group.size() - 1; o++) { | |
export.append(group.get(o)+(o==group.size()-1?"":",")); | |
} | |
export.append("}"+(i==facegroups.size()-1?"\n":",\n")); | |
} | |
export.append("},\n"); | |
println("Colors: "+colors.size()); | |
export.append("colors = {"); | |
for (int i = 0; i <= colors.size() - 1; i++) { | |
export.append(colors.get(i)+(i==colors.size()-1?"":",")); | |
} | |
export.append("}\n"); | |
export.append("}"); | |
//println("Final string: "); | |
//println(join(export.array(), " ")); | |
export.flush(); // Writes the remaining data to the file | |
export.close(); // Finishes the file | |
//saveStrings(exportfilename, export.array()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment