コード例 #1
0
ファイル: ShapeChunk.cs プロジェクト: w4x51m/Cerberus-V3
        public override void Read(BinaryReader br, string id)
        {
            //Console.WriteLine("Parsing chunk data from shape " + _shapeId);

            _offset    = br.BaseStream.Position;
            _textureId = br.ReadByte(); // 00
            var texture = (Texture)_scFile.GetTextures()[_textureId];

            if (texture == null)
            {
                throw new InvalidOperationException($"Texture {_textureId} wasn't loaded yet.");
            }

            byte shapePointCount = br.ReadByte(); // 04

            _xy = new PointF[shapePointCount];
            _uv = new PointF[shapePointCount];

            for (int i = 0; i < shapePointCount; i++)
            {
                var x = (float)((float)br.ReadInt32() * 0.05f);
                var y = (float)((float)br.ReadInt32() * 0.05f);
                _xy[i] = new PointF(x, y);
                //Console.WriteLine("x: " + x + ", y: " + y);
            }

            if (_chunkType == 22)
            {
                for (int i = 0; i < shapePointCount; i++)
                {
                    var u = (float)((float)br.ReadUInt16() / 65535f) * texture.GetImage().GetWidth();
                    var v = (float)((float)br.ReadUInt16() / 65535f) * texture.GetImage().GetHeight();
                    _uv[i] = new PointF(u, v);

                    //Console.WriteLine("u: " + u + ", v: " + v);
                }
            }
            else
            {
                for (int i = 0; i < shapePointCount; i++)
                {
                    float u = (float)((float)br.ReadUInt16()); // image.Width);
                    float v = (float)((float)br.ReadUInt16()); // image.Height);//(short) (65535 * br.ReadInt16() / image.Height);

                    //Console.WriteLine("u: " + u + ", v: " + v);
                    _uv[i] = new PointF(u, v);
                }
            }
        }
コード例 #2
0
        public override Bitmap Render(RenderingOptions options)
        {
            /*
             * Console.WriteLine("XY:");
             * foreach(ShapeChunk chunk in m_vChunks)
             * {
             *  foreach(var p in chunk.GetPointsXY())
             *  {
             *      Console.WriteLine("x: " + p.X + ", y: " + p.Y);
             *  }
             *  Console.WriteLine("");
             * }
             *
             * foreach (ShapeChunk chunk in m_vChunks)
             * {
             *  foreach (var p in chunk.GetPointsUV())
             *  {
             *      Console.WriteLine("u: " + p.X + ", u: " + p.Y);
             *  }
             *  Console.WriteLine("");
             * }
             */

            Console.WriteLine("Rendering image of " + _chunks.Count + " polygons");


            // Calculate et initialize the final shape size
            PointF[] pointsXY = _chunks.SelectMany(chunk => ((ShapeChunk)chunk).XY).ToArray();
            using (var xyPath = new GraphicsPath())
            {
                xyPath.AddPolygon(pointsXY.ToArray());

                var xyBound = Rectangle.Round(xyPath.GetBounds());

                var width = xyBound.Width;
                width = width > 0 ? width : 1;

                var height = xyBound.Height;
                height = height > 0 ? height : 1;

                var x = xyBound.X;
                var y = xyBound.Y;

                var finalShape = new Bitmap(width, height);
                Console.WriteLine($"Rendering shape: W:{finalShape.Width} H:{finalShape.Height}\n");

                // Assemble shape chunks
                foreach (ShapeChunk chunk in _chunks)
                {
                    var    texture = (Texture)_scFile.GetTextures()[chunk.GetTextureId()];
                    Bitmap bitmap  = texture.Bitmap;

                    using (var gpuv = new GraphicsPath())
                    {
                        gpuv.AddPolygon(chunk.UV.ToArray());

                        var gxyBound = Rectangle.Round(gpuv.GetBounds());

                        int gpuvWidth = gxyBound.Width;
                        gpuvWidth = gpuvWidth > 0 ? gpuvWidth : 1;

                        int gpuvHeight = gxyBound.Height;
                        gpuvHeight = gpuvHeight > 0 ? gpuvHeight : 1;

                        var shapeChunk = new Bitmap(gpuvWidth, gpuvHeight);

                        var chunkX = gxyBound.X;
                        var chunkY = gxyBound.Y;

                        using (var g = Graphics.FromImage(shapeChunk))
                        {
                            gpuv.Transform(new Matrix(1, 0, 0, 1, -chunkX, -chunkY));
                            g.SetClip(gpuv);
                            g.DrawImage(bitmap, -chunkX, -chunkY);
                        }

                        GraphicsPath gp = new GraphicsPath();
                        gp.AddPolygon(new[] { new Point(0, 0), new Point(gpuvWidth, 0), new Point(0, gpuvHeight) });

                        //Calculate transformation Matrix of UV
                        //double[,] matrixArrayUV = { { polygonUV[0].X, polygonUV[1].X, polygonUV[2].X }, { polygonUV[0].Y, polygonUV[1].Y, polygonUV[2].Y }, { 1, 1, 1 } };
                        double[,] matrixArrayUV =
                        {
                            {
                                gpuv.PathPoints[0].X, gpuv.PathPoints[1].X, gpuv.PathPoints[2].X
                            },
                            {
                                gpuv.PathPoints[0].Y, gpuv.PathPoints[1].Y, gpuv.PathPoints[2].Y
                            },
                            {
                                1, 1, 1
                            }
                        };
                        double[,] matrixArrayXY =
                        {
                            {
                                chunk.XY[0].X, chunk.XY[1].X, chunk.XY[2].X
                            },
                            {
                                chunk.XY[0].Y, chunk.XY[1].Y, chunk.XY[2].Y
                            },
                            {
                                1, 1, 1
                            }
                        };

                        var matrixUV = Matrix <double> .Build.DenseOfArray(matrixArrayUV);

                        var matrixXY = Matrix <double> .Build.DenseOfArray(matrixArrayXY);

                        var inverseMatrixUV = matrixUV.Inverse();
                        var transformMatrix = matrixXY * inverseMatrixUV;
                        var m = new Matrix((float)transformMatrix[0, 0], (float)transformMatrix[1, 0], (float)transformMatrix[0, 1], (float)transformMatrix[1, 1], (float)transformMatrix[0, 2], (float)transformMatrix[1, 2]);
                        //m = new Matrix((float)transformMatrix[0, 0], (float)transformMatrix[1, 0], (float)transformMatrix[0, 1], (float)transformMatrix[1, 1], (float)Math.Round(transformMatrix[0, 2]), (float)Math.Round(transformMatrix[1, 2]));

                        //Perform transformations
                        gp.Transform(m);

                        using (Graphics g = Graphics.FromImage(finalShape))
                        {
                            //Set origin
                            Matrix originTransform = new Matrix();
                            originTransform.Translate(-x, -y);
                            g.Transform = originTransform;

                            g.DrawImage(shapeChunk, gp.PathPoints, gpuv.GetBounds(), GraphicsUnit.Pixel);

                            if (options.ViewPolygons)
                            {
                                gpuv.Transform(m);
                                g.DrawPath(new Pen(Color.DeepSkyBlue, 1), gpuv);
                            }
                            g.Flush();
                        }
                    }
                }
                return(finalShape);
            }
        }
コード例 #3
0
ファイル: Export.cs プロジェクト: w4x51m/Cerberus-V3
        public override void Write(FileStream input)
        {
            input.Seek(0, SeekOrigin.Begin);
            byte[] file = new byte[input.Length];
            input.Read(file, 0, file.Length);

            int cursor = (int)_scFile.GetStartExportsOffset();

            input.Seek(_scFile.GetStartExportsOffset(), SeekOrigin.Begin);

            ushort exportCount = BitConverter.ToUInt16(file, cursor);

            input.Write(BitConverter.GetBytes((ushort)(exportCount + 1)), 0, 2);
            cursor += 2;

            input.Seek(exportCount * 2, SeekOrigin.Current);
            cursor += exportCount * 2;
            input.Write(BitConverter.GetBytes(_exportId), 0, 2);

            for (int i = 0; i < exportCount; i++)
            {
                byte nameLength = file[cursor];
                cursor++;
                byte[] exportName = new byte[nameLength];
                Array.Copy(file, cursor, exportName, 0, nameLength);
                input.WriteByte(nameLength);
                input.Write(exportName, 0, nameLength);
                cursor += nameLength;
            }

            input.WriteByte((byte)_exportName.Length);
            input.Write(Encoding.UTF8.GetBytes(_exportName), 0, (byte)_exportName.Length);

            while (cursor < file.Length)
            {
                input.WriteByte(file[cursor]);
                cursor++;
            }

            //refresh all offsets
            foreach (Texture t in _scFile.GetTextures())
            {
                long offset = t.GetOffset();
                if (offset > 0)
                {
                    offset += 2 + 1 + _exportName.Length;
                }
                else
                {
                    offset = offset - 2 - 1 - _exportName.Length;
                }
                t.SetOffset(offset);
            }
            foreach (Shape s in _scFile.GetShapes())
            {
                long offset = s.GetOffset();
                if (offset > 0)
                {
                    offset += 2 + 1 + _exportName.Length;
                }
                else
                {
                    offset = offset - 2 - 1 - _exportName.Length;
                }
                s.SetOffset(offset);
                foreach (ShapeChunk sc in s.GetChunks())
                {
                    long chunkOffset = sc.GetOffset();
                    if (chunkOffset > 0)
                    {
                        chunkOffset += 2 + 1 + _exportName.Length;
                    }
                    else
                    {
                        chunkOffset = chunkOffset - 2 - 1 - _exportName.Length;
                    }
                    sc.SetOffset(chunkOffset);
                }
            }
            foreach (MovieClip mc in _scFile.GetMovieClips())
            {
                long offset = mc.GetOffset();
                if (offset > 0)
                {
                    offset += 2 + 1 + _exportName.Length;
                }
                else
                {
                    offset = offset - 2 - 1 - _exportName.Length;
                }
                mc.SetOffset(offset);
            }
            _scFile.SetEofOffset(_scFile.GetEofOffset() + 2 + 1 + _exportName.Length);
            //ne pas oublier eofoffset
        }
コード例 #4
0
ファイル: Texture.cs プロジェクト: w4x51m/Cerberus-V3
 public Texture(ScFile scs)
 {
     _scFile    = scs;
     _textureId = (ushort)_scFile.GetTextures().Count();
 }