From 58f30cf003a6b7b3686f70fa0539aa45d126b457 Mon Sep 17 00:00:00 2001
From: Treeki <treeki@gmail.com>
Date: Sun, 6 Feb 2011 04:02:38 +0100
Subject: shader reading support

---
 NW4RTools/BrresReader.cs              |  33 ++++++++++++++++++++++++++-------
 NW4RTools/Models/Material.cs          |   1 +
 NW4RTools/Models/Shader.cs            |   5 +++++
 NW4RTools/bin/Debug/NW4RTools.dll     | Bin 28672 -> 29184 bytes
 NW4RTools/bin/Debug/NW4RTools.dll.mdb | Bin 10087 -> 10124 bytes
 TestApp/bin/Debug/NW4RTools.dll       | Bin 28672 -> 29184 bytes
 TestApp/bin/Debug/NW4RTools.dll.mdb   | Bin 10087 -> 10124 bytes
 TestApp/bin/Debug/TestApp.exe         | Bin 3584 -> 3584 bytes
 TestApp/bin/Debug/TestApp.exe.mdb     | Bin 416 -> 416 bytes
 9 files changed, 32 insertions(+), 7 deletions(-)

diff --git a/NW4RTools/BrresReader.cs b/NW4RTools/BrresReader.cs
index 36634d0..cd9452d 100644
--- a/NW4RTools/BrresReader.cs
+++ b/NW4RTools/BrresReader.cs
@@ -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"))
@@ -155,15 +155,18 @@ namespace NW4RTools {
 					mdl.VtxClrData = ReadAndConvertDict<VertexClrData>(ins.At(startPos + vtxClrOffset), ConvertVtxClrData);
 				using (var c2 = Debug.Push("Vertex TexCoord Data"))
 					mdl.VtxTexCoordData = ReadAndConvertDict<VertexTexCoordData>(ins.At(startPos + texCoordOffset), ConvertVtxTexCoordData);
-				*/
+				//*/
 
-				// Load materials
-		using (var c2 = Debug.Push("Materials"))
-					mdl.Materials = ReadAndConvertDict<Material>(ins.At(startPos + materialOffset), ConvertModelMaterial);
+				// 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>();
 
 				using (var c2 = Debug.Push("Shaders"))
 					mdl.Shaders = ReadAndConvertDict<Shader>(ins.At(startPos + shaderOffset), ConvertModelShader);
 
+				using (var c2 = Debug.Push("Materials"))
+					mdl.Materials = ReadAndConvertDict<Material>(ins.At(startPos + materialOffset), ConvertModelMaterial);
+
 				return mdl;
 			}
 		}
@@ -412,9 +415,8 @@ namespace NW4RTools {
 			ins.Skip(1);
 
 			// Other stuff
-			// Shader offset points to an entry in the Shaders group, must fix this up later
-			// like I did with Node classes
 			Int32 shaderOffset = ins.ReadInt32();
+			m.ShaderRef = ShaderOffsetRefs[startPos + shaderOffset];
 			//Debug.Send("Shader offset for {0}: {1:X} [dest {2:X}]", name, shaderOffset, startPos + shaderOffset);
 
 			UInt32 boundTexCount = ins.ReadUInt32();
@@ -556,6 +558,8 @@ namespace NW4RTools {
 
 
 
+		private Dictionary<int, Models.Shader> ShaderOffsetRefs;
+
 		private Models.Shader ConvertModelShader(string name, InputStream ins) {
 			var s = new Models.Shader();
 
@@ -569,6 +573,21 @@ namespace NW4RTools {
 			// if I'm making sense or not. Too tired to look it up, it's 4:51am...
 			OffsetMap[ins.Position] = "Shader: " + name;
 
+			UInt32 size = ins.ReadUInt32();
+			Int32 mdlOffset = ins.ReadInt32();
+
+			s.Index = ins.ReadUInt32();
+			s.TevStageCount = ins.ReadByte();
+			ins.Skip(3);
+
+			s.Unk1 = ins.ReadUInt32();
+			s.Unk2 = ins.ReadUInt32();
+			ins.Skip(8);
+
+			s.DisplayList = ins.ReadBytes(0x1E0);
+
+			ShaderOffsetRefs[startPos] = s;
+
 			return s;
 		}
 
diff --git a/NW4RTools/Models/Material.cs b/NW4RTools/Models/Material.cs
index 8ece6ad..8ca5082 100644
--- a/NW4RTools/Models/Material.cs
+++ b/NW4RTools/Models/Material.cs
@@ -55,6 +55,7 @@ namespace NW4RTools.Models {
 
 		// Other
 		public List<BoundTextureInfo> BoundTextures;
+		public Shader ShaderRef;
 
 		// Display Lists
 		public byte[] PixDL;
diff --git a/NW4RTools/Models/Shader.cs b/NW4RTools/Models/Shader.cs
index 42586da..767470e 100644
--- a/NW4RTools/Models/Shader.cs
+++ b/NW4RTools/Models/Shader.cs
@@ -3,6 +3,11 @@ using System.Collections.Generic;
 
 namespace NW4RTools.Models {
 	public class Shader {
+		public UInt32 Index;
+		public byte TevStageCount;
+		public UInt32 Unk1, Unk2; // TODO: figure these out
+		public byte[] DisplayList;
+
 		public Shader() {
 		}
 	}
diff --git a/NW4RTools/bin/Debug/NW4RTools.dll b/NW4RTools/bin/Debug/NW4RTools.dll
index fcfb2a6..dc75f55 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 7d4ac90..00c8b7b 100644
Binary files a/NW4RTools/bin/Debug/NW4RTools.dll.mdb and b/NW4RTools/bin/Debug/NW4RTools.dll.mdb differ
diff --git a/TestApp/bin/Debug/NW4RTools.dll b/TestApp/bin/Debug/NW4RTools.dll
index fcfb2a6..dc75f55 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 7d4ac90..00c8b7b 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 04c714d..76702c5 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 9bddbc7..0d3722e 100644
Binary files a/TestApp/bin/Debug/TestApp.exe.mdb and b/TestApp/bin/Debug/TestApp.exe.mdb differ
-- 
cgit v1.2.3