diff options
author | Treeki <treeki@gmail.com> | 2011-03-16 05:14:49 +0100 |
---|---|---|
committer | Treeki <treeki@gmail.com> | 2011-03-16 05:14:49 +0100 |
commit | b625da59b0bbe60a9380dbd00df1ae982e6b5a58 (patch) | |
tree | e2622b0c2e235a68ac3c4f8913117e158d3335bf | |
parent | 51b0e39ceb5962ffd9b22f1b4722d2b243dba60f (diff) | |
download | nw4rtools-b625da59b0bbe60a9380dbd00df1ae982e6b5a58.tar.gz nw4rtools-b625da59b0bbe60a9380dbd00df1ae982e6b5a58.zip |
now uses 8-bit vertex data indexes where possible. quite a big optimisation!
-rw-r--r-- | NW4RTools.userprefs | 12 | ||||
-rw-r--r-- | NW4RTools/NW4RTools.pidb | bin | 583263 -> 583120 bytes | |||
-rwxr-xr-x | NW4RTools/ObjImporter.cs | 154 | ||||
-rwxr-xr-x | NW4RTools/bin/Debug/NW4RTools.dll | bin | 236544 -> 237568 bytes | |||
-rw-r--r-- | NW4RTools/bin/Debug/NW4RTools.dll.mdb | bin | 114305 -> 114410 bytes | |||
-rw-r--r-- | TestApp/Main.cs | 18 | ||||
-rw-r--r-- | TestApp/RenderWindow.cs | 4 | ||||
-rw-r--r-- | TestApp/TestApp.pidb | bin | 4427 -> 4427 bytes | |||
-rwxr-xr-x | TestApp/bin/Debug/NW4RTools.dll | bin | 236544 -> 237568 bytes | |||
-rw-r--r-- | TestApp/bin/Debug/NW4RTools.dll.mdb | bin | 114305 -> 114410 bytes | |||
-rwxr-xr-x | TestApp/bin/Debug/TestApp.exe | bin | 7680 -> 7680 bytes | |||
-rw-r--r-- | 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 @@ <MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" /> <MonoDevelop.Ide.Workbench ActiveDocument="NW4RTools/ObjImporter.cs"> <Files> - <File FileName="NW4RTools/ObjImporter.cs" Line="97" Column="13" /> + <File FileName="NW4RTools/ObjImporter.cs" Line="858" Column="20" /> <File FileName="NW4RTools/ObjExporter.cs" Line="105" Column="25" /> - <File FileName="NW4RTools/BrresReader.cs" Line="182" Column="1" /> - <File FileName="NW4RTools/BrresWriter.cs" Line="1327" Column="9" /> + <File FileName="NW4RTools/BrresReader.cs" Line="516" Column="1" /> + <File FileName="NW4RTools/BrresWriter.cs" Line="1262" Column="1" /> <File FileName="NW4RTools/Models/Model.cs" Line="1" Column="1" /> <File FileName="NW4RTools/Models/ByteCode.cs" Line="1" Column="1" /> - <File FileName="TestApp/Main.cs" Line="25" Column="13" /> + <File FileName="TestApp/Main.cs" Line="28" Column="64" /> <File FileName="NW4RTools/Types.cs" Line="1" Column="1" /> <File FileName="NW4RTools/Models/Material.cs" Line="70" Column="1" /> <File FileName="NW4RTools/VertexSettings.cs" Line="184" Column="46" /> <File FileName="NW4RTools/Models/VertexData.cs" Line="238" Column="31" /> <File FileName="NW4RTools/Models/OpenGL/GLModel.cs" Line="218" Column="9" /> <File FileName="NW4RTools/InputStream.cs" Line="86" Column="1" /> - <File FileName="TestApp/RenderWindow.cs" Line="65" Column="57" /> + <File FileName="TestApp/RenderWindow.cs" Line="64" Column="52" /> <File FileName="NW4RTools/Models/OpenGL/GLDisplayList.cs" Line="20" Column="1" /> <File FileName="NW4RTools/Enums.cs" Line="48" Column="2" /> <File FileName="NW4RTools/DisplayListWriter.cs" Line="76" Column="21" /> @@ -26,7 +26,7 @@ </MonoDevelop.Ide.Workbench> <MonoDevelop.Ide.DebuggingService.Breakpoints> <BreakpointStore> - <Breakpoint file="/home/me/Dev/NW4RTools/NW4RTools/BrresWriter.cs" line="1262" /> + <Breakpoint file="/home/me/Dev/NW4RTools/NW4RTools/BrresReader.cs" line="558" /> </BreakpointStore> </MonoDevelop.Ide.DebuggingService.Breakpoints> <MonoDevelop.Ide.DebuggingService.PinnedWatches /> diff --git a/NW4RTools/NW4RTools.pidb b/NW4RTools/NW4RTools.pidb Binary files differindex ccf0cfb..9cf16d4 100644 --- a/NW4RTools/NW4RTools.pidb +++ b/NW4RTools/NW4RTools.pidb 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 Binary files differindex 6f24c8e..6cd7365 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 0dab4c1..208c344 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 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 Binary files differindex 4763179..af45428 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 6f24c8e..6cd7365 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 0dab4c1..208c344 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 48903c9..ee143e2 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 f2f707d..b46c061 100644 --- a/TestApp/bin/Debug/TestApp.exe.mdb +++ b/TestApp/bin/Debug/TestApp.exe.mdb |