Esempio n. 1
0
        protected void ResetView()
        {
            vp.Reset();

            try
            {
                Vector3 min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
                Vector3 max = new Vector3(float.MinValue, float.MinValue, float.MinValue);

                int[] rank = { mesh.NumberVertices };
                if (mesh.VertexBuffer.Description.VertexFormat == (VertexFormats)0x112)
                {
                    CustomVertex.PositionNormalTextured[] vertices = (CustomVertex.PositionNormalTextured[])mesh.LockVertexBuffer(typeof(CustomVertex.PositionNormalTextured), LockFlags.None, rank);
                    try
                    {
                        foreach (CustomVertex.PositionNormalTextured v in vertices)
                        {
                            if (v.X < min.X)
                            {
                                min.X = v.X;
                            }
                            if (v.Y < min.Y)
                            {
                                min.Y = v.Y;
                            }
                            if (v.Z < min.Z)
                            {
                                min.Z = v.Z;
                            }

                            if (v.X > max.X)
                            {
                                max.X = v.X;
                            }
                            if (v.Y > max.Y)
                            {
                                max.Y = v.Y;
                            }
                            if (v.Z > max.Z)
                            {
                                max.Z = v.Z;
                            }
                        }
                    }
                    finally
                    {
                        mesh.UnlockVertexBuffer();
                    }
                }
                else if (mesh.VertexBuffer.Description.VertexFormat == VertexFormats.PositionNormal)
                {
                    CustomVertex.PositionNormal[] vertices = (CustomVertex.PositionNormal[])mesh.LockVertexBuffer(typeof(CustomVertex.PositionNormalTextured), LockFlags.None, rank);
                    try
                    {
                        foreach (CustomVertex.PositionNormal v in vertices)
                        {
                            if (v.X < min.X)
                            {
                                min.X = v.X;
                            }
                            if (v.Y < min.Y)
                            {
                                min.Y = v.Y;
                            }
                            if (v.Z < min.Z)
                            {
                                min.Z = v.Z;
                            }

                            if (v.X > max.X)
                            {
                                max.X = v.X;
                            }
                            if (v.Y > max.Y)
                            {
                                max.Y = v.Y;
                            }
                            if (v.Z > max.Z)
                            {
                                max.Z = v.Z;
                            }
                        }
                    }
                    finally
                    {
                        mesh.UnlockVertexBuffer();
                    }
                }

                if (min == max)
                {
                    max.X = min.X + 1;
                    max.Y = min.Y + 1;
                    max.Z = min.Z + 1;
                }


                Vector3 center = new Vector3((float)((max.X + min.X) / 2.0), (float)((max.Y + min.Y) / 2.0), (float)((max.Z + min.Z) / 2.0));
                //vp.Aspect = 1;//this.Size.Width / this.Size.Height;
                //vp.Aspect = Math.Max(this.Width / this.Height , this.Size.Height / this.Size.Width);

                Vector3 boundingSphereRadius = new Vector3(min.X - center.X, min.Y - center.Y, min.Z - center.Z);
                double  radius = boundingSphereRadius.Length() / vp.Aspect;
                double  dist   = radius / Math.Sin(vp.FoV / 2.0);

                //for (int i=0; i< mesh.NumberVertices; i++) vertices[i].Subtract(center);

                vp.ObjectCenter   = center;
                vp.X              = -center.X;
                vp.Y              = -center.Y;
                vp.Z              = -center.Z;
                vp.CameraTarget   = new Vector3(0, 0, 0);
                vp.CameraPosition = new Vector3(
                    0,                     //center.X,
                    0,                     //center.Y,
                    (float)dist);          // + center.Z);
                vp.NearPlane = 0.01f;      //(float)(dist-radius);
                vp.FarPlane  = (float)((dist + radius));


                //bbox = Mesh.Box(device, max.X-min.X, max.Y-min.Y, max.Z-min.Z);
#if DEBUG
                bbox = Mesh.Sphere(device, 0.05f, 12, 4);
#endif

                //Unlock the buffer
            }
            catch {}
        }