-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExportObj.pde
172 lines (145 loc) · 5.19 KB
/
ExportObj.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// http://paulbourke.net/dataformats/obj/
// http://paulbourke.net/dataformats/ply/
//String fileName = "test.obj";
//String rgbFilename = "rgb.png";
//String depthFilename = "depth.png";
void exportObj(String fileName, PImage depth, PImage rgb) {
boolean vertexColors = true;
boolean saveMtl = true;
boolean calcNormals = true;
boolean calcFaces = true;
String[] s = split(fileName, ".");
String fileType = s[s.length-1];
fileName = "";
for (int i=0; i<s.length-1; i++) {
fileName += s[i];
}
boolean exportPly = fileType.toLowerCase().equals("ply");
int vertexCounter = 0;
int faceCounter = 0;
println("Exporting: " + fileName + "." + fileType);
depth.loadPixels();
rgb.loadPixels();
// OBJ FILE WRITE
ArrayList<String> obj = new ArrayList<String>();
int depthHeight = depth.height;
int depthWidth = depth.width;
if (!exportPly) {
// The obj file header--be careful modifying it.
// Choose obj if you want to work with textures, or an app that you know supports obj vertex colors.
obj.add("# -----------------");
obj.add("# Start of obj file");
obj.add("g depth");
if (saveMtl) obj.add("mtllib " + fileName + ".mtl");
} else {
// The ply file header--be careful modifying it.
// Choose ply if you know you want to work only with vertex colors instead of textures.
obj.add("ply");
obj.add("format ascii 1.0");
obj.add("comment VCGLIB generated");
obj.add("element vertex "); // line 3, vertexCounter will be added here
obj.add("property float x");
obj.add("property float y");
obj.add("property float z");
obj.add("property uchar red");
obj.add("property uchar green");
obj.add("property uchar blue");
obj.add("property uchar alpha");
obj.add("element face "); // line 11, faceCounter will be added here
obj.add("property list uchar int vertex_indices");
obj.add("end_header");
}
for (int y = 0; y < depthHeight; y++) { // WRITE VERTICES
for (int x = 0; x < depthWidth; x++) {
int loc = y * depthWidth + x;
PVector r = reproject(x, y, depth.pixels[loc]);
color c = rgb.pixels[loc];
PVector col = getColor(c);
if (!exportPly) {
// obj vertices
if (vertexColors) {
obj.add("v " + r.x + " " + r.y + " " + r.z + " " + col.x + " " + col.y + " " + col.z);
} else {
obj.add("v " + r.x + " " + r.y + " " + r.z);
}
} else {
// ply vertices
obj.add(r.x + " " + r.y + " " + r.z + " " + int(red(c)) + " " + int(green(c)) + " " + int(blue(c)) + " 255");
vertexCounter++;
}
}
}
if (!exportPly) {
if (calcNormals) {
obj.add("\n");
for (int y = 0; y < depthHeight; y++) { // WRITE NORMALS. TODO: CALCULATE NORMALS CORRECTLY
for (int x = 0; x < depthWidth ; x++) {
obj.add("vn " + 0.0 + " " + 0.0 + " " + 1.0);
}
}
}
obj.add("\n");
for (int y = 0; y < depthHeight; y++) { // WRITE TEXTURE MAPPING.
for (int x = 0; x < depthWidth ; x++) {
obj.add("vt " + float(x)/depthWidth + " " + float(-y)/depthHeight);
}
}
obj.add("\n");
obj.add("usemtl rgb");
}
if (calcFaces) {
for (int y = 0; y < depthHeight -1; y++) {//WRITE FACE INDEXES
for (int x = 0; x < depthWidth -1 ; x++) {
int b = y * depthWidth + x ;
int a = b + 1;
int c = (y + 1)* depthWidth + x ;
int d = c + 1 ;
if (!exportPly) {
// obj faces
//color color0 = color(0);
//if (depth.pixels[a] > color0 && depth.pixels[b] > color0 && depth.pixels[c] > color0) {
obj.add("f " + toObjFaceIndex(a + 1) + " " + toObjFaceIndex(b + 1) + " " + toObjFaceIndex(c + 1));
//}
//if (depth.pixels[a] > color0 && depth.pixels[d] > color0 && depth.pixels[c] > color0) {
obj.add("f " + toObjFaceIndex(a + 1) + " " + toObjFaceIndex(c + 1) + " " + toObjFaceIndex(d + 1));
//}
} else {
//ply faces
obj.add("3 " + a + " " + b + " " + c);
obj.add("3 " + a + " " + c + " " + d);
faceCounter+= 2;
}
}
}
}
if (!exportPly) {
if (saveMtl) {
// MTL FILE WRITE
ArrayList<String> mtl = new ArrayList<String>();
mtl.add("newmtl rgb");
mtl.add("Ka 1.000000 1.000000 1.000000");
mtl.add("Kd 1.000000 1.000000 1.000000");
mtl.add("Ks 0.000000 0.000000 0.000000");
mtl.add("illum 0");
mtl.add("map_Kd " + fileName + ".png");
rgb.save(fileName + ".png");
saveStrings(fileName + ".mtl", mtl.toArray(new String[mtl.size()]));
}
}
String[] objArray = obj.toArray(new String[obj.size()]);
if (exportPly) {
objArray[3] += vertexCounter;
if (calcFaces) objArray[11] += faceCounter;
}
saveStrings(fileName + "." + fileType, objArray);
println("Export finished.");
}
String toObjFaceIndex(int index) {
return "" + index + "/" + index + "/" + index;
}
PVector reproject(float x, float y, float z) {
return new PVector(x, -y, z/8000);
}
PVector getColor(color c) {
return new PVector(red(c)/255.0, green(c)/255.0, blue(c)/255.0);
}