GLSharp.Content.MeshItem.ComputeBoundingVolume C# (CSharp) Method

ComputeBoundingVolume() public method

public ComputeBoundingVolume ( ) : void
return void
        public void ComputeBoundingVolume()
        {
            Vector3 vertexAverage = new Vector3(null);

            /*compute center as vertex average*/
            for(int i = 0; i < OffsetNormal; i += 3) {
                vertexAverage.Add3(Mesh[i], Mesh[i + 1], Mesh[i + 2]);
            }

            vertexAverage.Scale(1.0f / (OffsetNormal / 3.0f));

            int vertexIndex = -1;
            float dist = -1.0f;
            float maxDist = -1.0f;

            /*get the maximum distance from center and the furthest vertex*/
            for (int i = 0; i < OffsetNormal; i += 3) {
                dist = vertexAverage.Distance3(Mesh[i], Mesh[i + 1], Mesh[i + 2]);

                if (dist > maxDist) {
                    maxDist = dist;
                    vertexIndex = i / 3;
                }
            }

            this.BoundingVolume = new VertexBoundingVolume();
            this.BoundingVolume.Center = vertexAverage;
            this.BoundingVolume.VertexIndex = vertexIndex;
        }

Usage Example

Beispiel #1
0
        //------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------
        public ResourceItem Convert(object input, String collection)
        {
            if (input == null)
                return null;

            MeshObject obj = (MeshObject) input;
            MeshItem ret = new MeshItem(this._graphics);

            if (obj.Indexes == null) obj.Indexes = SystemCore.Environment.CreateUInt16Array(0);
            if (obj.Vertices == null) obj.Vertices = SystemCore.Environment.CreateFloat32Array(0);
            if (obj.Normals == null) obj.Normals = SystemCore.Environment.CreateFloat32Array(0);
            if (obj.UVs == null) obj.UVs = SystemCore.Environment.CreateFloat32Array(0);

            ret.Indexes = SystemCore.Environment.CreateUInt16ArrayFromArray(obj.Indexes);
            ret.Mesh = SystemCore.Environment.CreateFloat32Array((ulong) (obj.Vertices.Length + obj.Normals.Length + obj.UVs.Length));
            ret.OffsetPosition = 0;
            ret.OffsetNormal = obj.Vertices.Length;
            ret.OffsetUv = obj.Vertices.Length + obj.Normals.Length;

            /*mesh data*/
            for (int i = 0; i < obj.Vertices.Length; i++) ret.Mesh[i] = obj.Vertices[i];
            for (int i = 0; i < obj.Normals.Length; i++)  ret.Mesh[i + ret.OffsetNormal] = obj.Normals[i];
            for (int i = 0; i < obj.UVs.Length; i++)      ret.Mesh[i + ret.OffsetUv] = obj.UVs[i];
            /*bounding volume*/
            ret.ComputeBoundingVolume();

            return ret;
        }