| | 1 | | // <copyright file="ObjFileWriter.cs" company="Jérémy Ansel"> |
| | 2 | | // Copyright (c) 2017, 2019 Jérémy Ansel |
| | 3 | | // </copyright> |
| | 4 | | // <license> |
| | 5 | | // Licensed under the MIT license. See LICENSE.txt |
| | 6 | | // </license> |
| | 7 | |
|
| | 8 | | using System.Globalization; |
| | 9 | |
|
| | 10 | | namespace JeremyAnsel.Media.WavefrontObj |
| | 11 | | { |
| | 12 | | internal static class ObjFileWriter |
| | 13 | | { |
| | 14 | | public static void Write(ObjFile obj, StreamWriter stream) |
| | 15 | | { |
| 216 | 16 | | var context = new ObjFileWriterContext(obj); |
| | 17 | |
|
| 216 | 18 | | WriteHeaderText(obj, stream); |
| | 19 | |
|
| 216 | 20 | | WriteShadowObjectFileName(obj, stream); |
| 216 | 21 | | WriteTraceObjectFileName(obj, stream); |
| | 22 | |
|
| 216 | 23 | | WriteMapLibraries(obj, stream); |
| 216 | 24 | | WriteMaterialLibraries(obj, stream); |
| | 25 | |
|
| 216 | 26 | | WriteVertices(obj, stream); |
| 216 | 27 | | WriteParameterSpaceVertices(obj, stream); |
| 216 | 28 | | WriteVertexNormals(obj, stream); |
| 216 | 29 | | WriteTextureVertices(obj, stream); |
| | 30 | |
|
| 216 | 31 | | WritePoints(obj, stream, context); |
| 216 | 32 | | WriteLines(obj, stream, context); |
| 216 | 33 | | WriteFaces(obj, stream, context); |
| | 34 | |
|
| 216 | 35 | | WriteCurves(obj, stream, context); |
| 216 | 36 | | WriteCurves2D(obj, stream, context); |
| 216 | 37 | | WriteSurfaces(obj, stream, context); |
| | 38 | |
|
| 216 | 39 | | WriteSurfaceConnection(obj, stream); |
| 216 | 40 | | } |
| | 41 | |
|
| | 42 | | private static void WriteHeaderText(ObjFile obj, StreamWriter stream) |
| | 43 | | { |
| 216 | 44 | | if (string.IsNullOrEmpty(obj.HeaderText)) |
| | 45 | | { |
| 213 | 46 | | return; |
| | 47 | | } |
| | 48 | |
|
| 3 | 49 | | string[] headerLines = obj.HeaderText!.Split('\n'); |
| | 50 | |
|
| 18 | 51 | | foreach (string line in headerLines) |
| | 52 | | { |
| 6 | 53 | | stream.Write('#'); |
| 6 | 54 | | stream.WriteLine(line); |
| | 55 | | } |
| 3 | 56 | | } |
| | 57 | |
|
| | 58 | | private static void WriteShadowObjectFileName(ObjFile obj, StreamWriter stream) |
| | 59 | | { |
| 216 | 60 | | if (!string.IsNullOrEmpty(obj.ShadowObjectFileName)) |
| | 61 | | { |
| 9 | 62 | | stream.Write(string.Format(CultureInfo.InvariantCulture, "shadow_obj {0}\n", obj.ShadowObjectFileName)); |
| | 63 | | } |
| 216 | 64 | | } |
| | 65 | |
|
| | 66 | | private static void WriteTraceObjectFileName(ObjFile obj, StreamWriter stream) |
| | 67 | | { |
| 216 | 68 | | if (!string.IsNullOrEmpty(obj.TraceObjectFileName)) |
| | 69 | | { |
| 3 | 70 | | stream.Write(string.Format(CultureInfo.InvariantCulture, "trace_obj {0}\n", obj.TraceObjectFileName)); |
| | 71 | | } |
| 216 | 72 | | } |
| | 73 | |
|
| | 74 | | private static void WriteMapLibraries(ObjFile obj, StreamWriter stream) |
| | 75 | | { |
| 216 | 76 | | if (obj.MapLibraries.Count == 0) |
| | 77 | | { |
| 213 | 78 | | return; |
| | 79 | | } |
| | 80 | |
|
| 3 | 81 | | stream.Write("maplib"); |
| | 82 | |
|
| 18 | 83 | | foreach (string map in obj.MapLibraries) |
| | 84 | | { |
| 6 | 85 | | stream.Write(" "); |
| 6 | 86 | | stream.Write(map); |
| | 87 | | } |
| | 88 | |
|
| 3 | 89 | | stream.WriteLine(); |
| 3 | 90 | | } |
| | 91 | |
|
| | 92 | | private static void WriteMaterialLibraries(ObjFile obj, StreamWriter stream) |
| | 93 | | { |
| 216 | 94 | | if (obj.MaterialLibraries.Count == 0) |
| | 95 | | { |
| 213 | 96 | | return; |
| | 97 | | } |
| | 98 | |
|
| 3 | 99 | | stream.Write("mtllib"); |
| | 100 | |
|
| 18 | 101 | | foreach (string map in obj.MaterialLibraries) |
| | 102 | | { |
| 6 | 103 | | stream.Write(" "); |
| 6 | 104 | | stream.Write(map); |
| | 105 | | } |
| | 106 | |
|
| 3 | 107 | | stream.WriteLine(); |
| 3 | 108 | | } |
| | 109 | |
|
| | 110 | | private static void WriteVertices(ObjFile obj, StreamWriter stream) |
| | 111 | | { |
| 582 | 112 | | foreach (var vertex in obj.Vertices) |
| | 113 | | { |
| 75 | 114 | | var position = vertex.Position; |
| | 115 | |
|
| 75 | 116 | | if (vertex.Color.HasValue) |
| | 117 | | { |
| 6 | 118 | | var color = vertex.Color.Value; |
| | 119 | |
|
| 6 | 120 | | if (color.W == 1.0f) |
| | 121 | | { |
| 3 | 122 | | stream.WriteLine( |
| 3 | 123 | | "v {0} {1} {2} {3} {4} {5}", |
| 3 | 124 | | position.X.ToString("F6", CultureInfo.InvariantCulture), |
| 3 | 125 | | position.Y.ToString("F6", CultureInfo.InvariantCulture), |
| 3 | 126 | | position.Z.ToString("F6", CultureInfo.InvariantCulture), |
| 3 | 127 | | color.X.ToString("F6", CultureInfo.InvariantCulture), |
| 3 | 128 | | color.Y.ToString("F6", CultureInfo.InvariantCulture), |
| 3 | 129 | | color.Z.ToString("F6", CultureInfo.InvariantCulture)); |
| | 130 | | } |
| | 131 | | else |
| | 132 | | { |
| 3 | 133 | | stream.WriteLine( |
| 3 | 134 | | "v {0} {1} {2} {3} {4} {5} {6}", |
| 3 | 135 | | position.X.ToString("F6", CultureInfo.InvariantCulture), |
| 3 | 136 | | position.Y.ToString("F6", CultureInfo.InvariantCulture), |
| 3 | 137 | | position.Z.ToString("F6", CultureInfo.InvariantCulture), |
| 3 | 138 | | color.X.ToString("F6", CultureInfo.InvariantCulture), |
| 3 | 139 | | color.Y.ToString("F6", CultureInfo.InvariantCulture), |
| 3 | 140 | | color.Z.ToString("F6", CultureInfo.InvariantCulture), |
| 3 | 141 | | color.W.ToString("F6", CultureInfo.InvariantCulture)); |
| | 142 | | } |
| | 143 | | } |
| | 144 | | else |
| | 145 | | { |
| 69 | 146 | | if (position.W == 1.0f) |
| | 147 | | { |
| 66 | 148 | | stream.WriteLine( |
| 66 | 149 | | "v {0} {1} {2}", |
| 66 | 150 | | position.X.ToString("F6", CultureInfo.InvariantCulture), |
| 66 | 151 | | position.Y.ToString("F6", CultureInfo.InvariantCulture), |
| 66 | 152 | | position.Z.ToString("F6", CultureInfo.InvariantCulture)); |
| | 153 | | } |
| | 154 | | else |
| | 155 | | { |
| 3 | 156 | | stream.WriteLine( |
| 3 | 157 | | "v {0} {1} {2} {3}", |
| 3 | 158 | | position.X.ToString("F6", CultureInfo.InvariantCulture), |
| 3 | 159 | | position.Y.ToString("F6", CultureInfo.InvariantCulture), |
| 3 | 160 | | position.Z.ToString("F6", CultureInfo.InvariantCulture), |
| 3 | 161 | | position.W.ToString("F6", CultureInfo.InvariantCulture)); |
| | 162 | | } |
| | 163 | | } |
| | 164 | | } |
| 216 | 165 | | } |
| | 166 | |
|
| | 167 | | private static void WriteParameterSpaceVertices(ObjFile obj, StreamWriter stream) |
| | 168 | | { |
| 474 | 169 | | foreach (var vertex in obj.ParameterSpaceVertices) |
| | 170 | | { |
| 21 | 171 | | if (vertex.Z == 1.0f) |
| | 172 | | { |
| 6 | 173 | | if (vertex.Y == 0.0f) |
| | 174 | | { |
| 3 | 175 | | stream.WriteLine( |
| 3 | 176 | | "vp {0}", |
| 3 | 177 | | vertex.X.ToString("F6", CultureInfo.InvariantCulture)); |
| | 178 | | } |
| | 179 | | else |
| | 180 | | { |
| 3 | 181 | | stream.WriteLine( |
| 3 | 182 | | "vp {0} {1}", |
| 3 | 183 | | vertex.X.ToString("F6", CultureInfo.InvariantCulture), |
| 3 | 184 | | vertex.Y.ToString("F6", CultureInfo.InvariantCulture)); |
| | 185 | | } |
| | 186 | | } |
| | 187 | | else |
| | 188 | | { |
| 15 | 189 | | stream.WriteLine( |
| 15 | 190 | | "vp {0} {1} {2}", |
| 15 | 191 | | vertex.X.ToString("F6", CultureInfo.InvariantCulture), |
| 15 | 192 | | vertex.Y.ToString("F6", CultureInfo.InvariantCulture), |
| 15 | 193 | | vertex.Z.ToString("F6", CultureInfo.InvariantCulture)); |
| | 194 | | } |
| | 195 | | } |
| 216 | 196 | | } |
| | 197 | |
|
| | 198 | | private static void WriteVertexNormals(ObjFile obj, StreamWriter stream) |
| | 199 | | { |
| 438 | 200 | | foreach (var vertex in obj.VertexNormals) |
| | 201 | | { |
| 3 | 202 | | stream.WriteLine( |
| 3 | 203 | | "vn {0} {1} {2}", |
| 3 | 204 | | vertex.X.ToString("F6", CultureInfo.InvariantCulture), |
| 3 | 205 | | vertex.Y.ToString("F6", CultureInfo.InvariantCulture), |
| 3 | 206 | | vertex.Z.ToString("F6", CultureInfo.InvariantCulture)); |
| | 207 | | } |
| 216 | 208 | | } |
| | 209 | |
|
| | 210 | | private static void WriteTextureVertices(ObjFile obj, StreamWriter stream) |
| | 211 | | { |
| 450 | 212 | | foreach (var vertex in obj.TextureVertices) |
| | 213 | | { |
| 9 | 214 | | stream.WriteLine( |
| 9 | 215 | | "vt {0} {1} {2}", |
| 9 | 216 | | vertex.X.ToString("F6", CultureInfo.InvariantCulture), |
| 9 | 217 | | vertex.Y.ToString("F6", CultureInfo.InvariantCulture), |
| 9 | 218 | | vertex.Z.ToString("F6", CultureInfo.InvariantCulture)); |
| | 219 | | } |
| 216 | 220 | | } |
| | 221 | |
|
| | 222 | | private static void WriteSurfaceConnection(ObjFile obj, StreamWriter stream) |
| | 223 | | { |
| 438 | 224 | | foreach (ObjSurfaceConnection con in obj.SurfaceConnections) |
| | 225 | | { |
| 3 | 226 | | stream.WriteLine("con {0} {1} {2} {3}", |
| 3 | 227 | | con.Surface1.ToString(CultureInfo.InvariantCulture), |
| 3 | 228 | | con.Curve2D1.ToString(), |
| 3 | 229 | | con.Surface2.ToString(CultureInfo.InvariantCulture), |
| 3 | 230 | | con.Curve2D2.ToString()); |
| | 231 | | } |
| 216 | 232 | | } |
| | 233 | |
|
| | 234 | | private static void WritePoints(ObjFile obj, StreamWriter stream, ObjFileWriterContext context) |
| | 235 | | { |
| 594 | 236 | | foreach (ObjPoint point in obj.Points) |
| | 237 | | { |
| 102 | 238 | | context.WriteGroupNames(stream, point, g => g.Points); |
| 81 | 239 | | context.WriteAttributesOfElement(stream, point); |
| 81 | 240 | | context.WriteAttributesOfPolygonalElement(stream, point); |
| | 241 | |
|
| 81 | 242 | | stream.Write("p"); |
| | 243 | |
|
| 330 | 244 | | foreach (ObjTriplet vertex in point.Vertices) |
| | 245 | | { |
| 84 | 246 | | stream.Write(" {0}", vertex); |
| | 247 | | } |
| | 248 | |
|
| 81 | 249 | | stream.WriteLine(); |
| | 250 | | } |
| 216 | 251 | | } |
| | 252 | |
|
| | 253 | | private static void WriteLines(ObjFile obj, StreamWriter stream, ObjFileWriterContext context) |
| | 254 | | { |
| 438 | 255 | | foreach (ObjLine line in obj.Lines) |
| | 256 | | { |
| 6 | 257 | | context.WriteGroupNames(stream, line, g => g.Lines); |
| 3 | 258 | | context.WriteAttributesOfElement(stream, line); |
| 3 | 259 | | context.WriteAttributesOfPolygonalElement(stream, line); |
| | 260 | |
|
| 3 | 261 | | stream.Write("l"); |
| | 262 | |
|
| 24 | 263 | | foreach (ObjTriplet vertex in line.Vertices) |
| | 264 | | { |
| 9 | 265 | | stream.Write(" {0}", vertex); |
| | 266 | | } |
| | 267 | |
|
| 3 | 268 | | stream.WriteLine(); |
| | 269 | | } |
| 216 | 270 | | } |
| | 271 | |
|
| | 272 | | private static void WriteFaces(ObjFile obj, StreamWriter stream, ObjFileWriterContext context) |
| | 273 | | { |
| 438 | 274 | | foreach (ObjFace face in obj.Faces) |
| | 275 | | { |
| 6 | 276 | | context.WriteGroupNames(stream, face, g => g.Faces); |
| 3 | 277 | | context.WriteAttributesOfElement(stream, face); |
| 3 | 278 | | context.WriteAttributesOfPolygonalElement(stream, face); |
| | 279 | |
|
| 3 | 280 | | stream.Write("f"); |
| | 281 | |
|
| 36 | 282 | | foreach (ObjTriplet vertex in face.Vertices) |
| | 283 | | { |
| 15 | 284 | | stream.Write(" {0}", vertex); |
| | 285 | | } |
| | 286 | |
|
| 3 | 287 | | stream.WriteLine(); |
| | 288 | | } |
| 216 | 289 | | } |
| | 290 | |
|
| | 291 | | private static void WriteCurves(ObjFile obj, StreamWriter stream, ObjFileWriterContext context) |
| | 292 | | { |
| 636 | 293 | | foreach (ObjCurve curve in obj.Curves) |
| | 294 | | { |
| 105 | 295 | | context.WriteGroupNames(stream, curve, g => g.Curves); |
| 102 | 296 | | context.WriteAttributesOfElement(stream, curve); |
| 102 | 297 | | context.WriteAttributesOfFreeFormElement(stream, curve); |
| | 298 | |
|
| 102 | 299 | | stream.Write("curv {0} {1}", |
| 102 | 300 | | curve.StartParameter.ToString("F6", CultureInfo.InvariantCulture), |
| 102 | 301 | | curve.EndParameter.ToString("F6", CultureInfo.InvariantCulture)); |
| | 302 | |
|
| 816 | 303 | | foreach (int vertex in curve.Vertices) |
| | 304 | | { |
| 306 | 305 | | stream.Write(' '); |
| 306 | 306 | | stream.Write(vertex); |
| | 307 | | } |
| | 308 | |
|
| 102 | 309 | | stream.WriteLine(); |
| | 310 | |
|
| 102 | 311 | | WriteBodyOfFreeFormElement(stream, curve); |
| | 312 | | } |
| 216 | 313 | | } |
| | 314 | |
|
| | 315 | | private static void WriteCurves2D(ObjFile obj, StreamWriter stream, ObjFileWriterContext context) |
| | 316 | | { |
| 438 | 317 | | foreach (ObjCurve2D curve in obj.Curves2D) |
| | 318 | | { |
| 6 | 319 | | context.WriteGroupNames(stream, curve, g => g.Curves2D); |
| 3 | 320 | | context.WriteAttributesOfElement(stream, curve); |
| 3 | 321 | | context.WriteAttributesOfFreeFormElement(stream, curve); |
| | 322 | |
|
| 3 | 323 | | stream.Write("curv2"); |
| | 324 | |
|
| 24 | 325 | | foreach (int vertex in curve.ParameterSpaceVertices) |
| | 326 | | { |
| 9 | 327 | | stream.Write(' '); |
| 9 | 328 | | stream.Write(vertex); |
| | 329 | | } |
| | 330 | |
|
| 3 | 331 | | stream.WriteLine(); |
| | 332 | |
|
| 3 | 333 | | WriteBodyOfFreeFormElement(stream, curve); |
| | 334 | | } |
| 216 | 335 | | } |
| | 336 | |
|
| | 337 | | private static void WriteSurfaces(ObjFile obj, StreamWriter stream, ObjFileWriterContext context) |
| | 338 | | { |
| 438 | 339 | | foreach (ObjSurface surface in obj.Surfaces) |
| | 340 | | { |
| 6 | 341 | | context.WriteGroupNames(stream, surface, g => g.Surfaces); |
| 3 | 342 | | context.WriteAttributesOfElement(stream, surface); |
| 3 | 343 | | context.WriteAttributesOfFreeFormElement(stream, surface); |
| | 344 | |
|
| 3 | 345 | | stream.Write("surf {0} {1} {2} {3}", |
| 3 | 346 | | surface.StartParameterU.ToString("F6", CultureInfo.InvariantCulture), |
| 3 | 347 | | surface.EndParameterU.ToString("F6", CultureInfo.InvariantCulture), |
| 3 | 348 | | surface.StartParameterV.ToString("F6", CultureInfo.InvariantCulture), |
| 3 | 349 | | surface.EndParameterV.ToString("F6", CultureInfo.InvariantCulture)); |
| | 350 | |
|
| 24 | 351 | | foreach (ObjTriplet vertex in surface.Vertices) |
| | 352 | | { |
| 9 | 353 | | stream.Write(' '); |
| 9 | 354 | | stream.Write(vertex); |
| | 355 | | } |
| | 356 | |
|
| 3 | 357 | | stream.WriteLine(); |
| | 358 | |
|
| 3 | 359 | | WriteBodyOfFreeFormElement(stream, surface); |
| | 360 | | } |
| 216 | 361 | | } |
| | 362 | |
|
| | 363 | | private static void WriteBodyOfFreeFormElement(StreamWriter stream, ObjFreeFormElement element) |
| | 364 | | { |
| 108 | 365 | | bool writeEnd = false; |
| | 366 | |
|
| 108 | 367 | | if (element.ParametersU.Count != 0) |
| | 368 | | { |
| 3 | 369 | | writeEnd = true; |
| 3 | 370 | | stream.Write("parm u"); |
| | 371 | |
|
| 24 | 372 | | foreach (float value in element.ParametersU) |
| | 373 | | { |
| 9 | 374 | | stream.Write(' '); |
| 9 | 375 | | stream.Write(value.ToString("F6", CultureInfo.InvariantCulture)); |
| | 376 | | } |
| | 377 | |
|
| 3 | 378 | | stream.WriteLine(); |
| | 379 | | } |
| | 380 | |
|
| 108 | 381 | | if (element.ParametersV.Count != 0) |
| | 382 | | { |
| 3 | 383 | | writeEnd = true; |
| 3 | 384 | | stream.Write("parm v"); |
| | 385 | |
|
| 24 | 386 | | foreach (float value in element.ParametersV) |
| | 387 | | { |
| 9 | 388 | | stream.Write(' '); |
| 9 | 389 | | stream.Write(value.ToString("F6", CultureInfo.InvariantCulture)); |
| | 390 | | } |
| | 391 | |
|
| 3 | 392 | | stream.WriteLine(); |
| | 393 | | } |
| | 394 | |
|
| 108 | 395 | | if (element.OuterTrimmingCurves.Count != 0) |
| | 396 | | { |
| 3 | 397 | | writeEnd = true; |
| 3 | 398 | | stream.Write("trim"); |
| | 399 | |
|
| 18 | 400 | | foreach (ObjCurveIndex index in element.OuterTrimmingCurves) |
| | 401 | | { |
| 6 | 402 | | stream.Write(' '); |
| 6 | 403 | | stream.Write(index); |
| | 404 | | } |
| | 405 | |
|
| 3 | 406 | | stream.WriteLine(); |
| | 407 | | } |
| | 408 | |
|
| 108 | 409 | | if (element.InnerTrimmingCurves.Count != 0) |
| | 410 | | { |
| 3 | 411 | | writeEnd = true; |
| 3 | 412 | | stream.Write("hole"); |
| | 413 | |
|
| 18 | 414 | | foreach (ObjCurveIndex index in element.InnerTrimmingCurves) |
| | 415 | | { |
| 6 | 416 | | stream.Write(' '); |
| 6 | 417 | | stream.Write(index); |
| | 418 | | } |
| | 419 | |
|
| 3 | 420 | | stream.WriteLine(); |
| | 421 | | } |
| | 422 | |
|
| 108 | 423 | | if (element.SequenceCurves.Count != 0) |
| | 424 | | { |
| 3 | 425 | | writeEnd = true; |
| 3 | 426 | | stream.Write("scrv"); |
| | 427 | |
|
| 18 | 428 | | foreach (ObjCurveIndex index in element.SequenceCurves) |
| | 429 | | { |
| 6 | 430 | | stream.Write(' '); |
| 6 | 431 | | stream.Write(index); |
| | 432 | | } |
| | 433 | |
|
| 3 | 434 | | stream.WriteLine(); |
| | 435 | | } |
| | 436 | |
|
| 108 | 437 | | if (element.SpecialPoints.Count != 0) |
| | 438 | | { |
| 3 | 439 | | writeEnd = true; |
| 3 | 440 | | stream.Write("sp"); |
| | 441 | |
|
| 18 | 442 | | foreach (int point in element.SpecialPoints) |
| | 443 | | { |
| 6 | 444 | | stream.Write(' '); |
| 6 | 445 | | stream.Write(point); |
| | 446 | | } |
| | 447 | |
|
| 3 | 448 | | stream.WriteLine(); |
| | 449 | | } |
| | 450 | |
|
| 108 | 451 | | if (writeEnd) |
| | 452 | | { |
| 18 | 453 | | stream.WriteLine("end"); |
| | 454 | | } |
| 108 | 455 | | } |
| | 456 | | } |
| | 457 | | } |
| | 458 | |
|