private static void write_kcl(NitroFile kcl, List <Triangle> triangles, int max_triangles, int min_width, float scale, bool save = true) { 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))); if (save) { kcl.SaveChanges(); } }
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(); }