From 58dead7b909861732011325f5d4844eae21a9bc2 Mon Sep 17 00:00:00 2001 From: Treeki Date: Thu, 10 Feb 2011 05:03:53 +0100 Subject: more stuff. added shapes and texture/material pairs and refactored Logger a bit --- NW4RTools.userprefs | 14 ++- NW4RTools/BrresReader.cs | 203 +++++++++++++++++++++++++++------- NW4RTools/Logger.cs | 16 ++- NW4RTools/Models/Material.cs | 10 +- NW4RTools/Models/Model.cs | 5 + NW4RTools/Models/Shape.cs | 29 +++++ NW4RTools/Models/TextureInfo.cs | 21 ++++ NW4RTools/NW4RTools.csproj | 2 + NW4RTools/NW4RTools.pidb | Bin 73876 -> 78667 bytes NW4RTools/bin/Debug/NW4RTools.dll | Bin 29184 -> 31744 bytes NW4RTools/bin/Debug/NW4RTools.dll.mdb | Bin 10124 -> 11043 bytes TestApp/Main.cs | 3 +- TestApp/TestApp.pidb | Bin 2991 -> 3039 bytes TestApp/bin/Debug/NW4RTools.dll | Bin 29184 -> 31744 bytes TestApp/bin/Debug/NW4RTools.dll.mdb | Bin 10124 -> 11043 bytes TestApp/bin/Debug/TestApp.exe | Bin 3584 -> 3584 bytes TestApp/bin/Debug/TestApp.exe.mdb | Bin 416 -> 416 bytes 17 files changed, 243 insertions(+), 60 deletions(-) create mode 100644 NW4RTools/Models/Shape.cs create mode 100644 NW4RTools/Models/TextureInfo.cs diff --git a/NW4RTools.userprefs b/NW4RTools.userprefs index bad4528..f6cd241 100644 --- a/NW4RTools.userprefs +++ b/NW4RTools.userprefs @@ -1,13 +1,17 @@  - + - - - + + + - + + + + + 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 OffsetMap; private BrresReader() { - Debug = new Logger(); + Debug = new ConsoleLogger(); OffsetMap = new SortedDictionary(); } @@ -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(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(); + MaterialOffsetRefs = new Dictionary(); using (var c2 = Debug.Push("Shaders")) mdl.Shaders = ReadAndConvertDict(ins.At(startPos + shaderOffset), ConvertModelShader); @@ -167,6 +168,20 @@ namespace NW4RTools { using (var c2 = Debug.Push("Materials")) mdl.Materials = ReadAndConvertDict(ins.At(startPos + materialOffset), ConvertModelMaterial); + using (var c2 = Debug.Push("Shapes")) + mdl.Shapes = ReadAndConvertDict(ins.At(startPos + shapeOffset), ConvertModelShape); + + using (var c2 = Debug.Push("Texture Lookup for Texture Info/Material Pairing")) + mdl.PairingLookupByTexture = ReadAndConvertDict>(ins.At(startPos + textureOffset), ConvertPairingList); + + if (paletteOffset != 0) { + using (var c2 = Debug.Push("Palette Lookup for Texture Info/Material Pairing")) + mdl.PairingLookupByPalette = ReadAndConvertDict>(ins.At(startPos + paletteOffset), ConvertPairingList); + } else { + mdl.PairingLookupByPalette = new ResDict>(); + Debug.Send("Palette Lookup for Texture Info/Material Pairing: none"); + } + return mdl; } } @@ -384,6 +399,8 @@ namespace NW4RTools { + private Dictionary 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((int)boundTexCount); + ins.Seek(startPos + texInfoOffset); + m.TextureInfos = new List((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 ConvertPairingList(string name, InputStream ins) { + var list = new List(); + + 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 PrefixElements; private List Names; private int BlockCount; private string Prefix; - public Logger() { + public ConsoleLogger() { Names = new List(); 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 BoundTextures; + public List 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 VtxTexCoordData; public ResDict Materials; public ResDict Shaders; + public ResDict Shapes; + public ResDict> PairingLookupByTexture; + public ResDict> 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 @@ + + diff --git a/NW4RTools/NW4RTools.pidb b/NW4RTools/NW4RTools.pidb index 5769072..4d58aae 100644 Binary files a/NW4RTools/NW4RTools.pidb and b/NW4RTools/NW4RTools.pidb differ diff --git a/NW4RTools/bin/Debug/NW4RTools.dll b/NW4RTools/bin/Debug/NW4RTools.dll index dc75f55..cb655f9 100755 Binary files a/NW4RTools/bin/Debug/NW4RTools.dll and b/NW4RTools/bin/Debug/NW4RTools.dll differ diff --git a/NW4RTools/bin/Debug/NW4RTools.dll.mdb b/NW4RTools/bin/Debug/NW4RTools.dll.mdb index 00c8b7b..06fe2a2 100644 Binary files a/NW4RTools/bin/Debug/NW4RTools.dll.mdb and b/NW4RTools/bin/Debug/NW4RTools.dll.mdb differ 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 index bdecde3..8cd730e 100644 Binary files a/TestApp/TestApp.pidb and b/TestApp/TestApp.pidb differ diff --git a/TestApp/bin/Debug/NW4RTools.dll b/TestApp/bin/Debug/NW4RTools.dll index dc75f55..cb655f9 100755 Binary files a/TestApp/bin/Debug/NW4RTools.dll and b/TestApp/bin/Debug/NW4RTools.dll differ diff --git a/TestApp/bin/Debug/NW4RTools.dll.mdb b/TestApp/bin/Debug/NW4RTools.dll.mdb index 00c8b7b..06fe2a2 100644 Binary files a/TestApp/bin/Debug/NW4RTools.dll.mdb and b/TestApp/bin/Debug/NW4RTools.dll.mdb differ diff --git a/TestApp/bin/Debug/TestApp.exe b/TestApp/bin/Debug/TestApp.exe index 76702c5..37f5a57 100755 Binary files a/TestApp/bin/Debug/TestApp.exe and b/TestApp/bin/Debug/TestApp.exe differ diff --git a/TestApp/bin/Debug/TestApp.exe.mdb b/TestApp/bin/Debug/TestApp.exe.mdb index 0d3722e..ed77ca1 100644 Binary files a/TestApp/bin/Debug/TestApp.exe.mdb and b/TestApp/bin/Debug/TestApp.exe.mdb differ -- cgit v1.2.3