private void backgroundRendering_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // if exception occured in rendering thread -- we display the message
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            if (mesh != null)
            {
                mesh.Drop();
            }
            if (meshToAnimate != null)
            {
                meshToAnimate.Drop();
            }
            // if user want exit - we close main form
            // note: it is the only way to close form correctly -- only when device dropped,
            // so background worker not running
            if (userWantExit)
            {
                Close();
            }
            if (Logger != null)
            {
                Logger.LogString("Fast rendering stopped.", Logtype.Normal);
            }
        }
        private void exportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (var sf = new SaveFileDialog())
            {
                sf.Filter = "Irrlicht mesh | *.irrm | Collada mesh | *.coll | STL Mesh | *.stl | OBJ Mesh | *.obj | PLY Mesh | *.ply | B3D Mesh | *.b3d | FBX Mesh | *.fbx";
                if (sf.ShowDialog() == DialogResult.OK)
                {
                    MeshWriterType mwt = 0;
                    switch (Path.GetExtension(sf.FileName).Trim())
                    {
                    case ".irrm":
                        mwt = MeshWriterType.IrrMesh;
                        break;

                    case ".coll":
                        mwt = MeshWriterType.Collada;
                        break;

                    case ".obj":
                        mwt = MeshWriterType.Obj;
                        break;

                    case ".stl":
                        mwt = MeshWriterType.Stl;
                        break;

                    case ".ply":
                        mwt = MeshWriterType.Ply;
                        break;

                    case ".b3d":
                        mwt = MeshWriterType.B3d;
                        break;

                    case ".fbx":
                        mwt = MeshWriterType.Fbx;
                        break;

                    default:
                        MessageBox.Show(this, "Wrong file format selected, choose correct file format!", "WolvenKit", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    var          mw = smgr.CreateMeshWriter(mwt);
                    AnimatedMesh sm = mesh;
                    SkinnedMesh  s  = null;
                    if (meshToAnimate != null)
                    {
                        sm = meshToAnimate;
                    }
                    if (!string.IsNullOrEmpty(activeAnim))
                    {
                        s = helper.applyAnimation(activeAnim, meshToAnimate);
                        if (s != null)
                        {
                            sm = s;
                        }
                    }

                    if (mw.WriteMesh(dev.FileSystem.CreateWriteFile(sf.FileName), sm, MeshWriterFlag.None))
                    {
                        MessageBox.Show(this, "Sucessfully wrote file!", "WolvenKit", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show(this, "Failed to write file!", "WolvenKit", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    if (sm != null)
                    {
                        sm.Drop();
                    }
                    if (mw != null)
                    {
                        mw.Drop();
                    }
                    if (s != null)
                    {
                        s.Drop();
                    }
                }
            }
        }
Esempio n. 3
0
        private void backgroundRendering_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker   = sender as BackgroundWorker;
            DeviceSettings   settings = e.Argument as DeviceSettings;

            // create irrlicht device using provided settings

            IrrlichtDevice dev = IrrlichtDevice.CreateDevice(settings);

            if (dev == null)
            {
                throw new Exception("Failed to create Irrlicht device.");
            }

            dev.Logger.LogLevel = LogLevel.Warning;
            VideoDriver  drv  = dev.VideoDriver;
            SceneManager smgr = dev.SceneManager;

            smgr.FileSystem.AddFileArchive("E:/WitcherMods/modTest/Test/files/Mod/Bundle");
            smgr.FileSystem.AddFileArchive("E:/WitcherMods/modTest/Test/files/Raw/Mod/TextureCache");

            smgr.Attributes.AddValue("TW_TW3_LOAD_SKEL", true);
            smgr.Attributes.AddValue("TW_TW3_LOAD_BEST_LOD_ONLY", true);

            // setup a simple 3d scene

            //CameraSceneNode cam = smgr.AddCameraSceneNode();
            //cam.Target = new Vector3Df(0);

            // added by vl
            AnimatedMesh mesh = smgr.GetMesh("E:/WitcherMods/modTest/Test/files/Mod/Bundle/characters/models/animals/cat/model/t_01__cat.w2mesh");

            if (mesh == null)
            {
                throw new Exception("Failed to load mesh.");
            }

            smgr.MeshManipulator.RecalculateNormals(mesh);

            List <String> animList = null;

            float scaleMul               = 1.0f;
            AnimatedMeshSceneNode node   = smgr.AddAnimatedMeshSceneNode(mesh);
            MeshLoaderHelper      helper = smgr.GetMeshLoader(smgr.MeshLoaderCount - 1).getMeshLoaderHelper();

            if (node != null)
            {
                scaleMul   = node.BoundingBox.Radius / 4;
                node.Scale = new Vector3Df(3.0f);
                node.SetMaterialFlag(MaterialFlag.Lighting, false);

                SkinnedMesh sm = helper.loadRig("E:/WitcherMods/modTest/Test/files/Mod/Bundle/characters/base_entities/cat_base/cat_base.w2rig", mesh);
                if (sm == null)
                {
                    throw new Exception("Failed to load rig.");
                }

                animList = helper.loadAnimation("E:/WitcherMods/modTest/Test/files/Mod/Bundle/animations/animals/cat/cat_animation.w2anims", sm);
                if (animList.Count > 0)
                {
                    AnimatedMesh am = helper.applyAnimation(animList[0], sm);
                    node.Mesh = am;
                }
                //scaleSkeleton(sm, 3.0f);
                //sm.SkinMesh();

                mesh.Drop();
                sm.Drop();
                setMaterialsSettings(node);

                /*
                 * animList = helper.loadAnimation("E:/WitcherMods/modTest/Test/files/Mod/Bundle/animations/animals/cat/cat_animation.w2anims", node);
                 * if (animList.Count > 0)
                 * {
                 *      helper.applyAnimation(animList[0]);
                 * }
                 */
            }

            var camera = smgr.AddCameraSceneNode(null,
                                                 new Vector3Df(node.BoundingBox.Radius * 8, node.BoundingBox.Radius, 0),
                                                 new Vector3Df(0, node.BoundingBox.Radius, 0)
                                                 );

            camera.NearValue = 0.001f;
            camera.FOV       = 45.0f * 3.14f / 180.0f;

            node.DebugDataVisible ^= DebugSceneType.BBox | DebugSceneType.Skeleton;

            node.Position = new Vector3Df(0.0f);

            /*
             * SceneNodeAnimator anim = smgr.CreateFlyCircleAnimator(new Vector3Df(0, 15, 0), 30.0f);
             * cam.AddAnimator(anim);
             * anim.Drop();
             *
             * SceneNode cube = smgr.AddCubeSceneNode(20);
             * cube.SetMaterialTexture(0, drv.GetTexture("../../media/wall.bmp"));
             * cube.SetMaterialTexture(1, drv.GetTexture("../../media/water.jpg"));
             * cube.SetMaterialFlag(MaterialFlag.Lighting, false);
             * cube.SetMaterialType(MaterialType.Reflection2Layer);
             */

            if (settings.BackColor == null)
            {
                smgr.AddSkyBoxSceneNode(
                    "../../media/irrlicht2_up.jpg",
                    "../../media/irrlicht2_dn.jpg",
                    "../../media/irrlicht2_lf.jpg",
                    "../../media/irrlicht2_rt.jpg",
                    "../../media/irrlicht2_ft.jpg",
                    "../../media/irrlicht2_bk.jpg");
            }

            dev.GUIEnvironment.AddImage(
                drv.GetTexture("../../media/lime_logo_alpha.png"),
                new Vector2Di(30, 0));

            // draw all

            int lastFPS = -1;

            while (dev.Run())
            {
                if (settings.BackColor == null)
                {
                    // indeed, we do not need to spend time on cleaning color buffer if we use skybox
                    drv.BeginScene(ClearBufferFlag.Depth);
                }
                else
                {
                    drv.BeginScene(ClearBufferFlag.Depth | ClearBufferFlag.Color, settings.BackColor);
                }

                smgr.DrawAll();
                dev.GUIEnvironment.DrawAll();
                drv.EndScene();

                int fps = drv.FPS;
                if (lastFPS != fps)
                {
                    // report progress using common BackgroundWorker' method
                    // note: we cannot do just labelRenderingStatus.Text = "...",
                    // because we are running another thread
                    worker.ReportProgress(fps, drv.Name);
                    lastFPS = fps;
                }

                // if we requested to stop, we close the device
                if (worker.CancellationPending)
                {
                    dev.Close();
                }
            }

            // drop device
            dev.Drop();
        }