Beispiel #1
0
        static Texture2D LoadTexture(FoamExtension Ext)
        {
            using (MemoryStream MS = new MemoryStream(Ext.Data)) {
                MS.Seek(0, SeekOrigin.Begin);

                using (Bitmap Bmp = new Bitmap(NetImage.FromStream(MS))) {
                    BitmapData Data       = Bmp.LockBits(new System.Drawing.Rectangle(0, 0, Bmp.Width, Bmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                    uint *     OrigColors = (uint *)Data.Scan0;

                    int   Len    = Bmp.Width * Bmp.Height;
                    uint *Colors = (uint *)Marshal.AllocHGlobal(Len * sizeof(uint));

                    for (int i = 0; i < Len; i++)
                    {
                        uint Orig = OrigColors[i];

                        byte R = (byte)((Orig >> 16) & 255);
                        byte G = (byte)((Orig >> 8) & 255);
                        byte B = (byte)((Orig >> 0) & 255);
                        byte A = (byte)((Orig >> 24) & 255);

                        Colors[i] = (uint)((R << 0) | (G << 8) | (B << 16) | (A << 24));
                    }

                    Image Img = Raylib.LoadImagePro(new IntPtr(Colors), Bmp.Width, Bmp.Height, (int)RaylibSharp.PixelFormat.UNCOMPRESSED_R8G8B8A8);
                    Marshal.FreeHGlobal(new IntPtr(Colors));

                    Bmp.UnlockBits(Data);
                    return(LoadTexture(Img));
                }
            }
        }
Beispiel #2
0
        static void Run()
        {
            string ObjInput      = ArgumentParser.GetSingle("obj");
            string MtlInput      = ArgumentParser.GetSingle("mtl");
            string MapOutput     = ArgumentParser.GetSingle("out");
            bool   EmbedTextures = ArgumentParser.Defined("e");
            bool   ComputeLights = !ArgumentParser.Defined("l");

            string ObjName = "test";

            if (ObjInput == null)
            {
                ObjInput  = "sample/" + ObjName + ".obj";
                MtlInput  = "sample/" + ObjName + ".mtl";
                MapOutput = "sample/" + ObjName + ".mapfoam";
            }

            if (!File.Exists(ObjInput))
            {
                throw new Exception("Obj input file not found");
            }

            if (!File.Exists(MtlInput))
            {
                throw new Exception("Mtl input file not found");
            }

            string OutDir = Path.GetDirectoryName(Path.GetFullPath(MapOutput));

            Console.WriteLine("obj = '{0}'", ObjInput);
            Console.WriteLine("mtl = '{0}'", MtlInput);
            Console.WriteLine("out = '{0}'", MapOutput);
            Console.WriteLine("Embed textures? {0}", EmbedTextures);
            Console.WriteLine("Compute lights? {0}", ComputeLights);

            FoamModel LevelModel = ObjLoader.Load(ObjInput, MtlInput);


            MeshAtlasMap AtlasMap;

            LevelModel.CalcBounds(out Vector3 Min, out Vector3 Max);
            Console.WriteLine("Level min = {0}; max = {1}", Min, Max);

            // Generate atlas
            {
                Console.WriteLine("Generating lightmap");
                GenAtlas(OutDir, LevelModel, out AtlasMap);
            }

            if (ComputeLights)
            {
                Light[] Lights = new Light[] {
                    new Light(new Vector3(-32, 104, 368), new Vector3(1, 0, 0), 10000),
                    new Light(new Vector3(-80, 104, 704), new Vector3(0, 1, 0), 10000),
                    new Light(new Vector3(0, 312, 304), new Vector3(0, 0, 1), 20000)
                };

                //LightMapping.Compute(LevelModel, AtlasMap, Lights);

                //Vector4[] Pixels = new Vector4[1024 * 1024];
                //RayLightmapper.raylight_render_scene(1024, 1024, 512, new Vector3(1, 1, 1), 0, null, 0, null, null, null, ref Pixels);

                {
                    string TexName = "lightmap.png";

                    FoamMaterial LightmapMat = new FoamMaterial(Path.GetFileNameWithoutExtension(TexName), new[] { new FoamTexture(TexName, FoamTextureType.LightMap) });
                    Utils.Append(ref LevelModel.Materials, LightmapMat);

                    foreach (var M in LevelModel.Meshes)
                    {
                        M.MaterialIndex = LevelModel.Materials.Length - 1;
                    }

                    //AtlasMap.Atlas.Resize(4);
                    AtlasMap.Atlas.FlipY();
                    AtlasMap.Atlas.Save(Path.Combine(OutDir, TexName));

                    Utils.Append(ref LevelModel.Extensions, FoamExtension.CreateEmbeddedPng(TexName, AtlasMap.Atlas.GetImage()));
                }
            }

            //ObjLoader.Save(LevelModel, "sample/EXPORTED.obj");
            LevelModel.SaveToFile(MapOutput);

            //Console.WriteLine("Done!");
            //Console.ReadLine();
        }