Пример #1
0
 /*!
  * @param world Vixen 3D world
  * Initializes the Vixen 3D environment for the given world.
  * If you do not provide an initial world, one is created for you.
  * This function does not start 3D display - RunVixen does that.
  * The world can only be initialized once - subsequent calls do nothing.
  *
  * @return -> Vixen SharedWorld or null if initialization failed
  */
 static public SharedWorld StartVixen(SharedWorld world)
 {
     _world = world;
     if (_world == null)
     {
         _world = new Viewer3D();
     }
     else if (_world.FileName != null)
     {
         string dir = Path.GetDirectoryName(_world.FileName);
         if ((dir != null) && (dir.Length > 0))
         {
             Directory.SetCurrentDirectory(dir);
             _world.SetMediaDir(dir);
         }
     }
     if ((_world == null) || !_world.OnInit())
     {
         SharedWorld.LogError("cannot initialize Vixen");
         return(null);
     }
     GC.KeepAlive(_world);
     _world.MakeLock();
     if (UsePhysics)
     {
         Physics.Startup();
     }
     return(_world);
 }
Пример #2
0
 /*!
  * Events are moved from the Vixen SharedWorld to the C#
  * application through a shared event queue. Events
  * observed and handled by the Vixen SharedWorld object
  * are put into a queue. The Canvas3D class maintains a separate
  * thread which waits on this queue and converts any Vixen Events
  * it finds into C# Vixen.EventArgs and raises a .NET event
  * which can be handled in the C# application.
  */
 protected void EventLoop()
 {
     _stopevents = false;
     if ((_world == null) || (_scene == null))
     {
         return;
     }
     if ((_world.FileName != null) && SharedWorld.DoAsyncLoad)
     {
         _world.LoadAsync(_world.FileName, _scene);
     }
     while (!_stopevents)
     {
         try
         {
             Event ev = _world.NextEvent();
             if (ev == null)
             {
                 break;
             }
             string            name    = ev.GetName();
             EventArgs         args    = new VixenEventArgs(ev, RoutedVixenEvent);
             object[]          list    = { this, args };
             VixenEventHandler handler = new VixenEventHandler(PostVixenEvent);
             //SharedWorld.Trace("Canvas3D:Event " + name + "\n");
             Dispatcher.BeginInvoke(handler, list);
         }
         catch (Exception ex)
         {
             SharedWorld.LogError("exception in event loop " + ex.Message);
         }
     }
     _world.Stop();
 }
Пример #3
0
        /*!
         * Start up Vixen 3D display and event processing.
         * This function should not be called until the
         * underlying Window has been created and the
         * OS window handle is available. Vixen will display
         * the 3D content in this window.
         */
        public virtual bool RunVixen()
        {
            bool loadasync = World.DoAsyncLoad;

            if (_world == null)
            {
                SharedWorld.LogError("Vixen did not start");
                return(false);
            }
            if (_window == null)
            {
                SharedWorld.LogError("Cannot get window handle for parent window");
                return(false);
            }
            if (_world.IsRunning())
            {
                return(false);
            }
            //world.SetDebugLevel(1);
            if (MediaDir != null)
            {
                _world.SetMediaDir(MediaDir);
            }
            if (ContentFile == null)
            {
                loadasync = false;
            }
            else if (loadasync)
            {
                _world.FileName = GetMediaPath(ContentFile);
            }
            _world.Run((long)_window);
            if (_world.IsRunning())
            {
                ThreadStart eventloop = new ThreadStart(EventLoop);
                Thread      thread    = new Thread(eventloop);

                _scene = SharedWorld.MainScene;
                thread.Start();
                if (!loadasync)
                {
                    try
                    {
                        Scene scene = MakeScene();
                        if (scene != null)
                        {
                            GC.KeepAlive(scene);
                            _world.SetScene(scene);
                        }
                    }
                    catch (Exception ex)
                    {
                        SharedWorld.LogError("exception making initial scene " + ex.Message);
                    }
                }
                return(true);
            }
            return(false);
        }
Пример #4
0
 protected override void OnStartup(StartupEventArgs e)
 {
     if (VixWorld == null)
     {
         VixWorld = new Viewer3D();
     }
     if (e.Args.Length > 0)
     {
         VixWorld.ParseArgs(e.Args[0]);
     }
     DispatcherUnhandledException          += Application_DispatcherUnhandledException;
     TaskScheduler.UnobservedTaskException += Scheduler_DispatcherUnhandledException;
     Canvas3D.StartVixen(VixWorld);
     base.OnStartup(e);
 }
Пример #5
0
 /*!
  * @param world Vixen 3D world
  * Initializes the Vixen 3D environment for the given world.
  * If you do not provide an initial world, one is created for you.
  * This function does not start 3D display - RunVixen does that.
  * The world can only be initialized once - subsequent calls do nothing.
  *
  * @return -> Vixen SharedWorld or null if initialization failed
  */
 static public SharedWorld StartVixen(SharedWorld world)
 {
     if (world == null)
     {
         world = new Viewer3D();
     }
     if ((world == null) || !world.OnInit())
     {
         LogError("cannot initialize Vixen");
         return(null);
     }
     world.MakeLock();
     if (UsePhysics)
     {
         Physics.Startup();
     }
     return(world);
 }
Пример #6
0
        /*!
         * Events are moved from the Vixen SharedWorld to the C#
         * application through a shared event queue. Events
         * observed and handled by the Vixen SharedWorld object
         * are put into a queue. The Canvas3D class maintains a separate
         * thread which waits on this queue and converts any Vixen Events
         * it finds into C# Vixen.EventArgs and raises a .NET event
         * which can be handled in the C# application.
         */
        protected void EventLoop()
        {
            SharedWorld world = SharedWorld.Get();

            StopEvents = false;

            if (world.FileName != null)
            {
                world.LoadAsync(world.FileName, world.GetScene());
            }
            while (!StopEvents && (SharedWorld.Get() != null))
            {
                try
                {
                    Event ev = SharedWorld.Get().NextEvent();
                    if (ev == null)
                    {
                        break;
                    }
                    if (ev.GetType() == typeof(TrackEvent))
                    {
                        if (ev.Code != Event.TRACK)
                        {
                            Console.Write("ERROR: TrackEvent has bad opcode");
                        }
                    }
                    string            name    = ev.GetName();
                    EventArgs         args    = new VixenEventArgs(ev);
                    object[]          list    = { this, args };
                    VixenEventHandler handler = new VixenEventHandler(PostVixenEvent);
                    Trace("Canvas3D:Event " + name + "\n");
                    Invoke(handler, list);
                }
                catch (Exception ex)
                {
                    LogError("exception in event loop " + ex.Message);
                }
            }
            if (SharedWorld.Get() != null)
            {
                SharedWorld.Get().Stop();
            }
        }
Пример #7
0
        /*!
         * Shuts down 3D processing and releases all 3D resources.
         * This routine is called automatically when the window closes.
         */
        public virtual void StopVixen()
        {
            SharedWorld world = SharedWorld.Get();

            if (world != null)
            {
                Scene scene = world.GetScene();

                if (scene != null)
                {
                    scene.Window = 0;
                }
                world.StopEvents();
                if (UsePhysics)
                {
                    Physics.Shutdown();
                }
            }
        }
Пример #8
0
        public Vec3 WorldFromTrack2D(Vec3 p)
        {
            Scene  scene   = SharedWorld.MainScene;
            Camera cam     = scene.Camera;
            Box3   vvol    = cam.ViewVol;
            float  camdist = cam.Translation.Length;
            float  zdist   = camdist - ZOffset;
            Vec3   w       = new Vec3();
            Vec3   v       = new Vec3();
            Matrix mtx     = new Matrix();
            float  tmp     = 0.5f * vvol.Height / vvol.min.z;

            v.x = p.x * tmp * zdist;
            v.y = p.y * tmp * zdist;
            v.z = -zdist;
            cam.TotalTransform(mtx);
            mtx.Transform(v, w);
            SharedWorld.Trace(System.String.Format(Name + " Track({0}, {1}, {2}) -> World({3}, {4}, {5})\n", p.x, p.y, p.z, w.x, w.y, w.z));
            return(w);
        }
Пример #9
0
        public Vec3 WorldFromScreen(Vec3 pos)
        {
            Scene  scene = SharedWorld.MainScene;
            Camera cam   = scene.Camera;
            Box3   vv    = cam.ViewVol;
            Vec3   p     = Normalize(pos);
            Matrix mtx   = cam.GetViewTrans();
            Vec3   w     = new Vec3(vv.min.x + p.x * vv.Width,
                                    vv.max.y - p.y * vv.Height,
                                    vv.min.z - p.z * vv.Depth + ZOffset);
            float tmp = -0.5f * w.z / cam.Hither;

            tmp  = -w.z / cam.Hither;
            w.x *= tmp;
            w.y *= tmp;
            cam.TotalTransform(mtx);
            mtx.Transform(w, w);
            SharedWorld.Trace(System.String.Format(Name + " Screen({0}, {1}, {2}) -> World({3}, {4}, {5})\n", pos.x, pos.y, pos.z, w.x, w.y, w.z));
            return(w);
        }
Пример #10
0
    private bool ConnectClothAnim(Garment garment)
    {
        try
        {
            dynamic           g         = garment;
            Vixen.Model       clothmesh = garment.ClothMesh;
            Vixen.SharedWorld world     = Vixen.SharedWorld.Get();
            dynamic           d         = avatarCanvas.ConfigOpts;

            if (d.timeinc != null)
            {
                _timeinc = (float)Double.Parse(d.timeinc);
            }
            if (clothmesh == null)
            {
                return(false);
            }
            if (_clothAnim == null)
            {
                _clothAnim = new MagicMirror.Viewer.ClothRecorder(_timeinc);
            }
            else
            {
                _clothAnim.Clear();
            }
            _clothAnim.BaseName = g.Name;
            world.SuspendScene();
            _clothAnim.AnimRoot = null;
            _clothAnim.MeshRoot = clothmesh;
            _clothAnim.AnimRoot.PutAfter(Vixen.Physics.Get());
            world.ResumeScene();
            return(true);
        }
        catch (Exception ex)
        {
            Vixen.SharedWorld.LogError(ex.Message);
        }
        return(false);
    }
Пример #11
0
        /*!
         * Routes a mouse event from this window to Vixen.
         * Any Vixen objects which observe Event.MOUSE will get these events.
         * If DispatchMouseEvents is false, this functionality is disabled.
         * @see DispatchMouseEvents
         */
        private void DispatchMouse(MouseEventArgs e)
        {
            int         buttons   = 0;
            Keys        modifiers = Control.ModifierKeys;
            SharedWorld world     = SharedWorld.Get();
            Scene       scene     = SharedWorld.MainScene;

            if ((scene == null) || (world == null) || !world.IsRunning())
            {
                return;
            }
            if (!DispatchMouseEvents)
            {
                return;
            }
            if ((e.Button & MouseButtons.Left) != 0)
            {
                buttons |= MouseEvent.LEFT;
            }
            if ((e.Button & MouseButtons.Right) != 0)
            {
                buttons |= MouseEvent.RIGHT;
            }
            if ((e.Button & MouseButtons.Middle) != 0)
            {
                buttons |= MouseEvent.MIDDLE;
            }
            if ((modifiers & Keys.Control) != 0)
            {
                buttons |= MouseEvent.CONTROL;
            }
            if ((modifiers & Keys.Shift) != 0)
            {
                buttons |= MouseEvent.SHIFT;
            }
            world.OnMouse(e.X, e.Y, buttons, 0);
            OnMouse((float)e.X, (float)e.Y, buttons, 0);
        }
Пример #12
0
        /*!
         * Start up Vixen 3D display and event processing.
         * This function should not be called until the
         * underlying Window has been created and the
         * OS window handle is available. Vixen will display
         * the 3D content in this window.
         */
        public virtual bool RunVixen()
        {
            SharedWorld world        = SharedWorld.Get();
            IntPtr      windowHandle = Handle;
            Scene       scene        = null;

            try
            {
                if (windowHandle == null)
                {
                    LogError("Cannot get window handle for parent window");
                    return(false);
                }
                if ((world != null) && world.IsRunning())
                {
                    return(false);
                }
                //world.SetDebugLevel(1);
                world.Run((uint)windowHandle);
            }
            catch (Exception ex)
            {
                LogError("exception starting 3D  " + ex.Message);
            }
            if (world.IsRunning())
            {
                ThreadStart eventloop = new ThreadStart(EventLoop);
                Thread      thread    = new Thread(eventloop);
                bool        loadasync = World.DoAsyncLoad;

                if (MediaDir != null)
                {
                    world.SetMediaDir(MediaDir);
                }
                if (ContentFile != null)
                {
                    world.FileName = GetMediaPath(ContentFile);
                }
                else
                {
                    loadasync = false;
                }
                thread.Start();
                if (!loadasync)
                {
                    try
                    {
                        scene = MakeScene();
                        if (scene != null)
                        {
                            world.SetScene(scene);
                        }
                    }
                    catch (Exception ex)
                    {
                        SharedWorld.LogError("exception making initial scene " + ex.Message);
                    }
                }
                scene = world.GetScene();
                if (scene != null)
                {
                    scene.OnResize();
                }
                return(true);
            }
            return(false);
        }
Пример #13
0
 internal static HandleRef getCPtr(SharedWorld obj)
 {
     return((obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr);
 }