コード例 #1
0
ファイル: KCLWriter.cs プロジェクト: RicoPlays/sm64dse
        private static void write_kcl(NitroFile kcl, List <Triangle> triangles, int max_triangles, int min_width, float scale)
        {
            kcl.Clear();

            //Need to scale each vertex (1000 times scale of model)
            foreach (Triangle t in triangles)
            {
                t.u = t.u.mul(scale);
                t.v = t.v.mul(scale);
                t.w = t.w.mul(scale);
            }

            uint pos = 0;

            List <Face>  faces         = new List <Face>();
            VertexWelder vertex_welder = new VertexWelder(1 / 64f, (int)(Math.Floor((double)(triangles.Count / 256))));
            VertexWelder normal_welder = new VertexWelder(1 / 1024f, (int)(Math.Floor((double)(4 * triangles.Count / 256))));

            foreach (Triangle t in triangles)
            {
                Face f = new Face();

                Vector a = unit(cross(t.u.sub(t.w), t.n));
                Vector b = unit(cross(t.v.sub(t.u), t.n));
                Vector c = unit(cross(t.w.sub(t.v), t.n));

                f.length       = new FixedPoint(dot(t.v.sub(t.u), c), 1 / 65536f);
                f.vertex_index = (ushort)vertex_welder.add(t.u);
                f.normal_index = (ushort)normal_welder.add(t.n);
                f.a_index      = (ushort)normal_welder.add(a);
                f.b_index      = (ushort)normal_welder.add(b);
                f.c_index      = (ushort)normal_welder.add(c);
                f.group        = (ushort)t.group;

                faces.Add(f);
            }

            //Vertex Section
            pos += 56;
            kcl.Write32(0x00, pos);//Vertex section offset

            foreach (Vertex vtx in vertex_welder.vertices)
            {
                kcl.Write32(pos, (uint)vtx.x.valToWrite());
                kcl.Write32(pos + 4, (uint)vtx.y.valToWrite());
                kcl.Write32(pos + 8, (uint)vtx.z.valToWrite());

                pos += 12;
            }

            //Vector Section
            kcl.Write32(0x04, pos);//Vector section offset

            foreach (Vector vct in normal_welder.vectors)
            {
                kcl.Write16(pos, (ushort)vct.x.valToWrite());
                kcl.Write16(pos + 2, (ushort)vct.y.valToWrite());
                kcl.Write16(pos + 4, (ushort)vct.z.valToWrite());

                pos += 6;
            }

            pos = (uint)((pos + 3) & ~3);

            //Planes Section
            kcl.Write32(0x08, pos - 0x10);//Planes section offset

            foreach (Face f in faces)
            {
                kcl.Write32(pos, (uint)f.length.valToWrite());
                kcl.Write16(pos + 4, (ushort)f.vertex_index);
                kcl.Write16(pos + 6, (ushort)f.normal_index);
                kcl.Write16(pos + 8, (ushort)f.a_index);
                kcl.Write16(pos + 10, (ushort)f.b_index);
                kcl.Write16(pos + 12, (ushort)f.c_index);
                kcl.Write16(pos + 14, (ushort)f.group);

                pos += 16;
            }


            //Octree Section
            kcl.Write32(0x0C, pos);//Octree offset

            Octree octree = new Octree(triangles, max_triangles, (float)min_width);

            octree.pack(ref kcl, ref pos);

            //Header
            kcl.Write32(0x10, (uint)327680);//Unknown
            kcl.Write32(0x14, (uint)octree.bas.x.valToWrite());
            kcl.Write32(0x18, (uint)octree.bas.y.valToWrite());
            kcl.Write32(0x1C, (uint)octree.bas.z.valToWrite());
            kcl.Write32(0x20, (uint)(~((int)octree.width_x - 1) & 0xFFFFFFFF));
            kcl.Write32(0x24, (uint)(~((int)octree.width_y - 1) & 0xFFFFFFFF));
            kcl.Write32(0x28, (uint)(~((int)octree.width_z - 1) & 0xFFFFFFFF));
            kcl.Write32(0x2C, (uint)(Math.Log(octree.base_width, 2)));
            kcl.Write32(0x30, (uint)(Math.Log(octree.nx, 2)));
            kcl.Write32(0x34, (uint)(Math.Log(octree.nx, 2)) + (uint)(Math.Log(octree.ny, 2)));

            kcl.SaveChanges();
        }//End writeKCL
コード例 #2
0
ファイル: BCAWriter.cs プロジェクト: awiebe/SM64DSe
        public override void WriteModel(bool save = true)
        {
            ModelBase.BoneDefRoot boneTree = m_Model.m_BoneTree;
            Dictionary <string, ModelBase.AnimationDef> boneAnimationsMap = new Dictionary <string, ModelBase.AnimationDef>();

            foreach (ModelBase.AnimationDef anim in m_Model.m_Animations.Values)
            {
                boneAnimationsMap.Add(anim.m_BoneID, anim);
            }

            if (boneAnimationsMap.Count == 0)
            {
                return;
            }

            NitroFile bca = m_ModelFile;

            bca.Clear();

            uint dataoffset    = 0x00;
            uint headersize    = 0x18;
            int  numAnimations = boneTree.Count;
            int  numFrames     = boneAnimationsMap.ElementAt(0).Value.m_NumFrames;

            int numScale = 0;

            foreach (ModelBase.BoneDef boneDef in m_Model.m_BoneTree)
            {
                if (boneAnimationsMap.ContainsKey(boneDef.m_ID))
                {
                    numScale += boneAnimationsMap[boneDef.m_ID].GetScaleValuesCount();
                }
                else
                {
                    numScale += 3;// Original X, Y, Z
                }
            }
            int numRotation = 0;

            foreach (ModelBase.BoneDef boneDef in m_Model.m_BoneTree)
            {
                if (boneAnimationsMap.ContainsKey(boneDef.m_ID))
                {
                    numRotation += boneAnimationsMap[boneDef.m_ID].GetRotateValuesCount();
                }
                else
                {
                    numRotation += 3;// Original X, Y, Z
                }
            }
            int numTranslation = 0;

            foreach (ModelBase.BoneDef boneDef in m_Model.m_BoneTree)
            {
                if (boneAnimationsMap.ContainsKey(boneDef.m_ID))
                {
                    numTranslation += boneAnimationsMap[boneDef.m_ID].GetTranslateValuesCount();
                }
                else
                {
                    numTranslation += 3;// Original X, Y, Z
                }
            }
            uint scaleValuesOffset       = headersize;
            uint rotationValuesOffset    = scaleValuesOffset + (uint)(numScale * 4);
            uint translationValuesOffset = (uint)(((rotationValuesOffset + (uint)(numRotation * 2)) + 3) & ~3);
            uint animationDataOffset     = translationValuesOffset + (uint)(numTranslation * 4);

            bca.Write16(0x00, (ushort)numAnimations);   // Number of bones to be handled (should match the number of bones in the BMD)
            bca.Write16(0x02, (ushort)numFrames);       // Number of animation frames
            bca.Write32(0x04, 1);                       // Whether the animation loops, 0 - false, 1 - true
            bca.Write32(0x08, scaleValuesOffset);       // Offset to scale values section
            bca.Write32(0x0C, rotationValuesOffset);    // Offset to rotation values section
            bca.Write32(0x10, translationValuesOffset); // Offset to translation values section
            bca.Write32(0x14, animationDataOffset);     // Offset to animation section

            dataoffset = scaleValuesOffset;

            int boneScaleValuesOffset = 0;
            Dictionary <string, int> boneScaleValuesOffsets = new Dictionary <string, int>();
            int boneRotateValuesOffset = 0;
            Dictionary <string, int> boneRotateValuesOffsets = new Dictionary <string, int>();
            int boneTranslateValuesOffset = 0;
            Dictionary <string, int> boneTranslateValuesOffsets = new Dictionary <string, int>();

            foreach (ModelBase.BoneDef bone in boneTree)
            {
                boneScaleValuesOffsets.Add(bone.m_ID, boneScaleValuesOffset);

                if (boneAnimationsMap.ContainsKey(bone.m_ID))
                {
                    ModelBase.AnimationDef anim = boneAnimationsMap[bone.m_ID];

                    bca.WriteBlock(dataoffset,
                                   anim.m_AnimationComponents[ModelBase.AnimationComponentType.ScaleX].GetFixedPointValues());
                    dataoffset += (uint)(anim.m_AnimationComponents[ModelBase.AnimationComponentType.ScaleX].GetNumValues() * 4);

                    bca.WriteBlock(dataoffset,
                                   anim.m_AnimationComponents[ModelBase.AnimationComponentType.ScaleY].GetFixedPointValues());
                    dataoffset += (uint)(anim.m_AnimationComponents[ModelBase.AnimationComponentType.ScaleY].GetNumValues() * 4);

                    bca.WriteBlock(dataoffset,
                                   anim.m_AnimationComponents[ModelBase.AnimationComponentType.ScaleZ].GetFixedPointValues());
                    dataoffset += (uint)(anim.m_AnimationComponents[ModelBase.AnimationComponentType.ScaleZ].GetNumValues() * 4);

                    boneScaleValuesOffset += anim.GetScaleValuesCount();
                }
                else
                {
                    // Write bone's scale values
                    bca.Write32(dataoffset, bone.m_20_12Scale[0]);
                    dataoffset += 4;
                    bca.Write32(dataoffset, bone.m_20_12Scale[1]);
                    dataoffset += 4;
                    bca.Write32(dataoffset, bone.m_20_12Scale[2]);
                    dataoffset += 4;

                    boneScaleValuesOffset += 3;
                }
            }

            dataoffset = rotationValuesOffset;

            // Rotation is in Radians
            foreach (ModelBase.BoneDef bone in boneTree)
            {
                boneRotateValuesOffsets.Add(bone.m_ID, boneRotateValuesOffset);

                if (boneAnimationsMap.ContainsKey(bone.m_ID))
                {
                    ModelBase.AnimationDef anim = boneAnimationsMap[bone.m_ID];

                    bca.WriteBlock(dataoffset,
                                   anim.m_AnimationComponents[ModelBase.AnimationComponentType.RotateX].GetFixedPointValues());
                    dataoffset += (uint)(anim.m_AnimationComponents[ModelBase.AnimationComponentType.RotateX].GetNumValues() * 2);

                    bca.WriteBlock(dataoffset,
                                   anim.m_AnimationComponents[ModelBase.AnimationComponentType.RotateY].GetFixedPointValues());
                    dataoffset += (uint)(anim.m_AnimationComponents[ModelBase.AnimationComponentType.RotateY].GetNumValues() * 2);

                    bca.WriteBlock(dataoffset,
                                   anim.m_AnimationComponents[ModelBase.AnimationComponentType.RotateZ].GetFixedPointValues());
                    dataoffset += (uint)(anim.m_AnimationComponents[ModelBase.AnimationComponentType.RotateZ].GetNumValues() * 2);

                    boneRotateValuesOffset += anim.GetRotateValuesCount();
                }
                else
                {
                    // Write bone's rotation values
                    bca.Write16(dataoffset, bone.m_4_12Rotation[0]);
                    dataoffset += 2;
                    bca.Write16(dataoffset, bone.m_4_12Rotation[1]);
                    dataoffset += 2;
                    bca.Write16(dataoffset, bone.m_4_12Rotation[2]);
                    dataoffset += 2;

                    boneRotateValuesOffset += 3;
                }
            }

            dataoffset = translationValuesOffset;

            foreach (ModelBase.BoneDef bone in boneTree)
            {
                boneTranslateValuesOffsets.Add(bone.m_ID, boneTranslateValuesOffset);

                if (boneAnimationsMap.ContainsKey(bone.m_ID))
                {
                    ModelBase.AnimationDef anim = boneAnimationsMap[bone.m_ID];

                    bca.WriteBlock(dataoffset,
                                   anim.m_AnimationComponents[ModelBase.AnimationComponentType.TranslateX].GetFixedPointValues());
                    dataoffset += (uint)(anim.m_AnimationComponents[ModelBase.AnimationComponentType.TranslateX].GetNumValues() * 4);

                    bca.WriteBlock(dataoffset,
                                   anim.m_AnimationComponents[ModelBase.AnimationComponentType.TranslateY].GetFixedPointValues());
                    dataoffset += (uint)(anim.m_AnimationComponents[ModelBase.AnimationComponentType.TranslateY].GetNumValues() * 4);

                    bca.WriteBlock(dataoffset,
                                   anim.m_AnimationComponents[ModelBase.AnimationComponentType.TranslateZ].GetFixedPointValues());
                    dataoffset += (uint)(anim.m_AnimationComponents[ModelBase.AnimationComponentType.TranslateZ].GetNumValues() * 4);

                    boneTranslateValuesOffset += anim.GetTranslateValuesCount();
                }
                else
                {
                    // Write bone's translation values
                    bca.Write32(dataoffset, bone.m_20_12Translation[0]);
                    dataoffset += 4;
                    bca.Write32(dataoffset, bone.m_20_12Translation[1]);
                    dataoffset += 4;
                    bca.Write32(dataoffset, bone.m_20_12Translation[2]);
                    dataoffset += 4;

                    boneTranslateValuesOffset += 3;
                }
            }

            dataoffset = animationDataOffset;

            // For each bone, write the animation descriptor for each transformation component
            foreach (ModelBase.BoneDef bone in boneTree)
            {
                if (boneAnimationsMap.ContainsKey(bone.m_ID))
                {
                    ModelBase.AnimationDef anim = boneAnimationsMap[bone.m_ID];

                    // Scale X
                    WriteBCAAnimationDescriptor(bca, dataoffset,
                                                (byte)(anim.m_AnimationComponents[ModelBase.AnimationComponentType.ScaleX].GetFrameStep() >> 1),
                                                anim.m_AnimationComponents[ModelBase.AnimationComponentType.ScaleX].GetIsConstant(),
                                                boneScaleValuesOffsets[bone.m_ID]);
                    dataoffset += 4;

                    // Scale Y
                    WriteBCAAnimationDescriptor(bca, dataoffset,
                                                (byte)(anim.m_AnimationComponents[ModelBase.AnimationComponentType.ScaleY].GetFrameStep() >> 1),
                                                anim.m_AnimationComponents[ModelBase.AnimationComponentType.ScaleY].GetIsConstant(),
                                                (boneScaleValuesOffsets[bone.m_ID] +
                                                 anim.m_AnimationComponents[ModelBase.AnimationComponentType.ScaleX].GetNumValues()));
                    dataoffset += 4;

                    // Scale Z
                    WriteBCAAnimationDescriptor(bca, dataoffset,
                                                (byte)(anim.m_AnimationComponents[ModelBase.AnimationComponentType.ScaleZ].GetFrameStep() >> 1),
                                                anim.m_AnimationComponents[ModelBase.AnimationComponentType.ScaleZ].GetIsConstant(),
                                                (boneScaleValuesOffsets[bone.m_ID] +
                                                 anim.m_AnimationComponents[ModelBase.AnimationComponentType.ScaleX].GetNumValues() +
                                                 anim.m_AnimationComponents[ModelBase.AnimationComponentType.ScaleY].GetNumValues()));
                    dataoffset += 4;

                    // Rotate X
                    WriteBCAAnimationDescriptor(bca, dataoffset,
                                                (byte)(anim.m_AnimationComponents[ModelBase.AnimationComponentType.RotateX].GetFrameStep() >> 1),
                                                anim.m_AnimationComponents[ModelBase.AnimationComponentType.RotateX].GetIsConstant(),
                                                boneRotateValuesOffsets[bone.m_ID]);
                    dataoffset += 4;

                    // Rotate Y
                    WriteBCAAnimationDescriptor(bca, dataoffset,
                                                (byte)(anim.m_AnimationComponents[ModelBase.AnimationComponentType.RotateY].GetFrameStep() >> 1),
                                                anim.m_AnimationComponents[ModelBase.AnimationComponentType.RotateY].GetIsConstant(),
                                                (boneRotateValuesOffsets[bone.m_ID] +
                                                 anim.m_AnimationComponents[ModelBase.AnimationComponentType.RotateX].GetNumValues()));
                    dataoffset += 4;

                    // Rotate Z
                    WriteBCAAnimationDescriptor(bca, dataoffset,
                                                (byte)(anim.m_AnimationComponents[ModelBase.AnimationComponentType.RotateZ].GetFrameStep() >> 1),
                                                anim.m_AnimationComponents[ModelBase.AnimationComponentType.RotateZ].GetIsConstant(),
                                                (boneRotateValuesOffsets[bone.m_ID] +
                                                 anim.m_AnimationComponents[ModelBase.AnimationComponentType.RotateX].GetNumValues() +
                                                 anim.m_AnimationComponents[ModelBase.AnimationComponentType.RotateY].GetNumValues()));
                    dataoffset += 4;

                    // Translate X
                    WriteBCAAnimationDescriptor(bca, dataoffset,
                                                (byte)(anim.m_AnimationComponents[ModelBase.AnimationComponentType.TranslateX].GetFrameStep() >> 1),
                                                anim.m_AnimationComponents[ModelBase.AnimationComponentType.TranslateX].GetIsConstant(),
                                                boneTranslateValuesOffsets[bone.m_ID]);
                    dataoffset += 4;

                    // Translate Y
                    WriteBCAAnimationDescriptor(bca, dataoffset,
                                                (byte)(anim.m_AnimationComponents[ModelBase.AnimationComponentType.TranslateY].GetFrameStep() >> 1),
                                                anim.m_AnimationComponents[ModelBase.AnimationComponentType.TranslateY].GetIsConstant(),
                                                (boneTranslateValuesOffsets[bone.m_ID] +
                                                 anim.m_AnimationComponents[ModelBase.AnimationComponentType.TranslateX].GetNumValues()));
                    dataoffset += 4;

                    // Translate Z
                    WriteBCAAnimationDescriptor(bca, dataoffset,
                                                (byte)(anim.m_AnimationComponents[ModelBase.AnimationComponentType.TranslateZ].GetFrameStep() >> 1),
                                                anim.m_AnimationComponents[ModelBase.AnimationComponentType.TranslateZ].GetIsConstant(),
                                                (boneTranslateValuesOffsets[bone.m_ID] +
                                                 anim.m_AnimationComponents[ModelBase.AnimationComponentType.TranslateX].GetNumValues() +
                                                 anim.m_AnimationComponents[ModelBase.AnimationComponentType.TranslateY].GetNumValues()));
                    dataoffset += 4;
                }
                else
                {
                    // Set to use constant values (the bone's transformation as there's no animation)

                    // Scale X
                    WriteBCAAnimationDescriptor(bca, dataoffset, 0, true, (boneScaleValuesOffsets[bone.m_ID] + 0));
                    dataoffset += 4;

                    // Scale Y
                    WriteBCAAnimationDescriptor(bca, dataoffset, 0, true, (boneScaleValuesOffsets[bone.m_ID] + 1));
                    dataoffset += 4;

                    // Scale Z
                    WriteBCAAnimationDescriptor(bca, dataoffset, 0, true, (boneScaleValuesOffsets[bone.m_ID] + 2));
                    dataoffset += 4;

                    // Rotation X
                    WriteBCAAnimationDescriptor(bca, dataoffset, 0, true, (boneRotateValuesOffsets[bone.m_ID] + 0));
                    dataoffset += 4;

                    // Rotation Y
                    WriteBCAAnimationDescriptor(bca, dataoffset, 0, true, (boneRotateValuesOffsets[bone.m_ID] + 1));
                    dataoffset += 4;

                    // Rotation Z
                    WriteBCAAnimationDescriptor(bca, dataoffset, 0, true, (boneRotateValuesOffsets[bone.m_ID] + 2));
                    dataoffset += 4;

                    // Translation X
                    WriteBCAAnimationDescriptor(bca, dataoffset, 0, true, (boneTranslateValuesOffsets[bone.m_ID] + 0));
                    dataoffset += 4;

                    // Translation Y
                    WriteBCAAnimationDescriptor(bca, dataoffset, 0, true, (boneTranslateValuesOffsets[bone.m_ID] + 1));
                    dataoffset += 4;

                    // Translation Z
                    WriteBCAAnimationDescriptor(bca, dataoffset, 0, true, (boneTranslateValuesOffsets[bone.m_ID] + 2));
                    dataoffset += 4;
                }
            }

            if (m_BCAImportationOptions.m_Optimise)
            {
                Optimise(bca);
            }
            if (save)
            {
                bca.SaveChanges();
            }
        }
コード例 #3
0
        //020985f0 02098620
        public void saveSections()
        {
            Console.Out.WriteLine("Saving sections...");
            Program.m_ROM.BeginRW();
            MemoryStream o  = new MemoryStream();
            BinaryWriter bw = new BinaryWriter(o);

            foreach (Arm9BinSection s in sections)
            {
                Console.Out.WriteLine(String.Format("{0:X8} - {1:X8} - {2:X8}: {3:X8}",
                                                    s.ramAddr, s.ramAddr + s.len, s.ramAddr + s.len + s.bssSize, o.Position));

                bw.Write(s.data);
                while (o.Length % 4 != 0)
                {
                    bw.Write((byte)0);
                }
            }

            uint         sectionTableAddr = Program.m_ROM.ARM9RAMAddress + 0xE00;
            MemoryStream o2  = new MemoryStream();
            BinaryWriter bw2 = new BinaryWriter(o2);

            foreach (Arm9BinSection s in sections)
            {
                if (!s.real)
                {
                    continue;
                }
                if (s.len == 0)
                {
                    continue;
                }
                bw2.Write((uint)s.ramAddr);
                bw2.Write((uint)s.len);
                bw2.Write((uint)s.bssSize);
            }

            //Write BSS sections last
            //because they overwrite huge areas with zeros (?)
            foreach (Arm9BinSection s in sections)
            {
                if (!s.real)
                {
                    continue;
                }
                if (s.len != 0)
                {
                    continue;
                }
                bw2.Write((uint)s.ramAddr);
                bw2.Write((uint)s.len);
                bw2.Write((uint)s.bssSize);
            }

            byte[] data         = o.ToArray();
            byte[] sectionTable = o2.ToArray();
            Array.Copy(sectionTable, 0, data, sectionTableAddr - Program.m_ROM.ARM9RAMAddress, sectionTable.Length);
            f.m_Data = data;
            f.SaveChanges();

            f.Write32(getCodeSettingsOffs() + 0x00, (uint)sectionTableAddr);
            Console.Out.WriteLine(String.Format("{0:X8} {1:X8}", getCodeSettingsOffs() + 0x04, (uint)o2.Position + sectionTableAddr));
            f.Write32(getCodeSettingsOffs() + 0x04, (uint)o2.Position + sectionTableAddr);
            f.Write32(getCodeSettingsOffs() + 0x08, (uint)(sections[0].len + Program.m_ROM.ARM9RAMAddress));

            Console.Out.WriteLine("DONE");
        }
コード例 #4
0
ファイル: KCLWriter.cs プロジェクト: RicoPlays/sm64dse
        private static void write_kcl(NitroFile kcl, List<Triangle> triangles, int max_triangles, int min_width, float scale)
        {
            kcl.Clear();

            //Need to scale each vertex (1000 times scale of model)
            foreach (Triangle t in triangles)
            {
                t.u = t.u.mul(scale);
                t.v = t.v.mul(scale);
                t.w = t.w.mul(scale);
            }

            uint pos = 0;

            List<Face> faces = new List<Face>();
            VertexWelder vertex_welder = new VertexWelder(1 / 64f, (int)(Math.Floor((double)(triangles.Count / 256))));
            VertexWelder normal_welder = new VertexWelder(1 / 1024f, (int)(Math.Floor((double)(4 * triangles.Count / 256))));

            foreach (Triangle t in triangles)
            {
                Face f = new Face();

                Vector a = unit(cross(t.u.sub(t.w), t.n));
                Vector b = unit(cross(t.v.sub(t.u), t.n));
                Vector c = unit(cross(t.w.sub(t.v), t.n));

                f.length = new FixedPoint(dot(t.v.sub(t.u), c), 1 / 65536f);
                f.vertex_index = (ushort)vertex_welder.add(t.u);
                f.normal_index = (ushort)normal_welder.add(t.n);
                f.a_index = (ushort)normal_welder.add(a);
                f.b_index = (ushort)normal_welder.add(b);
                f.c_index = (ushort)normal_welder.add(c);
                f.group = (ushort)t.group;

                faces.Add(f);
            }

            //Vertex Section
            pos += 56;
            kcl.Write32(0x00, pos);//Vertex section offset

            foreach (Vertex vtx in vertex_welder.vertices)
            {
                kcl.Write32(pos, (uint)vtx.x.valToWrite());
                kcl.Write32(pos + 4, (uint)vtx.y.valToWrite());
                kcl.Write32(pos + 8, (uint)vtx.z.valToWrite());

                pos += 12;
            }

            //Vector Section
            kcl.Write32(0x04, pos);//Vector section offset

            foreach (Vector vct in normal_welder.vectors)
            {
                kcl.Write16(pos, (ushort)vct.x.valToWrite());
                kcl.Write16(pos + 2, (ushort)vct.y.valToWrite());
                kcl.Write16(pos + 4, (ushort)vct.z.valToWrite());

                pos += 6;
            }

            pos = (uint)((pos + 3) & ~3);

            //Planes Section
            kcl.Write32(0x08, pos - 0x10);//Planes section offset

            foreach (Face f in faces)
            {
                kcl.Write32(pos, (uint)f.length.valToWrite());
                kcl.Write16(pos + 4, (ushort)f.vertex_index);
                kcl.Write16(pos + 6, (ushort)f.normal_index);
                kcl.Write16(pos + 8, (ushort)f.a_index);
                kcl.Write16(pos + 10, (ushort)f.b_index);
                kcl.Write16(pos + 12, (ushort)f.c_index);
                kcl.Write16(pos + 14, (ushort)f.group);

                pos += 16;
            }

            //Octree Section
            kcl.Write32(0x0C, pos);//Octree offset

            Octree octree = new Octree(triangles, max_triangles, (float)min_width);
            octree.pack(ref kcl, ref pos);

            //Header
            kcl.Write32(0x10, (uint)327680);//Unknown
            kcl.Write32(0x14, (uint)octree.bas.x.valToWrite());
            kcl.Write32(0x18, (uint)octree.bas.y.valToWrite());
            kcl.Write32(0x1C, (uint)octree.bas.z.valToWrite());
            kcl.Write32(0x20, (uint)(~((int)octree.width_x - 1) & 0xFFFFFFFF));
            kcl.Write32(0x24, (uint)(~((int)octree.width_y - 1) & 0xFFFFFFFF));
            kcl.Write32(0x28, (uint)(~((int)octree.width_z - 1) & 0xFFFFFFFF));
            kcl.Write32(0x2C, (uint)(Math.Log(octree.base_width, 2)));
            kcl.Write32(0x30, (uint)(Math.Log(octree.nx, 2)));
            kcl.Write32(0x34, (uint)(Math.Log(octree.nx, 2)) + (uint)(Math.Log(octree.ny, 2)));

            kcl.SaveChanges();
        }