diff options
author | Treeki <treeki@gmail.com> | 2011-02-13 16:22:11 +0100 |
---|---|---|
committer | Treeki <treeki@gmail.com> | 2011-02-13 16:22:11 +0100 |
commit | 7d7491feb41bc9724bf63bef545b996226406889 (patch) | |
tree | 21e4ca70d88a1fb8071d8882c3babc4f9fb6646f | |
parent | 426ad45876649595e2d4e9669ff1cf8c250fb163 (diff) | |
download | nw4rtools-7d7491feb41bc9724bf63bef545b996226406889.tar.gz nw4rtools-7d7491feb41bc9724bf63bef545b996226406889.zip |
really messy code for materials/textures which KINDA works
-rw-r--r-- | NW4RTools.userprefs | 20 | ||||
-rw-r--r-- | NW4RTools/ColladaWriter.cs | 122 | ||||
-rw-r--r-- | NW4RTools/NW4RTools.pidb | bin | 530650 -> 531637 bytes | |||
-rw-r--r-- | NW4RTools/Texture.cs | 18 | ||||
-rw-r--r-- | NW4RTools/Util/IOrderedDictionary.cs | 3 | ||||
-rw-r--r-- | NW4RTools/Util/OrderedDictionary.cs | 6 | ||||
-rwxr-xr-x | NW4RTools/bin/Debug/NW4RTools.dll | bin | 164864 -> 166400 bytes | |||
-rw-r--r-- | NW4RTools/bin/Debug/NW4RTools.dll.mdb | bin | 90336 -> 90788 bytes | |||
-rw-r--r-- | TestApp/Main.cs | 4 | ||||
-rw-r--r-- | TestApp/TestApp.pidb | bin | 3039 -> 2991 bytes | |||
-rwxr-xr-x | TestApp/bin/Debug/NW4RTools.dll | bin | 164864 -> 166400 bytes | |||
-rw-r--r-- | TestApp/bin/Debug/NW4RTools.dll.mdb | bin | 90336 -> 90788 bytes | |||
-rwxr-xr-x | TestApp/bin/Debug/TestApp.exe | bin | 4608 -> 4608 bytes | |||
-rw-r--r-- | TestApp/bin/Debug/TestApp.exe.mdb | bin | 548 -> 548 bytes |
14 files changed, 153 insertions, 20 deletions
diff --git a/NW4RTools.userprefs b/NW4RTools.userprefs index 895a11a..843535e 100644 --- a/NW4RTools.userprefs +++ b/NW4RTools.userprefs @@ -1,22 +1,14 @@ <Properties> <MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" /> - <MonoDevelop.Ide.Workbench ActiveDocument="NW4RTools/BrresReader.cs"> + <MonoDevelop.Ide.Workbench ActiveDocument="TestApp/Main.cs"> <Files> - <File FileName="NW4RTools/ObjWriter.cs" Line="164" Column="3" /> - <File FileName="TestApp/Main.cs" Line="10" Column="30" /> - <File FileName="NW4RTools/BrresReader.cs" Line="652" Column="66" /> - <File FileName="NW4RTools/Models/Shape.cs" Line="30" Column="1" /> - <File FileName="NW4RTools/Models/Model.cs" Line="33" Column="39" /> - <File FileName="NW4RTools/InputStream.cs" Line="1" Column="1" /> - <File FileName="NW4RTools/Models/ByteCode.cs" Line="1" Column="1" /> - <File FileName="NW4RTools/VertexSettings.cs" Line="102" Column="1" /> - <File FileName="NW4RTools/Logger.cs" Line="1" Column="1" /> - <File FileName="NW4RTools/Enums.cs" Line="1" Column="1" /> - <File FileName="NW4RTools/Models/VertexData.cs" Line="23" Column="6" /> + <File FileName="TestApp/Main.cs" Line="11" Column="27" /> + <File FileName="NW4RTools/BrresReader.cs" Line="266" Column="1" /> + <File FileName="NW4RTools/Enums.cs" Line="13" Column="1" /> <File FileName="NW4RTools/Models/Node.cs" Line="27" Column="1" /> <File FileName="NW4RTools/Models/Material.cs" Line="1" Column="1" /> - <File FileName="NW4RTools/ColladaWriter.cs" Line="184" Column="15" /> - <File FileName="NW4RTools/ResDict.cs" Line="1" Column="1" /> + <File FileName="NW4RTools/ColladaWriter.cs" Line="376" Column="37" /> + <File FileName="NW4RTools/Texture.cs" Line="263" Column="102" /> </Files> </MonoDevelop.Ide.Workbench> <MonoDevelop.Ide.DebuggingService.Breakpoints> diff --git a/NW4RTools/ColladaWriter.cs b/NW4RTools/ColladaWriter.cs index b7c039a..f0f5b19 100644 --- a/NW4RTools/ColladaWriter.cs +++ b/NW4RTools/ColladaWriter.cs @@ -69,7 +69,7 @@ namespace NW4RTools { img.name = "Texture-" + texName; img.id = img.name; - img.Item = texName + ".png"; + img.Item = "./images/" + texName + ".png"; ImageList.Add(img); } @@ -193,7 +193,7 @@ namespace NW4RTools { } // Apply shapes to nodes - foreach (var kv in CurrentModel.Shapes) { + /*foreach (var kv in CurrentModel.Shapes) { Shape shape = kv.Value; Node origNode = CurrentModel.Nodes[CurrentModel.MatrixIDtoNodeID[shape.MatrixID]]; node cNode = NodeDefs[origNode]; @@ -211,8 +211,107 @@ namespace NW4RTools { cNode.instance_geometry = geoArrayCopy; // TODO: Add material handling, I'll probably have to parse DrawOpa/DrawXlu for this... + }*/ + + // WARNING: THIS NEEDS REFACTORING + + int drawID = 0; + + foreach (var insn in CurrentModel.Bytecode["DrawOpa"].Instructions) { + if (insn is ByteCode.DrawShapeInstruction) { + var dsInsn = insn as ByteCode.DrawShapeInstruction; + + Shape shape = CurrentModel.Shapes[dsInsn.ShapeID]; + string shapeName = CurrentModel.Shapes.GetKeyForIndex(dsInsn.ShapeID); + + Node origNode = CurrentModel.Nodes[dsInsn.NodeID]; + node cNode = NodeDefs[origNode]; + + Material mat = CurrentModel.Materials[dsInsn.MaterialID]; + string matName = CurrentModel.Materials.GetKeyForIndex(dsInsn.MaterialID); + + var newGeoEntry = new instance_geometry(); + newGeoEntry.name = String.Format("DrawOpa{0}-{1}", drawID, shapeName); + newGeoEntry.url = String.Format("#{0}-lib", shapeName); + + // now add the material + var bindMaterial = newGeoEntry.bind_material = new bind_material(); + bindMaterial.technique_common = new instance_material[1]; + var matTechnique = bindMaterial.technique_common[0] = new instance_material(); + + // constant marker so that I don't have to set a unique material name in each primitive + // it doesn't matter, since each geometry instance only uses one material anyway + matTechnique.symbol = "NW4R_MATERIAL"; + matTechnique.target = "#Material-" + matName; + + matTechnique.bind_vertex_input = new instance_materialBind_vertex_input[1]; + matTechnique.bind_vertex_input[0] = new instance_materialBind_vertex_input(); + matTechnique.bind_vertex_input[0].semantic = "CHANNEL1"; + matTechnique.bind_vertex_input[0].input_semantic = "TEXCOORD"; + matTechnique.bind_vertex_input[0].input_set = 0; + + // ok, now add the instance_geometry into the node + instance_geometry[] geoArrayCopy = cNode.instance_geometry; + if (geoArrayCopy == null) + geoArrayCopy = new instance_geometry[1]; + else + Array.Resize<instance_geometry>(ref geoArrayCopy, geoArrayCopy.Length + 1); + geoArrayCopy[geoArrayCopy.Length - 1] = newGeoEntry; + cNode.instance_geometry = geoArrayCopy; + + drawID++; + } + } + + drawID = 0; + + foreach (var insn in CurrentModel.Bytecode["DrawXlu"].Instructions) { + if (insn is ByteCode.DrawShapeInstruction) { + var dsInsn = insn as ByteCode.DrawShapeInstruction; + + Shape shape = CurrentModel.Shapes[dsInsn.ShapeID]; + string shapeName = CurrentModel.Shapes.GetKeyForIndex(dsInsn.ShapeID); + + Node origNode = CurrentModel.Nodes[dsInsn.NodeID]; + node cNode = NodeDefs[origNode]; + + Material mat = CurrentModel.Materials[dsInsn.MaterialID]; + string matName = CurrentModel.Materials.GetKeyForIndex(dsInsn.MaterialID); + + var newGeoEntry = new instance_geometry(); + newGeoEntry.name = String.Format("DrawXlu{0}-{1}", drawID, shapeName); + newGeoEntry.url = String.Format("#{0}-lib", shapeName); + + // now add the material + var bindMaterial = newGeoEntry.bind_material = new bind_material(); + bindMaterial.technique_common = new instance_material[1]; + var matTechnique = bindMaterial.technique_common[0] = new instance_material(); + + // constant marker so that I don't have to set a unique material name in each primitive + // it doesn't matter, since each geometry instance only uses one material anyway + matTechnique.symbol = "NW4R_MATERIAL"; + matTechnique.target = "#Material-" + matName; + + matTechnique.bind_vertex_input = new instance_materialBind_vertex_input[1]; + matTechnique.bind_vertex_input[0] = new instance_materialBind_vertex_input(); + matTechnique.bind_vertex_input[0].semantic = "CHANNEL1"; + matTechnique.bind_vertex_input[0].input_semantic = "TEXCOORD"; + matTechnique.bind_vertex_input[0].input_set = 0; + + // ok, now add the instance_geometry into the node + instance_geometry[] geoArrayCopy = cNode.instance_geometry; + if (geoArrayCopy == null) + geoArrayCopy = new instance_geometry[1]; + else + Array.Resize<instance_geometry>(ref geoArrayCopy, geoArrayCopy.Length + 1); + geoArrayCopy[geoArrayCopy.Length - 1] = newGeoEntry; + cNode.instance_geometry = geoArrayCopy; + + drawID++; + } } + /*foreach (var kv in CurrentModel.Shapes) { var thisNode = new node(); @@ -277,10 +376,27 @@ namespace NW4RTools { sampler2d.source = "Surface-" + mat.TextureInfos[0].TextureName; + samplerParam.sid = "Sampler-" + mat.TextureInfos[0].TextureName; samplerParam.ItemElementName = ItemChoiceType.sampler2D; samplerParam.Item = sampler2d; profile.Items[1] = samplerParam; + // now make a technique + // should I really use blinn...? + profile.technique = new effectFx_profile_abstractProfile_COMMONTechnique(); + profile.technique.sid = "common"; + + var pShader = new effectFx_profile_abstractProfile_COMMONTechniquePhong(); + pShader.diffuse = new common_color_or_texture_type(); + + var diffuseTex = new common_color_or_texture_typeTexture(); + diffuseTex.texture = "Sampler-" + mat.TextureInfos[0].TextureName; + diffuseTex.texcoord = "CHANNEL1"; + + pShader.diffuse.Item = diffuseTex; + + profile.technique.Item = pShader; + return eff; } @@ -529,6 +645,7 @@ namespace NW4RTools { switch (prim) { case PrimitiveType.Triangles: var pTri = new triangles(); + pTri.material = "NW4R_MATERIAL"; pTri.count = (ulong)(vtxCount / 3); // should be 1? dunno pTri.input = inputArray; @@ -567,6 +684,7 @@ namespace NW4RTools { // If any tristrips were found, add them! if (triStrips.Count > 0) { var pTriStrips = new tristrips(); + pTriStrips.material = "NW4R_MATERIAL"; pTriStrips.input = inputArray; pTriStrips.count = (ulong)triStrips.Count; pTriStrips.p = triStrips.ToArray(); diff --git a/NW4RTools/NW4RTools.pidb b/NW4RTools/NW4RTools.pidb Binary files differindex 5cd6dc3..ff53d24 100644 --- a/NW4RTools/NW4RTools.pidb +++ b/NW4RTools/NW4RTools.pidb diff --git a/NW4RTools/Texture.cs b/NW4RTools/Texture.cs index 954852d..2de9ad8 100644 --- a/NW4RTools/Texture.cs +++ b/NW4RTools/Texture.cs @@ -137,6 +137,8 @@ namespace NW4RTools { case TextureFormat.RGB5A3: case TextureFormat.I4: case TextureFormat.I8: + case TextureFormat.IA4: + case TextureFormat.IA8: // I4 is stupid and breaks my parser. I'll keep some state here bool alreadyHaveI4Nybble = false; byte currentI4Byte = 0; @@ -194,6 +196,18 @@ namespace NW4RTools { } else if (format == TextureFormat.I8) { byte val = data.ReadByte(); *pPixel = 0xFF000000 | (uint)(val << 16) | (uint)(val << 8) | val; + + } else if (format == TextureFormat.IA4) { + byte val = data.ReadByte(); + uint i = (uint)(val >> 4); + uint a = (uint)((val & 0xF0) << 4); + *pPixel = (a << 24) | (i << 16) | (i << 8) | i; + + } else if (format == TextureFormat.IA8) { + byte i = data.ReadByte(); + byte a = data.ReadByte(); + *pPixel = (uint)(a << 24) | (uint)(i << 16) | (uint)(i << 8) | i; + } } } @@ -259,7 +273,9 @@ namespace NW4RTools { image.UnlockBits(bits); - + + image.RotateFlip(RotateFlipType.RotateNoneFlipY); // TODO: remove this, and fix texcoords properly + BaseImage = image; Format = format; } diff --git a/NW4RTools/Util/IOrderedDictionary.cs b/NW4RTools/Util/IOrderedDictionary.cs index a574c7c..6cf9b85 100644 --- a/NW4RTools/Util/IOrderedDictionary.cs +++ b/NW4RTools/Util/IOrderedDictionary.cs @@ -55,5 +55,8 @@ namespace NW4RTools.Util get;
set;
}
+
+
+ TKey GetKeyForIndex(int index);
}
}
diff --git a/NW4RTools/Util/OrderedDictionary.cs b/NW4RTools/Util/OrderedDictionary.cs index 3dd4fb4..98e523a 100644 --- a/NW4RTools/Util/OrderedDictionary.cs +++ b/NW4RTools/Util/OrderedDictionary.cs @@ -162,11 +162,15 @@ namespace NW4RTools.Util return List.GetEnumerator();
}
- IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey,TValue>>.GetEnumerator()
+ IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
{
return List.GetEnumerator();
}
+ public TKey GetKeyForIndex(int index) {
+ return List[index].Key;
+ }
+
/// <summary>
/// Inserts a new entry into the <see cref="OrderedDictionary{TKey,TValue}">OrderedDictionary<TKey,TValue></see> collection with the specified key and value at the specified index.
/// </summary>
diff --git a/NW4RTools/bin/Debug/NW4RTools.dll b/NW4RTools/bin/Debug/NW4RTools.dll Binary files differindex a87dfa6..91a4b75 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 4b18386..127ef0f 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 408c95f..3eb49a4 100644 --- a/TestApp/Main.cs +++ b/TestApp/Main.cs @@ -8,7 +8,7 @@ namespace TestApp { public static void Main(string[] args) { string mdlPath = "/home/me/Games/Newer/ModelRev/"; - string mdlName = "CS_W9"; + string mdlName = "CS_W1"; //string mdlName = "bgB_4502"; string whatever = (mdlName == "CS_W2" || mdlName == "CS_W3" || mdlName == "CS_W6") ? "a" : ""; @@ -19,7 +19,7 @@ namespace TestApp { var texs = rf.GetGroup<Texture>("Textures(NW4R)"); // wtf C#?! foreach (var kv in (IEnumerable<KeyValuePair<string,Texture>>)texs) { - kv.Value.BaseImage.Save(mdlPath + kv.Key + ".png"); + kv.Value.BaseImage.Save(mdlPath + "images/" + kv.Key + ".png"); } //var objFile = File.Open(mdlPath + mdlName + ".obj", FileMode.OpenOrCreate); diff --git a/TestApp/TestApp.pidb b/TestApp/TestApp.pidb Binary files differindex d59d1d6..bf2fb29 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 a87dfa6..91a4b75 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 4b18386..127ef0f 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 5026c79..1bce1bb 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 ff5ef5f..00eab08 100644 --- a/TestApp/bin/Debug/TestApp.exe.mdb +++ b/TestApp/bin/Debug/TestApp.exe.mdb |