Esempio n. 1
0
        public ViewControl()
        {           
            // this control will be painted using some 3d API
            // no need for double buffering
            // also no need for the OS to paint it.
            this.ResizeRedraw = false;
            this.DoubleBuffered = false;
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.Opaque, true);
            AllowDrop = true;
            m_camera = new Camera();


            m_camera.SetPerspective((float)(Math.PI / 4), 1.0f, 0.1f, 2048);
            m_cameraController = new MayaStyleCameraController();
            m_cameraController.Camera = m_camera;

            Sphere3F sphere = new Sphere3F(new Vec3F(0, 0, 0), 25.0f);
            float nearZ = m_camera.PerspectiveNearZ;
            m_camera.ZoomOnSphere(sphere);
            m_camera.PerspectiveNearZ = nearZ;
            m_camera.CameraChanged += new EventHandler(CameraChanged);

        }
Esempio n. 2
0
        /// <summary>
        /// Extends sphere to enclose another sphere</summary>
        /// <param name="sphere">Sphere to enclose</param>
        /// <returns>Extended sphere</returns>
        public Sphere3F Extend(Sphere3F sphere)
        {
            if (!m_initialized)
            {
                Center = sphere.Center;
                Radius = sphere.Radius;
                m_initialized = true;
            }
            else if (!Contains(sphere))
            {
                if (Center == sphere.Center)
                {
                    Radius = sphere.Radius;
                }
                else
                {
                    Vec3F normal = Vec3F.Normalize(sphere.Center - Center);
                    Vec3F p1 = sphere.Center + (normal * sphere.Radius);
                    Vec3F p2 = Center - (normal * Radius);
                    Radius = (p2 - p1).Length / 2.0f;
                    Center = (p1 + p2) / 2.0f;
                }
            }

            return this;
        }
Esempio n. 3
0
        /// <summary>
        /// Extends sphere to enclose another sphere</summary>
        /// <param name="sphere">Sphere to enclose</param>
        /// <returns>Extended sphere</returns>
        public Sphere3F Extend(Sphere3F sphere)
        {
            if (!m_initialized)
            {
                Center        = sphere.Center;
                Radius        = sphere.Radius;
                m_initialized = true;
            }
            else if (!Contains(sphere))
            {
                if (Center == sphere.Center)
                {
                    Radius = sphere.Radius;
                }
                else
                {
                    Vec3F normal = Vec3F.Normalize(sphere.Center - Center);
                    Vec3F p1     = sphere.Center + (normal * sphere.Radius);
                    Vec3F p2     = Center - (normal * Radius);
                    Radius = (p2 - p1).Length / 2.0f;
                    Center = (p1 + p2) / 2.0f;
                }
            }

            return(this);
        }
Esempio n. 4
0
        private void TestToStringWithCulture(CultureInfo culture)
        {
            CultureInfo originalCulture = Thread.CurrentThread.CurrentCulture;
            Thread.CurrentThread.CurrentCulture = culture;
            try
            {
                string listSeparator = culture.TextInfo.ListSeparator;
                string decimalSeparator = culture.NumberFormat.NumberDecimalSeparator;
                var o = new Sphere3F(new Vec3F(1.1f, 2.2f, 3.3f), 4.4f);

                string s = o.ToString(null, null);
                TestToStringResults(o, s, listSeparator, decimalSeparator);

                string s2 = o.ToString();
                Assert.AreEqual(s, s2);

                s = o.ToString("G", culture);
                TestToStringResults(o, s, listSeparator, decimalSeparator);

                s = o.ToString("R", culture);
                TestToStringResults(o, s, listSeparator, decimalSeparator);
            }
            finally
            {
                Thread.CurrentThread.CurrentCulture = originalCulture;
            }
        }
Esempio n. 5
0
File: Box.cs Progetto: zparr/ATF
        /// <summary>
        /// Extends box to contain sphere, initializing this box if it is currently uninitialized</summary>
        /// <param name="sphere">Input sphere</param>
        /// <returns>The extended box</returns>
        public Box Extend(Sphere3F sphere)
        {
            float r = sphere.Radius;

            Extend(sphere.Center + new Vec3F(r, r, r));
            Extend(sphere.Center - new Vec3F(r, r, r));
            return(this);
        }
Esempio n. 6
0
        /// <summary>
        /// Extends sphere to enclose box</summary>
        /// <param name="box">Axis-aligned box to enclose</param>
        /// <returns>Extended sphere</returns>
        public Sphere3F Extend(Box box)
        {
            Sphere3F tmp = new Sphere3F((box.Max + box.Min) / 2.0f,
                                        (box.Max - box.Min).Length / 2.0f);

            Extend(tmp);

            return(this);
        }
Esempio n. 7
0
 private void TestToStringResults(Sphere3F o, string s, string listSeparator, string decimalSeparator)
 {
     string[] results = s.Split(new[] { listSeparator }, StringSplitOptions.RemoveEmptyEntries);
     Assert.AreEqual(results.Length, 4);
     foreach (string oneFloatString in results)
         Assert.True(oneFloatString.Contains(decimalSeparator));
     Assert.AreEqual(float.Parse(results[0]), o.Center.X);
     Assert.AreEqual(float.Parse(results[1]), o.Center.Y);
     Assert.AreEqual(float.Parse(results[2]), o.Center.Z);
     Assert.AreEqual(float.Parse(results[3]), o.Radius);
 }
Esempio n. 8
0
 /// <summary>
 /// Gets the DomNode attribute as a Sphere3F</summary>
 /// <param name="domNode">DomNode holding value</param>
 /// <param name="attribute">Attribute of the DomNode that contains the data</param>
 /// <returns>DomNode attribute as a Sphere3F</returns>
 public static Sphere3F GetSphere(DomNode domNode, AttributeInfo attribute)
 {
     Sphere3F s = new Sphere3F();
     float[] value = domNode.GetAttribute(attribute) as float[];
     if (value != null)
     {
         s.Center = new Vec3F(value[0], value[1], value[2]);
         s.Radius = value[3];
     }
     return s;
 }
Esempio n. 9
0
 public static void DrawSphere(Sphere3F sphere, Color c)
 {
     // create matrix from AABB            
     T1.Set(sphere.Center);
     float scale = 2*sphere.Radius;
     T1.M11 = scale;
     T1.M22 = scale;
     T1.M33 = scale;
     DrawSphere(T1, c);
     
 }
Esempio n. 10
0
 public static void DrawSphere(GUILayer.SimpleRenderingContext context, Sphere3F sphere, Color c)
 {
     // create matrix from AABB            
     T1.Set(sphere.Center);
     float scale = 2*sphere.Radius;
     T1.M11 = scale;
     T1.M22 = scale;
     T1.M33 = scale;
     DrawSphere(context, T1, c);
     
 }
Esempio n. 11
0
        /// <summary>
        /// Constructor</summary>
        /// <param name="yFOV">Y field of view, in radians</param>
        /// <param name="nearZ">Near z plane constant</param>
        /// <param name="farZ">Far z plane constant</param>
        public CanvasControl3D(float yFOV, float nearZ, float farZ)
        {
            m_camera = new Camera();
            m_camera.SetPerspective(yFOV, 1.0f, nearZ, farZ);

            m_cameraController = new TrackBallCameraController();
            m_cameraController.Camera = m_camera;

            m_camera.CameraChanged += CameraChanged;
            Sphere3F sphere = new Sphere3F(new Vec3F(0, 0, 0), 25.0f);
            m_camera.ZoomOnSphere(sphere);
        }
Esempio n. 12
0
 /// <summary>
 /// Tests if frustum contains the given sphere</summary>
 /// <param name="sphere">Sphere</param>
 /// <returns>True iff frustum contains the given sphere</returns>
 public bool Contains(Sphere3F sphere)
 {
     for (int i = 0; i < 6; i++)
     {
         float distance = m_planes[i].SignedDistance(sphere.Center);
         if (distance < -sphere.Radius)
         {
             return(false);
         }
     }
     return(true);
 }
Esempio n. 13
0
        private void GenThumbnail(Uri resourceUri, string thumbnailPath)
        {
            NativeObjectAdapter gameLevel = GameEngine.GetGameLevel();

            try
            {
                IResource   resource = m_resourceService.Load(resourceUri);
                IGameObject gob      = m_resourceConverterService.Convert(resource);
                if (gob == null)
                {
                    return;
                }

                m_game.RootGameObjectFolder.GameObjects.Add(gob);

                GameEngine.SetRenderState(m_renderState);
                GameEngine.SetGameLevel(m_game.Cast <NativeObjectAdapter>());
                GameEngine.Update(0, 0, true);

                IBoundable boundable = gob.Cast <IBoundable>();
                Sce.Atf.VectorMath.Sphere3F sphere = boundable.BoundingBox.ToSphere();

                if (Math.Abs(sphere.Radius) <= float.Epsilon)
                {
                    sphere.Radius = 1.0f;
                }

                m_cam.SetPerspective(
                    (float)Math.PI / 4,
                    1.0f,
                    sphere.Radius * 0.01f,
                    sphere.Radius * 4.0f);


                Vec3F camPos = sphere.Center + new Vec3F(sphere.Radius, sphere.Radius, sphere.Radius) * 1.5f;
                m_cam.Set(camPos, sphere.Center, new Vec3F(0, 1, 0));

                GameEngine.Begin(m_renderSurface.InstanceId, m_cam.ViewMatrix, m_cam.ProjectionMatrix);
                GameEngine.RenderGame();
                GameEngine.End();
                GameEngine.SaveRenderSurfaceToFile(m_renderSurface.InstanceId, thumbnailPath);
                m_game.RootGameObjectFolder.GameObjects.Remove(gob);

                m_resourceService.Unload(resourceUri);
            }
            finally
            {
                GameEngine.SetGameLevel(gameLevel);
            }
        }
Esempio n. 14
0
 /// <summary>
 /// Constructs a sphere from another sphere</summary>
 /// <param name="sphere">Other sphere</param>
 public Sphere3F(Sphere3F sphere)
 {
     Center = sphere.Center;
     Radius = sphere.Radius;
     m_initialized = true;
 }
Esempio n. 15
0
File: AABB.cs Progetto: ldh9451/XLE
 /// <summary>
 /// Extends box to contain sphere, initializing this box if it is currently uninitialized</summary>
 /// <param name="sphere">Input sphere</param>
 /// <returns>The extended box</returns>
 public void Extend(Sphere3F sphere)
 {
     float r = sphere.Radius;
     Extend(sphere.Center + new Vec3F(r, r, r));
     Extend(sphere.Center - new Vec3F(r, r, r));
 }
Esempio n. 16
0
        /// <summary>
        /// Extends sphere to enclose box</summary>
        /// <param name="box">Axis-aligned box to enclose</param>
        /// <returns>Extended sphere</returns>
        public Sphere3F Extend(Box box)
        {
            Sphere3F tmp = new Sphere3F((box.Max + box.Min) / 2.0f,
                (box.Max - box.Min).Length / 2.0f);

            Extend(tmp);

            return this;
        }
Esempio n. 17
0
 /// <summary>
 /// Constructs a sphere from another sphere</summary>
 /// <param name="sphere">Other sphere</param>
 public Sphere3F(Sphere3F sphere)
 {
     Center        = sphere.Center;
     Radius        = sphere.Radius;
     m_initialized = true;
 }
Esempio n. 18
0
        /// <summary>
        /// Tests if other sphere is inside this sphere</summary>
        /// <param name="sphere">Other sphere</param>
        /// <returns>True iff other sphere is inside this sphere</returns>
        public bool Contains(Sphere3F sphere)
        {
            float length = (sphere.Center - Center).Length + sphere.Radius;

            return(length < Radius);
        }
Esempio n. 19
0
 /// <summary>
 /// Sets the DomNode attribute value to the given Sphere3F</summary>
 /// <param name="domNode">DomNode holding value</param>
 /// <param name="attribute">Attribute of the DomNode that contains the data</param>
 /// <param name="s">Sphere3F value to be set</param>
 public static void SetSphere(DomNode domNode, AttributeInfo attribute, Sphere3F s)
 {
     float[] value = new float[4];
     value[0] = s.Center.X;
     value[1] = s.Center.Y;
     value[2] = s.Center.Z;
     value[3] = s.Radius;
     domNode.SetAttribute(attribute, value);
 }
Esempio n. 20
0
            // render the scene.
            public void Render()
            {
                if (GameEngine.IsInError ||
                    SurfaceId == 0 ||
                    Visible == false ||
                    Width == 0 ||
                    Height == 0 ||
                    Game == null)
                {
                    return;
                }


                NativeObjectAdapter gameLevel = GameEngine.GetGameLevel();

                try
                {
                    NativeObjectAdapter game = Game.As <NativeObjectAdapter>();
                    GameEngine.SetGameLevel(game);
                    GameEngine.SetRenderState(m_renderState);
                    if (Game.RootGameObjectFolder.GameObjects.Count > 0)
                    {
                        GameEngine.Update(0, 0, true);
                    }

                    if (ResetCamera)
                    {
                        // save view type
                        ViewTypes viewtype = this.ViewType;
                        ViewType = ViewTypes.Perspective;
                        Size       sz        = ClientSize;
                        float      aspect    = (float)sz.Width / (float)sz.Height;
                        IBoundable boundable = Game.RootGameObjectFolder.Cast <IBoundable>();
                        Sce.Atf.VectorMath.Sphere3F sphere = boundable.BoundingBox.ToSphere();
                        float nearZ = sphere.Radius * 0.01f;
                        nearZ = Math.Min(0.1f, nearZ);
                        Camera.SetPerspective(
                            (float)Math.PI / 4,
                            aspect,
                            nearZ,
                            sphere.Radius * 10.0f);

                        Vec3F camPos = sphere.Center + new Vec3F(sphere.Radius, sphere.Radius, sphere.Radius) * 1.2f;
                        Camera.Set(camPos, sphere.Center, new Vec3F(0, 1, 0));
                        ViewType    = viewtype;
                        ResetCamera = false;
                    }

                    GameEngine.Begin(SurfaceId, Camera.ViewMatrix, Camera.ProjectionMatrix);
                    if (Game.RootGameObjectFolder.GameObjects.Count > 0)
                    {
                        GameEngine.RenderGame();
                    }
                    string str = "View Type: " + ViewType.ToString();
                    GameEngine.DrawText2D(str, Util3D.CaptionFont, 1, 1, Color.White);
                    GameEngine.End();
                }
                finally
                {
                    GameEngine.SetGameLevel(gameLevel);
                }
            }
Esempio n. 21
0
File: Util.cs Progetto: ldh9451/XLE
 public static void ZoomOnSphere(Camera cam, Sphere3F sphere)
 {
     float nearZ = cam.PerspectiveNearZ;
     // todo refactor cam.ZoomOnSphere()
     cam.ZoomOnSphere(sphere);
     cam.PerspectiveNearZ = nearZ;
 }
Esempio n. 22
0
 /// <summary>
 /// Extends box to contain sphere, initializing this box if it is currently uninitialized</summary>
 /// <param name="sphere">Input sphere</param>
 /// <returns>The extended box</returns>
 public Box Extend(Sphere3F sphere)
 {
     float r = sphere.Radius;
     Extend(sphere.Center + new Vec3F(r, r, r));
     Extend(sphere.Center - new Vec3F(r, r, r));
     return this;
 }
Esempio n. 23
0
        /// <summary>
        /// Positions and orients the camera so that the given sphere fills the view</summary>
        /// <param name="sphere">Sphere to zoom to, in world space</param>
        public void ZoomOnSphere(Sphere3F sphere)
        {
            float d = sphere.Radius;
            if (d == 0)
                d = 1;
            
            // avoid getting too far away or too close
            if (d > FarZ)
                d = FarZ;
            else if (d < NearZ)
                d = NearZ;

            m_focusRadius = d;

            // The eye and the look-at-point are in what might be called the "world view" system.
            // So, the world coordinate of the sphere center needs to be xformed by AxisSystem.
            Vec3F lookAt;
            Vec3F up;
            GetViewVectors(out lookAt, out up);

            Vec3F lookAtPoint;
            AxisSystem.Transform(sphere.Center, out lookAtPoint);

            Vec3F eye = lookAtPoint - lookAt * d;
            
            Set(eye, lookAtPoint, up);

            // Adjust near plane if we're in perspective mode. Otherwise, we should leave it alone
            //  because orthographic modes might need a large negative near-Z.
            if (ViewType == ViewTypes.Perspective)
            {
                float nearZ = Math.Abs(sphere.Radius) * 0.1f;
                nearZ = Math.Max(nearZ, 0.001f);
                PerspectiveNearZ = nearZ;
            }
        }
Esempio n. 24
0
 /// <summary>
 /// Tests if frustum contains the given sphere</summary>
 /// <param name="sphere">Sphere</param>
 /// <returns>True iff frustum contains the given sphere</returns>
 public bool Contains(Sphere3F sphere)
 {
     for (int i = 0; i < 6; i++)
     {
         float distance = m_planes[i].SignedDistance(sphere.Center);
         if (distance < -sphere.Radius)
         {
             return false;
         }
     }
     return true;
 }
Esempio n. 25
0
        private Sphere3F CalcBoundSphere(SceneNode root)
        {
            Sphere3F sphere = Sphere3F.Empty;

            foreach (SceneNode node in root.Children)
            {
                IBoundable boundable = Adapters.As<IBoundable>(node.DomNode);
                Sphere3F localSphere = new Sphere3F();

                if (boundable != null)
                {
                    localSphere.Extend(boundable.BoundingBox);
                }

                if (localSphere.Radius > 0)
                {
                    sphere.Extend(localSphere);
                }
            }

            return sphere;
        }
Esempio n. 26
0
 /// <summary>
 /// Tests if other sphere is inside this sphere</summary>
 /// <param name="sphere">Other sphere</param>
 /// <returns>True iff other sphere is inside this sphere</returns>
 public bool Contains(Sphere3F sphere)
 {
     float length = (sphere.Center - Center).Length + sphere.Radius;
     return (length < Radius);
 }