From b625da59b0bbe60a9380dbd00df1ae982e6b5a58 Mon Sep 17 00:00:00 2001 From: Treeki Date: Wed, 16 Mar 2011 05:14:49 +0100 Subject: now uses 8-bit vertex data indexes where possible. quite a big optimisation! --- NW4RTools.userprefs | 12 +-- NW4RTools/NW4RTools.pidb | Bin 583263 -> 583120 bytes NW4RTools/ObjImporter.cs | 154 ++++++++++++++++++++++++++-------- NW4RTools/bin/Debug/NW4RTools.dll | Bin 236544 -> 237568 bytes NW4RTools/bin/Debug/NW4RTools.dll.mdb | Bin 114305 -> 114410 bytes TestApp/Main.cs | 18 +++- TestApp/RenderWindow.cs | 4 +- TestApp/TestApp.pidb | Bin 4427 -> 4427 bytes TestApp/bin/Debug/NW4RTools.dll | Bin 236544 -> 237568 bytes TestApp/bin/Debug/NW4RTools.dll.mdb | Bin 114305 -> 114410 bytes TestApp/bin/Debug/TestApp.exe | Bin 7680 -> 7680 bytes TestApp/bin/Debug/TestApp.exe.mdb | Bin 1246 -> 1300 bytes 12 files changed, 139 insertions(+), 49 deletions(-) diff --git a/NW4RTools.userprefs b/NW4RTools.userprefs index eef25ca..23a624e 100644 --- a/NW4RTools.userprefs +++ b/NW4RTools.userprefs @@ -2,20 +2,20 @@ - + - - + + - + - + @@ -26,7 +26,7 @@ - + diff --git a/NW4RTools/NW4RTools.pidb b/NW4RTools/NW4RTools.pidb index ccf0cfb..9cf16d4 100644 Binary files a/NW4RTools/NW4RTools.pidb and b/NW4RTools/NW4RTools.pidb differ diff --git a/NW4RTools/ObjImporter.cs b/NW4RTools/ObjImporter.cs index a1f04b7..5410227 100755 --- a/NW4RTools/ObjImporter.cs +++ b/NW4RTools/ObjImporter.cs @@ -652,12 +652,16 @@ namespace NW4RTools { int pos1 = s.IndexOf('/'); int pos2 = s.IndexOf('/', pos1 + 1); + // thanks for generating groups with no texcoords, Max + bool hasTexCoord = (pos1 != pos2 - 1); + int vnum = int.Parse(s.Substring(0, pos1)) - 1; - int tcnum = int.Parse(s.Substring(pos1 + 1, pos2 - pos1 - 1)) - 1; + int tcnum = hasTexCoord ? (int.Parse(s.Substring(pos1 + 1, pos2 - pos1 - 1)) - 1) : 0; int nnum = int.Parse(s.Substring(pos2 + 1)) - 1; UsedVertices[vnum] = true; - UsedTexCoords[tcnum] = true; + if (hasTexCoord) + UsedTexCoords[tcnum] = true; UsedNormals[nnum] = true; output[i * 3] = (ushort)vnum; @@ -675,6 +679,19 @@ namespace NW4RTools { private void EndShape() { // Let's assemble the display lists. + // First off, compute the vertex data arrays so we can decide on whether + // to use 8-bit or 16-bit indexes to vertex data + ushort[] posIndexes, texCoordIndexes, normalIndexes; + + var posDataArray = ComputeVertexDataArray(Positions, UsedVertices, out posIndexes); + var tcDataArray = ComputeVertexDataArray(TexCoords, UsedTexCoords, out texCoordIndexes); + var nrmDataArray = ComputeVertexDataArray(Normals, UsedNormals, out normalIndexes); + + bool u16PosIdx = (posDataArray.Length > 255); + bool u16TcIdx = (tcDataArray.Length > 255); + bool u16NrmIdx = (nrmDataArray.Length > 255); + + // ** Reverse Engineering DL 1 ** // 0x0A : CP command: Vertex Descriptor part 1 // 0x10 : CP command: Vertex Descriptor part 2 @@ -712,15 +729,15 @@ namespace NW4RTools { // Now create vertex settings var vs = new VertexSettings(); - vs.PositionDesc = VertexSettings.DescType.Index16; + vs.PositionDesc = u16PosIdx ? VertexSettings.DescType.Index16 : VertexSettings.DescType.Index8; vs.PositionFormat = VertexSettings.CompType.Float32; vs.PositionCount = VertexSettings.CompCount.Position3; - vs.TexCoordDesc[0] = VertexSettings.DescType.Index16; + vs.TexCoordDesc[0] = u16TcIdx ? VertexSettings.DescType.Index16 : VertexSettings.DescType.Index8; vs.TexCoordFormat[0] = VertexSettings.CompType.Float32; vs.TexCoordCount[0] = VertexSettings.CompCount.TexCoord2; - vs.NormalDesc = VertexSettings.DescType.Index16; + vs.NormalDesc = u16NrmIdx ? VertexSettings.DescType.Index16 : VertexSettings.DescType.Index8; vs.NormalFormat = VertexSettings.CompType.Float32; vs.NormalCount = VertexSettings.CompCount.Normal3; @@ -764,12 +781,7 @@ namespace NW4RTools { // Display List 2 is where all the primitive-related fun happens - // However, before we do that, let's compute the vertex data arrays - ushort[] posIndexes, texCoordIndexes, normalIndexes; - - var posDataArray = ComputeVertexDataArray(Positions, UsedVertices, out posIndexes); - var tcDataArray = ComputeVertexDataArray(TexCoords, UsedTexCoords, out texCoordIndexes); - var nrmDataArray = ComputeVertexDataArray(Normals, UsedNormals, out normalIndexes); + // However, before we do that, let's create the vertex data arrays // todo: better names var posData = new VertexPosData(); @@ -781,9 +793,11 @@ namespace NW4RTools { posData.Data = posDataArray; posData.Save(); + CurrentShape.PosData = posData; CurrentModel.VtxPosData.Add("P_" + CurrentModel.VtxPosData.Count.ToString(), posData); var tcData = new VertexTexCoordData(); + tcData.ComponentCount = VertexSettings.CompCount.TexCoord2; tcData.ComponentType = VertexSettings.CompType.Float32; tcData.EntryCount = (ushort)tcDataArray.Length; @@ -792,6 +806,7 @@ namespace NW4RTools { tcData.Data = tcDataArray; tcData.Save(); + CurrentShape.TexCoordData[0] = tcData; CurrentModel.VtxTexCoordData.Add("T_" + CurrentModel.VtxTexCoordData.Count.ToString(), tcData); var nrmData = new VertexNrmData(); @@ -803,11 +818,8 @@ namespace NW4RTools { nrmData.Data = nrmDataArray; nrmData.Save(); - CurrentModel.VtxNrmData.Add("N_" + CurrentModel.VtxNrmData.Count.ToString(), nrmData); - - CurrentShape.PosData = posData; - CurrentShape.TexCoordData[0] = tcData; CurrentShape.NrmData = nrmData; + CurrentModel.VtxNrmData.Add("N_" + CurrentModel.VtxNrmData.Count.ToString(), nrmData); // For lightmaps, before we get baked lighting working if (usingLightmaps) { @@ -815,7 +827,7 @@ namespace NW4RTools { clrData.ComponentCount = VertexSettings.CompCount.Color4; clrData.ComponentType = VertexSettings.CompType.UInt8; clrData.EntryCount = 1; - clrData.EntrySize = 1; + clrData.EntrySize = 4; clrData.Fraction = 0; clrData.RawData = new byte[] { 255, 255, 255, 255 }; @@ -836,21 +848,50 @@ namespace NW4RTools { dl.BeginPrimitives(PrimitiveType.Triangles, 0, (ushort)(Triangles.Count * 3)); foreach (var tri in Triangles) { - dl.WriteUInt16(posIndexes[tri[6]]); - dl.WriteUInt16(normalIndexes[tri[7]]); + if (u16PosIdx) + dl.WriteUInt16(posIndexes[tri[6]]); + else + dl.WriteByte((byte)posIndexes[tri[6]]); + if (u16NrmIdx) + dl.WriteUInt16(normalIndexes[tri[7]]); + else + dl.WriteByte((byte)normalIndexes[tri[7]]); if (usingLightmaps) dl.WriteByte(0); - dl.WriteUInt16(texCoordIndexes[tri[8]]); - dl.WriteUInt16(posIndexes[tri[3]]); - dl.WriteUInt16(normalIndexes[tri[4]]); + if (u16TcIdx) + dl.WriteUInt16(texCoordIndexes[tri[8]]); + else + dl.WriteByte((byte)texCoordIndexes[tri[8]]); + + if (u16PosIdx) + dl.WriteUInt16(posIndexes[tri[3]]); + else + dl.WriteByte((byte)posIndexes[tri[3]]); + if (u16NrmIdx) + dl.WriteUInt16(normalIndexes[tri[4]]); + else + dl.WriteByte((byte)normalIndexes[tri[4]]); if (usingLightmaps) dl.WriteByte(0); - dl.WriteUInt16(texCoordIndexes[tri[5]]); - dl.WriteUInt16(posIndexes[tri[0]]); - dl.WriteUInt16(normalIndexes[tri[1]]); + if (u16TcIdx) + dl.WriteUInt16(texCoordIndexes[tri[5]]); + else + dl.WriteByte((byte)texCoordIndexes[tri[5]]); + + if (u16PosIdx) + dl.WriteUInt16(posIndexes[tri[0]]); + else + dl.WriteByte((byte)posIndexes[tri[0]]); + if (u16NrmIdx) + dl.WriteUInt16(normalIndexes[tri[1]]); + else + dl.WriteByte((byte)normalIndexes[tri[1]]); if (usingLightmaps) dl.WriteByte(0); - dl.WriteUInt16(texCoordIndexes[tri[2]]); + if (u16TcIdx) + dl.WriteUInt16(texCoordIndexes[tri[2]]); + else + dl.WriteByte((byte)texCoordIndexes[tri[2]]); } } @@ -858,26 +899,65 @@ namespace NW4RTools { dl.BeginPrimitives(PrimitiveType.Quads, 0, (ushort)(Quads.Count * 4)); foreach (var quad in Quads) { - dl.WriteUInt16(posIndexes[quad[9]]); - dl.WriteUInt16(normalIndexes[quad[10]]); + if (u16PosIdx) + dl.WriteUInt16(posIndexes[quad[9]]); + else + dl.WriteByte((byte)posIndexes[quad[9]]); + if (u16NrmIdx) + dl.WriteUInt16(normalIndexes[quad[10]]); + else + dl.WriteByte((byte)normalIndexes[quad[10]]); if (usingLightmaps) dl.WriteByte(0); - dl.WriteUInt16(texCoordIndexes[quad[11]]); - dl.WriteUInt16(posIndexes[quad[6]]); - dl.WriteUInt16(normalIndexes[quad[7]]); + if (u16TcIdx) + dl.WriteUInt16(texCoordIndexes[quad[11]]); + else + dl.WriteByte((byte)texCoordIndexes[quad[11]]); + + if (u16PosIdx) + dl.WriteUInt16(posIndexes[quad[6]]); + else + dl.WriteByte((byte)posIndexes[quad[6]]); + if (u16NrmIdx) + dl.WriteUInt16(normalIndexes[quad[7]]); + else + dl.WriteByte((byte)normalIndexes[quad[7]]); if (usingLightmaps) dl.WriteByte(0); - dl.WriteUInt16(texCoordIndexes[quad[8]]); - dl.WriteUInt16(posIndexes[quad[3]]); - dl.WriteUInt16(normalIndexes[quad[4]]); + if (u16TcIdx) + dl.WriteUInt16(texCoordIndexes[quad[8]]); + else + dl.WriteByte((byte)texCoordIndexes[quad[8]]); + + if (u16PosIdx) + dl.WriteUInt16(posIndexes[quad[3]]); + else + dl.WriteByte((byte)posIndexes[quad[3]]); + if (u16NrmIdx) + dl.WriteUInt16(normalIndexes[quad[4]]); + else + dl.WriteByte((byte)normalIndexes[quad[4]]); if (usingLightmaps) dl.WriteByte(0); - dl.WriteUInt16(texCoordIndexes[quad[5]]); - dl.WriteUInt16(posIndexes[quad[0]]); - dl.WriteUInt16(normalIndexes[quad[1]]); + if (u16TcIdx) + dl.WriteUInt16(texCoordIndexes[quad[5]]); + else + dl.WriteByte((byte)texCoordIndexes[quad[5]]); + + if (u16PosIdx) + dl.WriteUInt16(posIndexes[quad[0]]); + else + dl.WriteByte((byte)posIndexes[quad[0]]); + if (u16NrmIdx) + dl.WriteUInt16(normalIndexes[quad[1]]); + else + dl.WriteByte((byte)normalIndexes[quad[1]]); if (usingLightmaps) dl.WriteByte(0); - dl.WriteUInt16(texCoordIndexes[quad[2]]); + if (u16TcIdx) + dl.WriteUInt16(texCoordIndexes[quad[2]]); + else + dl.WriteByte((byte)texCoordIndexes[quad[2]]); } } diff --git a/NW4RTools/bin/Debug/NW4RTools.dll b/NW4RTools/bin/Debug/NW4RTools.dll index 6f24c8e..6cd7365 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 0dab4c1..208c344 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 56991bd..71b577b 100644 --- a/TestApp/Main.cs +++ b/TestApp/Main.cs @@ -17,7 +17,7 @@ namespace TestApp { // Going to create a model! //string filename = "crapmap", resmdlname = "CrapMap"; - string filename = "SMGoldwood"; + /*string filename = "SMGoldwood"; ResFile rf = new ResFile(); @@ -25,15 +25,25 @@ namespace TestApp { ObjImporter.ImportModel(mdlPath, File.OpenText(mdlPath + "BrownTree.obj"), rf, "BrownTree", ObjImporter.LightmapType.MapObj); ObjImporter.ImportModel(mdlPath, File.OpenText(mdlPath + "RedTree.obj"), rf, "RedTree", ObjImporter.LightmapType.MapObj); - File.WriteAllBytes(mdlPath + "SMGoldwood.brres", BrresWriter.WriteFile(rf)); + File.WriteAllBytes(mdlPath + "SMGoldwood.brres", BrresWriter.WriteFile(rf));*/ - /*ResFile rf2 = BrresReader.LoadFile(File.ReadAllBytes(mdlPath + filename + ".brres")); + string filename = "MMFullWorld"; + string resmdlname = "WorldBase"; + + ResFile rf = new ResFile(); + + ObjImporter.ImportModel(mdlPath, File.OpenText(mdlPath + "fullworld-Z6-fail.obj"), rf, resmdlname, ObjImporter.LightmapType.Map); + + File.WriteAllBytes(mdlPath + filename + ".brres", BrresWriter.WriteFile(rf)); + + ResFile rf2 = BrresReader.LoadFile(File.ReadAllBytes(mdlPath + filename + ".brres")); using (var gw = new RenderWindow()) { gw.Title = filename; gw.SetModel(rf2, resmdlname); gw.Run(1, 1); - }*/ + } + //*/ diff --git a/TestApp/RenderWindow.cs b/TestApp/RenderWindow.cs index f314146..fed1b9a 100644 --- a/TestApp/RenderWindow.cs +++ b/TestApp/RenderWindow.cs @@ -61,8 +61,8 @@ namespace TestApp { GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); - //Matrix4 modelview = Matrix4.LookAt(new Vector3(1000, 400, 1000), new Vector3(1000, 0, 0), Vector3.UnitY); - Matrix4 modelview = Matrix4.LookAt(new Vector3(-50, 20, 50), new Vector3(0, 0, 0), Vector3.UnitY); + Matrix4 modelview = Matrix4.LookAt(new Vector3(0, 400, 1000), new Vector3(0, 0, 0), Vector3.UnitY); + //Matrix4 modelview = Matrix4.LookAt(new Vector3(-50, 20, 50), new Vector3(0, 0, 0), Vector3.UnitY); GL.MatrixMode(MatrixMode.Modelview); GL.LoadMatrix(ref modelview); diff --git a/TestApp/TestApp.pidb b/TestApp/TestApp.pidb index 4763179..af45428 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 6f24c8e..6cd7365 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 0dab4c1..208c344 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 48903c9..ee143e2 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 f2f707d..b46c061 100644 Binary files a/TestApp/bin/Debug/TestApp.exe.mdb and b/TestApp/bin/Debug/TestApp.exe.mdb differ -- cgit v1.2.3