summaryrefslogtreecommitdiff
path: root/NW4RTools/BrresReader.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--NW4RTools/BrresReader.cs203
1 files changed, 162 insertions, 41 deletions
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();