2014-12-03 23:15:26 +00:00
|
|
|
|
#region ================== Namespaces
|
|
|
|
|
|
|
|
|
|
using System;
|
2013-03-18 13:52:27 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Drawing.Imaging;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using CodeImp.DoomBuilder.Data;
|
|
|
|
|
using CodeImp.DoomBuilder.Geometry;
|
|
|
|
|
using CodeImp.DoomBuilder.Map;
|
|
|
|
|
using CodeImp.DoomBuilder.Rendering;
|
|
|
|
|
using CodeImp.DoomBuilder.Windows;
|
2014-01-08 15:20:56 +00:00
|
|
|
|
using System.Windows.Forms;
|
2013-03-18 13:52:27 +00:00
|
|
|
|
|
2014-12-03 23:15:26 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2013-03-18 13:52:27 +00:00
|
|
|
|
namespace CodeImp.DoomBuilder.BuilderModes.IO
|
|
|
|
|
{
|
2014-12-03 23:15:26 +00:00
|
|
|
|
#region ================== Structs
|
|
|
|
|
|
2013-03-18 13:52:27 +00:00
|
|
|
|
internal struct WavefrontExportSettings
|
|
|
|
|
{
|
|
|
|
|
public string Obj;
|
2014-10-28 09:23:21 +00:00
|
|
|
|
public readonly string ObjName;
|
|
|
|
|
public readonly string ObjPath;
|
|
|
|
|
public readonly float Scale;
|
|
|
|
|
public readonly bool FixScale;
|
|
|
|
|
public readonly bool ExportTextures;
|
2013-03-18 13:52:27 +00:00
|
|
|
|
public bool Valid;
|
|
|
|
|
public string[] Textures;
|
2014-10-28 09:23:21 +00:00
|
|
|
|
public string[] Flats;
|
2013-03-18 13:52:27 +00:00
|
|
|
|
|
2014-12-03 23:15:26 +00:00
|
|
|
|
public WavefrontExportSettings(string name, string path, float scale, bool fixScale, bool exportTextures)
|
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
|
ObjName = name;
|
|
|
|
|
ObjPath = path;
|
|
|
|
|
Scale = scale;
|
|
|
|
|
FixScale = fixScale;
|
|
|
|
|
ExportTextures = exportTextures;
|
|
|
|
|
|
|
|
|
|
Valid = false;
|
|
|
|
|
Obj = string.Empty;
|
|
|
|
|
Textures = null;
|
2014-10-28 09:23:21 +00:00
|
|
|
|
Flats = null;
|
2013-03-18 13:52:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-12-03 23:15:26 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2013-03-18 13:52:27 +00:00
|
|
|
|
internal class WavefrontExporter
|
|
|
|
|
{
|
2014-12-03 23:15:26 +00:00
|
|
|
|
|
|
|
|
|
#region ================== Variables and structs
|
|
|
|
|
|
2013-03-18 13:52:27 +00:00
|
|
|
|
private const string DEFAULT = "Default";
|
|
|
|
|
|
2013-12-11 13:28:26 +00:00
|
|
|
|
private struct VertexIndices
|
|
|
|
|
{
|
|
|
|
|
public int PositionIndex;
|
|
|
|
|
public int UVIndex;
|
|
|
|
|
public int NormalIndex;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-03 23:15:26 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region ================== Export
|
|
|
|
|
|
|
|
|
|
public void Export(ICollection<Sector> sectors, WavefrontExportSettings settings)
|
|
|
|
|
{
|
|
|
|
|
CreateObjFromSelection(sectors, ref settings);
|
2013-03-18 13:52:27 +00:00
|
|
|
|
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if(!settings.Valid)
|
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
|
General.Interface.DisplayStatus(StatusType.Warning, "OBJ creation failed. Check 'Errors and Warnings' window for details.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if(settings.ExportTextures)
|
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
|
//save all used textures
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if(settings.Textures != null)
|
|
|
|
|
{
|
|
|
|
|
foreach(string s in settings.Textures)
|
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
|
if(s == DEFAULT) continue;
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if(General.Map.Data.GetTextureExists(s))
|
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
|
ImageData id = General.Map.Data.GetTextureImage(s);
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if(id.Width == 0 || id.Height == 0)
|
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
|
General.ErrorLogger.Add(ErrorType.Warning, "OBJ Exporter: texture '" + s + "' has invalid size (" + id.Width + "x" + id.Height + ")!");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-28 09:23:21 +00:00
|
|
|
|
if(!id.IsImageLoaded) id.LoadImage();
|
2013-03-18 13:52:27 +00:00
|
|
|
|
Bitmap bmp = id.GetBitmap();
|
2015-06-30 12:16:03 +00:00
|
|
|
|
bmp.Save(Path.Combine(settings.ObjPath, Path.GetFileNameWithoutExtension(s) + ".PNG"), ImageFormat.Png);
|
2014-12-03 23:15:26 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
|
General.ErrorLogger.Add(ErrorType.Warning, "OBJ Exporter: texture '" + s + "' does not exist!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-28 09:23:21 +00:00
|
|
|
|
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if (settings.Flats != null)
|
|
|
|
|
{
|
|
|
|
|
foreach(string s in settings.Flats)
|
|
|
|
|
{
|
2014-10-28 09:23:21 +00:00
|
|
|
|
if(s == DEFAULT) continue;
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if(General.Map.Data.GetFlatExists(s))
|
|
|
|
|
{
|
2014-10-28 09:23:21 +00:00
|
|
|
|
ImageData id = General.Map.Data.GetFlatImage(s);
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if(id.Width == 0 || id.Height == 0)
|
|
|
|
|
{
|
2014-10-28 09:23:21 +00:00
|
|
|
|
General.ErrorLogger.Add(ErrorType.Warning, "OBJ Exporter: flat '" + s + "' has invalid size (" + id.Width + "x" + id.Height + ")!");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!id.IsImageLoaded) id.LoadImage();
|
|
|
|
|
Bitmap bmp = id.GetBitmap();
|
|
|
|
|
|
|
|
|
|
// Handle duplicate names
|
|
|
|
|
string flatname = s;
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if(settings.Textures != null && Array.IndexOf(settings.Textures, s) != -1)
|
2014-10-28 09:23:21 +00:00
|
|
|
|
flatname += "_FLAT";
|
|
|
|
|
|
2015-06-30 12:16:03 +00:00
|
|
|
|
bmp.Save(Path.Combine(settings.ObjPath, Path.GetFileNameWithoutExtension(flatname) + ".PNG"), ImageFormat.Png);
|
2014-10-28 09:23:21 +00:00
|
|
|
|
|
2014-12-03 23:15:26 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-10-28 09:23:21 +00:00
|
|
|
|
General.ErrorLogger.Add(ErrorType.Warning, "OBJ Exporter: flat '" + s + "' does not exist!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-03-18 13:52:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//write obj
|
|
|
|
|
string savePath = Path.Combine(settings.ObjPath, settings.ObjName);
|
|
|
|
|
using(StreamWriter sw = new StreamWriter(savePath + ".obj", false))
|
|
|
|
|
sw.Write(settings.Obj);
|
|
|
|
|
|
|
|
|
|
//create mtl
|
|
|
|
|
StringBuilder mtl = new StringBuilder();
|
|
|
|
|
mtl.Append("# MTL for " + General.Map.FileTitle + ", map " + General.Map.Options.LevelName + Environment.NewLine);
|
2014-01-08 15:20:56 +00:00
|
|
|
|
mtl.Append("# Created by GZDoom Builder " + Application.ProductVersion + Environment.NewLine + Environment.NewLine);
|
2013-03-18 13:52:27 +00:00
|
|
|
|
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if (settings.Textures != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (string s in settings.Textures)
|
|
|
|
|
{
|
2014-10-28 09:23:21 +00:00
|
|
|
|
if (s == DEFAULT) continue;
|
|
|
|
|
mtl.Append("newmtl " + s.ToUpperInvariant() + Environment.NewLine);
|
|
|
|
|
mtl.Append("Kd 1.0 1.0 1.0" + Environment.NewLine);
|
|
|
|
|
if (settings.ExportTextures) mtl.Append("map_Kd " + Path.Combine(settings.ObjPath, s.ToUpperInvariant() + ".PNG") + Environment.NewLine);
|
|
|
|
|
mtl.Append(Environment.NewLine);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if(settings.Flats != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (string s in settings.Flats)
|
|
|
|
|
{
|
2014-10-28 09:23:21 +00:00
|
|
|
|
if (s == DEFAULT) continue;
|
|
|
|
|
mtl.Append("newmtl " + s.ToUpperInvariant() + Environment.NewLine);
|
|
|
|
|
mtl.Append("Kd 1.0 1.0 1.0" + Environment.NewLine);
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if (settings.ExportTextures)
|
|
|
|
|
{
|
2014-10-28 09:23:21 +00:00
|
|
|
|
// Handle duplicate names
|
|
|
|
|
string flatname = s;
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if(settings.Textures != null && Array.IndexOf(settings.Textures, s) != -1)
|
2014-10-28 09:23:21 +00:00
|
|
|
|
flatname += "_FLAT";
|
|
|
|
|
|
|
|
|
|
mtl.Append("map_Kd " + Path.Combine(settings.ObjPath, flatname.ToUpperInvariant() + ".PNG") + Environment.NewLine);
|
|
|
|
|
}
|
|
|
|
|
mtl.Append(Environment.NewLine);
|
|
|
|
|
}
|
2013-03-18 13:52:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//write mtl
|
|
|
|
|
using(StreamWriter sw = new StreamWriter(savePath + ".mtl", false))
|
|
|
|
|
sw.Write(mtl.ToString());
|
|
|
|
|
|
|
|
|
|
//done
|
|
|
|
|
General.Interface.DisplayStatus(StatusType.Warning, "Geometry exported to '" + savePath + ".obj'");
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-03 23:15:26 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region ================== Utility
|
|
|
|
|
|
|
|
|
|
private static void CreateObjFromSelection(ICollection<Sector> sectors, ref WavefrontExportSettings data)
|
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
|
BaseVisualMode mode = new BaseVisualMode();
|
|
|
|
|
bool renderingEffectsDisabled = false;
|
|
|
|
|
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if(!BaseVisualMode.GZDoomRenderingEffects)
|
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
|
renderingEffectsDisabled = true;
|
|
|
|
|
mode.ToggleGZDoomRenderingEffects();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mode.RebuildElementData();
|
|
|
|
|
|
|
|
|
|
List<BaseVisualSector> visualSectors = new List<BaseVisualSector>();
|
|
|
|
|
|
|
|
|
|
//create visual geometry
|
2014-12-03 23:15:26 +00:00
|
|
|
|
foreach(Sector s in sectors)
|
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
|
BaseVisualSector bvs = mode.CreateBaseVisualSector(s);
|
|
|
|
|
if(bvs != null) visualSectors.Add(bvs);
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if(visualSectors.Count == 0)
|
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
|
General.ErrorLogger.Add(ErrorType.Error, "OBJ Exporter: no visual sectors to export!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//sort geometry
|
2014-12-03 23:15:26 +00:00
|
|
|
|
List<Dictionary<string, List<WorldVertex[]>>> geometryByTexture = SortGeometry(visualSectors);
|
2013-03-18 13:52:27 +00:00
|
|
|
|
|
|
|
|
|
//restore vm settings
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if(renderingEffectsDisabled) mode.ToggleGZDoomRenderingEffects();
|
2013-03-18 13:52:27 +00:00
|
|
|
|
mode.Dispose();
|
|
|
|
|
|
|
|
|
|
//create obj
|
2014-12-03 23:15:26 +00:00
|
|
|
|
StringBuilder obj = CreateObjGeometry(geometryByTexture, data);
|
2013-03-18 13:52:27 +00:00
|
|
|
|
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if(obj.Length == 0)
|
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
|
General.ErrorLogger.Add(ErrorType.Error, "OBJ Exporter: failed to create geometry!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//add header
|
|
|
|
|
obj.Insert(0, "o " + General.Map.Options.LevelName + Environment.NewLine); //name
|
2014-01-08 15:20:56 +00:00
|
|
|
|
obj.Insert(0, "# Created by GZDoom Builder " + Application.ProductVersion + Environment.NewLine + Environment.NewLine);
|
2013-12-11 13:28:26 +00:00
|
|
|
|
obj.Insert(0, "# " + General.Map.FileTitle + ", map " + General.Map.Options.LevelName + Environment.NewLine);
|
2013-03-18 13:52:27 +00:00
|
|
|
|
data.Obj = obj.ToString();
|
|
|
|
|
|
2014-10-28 09:23:21 +00:00
|
|
|
|
string[] textures = new string[geometryByTexture[0].Keys.Count];
|
|
|
|
|
geometryByTexture[0].Keys.CopyTo(textures, 0);
|
2013-03-18 13:52:27 +00:00
|
|
|
|
Array.Sort(textures);
|
|
|
|
|
data.Textures = textures;
|
|
|
|
|
|
2014-10-28 09:23:21 +00:00
|
|
|
|
string[] flats = new string[geometryByTexture[1].Keys.Count];
|
|
|
|
|
geometryByTexture[1].Keys.CopyTo(flats, 0);
|
|
|
|
|
Array.Sort(flats);
|
|
|
|
|
data.Flats = flats;
|
|
|
|
|
|
2013-03-18 13:52:27 +00:00
|
|
|
|
data.Valid = true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-03 23:15:26 +00:00
|
|
|
|
private static List<Dictionary<string, List<WorldVertex[]>>> SortGeometry(List<BaseVisualSector> visualSectors)
|
|
|
|
|
{
|
2014-10-28 09:23:21 +00:00
|
|
|
|
var texturegeo = new Dictionary<string, List<WorldVertex[]>>(StringComparer.Ordinal);
|
|
|
|
|
texturegeo.Add(DEFAULT, new List<WorldVertex[]>());
|
|
|
|
|
var flatgeo = new Dictionary<string, List<WorldVertex[]>>(StringComparer.Ordinal);
|
|
|
|
|
flatgeo.Add(DEFAULT, new List<WorldVertex[]>());
|
2013-03-18 13:52:27 +00:00
|
|
|
|
string texture;
|
|
|
|
|
|
2014-12-03 23:15:26 +00:00
|
|
|
|
foreach(BaseVisualSector vs in visualSectors)
|
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
|
//floor
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if(vs.Floor != null)
|
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
|
texture = vs.Sector.FloorTexture;
|
2014-12-03 23:15:26 +00:00
|
|
|
|
CheckTextureName(ref flatgeo, ref texture);
|
2014-10-28 09:23:21 +00:00
|
|
|
|
flatgeo[texture].AddRange(OptimizeGeometry(vs.Floor.Vertices, vs.Floor.GeometryType, vs.Floor.Sector.Sector.Labels.Count > 1));
|
2013-03-18 13:52:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//ceiling
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if(vs.Ceiling != null)
|
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
|
texture = vs.Sector.CeilTexture;
|
2014-12-03 23:15:26 +00:00
|
|
|
|
CheckTextureName(ref flatgeo, ref texture);
|
2014-10-28 09:23:21 +00:00
|
|
|
|
flatgeo[texture].AddRange(OptimizeGeometry(vs.Ceiling.Vertices, vs.Ceiling.GeometryType, vs.Ceiling.Sector.Sector.Labels.Count > 1));
|
2013-03-18 13:52:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//walls
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if(vs.Sides != null)
|
|
|
|
|
{
|
|
|
|
|
foreach(VisualSidedefParts part in vs.Sides.Values)
|
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
|
//upper
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if(part.upper != null && part.upper.Vertices != null)
|
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
|
texture = part.upper.Sidedef.HighTexture;
|
2014-12-03 23:15:26 +00:00
|
|
|
|
CheckTextureName(ref texturegeo, ref texture);
|
2014-10-28 09:23:21 +00:00
|
|
|
|
texturegeo[texture].AddRange(OptimizeGeometry(part.upper.Vertices, part.upper.GeometryType));
|
2013-03-18 13:52:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//middle single
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if(part.middlesingle != null && part.middlesingle.Vertices != null)
|
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
|
texture = part.middlesingle.Sidedef.MiddleTexture;
|
2014-12-03 23:15:26 +00:00
|
|
|
|
CheckTextureName(ref texturegeo, ref texture);
|
2014-10-28 09:23:21 +00:00
|
|
|
|
texturegeo[texture].AddRange(OptimizeGeometry(part.middlesingle.Vertices, part.middlesingle.GeometryType));
|
2013-03-18 13:52:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//middle double
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if(part.middledouble != null && part.middledouble.Vertices != null)
|
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
|
texture = part.middledouble.Sidedef.MiddleTexture;
|
2014-12-03 23:15:26 +00:00
|
|
|
|
CheckTextureName(ref texturegeo, ref texture);
|
2014-10-28 09:23:21 +00:00
|
|
|
|
texturegeo[texture].AddRange(OptimizeGeometry(part.middledouble.Vertices, part.middledouble.GeometryType));
|
2013-03-18 13:52:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//middle3d
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if(part.middle3d != null && part.middle3d.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
foreach(VisualMiddle3D m3d in part.middle3d)
|
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
|
if(m3d.Vertices == null) continue;
|
2014-10-28 09:23:21 +00:00
|
|
|
|
texture = m3d.GetControlLinedef().Front.MiddleTexture;
|
2014-12-03 23:15:26 +00:00
|
|
|
|
CheckTextureName(ref texturegeo, ref texture);
|
2014-10-28 09:23:21 +00:00
|
|
|
|
texturegeo[texture].AddRange(OptimizeGeometry(m3d.Vertices, m3d.GeometryType));
|
2013-03-18 13:52:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//backsides(?)
|
|
|
|
|
|
|
|
|
|
//lower
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if(part.lower != null && part.lower.Vertices != null)
|
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
|
texture = part.lower.Sidedef.LowTexture;
|
2014-12-03 23:15:26 +00:00
|
|
|
|
CheckTextureName(ref texturegeo, ref texture);
|
2014-10-28 09:23:21 +00:00
|
|
|
|
texturegeo[texture].AddRange(OptimizeGeometry(part.lower.Vertices, part.lower.GeometryType));
|
2013-03-18 13:52:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//3d ceilings
|
2014-12-03 23:15:26 +00:00
|
|
|
|
foreach(VisualCeiling vc in vs.ExtraCeilings)
|
|
|
|
|
{
|
2014-10-28 09:23:21 +00:00
|
|
|
|
texture = vc.GetControlSector().CeilTexture;
|
2014-12-03 23:15:26 +00:00
|
|
|
|
CheckTextureName(ref flatgeo, ref texture);
|
2014-10-28 09:23:21 +00:00
|
|
|
|
flatgeo[texture].AddRange(OptimizeGeometry(vc.Vertices, (vc.ExtraFloor.VavoomType ? vc.GeometryType : VisualModes.VisualGeometryType.FLOOR)));
|
2013-03-18 13:52:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//3d floors
|
2014-12-03 23:15:26 +00:00
|
|
|
|
foreach(VisualFloor vf in vs.ExtraFloors)
|
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
|
texture = vf.GetControlSector().FloorTexture;
|
2014-12-03 23:15:26 +00:00
|
|
|
|
CheckTextureName(ref flatgeo, ref texture);
|
2014-10-28 09:23:21 +00:00
|
|
|
|
flatgeo[texture].AddRange(OptimizeGeometry(vf.Vertices, (vf.ExtraFloor.VavoomType ? vf.GeometryType : VisualModes.VisualGeometryType.CEILING)));
|
2013-03-18 13:52:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//backsides(?)
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-28 09:23:21 +00:00
|
|
|
|
return new List<Dictionary<string, List<WorldVertex[]>>> { texturegeo, flatgeo };
|
2013-03-18 13:52:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-03 23:15:26 +00:00
|
|
|
|
private static void CheckTextureName(ref Dictionary<string, List<WorldVertex[]>> geo, ref string texture)
|
|
|
|
|
{
|
|
|
|
|
if(!string.IsNullOrEmpty(texture) && texture != "-")
|
|
|
|
|
{
|
|
|
|
|
if(!geo.ContainsKey(texture)) geo.Add(texture, new List<WorldVertex[]>());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
|
texture = DEFAULT;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-03 23:15:26 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region ================== Surface optimization
|
|
|
|
|
|
2014-10-28 09:23:21 +00:00
|
|
|
|
private static List<WorldVertex[]> OptimizeGeometry(WorldVertex[] verts, VisualModes.VisualGeometryType geotype)
|
|
|
|
|
{
|
|
|
|
|
return OptimizeGeometry(verts, geotype, false);
|
2013-03-18 13:52:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-03 23:15:26 +00:00
|
|
|
|
private static List<WorldVertex[]> OptimizeGeometry(WorldVertex[] verts, VisualModes.VisualGeometryType geotype, bool skiprectoptimization)
|
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
|
List<WorldVertex[]> groups = new List<WorldVertex[]>();
|
|
|
|
|
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if(!skiprectoptimization && verts.Length == 6) //rectangular surface
|
|
|
|
|
{
|
|
|
|
|
if(geotype == VisualModes.VisualGeometryType.CEILING)
|
|
|
|
|
{
|
2013-12-11 13:28:26 +00:00
|
|
|
|
verts = new[] { verts[2], verts[5], verts[1], verts[0] };
|
2014-12-03 23:15:26 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-10-28 09:23:21 +00:00
|
|
|
|
verts = new[] { verts[5], verts[2], verts[1], verts[0] };
|
2013-12-11 13:28:26 +00:00
|
|
|
|
}
|
2013-03-18 13:52:27 +00:00
|
|
|
|
groups.Add(verts);
|
2014-12-03 23:15:26 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < verts.Length; i += 3)
|
|
|
|
|
{
|
2013-12-11 13:28:26 +00:00
|
|
|
|
groups.Add(new[] { verts[i + 2], verts[i + 1], verts[i] });
|
|
|
|
|
}
|
2013-03-18 13:52:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return groups;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-03 23:15:26 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region ================== OBJ Creation
|
|
|
|
|
|
|
|
|
|
private static StringBuilder CreateObjGeometry(List<Dictionary<string, List<WorldVertex[]>>> geometryByTexture, WavefrontExportSettings data)
|
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
|
StringBuilder obj = new StringBuilder();
|
2013-12-11 13:28:26 +00:00
|
|
|
|
const string vertexFormatter = "{0} {2} {1}\n";
|
2013-03-18 13:52:27 +00:00
|
|
|
|
|
2013-12-12 09:07:30 +00:00
|
|
|
|
Dictionary<Vector3D, int> uniqueVerts = new Dictionary<Vector3D, int>();
|
|
|
|
|
Dictionary<Vector3D, int> uniqueNormals = new Dictionary<Vector3D, int>();
|
|
|
|
|
Dictionary<PointF, int> uniqueUVs = new Dictionary<PointF, int>();
|
2013-03-18 13:52:27 +00:00
|
|
|
|
|
2014-02-26 14:11:06 +00:00
|
|
|
|
var vertexDataByTexture = new Dictionary<string, Dictionary<WorldVertex, VertexIndices>>(StringComparer.Ordinal);
|
2013-12-12 09:07:30 +00:00
|
|
|
|
int ni;
|
|
|
|
|
int pc = 0;
|
|
|
|
|
int nc = 0;
|
|
|
|
|
int uvc = 0;
|
2013-12-11 13:28:26 +00:00
|
|
|
|
|
2013-03-18 13:52:27 +00:00
|
|
|
|
//optimize geometry
|
2014-12-03 23:15:26 +00:00
|
|
|
|
foreach(Dictionary<string, List<WorldVertex[]>> dictionary in geometryByTexture)
|
|
|
|
|
{
|
|
|
|
|
foreach(KeyValuePair<string, List<WorldVertex[]>> group in dictionary)
|
|
|
|
|
{
|
2014-10-28 09:23:21 +00:00
|
|
|
|
Dictionary<WorldVertex, VertexIndices> vertsData = new Dictionary<WorldVertex, VertexIndices>();
|
2014-12-03 23:15:26 +00:00
|
|
|
|
foreach(WorldVertex[] verts in group.Value)
|
|
|
|
|
{
|
2014-10-28 09:23:21 +00:00
|
|
|
|
//vertex normals
|
|
|
|
|
Vector3D n = new Vector3D(verts[0].nx, verts[0].ny, verts[0].nz).GetNormal();
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if (uniqueNormals.ContainsKey(n))
|
|
|
|
|
{
|
2014-10-28 09:23:21 +00:00
|
|
|
|
ni = uniqueNormals[n];
|
2014-12-03 23:15:26 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-10-28 09:23:21 +00:00
|
|
|
|
uniqueNormals.Add(n, ++nc);
|
|
|
|
|
ni = nc;
|
2013-12-11 13:28:26 +00:00
|
|
|
|
}
|
2013-03-18 13:52:27 +00:00
|
|
|
|
|
2014-12-03 23:15:26 +00:00
|
|
|
|
foreach(WorldVertex v in verts)
|
|
|
|
|
{
|
2014-10-28 09:23:21 +00:00
|
|
|
|
if (vertsData.ContainsKey(v)) continue;
|
|
|
|
|
VertexIndices indices = new VertexIndices();
|
|
|
|
|
indices.NormalIndex = ni;
|
|
|
|
|
|
|
|
|
|
//vertex coords
|
|
|
|
|
Vector3D vc = new Vector3D(v.x, v.y, v.z);
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if (uniqueVerts.ContainsKey(vc))
|
|
|
|
|
{
|
2014-10-28 09:23:21 +00:00
|
|
|
|
indices.PositionIndex = uniqueVerts[vc];
|
2014-12-03 23:15:26 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-10-28 09:23:21 +00:00
|
|
|
|
uniqueVerts.Add(vc, ++pc);
|
|
|
|
|
indices.PositionIndex = pc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//uv
|
|
|
|
|
PointF uv = new PointF(v.u, v.v);
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if (uniqueUVs.ContainsKey(uv))
|
|
|
|
|
{
|
2014-10-28 09:23:21 +00:00
|
|
|
|
indices.UVIndex = uniqueUVs[uv];
|
2014-12-03 23:15:26 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-10-28 09:23:21 +00:00
|
|
|
|
uniqueUVs.Add(uv, ++uvc);
|
|
|
|
|
indices.UVIndex = uvc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vertsData.Add(v, indices);
|
2013-12-11 13:28:26 +00:00
|
|
|
|
}
|
2014-10-28 09:23:21 +00:00
|
|
|
|
}
|
2013-03-18 13:52:27 +00:00
|
|
|
|
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if(vertsData.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
if(vertexDataByTexture.ContainsKey(group.Key))
|
|
|
|
|
{
|
|
|
|
|
foreach(KeyValuePair<WorldVertex, VertexIndices> g in vertsData)
|
2014-10-28 09:23:21 +00:00
|
|
|
|
vertexDataByTexture[group.Key].Add(g.Key, g.Value);
|
2014-12-03 23:15:26 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-10-28 09:23:21 +00:00
|
|
|
|
vertexDataByTexture.Add(group.Key, vertsData);
|
|
|
|
|
}
|
2013-12-11 13:28:26 +00:00
|
|
|
|
}
|
2013-03-18 13:52:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//write geometry
|
|
|
|
|
//write vertices
|
2014-12-03 23:15:26 +00:00
|
|
|
|
if(data.FixScale)
|
|
|
|
|
{
|
2013-12-12 09:07:30 +00:00
|
|
|
|
foreach(KeyValuePair<Vector3D, int> group in uniqueVerts)
|
|
|
|
|
obj.Append(string.Format(CultureInfo.InvariantCulture, "v " + vertexFormatter, -group.Key.x * data.Scale, group.Key.y * data.Scale, group.Key.z * data.Scale * 1.2f));
|
2014-12-03 23:15:26 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-12-12 09:07:30 +00:00
|
|
|
|
foreach(KeyValuePair<Vector3D, int> group in uniqueVerts)
|
|
|
|
|
obj.Append(string.Format(CultureInfo.InvariantCulture, "v " + vertexFormatter, -group.Key.x * data.Scale, group.Key.y * data.Scale, group.Key.z * data.Scale));
|
2013-03-18 13:52:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//write normals
|
2013-12-12 09:07:30 +00:00
|
|
|
|
foreach(KeyValuePair<Vector3D, int> group in uniqueNormals)
|
|
|
|
|
obj.Append(string.Format(CultureInfo.InvariantCulture, "vn " + vertexFormatter, group.Key.x, group.Key.y, group.Key.z));
|
2013-03-18 13:52:27 +00:00
|
|
|
|
|
2013-12-11 13:28:26 +00:00
|
|
|
|
//write UV coords
|
2013-12-12 09:07:30 +00:00
|
|
|
|
foreach(KeyValuePair<PointF, int> group in uniqueUVs)
|
|
|
|
|
obj.Append(string.Format(CultureInfo.InvariantCulture, "vt {0} {1}\n", group.Key.X, -group.Key.Y));
|
2013-03-18 13:52:27 +00:00
|
|
|
|
|
2013-12-11 13:28:26 +00:00
|
|
|
|
//write material library
|
|
|
|
|
obj.Append("mtllib ").Append(data.ObjName + ".mtl").Append("\n");
|
|
|
|
|
|
2013-03-18 13:52:27 +00:00
|
|
|
|
//write materials and surface indices
|
2014-12-03 23:15:26 +00:00
|
|
|
|
foreach(Dictionary<string, List<WorldVertex[]>> dictionary in geometryByTexture)
|
|
|
|
|
{
|
|
|
|
|
foreach(KeyValuePair<string, List<WorldVertex[]>> group in dictionary)
|
|
|
|
|
{
|
2014-10-28 09:23:21 +00:00
|
|
|
|
//material
|
|
|
|
|
obj.Append("usemtl ").Append(group.Key).Append("\n");
|
|
|
|
|
|
2014-12-03 23:15:26 +00:00
|
|
|
|
foreach(WorldVertex[] verts in group.Value)
|
|
|
|
|
{
|
2014-10-28 09:23:21 +00:00
|
|
|
|
//surface indices
|
|
|
|
|
obj.Append("f");
|
2014-12-03 23:15:26 +00:00
|
|
|
|
foreach(WorldVertex v in verts)
|
|
|
|
|
{
|
2014-10-28 09:23:21 +00:00
|
|
|
|
VertexIndices vi = vertexDataByTexture[group.Key][v];
|
|
|
|
|
obj.Append(" " + vi.PositionIndex + "/" + vi.UVIndex + "/" + vi.NormalIndex);
|
|
|
|
|
}
|
|
|
|
|
obj.Append("\n");
|
2013-03-18 13:52:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
2014-12-03 23:15:26 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
2013-03-18 13:52:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|