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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
/* This code is *ugly*. It may blind you.
*
* I hereby pollute the software world by putting this into public domain.
*
* SavageX
*/
package objtomap;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;
public class ObjToMap {
private Vector points, faces;
private Configuration config;
public ObjToMap(Configuration c) {
config = c;
}
public void parseOBJ() throws IOException {
points = new Vector();
faces = new Vector();
double scale = config.scale;
config.simpleterrain = true;
config.minz = Double.MAX_VALUE;
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(config.objfile));
} catch(Exception e) {
System.err.println("Input file not found!");
return;
}
String currentmat = "common/caulk";
while(in.ready()) {
String line = in.readLine();
line.trim();
line = line.replaceAll(" ", " ");
String[] tokens = line.split(" ");
if(tokens.length > 1) {
if(tokens[0].equals("v")) {
// vertices
Vector3D p = new Vector3D();
p.x = Double.parseDouble(tokens[3]) * scale;
p.y = Double.parseDouble(tokens[1]) * scale;
p.z = Double.parseDouble(tokens[2]) * scale;
points.add(p);
if(p.z < config.minz)
config.minz = p.z;
} else if(tokens[0].equals("f")) {
// faces
if(tokens.length == 4) {
// TriFace
String[] facetokens1 = tokens[1].split("/");
String[] facetokens2 = tokens[2].split("/");
String[] facetokens3 = tokens[3].split("/");
Face f = new Face();
f.material = currentmat;
Vector3D p1, p2, p3;
p1 = (Vector3D)points.get(Integer.parseInt(facetokens1[0]) - 1);
p2 = (Vector3D)points.get(Integer.parseInt(facetokens2[0]) - 1);
p3 = (Vector3D)points.get(Integer.parseInt(facetokens3[0]) - 1);
f.setPoints(p1, p2, p3);
if(f.getXYangle() >= 90.0)
config.simpleterrain = false;
faces.add(f);
} else if(tokens.length == 5) {
// QuadFace
String[] facetokens1 = tokens[1].split("/");
String[] facetokens2 = tokens[2].split("/");
String[] facetokens3 = tokens[3].split("/");
String[] facetokens4 = tokens[4].split("/");
Vector3D p1, p2, p3;
Face f1 = new Face();
f1.material = currentmat;
p1 = (Vector3D)points.get(Integer.parseInt(facetokens1[0]) - 1);
p2 = (Vector3D)points.get(Integer.parseInt(facetokens2[0]) - 1);
p3 = (Vector3D)points.get(Integer.parseInt(facetokens3[0]) - 1);
f1.setPoints(p1, p2, p3);
if(f1.getXYangle() >= 90.0)
config.simpleterrain = false;
faces.add(f1);
Face f2 = new Face();
f2.material = currentmat;
p1 = (Vector3D)points.get(Integer.parseInt(facetokens1[0]) - 1);
p2 = (Vector3D)points.get(Integer.parseInt(facetokens3[0]) - 1);
p3 = (Vector3D)points.get(Integer.parseInt(facetokens4[0]) - 1);
f2.setPoints(p1, p2, p3);
if(f2.getXYangle() >= 90.0)
config.simpleterrain = false;
faces.add(f2);
}
} else if(tokens[0].equals("usemtl")) {
//change material
currentmat = tokens[1];
}
}
}
System.out.println("Read points: " + points.size() + " Read faces: " + faces.size());
}
public void writeMap() {
if(faces == null) return;
PrintWriter out = null;
try {
out = new PrintWriter(new FileWriter(config.mapfile));
} catch(Exception e) {
System.err.println("Can't open output file?!");
return;
}
out.print("{\n\"classname\" \"worldspawn\"\n");
for(int i = 0; i < faces.size(); i++) {
Face f = (Face)faces.get(i);
out.print(f.generateBrush());
}
out.print("}\n");
out.flush();
out.close();
}
private class Vector3D {
public double x, y, z;
public Vector3D() {
this(0.0, 0.0, 0.0);
}
public Vector3D(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public Vector3D crossproduct(Vector3D p1) {
Vector3D result = new Vector3D();
result.x = this.y * p1.z - this.z * p1.y;
result.y = this.z * p1.x - this.x * p1.z;
result.z = this.x * p1.y - this.y * p1.x;
return result;
}
public double dotproduct(Vector3D p1) {
return this.x * p1.x + this.y * p1.y + this.z * p1.z;
}
public Vector3D substract(Vector3D p1) {
Vector3D result = new Vector3D();
result.x = this.x - p1.x;
result.y = this.y - p1.y;
result.z = this.z - p1.z;
return result;
}
public void scale(double factor) {
x *= factor;
y *= factor;
z *= factor;
}
public double length() {
return Math.sqrt((x*x) + (y*y) + (z*z));
}
public void normalize() {
double l = length();
x /= l;
y /= l;
z /= l;
}
}
private class Face {
private Vector3D p1, p2, p3, normal;
private double angle_xy = 0.0;
public String material;
public void setPoints(Vector3D p1, Vector3D p2, Vector3D p3) {
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
computeNormal();
computeXYangle();
}
public double getXYangle() {
return angle_xy;
}
private void computeNormal() {
Vector3D vector1 = p1.substract(p2);
Vector3D vector2 = p1.substract(p3);
normal = vector1.crossproduct(vector2);
normal.normalize();
}
private void computeXYangle() {
Vector3D normal_xy = new Vector3D(0.0, 0.0, 1.0);
angle_xy = Math.acos(normal.dotproduct(normal_xy)) / (2 * Math.PI) * 360.0;
}
public String generateBrush() {
String result = "{\n";
// this looks like a floor, extrude along the z-axis
if(angle_xy < 70.0) {
normal.x = 0.0;
normal.y = 0.0;
normal.z = 1.0;
}
normal.scale(config.brush_thickness);
Vector3D p1_, p2_, p3_;
if(!config.simpleterrain) {
p1_ = p1.substract(normal);
p2_ = p2.substract(normal);
p3_ = p3.substract(normal);
} else {
double min = config.minz;
min -= 16.0;
p1_ = new Vector3D(p1.x, p1.y, min);
p2_ = new Vector3D(p2.x, p2.y, min);
p3_ = new Vector3D(p3.x, p3.y, min);
}
String mat = material;
if(config.autotexturing.size() > 0) {
double maxangle = -1.0;
for(int i = 0; i < config.autotexturing.size(); i++) {
AutoTexturingEntry e = (AutoTexturingEntry)config.autotexturing.get(i);
if(angle_xy >= e.angle && e.angle > maxangle) {
mat = e.texturename;
maxangle = e.angle;
}
}
}
// top face, apply texture here
result += getMapPlaneString(p3, p2, p1, mat);
// bottom face
result += getMapPlaneString(p1_, p2_, p3_, "common/caulk");
// extruded side 1
result += getMapPlaneString(p1, p1_, p3_, "common/caulk");
// extruded side 2
result += getMapPlaneString(p2, p3, p3_, "common/caulk");
// extruded side 3
result += getMapPlaneString(p1, p2, p2_, "common/caulk");
result += "}\n";
return result;
}
private String getMapPlaneString(Vector3D p1, Vector3D p2, Vector3D p3, String material) {
int flag;
if(config.detail)
flag = 134217728;
else
flag = 0;
return "( " + p1.x + " " + p1.y + " " + p1.z + " ) ( " + p2.x + " " + p2.y + " " + p2.z + " ) ( " + p3.x + " " + p3.y + " " + p3.z + " ) " + material + " 0 0 0 " + config.texture_scale + " " + config.texture_scale + " " + flag + " 0 0\n";
}
}
}
|