diff options
-rw-r--r-- | NW4RTools.userprefs | 14 | ||||
-rw-r--r-- | NW4RTools/BrresReader.cs | 203 | ||||
-rw-r--r-- | NW4RTools/Logger.cs | 16 | ||||
-rw-r--r-- | NW4RTools/Models/Material.cs | 10 | ||||
-rw-r--r-- | NW4RTools/Models/Model.cs | 5 | ||||
-rw-r--r-- | NW4RTools/Models/Shape.cs | 29 | ||||
-rw-r--r-- | NW4RTools/Models/TextureInfo.cs | 21 | ||||
-rw-r--r-- | NW4RTools/NW4RTools.csproj | 2 | ||||
-rw-r--r-- | NW4RTools/NW4RTools.pidb | bin | 73876 -> 78667 bytes | |||
-rwxr-xr-x | NW4RTools/bin/Debug/NW4RTools.dll | bin | 29184 -> 31744 bytes | |||
-rw-r--r-- | NW4RTools/bin/Debug/NW4RTools.dll.mdb | bin | 10124 -> 11043 bytes | |||
-rw-r--r-- | TestApp/Main.cs | 3 | ||||
-rw-r--r-- | TestApp/TestApp.pidb | bin | 2991 -> 3039 bytes | |||
-rwxr-xr-x | TestApp/bin/Debug/NW4RTools.dll | bin | 29184 -> 31744 bytes | |||
-rw-r--r-- | TestApp/bin/Debug/NW4RTools.dll.mdb | bin | 10124 -> 11043 bytes | |||
-rwxr-xr-x | TestApp/bin/Debug/TestApp.exe | bin | 3584 -> 3584 bytes | |||
-rw-r--r-- | TestApp/bin/Debug/TestApp.exe.mdb | bin | 416 -> 416 bytes |
17 files changed, 243 insertions, 60 deletions
diff --git a/NW4RTools.userprefs b/NW4RTools.userprefs index bad4528..f6cd241 100644 --- a/NW4RTools.userprefs +++ b/NW4RTools.userprefs @@ -1,13 +1,17 @@ <Properties> <MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" /> - <MonoDevelop.Ide.Workbench ActiveDocument="NW4RTools/BrresReader.cs"> + <MonoDevelop.Ide.Workbench ActiveDocument="TestApp/Main.cs"> <Files> <File FileName="NW4RTools/Types.cs" Line="71" Column="8" /> - <File FileName="NW4RTools/BrresReader.cs" Line="433" Column="74" /> - <File FileName="NW4RTools/Models/Model.cs" Line="19" Column="34" /> - <File FileName="NW4RTools/Models/Material.cs" Line="28" Column="36" /> + <File FileName="NW4RTools/BrresReader.cs" Line="146" Column="1" /> + <File FileName="NW4RTools/Models/Model.cs" Line="2" Column="34" /> + <File FileName="NW4RTools/Models/Material.cs" Line="49" Column="40" /> <File FileName="NW4RTools/InputStream.cs" Line="94" Column="15" /> - <File FileName="NW4RTools/Models/Shader.cs" Line="2" Column="34" /> + <File FileName="NW4RTools/Models/Shader.cs" Line="9" Column="29" /> + <File FileName="NW4RTools/Models/Shape.cs" Line="21" Column="32" /> + <File FileName="NW4RTools/Logger.cs" Line="27" Column="9" /> + <File FileName="NW4RTools/Models/TextureInfo.cs" Line="18" Column="28" /> + <File FileName="TestApp/Main.cs" Line="9" Column="108" /> </Files> </MonoDevelop.Ide.Workbench> <MonoDevelop.Ide.DebuggingService.Breakpoints> diff --git a/NW4RTools/BrresReader.cs b/NW4RTools/BrresReader.cs index cd9452d..eeccd94 100644 --- a/NW4RTools/BrresReader.cs +++ b/NW4RTools/BrresReader.cs @@ -18,11 +18,11 @@ namespace NW4RTools { private ResFile File; - private Logger Debug; + private ILogger Debug; private SortedDictionary<int, string> OffsetMap; private BrresReader() { - Debug = new Logger(); + Debug = new ConsoleLogger(); OffsetMap = new SortedDictionary<int, string>(); } @@ -146,7 +146,7 @@ namespace NW4RTools { // Load vertex data // COMMENTED OUT TO MAKE DEUBG OUTPUT CLEARER FOR NOW - /* + using (var c2 = Debug.Push("Vertex Position Data")) mdl.VtxPosData = ReadAndConvertDict<VertexPosData>(ins.At(startPos + vtxPosOffset), ConvertVtxPosData); using (var c2 = Debug.Push("Vertex Normal Data")) @@ -160,6 +160,7 @@ namespace NW4RTools { // Load materials and associated structs // Material classes refer to Shaders using an offset, so this is used to fix those up ShaderOffsetRefs = new Dictionary<int, Shader>(); + MaterialOffsetRefs = new Dictionary<int, Material>(); using (var c2 = Debug.Push("Shaders")) mdl.Shaders = ReadAndConvertDict<Shader>(ins.At(startPos + shaderOffset), ConvertModelShader); @@ -167,6 +168,20 @@ namespace NW4RTools { using (var c2 = Debug.Push("Materials")) mdl.Materials = ReadAndConvertDict<Material>(ins.At(startPos + materialOffset), ConvertModelMaterial); + using (var c2 = Debug.Push("Shapes")) + mdl.Shapes = ReadAndConvertDict<Shape>(ins.At(startPos + shapeOffset), ConvertModelShape); + + using (var c2 = Debug.Push("Texture Lookup for Texture Info/Material Pairing")) + mdl.PairingLookupByTexture = ReadAndConvertDict<List<TexMatPairing>>(ins.At(startPos + textureOffset), ConvertPairingList); + + if (paletteOffset != 0) { + using (var c2 = Debug.Push("Palette Lookup for Texture Info/Material Pairing")) + mdl.PairingLookupByPalette = ReadAndConvertDict<List<TexMatPairing>>(ins.At(startPos + paletteOffset), ConvertPairingList); + } else { + mdl.PairingLookupByPalette = new ResDict<List<TexMatPairing>>(); + Debug.Send("Palette Lookup for Texture Info/Material Pairing: none"); + } + return mdl; } } @@ -384,6 +399,8 @@ namespace NW4RTools { + private Dictionary<int, Models.Material> MaterialOffsetRefs; + private Models.Material ConvertModelMaterial(string name, InputStream ins) { Debug.Send("Reading {0}...", name); @@ -419,8 +436,8 @@ namespace NW4RTools { m.ShaderRef = ShaderOffsetRefs[startPos + shaderOffset]; //Debug.Send("Shader offset for {0}: {1:X} [dest {2:X}]", name, shaderOffset, startPos + shaderOffset); - UInt32 boundTexCount = ins.ReadUInt32(); - Int32 boundTexOffset = ins.ReadInt32(); + UInt32 texInfoCount = ins.ReadUInt32(); + Int32 texInfoOffset = ins.ReadInt32(); Int32 furOffset = ins.ReadInt32(); Debug.Send("Fur offset for {0}: {1:X} [dest {2:X}]", name, furOffset, startPos + furOffset); @@ -433,7 +450,7 @@ namespace NW4RTools { // ResTexObj int ResTexObjPos = ins.Position; - OffsetMap.Add(ins.Position, "Material ResTexObj: " + name); + //OffsetMap.Add(ins.Position, "Material ResTexObj: " + name); UInt32 textureFlag = ins.ReadUInt32(); m.TexObj = new byte[8][]; @@ -448,7 +465,7 @@ namespace NW4RTools { // ResTlutObj int ResTlutObjPos = ins.Position; - OffsetMap.Add(ins.Position, "Material ResTlutObj: " + name); + //OffsetMap.Add(ins.Position, "Material ResTlutObj: " + name); UInt32 tlutFlag = ins.ReadUInt32(); if ((tlutFlag & 0xFC) == 0) { @@ -463,7 +480,7 @@ namespace NW4RTools { // ResTexSrt int ResTexSrtPos = ins.Position; - OffsetMap.Add(ins.Position, "Material ResTexSrt: " + name); + //OffsetMap.Add(ins.Position, "Material ResTexSrt: " + name); UInt32 srtFlag = ins.ReadUInt32(); m.TexMatrixType = ins.ReadUInt32(); @@ -493,7 +510,7 @@ namespace NW4RTools { ins.Seek(ResTexSrtPos + 8 + (0x14 * 8) + (0x34 * 8)); // ResMatChan - OffsetMap.Add(ins.Position, "Material ResMatChan: " + name); + //OffsetMap.Add(ins.Position, "Material ResMatChan: " + name); m.ChanCtrls = new ChanCtrl[2]; @@ -507,41 +524,17 @@ namespace NW4RTools { chanInfo.FlagA = ins.ReadUInt32(); } - // Bound Textures - if (boundTexOffset != 0) - OffsetMap.Add(startPos + boundTexOffset, "Material Bound Textures: " + name); + // Texture Info + if (texInfoOffset != 0) + OffsetMap.Add(startPos + texInfoOffset, "Material Texture Info: " + name); - ins.Seek(startPos + boundTexOffset); - m.BoundTextures = new List<BoundTextureInfo>((int)boundTexCount); + ins.Seek(startPos + texInfoOffset); + m.TextureInfos = new List<TextureInfo>((int)texInfoCount); - for (int i = 0; i < boundTexCount; i++) { - //Debug.Send("Reading {0} bound texture at {1:X}", i, ins.Position); + for (int i = 0; i < texInfoCount; i++) { + //Debug.Send("Reading {0} texture info at {1:X}", i, ins.Position); - int bPos = ins.Position; - var bTex = new BoundTextureInfo(); - m.BoundTextures.Add(bTex); - - Int32 texOffs = ins.ReadInt32(); - bTex.TextureName = texOffs == 0 ? null : ins.At(bPos + texOffs - 4).ReadName(); - Int32 palOffs = ins.ReadInt32(); - bTex.PaletteName = palOffs == 0 ? null : ins.At(bPos + palOffs - 4).ReadName(); - - //Debug.Send("<{0}> <{1}>", bTex.TextureName, bTex.PaletteName); - - // placeholder pointers, don't need these - ins.Skip(8); - - bTex.TexMapID = ins.ReadUInt32(); - bTex.TlutID = ins.ReadUInt32(); - bTex.WrapS = ins.ReadUInt32(); - bTex.WrapT = ins.ReadUInt32(); - bTex.MinFilt = ins.ReadUInt32(); - bTex.MagFilt = ins.ReadUInt32(); - bTex.LODBias = ins.ReadFloat(); - bTex.MaxAniso = ins.ReadUInt32(); - bTex.BiasClamp = (ins.ReadByte() != 0); - bTex.DoEdgeLOD = (ins.ReadByte() != 0); - ins.Skip(2); + m.TextureInfos.Add(ReadTextureInfo(ins)); } // Display Lists @@ -553,6 +546,8 @@ namespace NW4RTools { m.IndMtxAndScaleDL = ins.ReadBytes(0x40); m.TexCoordGenDL = ins.ReadBytes(0xA0); + MaterialOffsetRefs[startPos] = m; + return m; } @@ -593,6 +588,132 @@ namespace NW4RTools { + private Models.Shape ConvertModelShape(string name, InputStream ins) { + var s = new Models.Shape(); + + int startPos = ins.Position; + OffsetMap.Add(ins.Position, "Shape: " + name); + + // 0x00 + UInt32 size = ins.ReadUInt32(); + // 0x04 + Int32 mdlOffset = ins.ReadInt32(); + + // 0x08 + s.MatrixID = ins.ReadInt32(); + // 0x0C + s.Unk = ins.ReadBytes(12); + + int dlBase1 = ins.Position; + // 0x18 + UInt32 bufferSize1 = ins.ReadUInt32(); + // 0x1C + UInt32 dataSize1 = ins.ReadUInt32(); + // 0x20 + Int32 offset1 = ins.ReadInt32(); + s.DisplayList1 = ins.At(dlBase1 + offset1).ReadBytes((int)dataSize1); + + int dlBase2 = ins.Position; + // 0x24 + UInt32 bufferSize2 = ins.ReadUInt32(); + // 0x28 + UInt32 dataSize2 = ins.ReadUInt32(); + // 0x2C + Int32 offset2 = ins.ReadInt32(); + s.DisplayList2 = ins.At(dlBase2 + offset2).ReadBytes((int)dataSize2); + + // 0x30 + s.DataFlags = ins.ReadUInt32(); + // 0x34 + s.Flags = ins.ReadUInt32(); + + // 0x38 + int nameOffset = ins.ReadInt32(); + // 0x3C + s.Index = ins.ReadUInt32(); + + // 0x40 + s.VertexCount = ins.ReadUInt32(); + // 0x44 + s.PolygonCount = ins.ReadUInt32(); + + // 0x48 + s.PosDataIndex = ins.ReadInt16(); + // 0x4A + s.NrmDataIndex = ins.ReadInt16(); + + // 0x4C + s.ClrDataIndex = new short[2]; + for (int i = 0; i < 2; i++) + s.ClrDataIndex[i] = ins.ReadInt16(); + + // 0x50 + s.TexCoordDataIndex = new short[8]; + for (int i = 0; i < 8; i++) + s.TexCoordDataIndex[i] = ins.ReadInt16(); + + // 0x60 + s.FurVecDataIndex = ins.ReadInt16(); + // 0x62 + s.FurPosDataIndex = ins.ReadInt16(); + + // 0x64 + Int32 extraDataOffset = ins.ReadInt32(); + + return s; + } + + + + public List<TexMatPairing> ConvertPairingList(string name, InputStream ins) { + var list = new List<TexMatPairing>(); + + int startPos = ins.Position; + OffsetMap.Add(ins.Position, "Texture/Material Pairing List for: " + name); + + UInt32 count = ins.ReadUInt32(); + for (int i = 0; i < count; i++) { + var p = new TexMatPairing(); + p.Material = MaterialOffsetRefs[startPos + ins.ReadInt32()]; + p.Texture = ReadTextureInfo(ins.At(startPos + ins.ReadInt32())); + list.Add(p); + } + + return list; + } + + + + public TextureInfo ReadTextureInfo(InputStream ins) { + int bPos = ins.Position; + var bTex = new Models.TextureInfo(); + + Int32 texOffs = ins.ReadInt32(); + bTex.TextureName = texOffs == 0 ? null : ins.At(bPos + texOffs - 4).ReadName(); + Int32 palOffs = ins.ReadInt32(); + bTex.PaletteName = palOffs == 0 ? null : ins.At(bPos + palOffs - 4).ReadName(); + + // placeholder pointers, don't need these + ins.Skip(8); + + bTex.TexMapID = ins.ReadUInt32(); + bTex.TlutID = ins.ReadUInt32(); + bTex.WrapS = ins.ReadUInt32(); + bTex.WrapT = ins.ReadUInt32(); + bTex.MinFilt = ins.ReadUInt32(); + bTex.MagFilt = ins.ReadUInt32(); + bTex.LODBias = ins.ReadFloat(); + bTex.MaxAniso = ins.ReadUInt32(); + bTex.BiasClamp = (ins.ReadByte() != 0); + bTex.DoEdgeLOD = (ins.ReadByte() != 0); + ins.Skip(2); + + return bTex; + } + + + + private RawStreamResDict ReadDict(InputStream ins) { RawStreamResDict output = new RawStreamResDict(); diff --git a/NW4RTools/Logger.cs b/NW4RTools/Logger.cs index d5a76f3..a6fbb70 100644 --- a/NW4RTools/Logger.cs +++ b/NW4RTools/Logger.cs @@ -5,9 +5,9 @@ using System.Text; namespace NW4RTools { public class LogContext : IDisposable { // An awful, awful hack - private readonly Logger Owner; + private readonly ILogger Owner; - public LogContext(Logger owner) { + public LogContext(ILogger owner) { Owner = owner; } @@ -16,13 +16,21 @@ namespace NW4RTools { } } - public class Logger { + public interface ILogger { + void Send(string format, params object[] args); + LogContext Push(string format, params object[] args); + void Pop(); + void Block(); + void Unblock(); + } + + public class ConsoleLogger : ILogger { private List<string> PrefixElements; private List<string> Names; private int BlockCount; private string Prefix; - public Logger() { + public ConsoleLogger() { Names = new List<string>(); Names.Add("root"); diff --git a/NW4RTools/Models/Material.cs b/NW4RTools/Models/Material.cs index 8ca5082..200c6e7 100644 --- a/NW4RTools/Models/Material.cs +++ b/NW4RTools/Models/Material.cs @@ -20,14 +20,6 @@ namespace NW4RTools.Models { public Matrix TexMatrix; } - public class BoundTextureInfo { - public string TextureName, PaletteName; - public UInt32 TexMapID, TlutID, WrapS, WrapT, MinFilt, MagFilt; - public float LODBias; - public UInt32 MaxAniso; - public bool BiasClamp, DoEdgeLOD; - } - public class Material { @@ -54,7 +46,7 @@ namespace NW4RTools.Models { public ChanCtrl[] ChanCtrls; // Other - public List<BoundTextureInfo> BoundTextures; + public List<TextureInfo> TextureInfos; public Shader ShaderRef; // Display Lists diff --git a/NW4RTools/Models/Model.cs b/NW4RTools/Models/Model.cs index d1c170b..c0297db 100644 --- a/NW4RTools/Models/Model.cs +++ b/NW4RTools/Models/Model.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; + namespace NW4RTools.Models { public class Model { public enum ScaleModeType { @@ -17,6 +19,9 @@ namespace NW4RTools.Models { public ResDict<VertexTexCoordData> VtxTexCoordData; public ResDict<Material> Materials; public ResDict<Shader> Shaders; + public ResDict<Shape> Shapes; + public ResDict<List<TexMatPairing>> PairingLookupByTexture; + public ResDict<List<TexMatPairing>> PairingLookupByPalette; /*public ResDict Bytecode, Nodes, VtxPosData, VtsNrmData, VtxClrData, VtxTexCoordData; public ResDict VtxFurVecData, VtxFurPosData, Materials, Shaders, Shapes; diff --git a/NW4RTools/Models/Shape.cs b/NW4RTools/Models/Shape.cs new file mode 100644 index 0000000..72cdbaa --- /dev/null +++ b/NW4RTools/Models/Shape.cs @@ -0,0 +1,29 @@ +using System; + +namespace NW4RTools.Models { + public class Shape { + public Int32 MatrixID; + public byte[] Unk; + public byte[] DisplayList1, DisplayList2; + public UInt32 DataFlags; + public UInt32 Flags; + public UInt32 Index; + + public UInt32 VertexCount; + public UInt32 PolygonCount; + + // todo: use refs for this + public Int16 PosDataIndex; + public Int16 NrmDataIndex; + public Int16[] ClrDataIndex; + public Int16[] TexCoordDataIndex; + public Int16 FurVecDataIndex; + public Int16 FurPosDataIndex; + + // todo: more + + public Shape() { + } + } +} + diff --git a/NW4RTools/Models/TextureInfo.cs b/NW4RTools/Models/TextureInfo.cs new file mode 100644 index 0000000..c8bc9a4 --- /dev/null +++ b/NW4RTools/Models/TextureInfo.cs @@ -0,0 +1,21 @@ +using System; + +namespace NW4RTools.Models { + public class TextureInfo { + public string TextureName, PaletteName; + public UInt32 TexMapID, TlutID, WrapS, WrapT, MinFilt, MagFilt; + public float LODBias; + public UInt32 MaxAniso; + public bool BiasClamp, DoEdgeLOD; + + public TextureInfo() { + } + } + + + public class TexMatPairing { + public TextureInfo Texture; + public Material Material; + } +} + diff --git a/NW4RTools/NW4RTools.csproj b/NW4RTools/NW4RTools.csproj index 9050c94..71ad2ba 100644 --- a/NW4RTools/NW4RTools.csproj +++ b/NW4RTools/NW4RTools.csproj @@ -48,6 +48,8 @@ <Compile Include="Models\VertexData.cs" /> <Compile Include="Models\Material.cs" /> <Compile Include="Models\Shader.cs" /> + <Compile Include="Models\Shape.cs" /> + <Compile Include="Models\TextureInfo.cs" /> </ItemGroup> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <ItemGroup> diff --git a/NW4RTools/NW4RTools.pidb b/NW4RTools/NW4RTools.pidb Binary files differindex 5769072..4d58aae 100644 --- a/NW4RTools/NW4RTools.pidb +++ b/NW4RTools/NW4RTools.pidb diff --git a/NW4RTools/bin/Debug/NW4RTools.dll b/NW4RTools/bin/Debug/NW4RTools.dll Binary files differindex dc75f55..cb655f9 100755 --- a/NW4RTools/bin/Debug/NW4RTools.dll +++ b/NW4RTools/bin/Debug/NW4RTools.dll diff --git a/NW4RTools/bin/Debug/NW4RTools.dll.mdb b/NW4RTools/bin/Debug/NW4RTools.dll.mdb Binary files differindex 00c8b7b..06fe2a2 100644 --- a/NW4RTools/bin/Debug/NW4RTools.dll.mdb +++ b/NW4RTools/bin/Debug/NW4RTools.dll.mdb diff --git a/TestApp/Main.cs b/TestApp/Main.cs index 9fb3771..e6fa82f 100644 --- a/TestApp/Main.cs +++ b/TestApp/Main.cs @@ -5,7 +5,8 @@ namespace TestApp { class MainClass { public static void Main(string[] args) { //byte[] file = System.IO.File.ReadAllBytes("/home/me/Games/Newer/miscStuff/CS_W1/g3d/model.brres"); - byte[] file = System.IO.File.ReadAllBytes("/mnt/h/ISOs/NSMBWii/ais0.1.3/model.brres"); + //byte[] file = System.IO.File.ReadAllBytes("/mnt/h/ISOs/NSMBWii/ais0.1.3/model.brres"); + byte[] file = System.IO.File.ReadAllBytes("/home/me/Games/Newer/miscStuff/bgB_4502/g3d/bgB_4502.brres"); ResFile rf = BrresReader.LoadFile(file); } } diff --git a/TestApp/TestApp.pidb b/TestApp/TestApp.pidb Binary files differindex bdecde3..8cd730e 100644 --- a/TestApp/TestApp.pidb +++ b/TestApp/TestApp.pidb diff --git a/TestApp/bin/Debug/NW4RTools.dll b/TestApp/bin/Debug/NW4RTools.dll Binary files differindex dc75f55..cb655f9 100755 --- a/TestApp/bin/Debug/NW4RTools.dll +++ b/TestApp/bin/Debug/NW4RTools.dll diff --git a/TestApp/bin/Debug/NW4RTools.dll.mdb b/TestApp/bin/Debug/NW4RTools.dll.mdb Binary files differindex 00c8b7b..06fe2a2 100644 --- a/TestApp/bin/Debug/NW4RTools.dll.mdb +++ b/TestApp/bin/Debug/NW4RTools.dll.mdb diff --git a/TestApp/bin/Debug/TestApp.exe b/TestApp/bin/Debug/TestApp.exe Binary files differindex 76702c5..37f5a57 100755 --- a/TestApp/bin/Debug/TestApp.exe +++ b/TestApp/bin/Debug/TestApp.exe diff --git a/TestApp/bin/Debug/TestApp.exe.mdb b/TestApp/bin/Debug/TestApp.exe.mdb Binary files differindex 0d3722e..ed77ca1 100644 --- a/TestApp/bin/Debug/TestApp.exe.mdb +++ b/TestApp/bin/Debug/TestApp.exe.mdb |