Example #1
0
 /// <summary>
 /// Returns true if two vertices are nearly equal. For example the
 /// tangent or normal data does not have to match 100%.
 /// Used to optimize vertex buffers and to generate indices.
 /// </summary>
 /// <param name="a">A</param>
 /// <param name="b">B</param>
 /// <returns>Bool</returns>
 public static bool NearlyEquals(TangentVertex a, TangentVertex b)
 {
     // Position has to match, else it is just different vertex
     return a.pos == b.pos &&
         // Ignore blend indices and blend weights, they are the same
         // anyway, because they are calculated from the bone distances.
         Math.Abs(a.uv.X - b.uv.X) < 0.0001f &&
         Math.Abs(a.uv.Y - b.uv.Y) < 0.0001f &&
         // Normals and tangents do not have to be very close, we can't see
         // any difference between small variations here, but by optimizing
         // similar vertices we can improve the overall rendering performance.
         (a.normal - b.normal).Length() < 0.1f &&
         (a.tangent - b.tangent).Length() < 0.1f;
 }
Example #2
0
        /// <summary>
        /// Draw plane vertices
        /// </summary>
        private void DrawPlaneVertices()
        {
            // Calculate right and dir vectors for constructing the plane.
            // The following code might look strange, but we have to make sure
            // that we always get correct up, right and dir vectors. Cross products
            // can return (0, 0, 0) if the vectors are parallel!
            Vector3 up = plane.Normal;
            if (up.Length() == 0)
                up = new Vector3(0, 0, 1);
            Vector3 helperVec = Vector3.Cross(up, new Vector3(1, 0, 0));
            if (helperVec.Length() == 0)
                helperVec = new Vector3(0, 1, 0);
            Vector3 right = Vector3.Cross(helperVec, up);
            Vector3 dir = Vector3.Cross(up, right);
            float dist = plane.D;

            TangentVertex[] vertices = new TangentVertex[]
            {
                // Make plane VERY big and tile texture every 10 meters
                new TangentVertex(
                    (-right-dir)*size+up*dist, -size/Tiling, -size/Tiling, up, right),
                new TangentVertex(
                    (-right+dir)*size+up*dist, -size/Tiling, +size/Tiling, up, right),
                new TangentVertex(
                    (right-dir)*size+up*dist, +size/Tiling, -size/Tiling, up, right),
                new TangentVertex(
                    (right+dir)*size+up*dist, +size/Tiling, +size/Tiling, up, right),
            };

            // Draw the plane (just 2 simple triangles)
            BaseGame.Device.DrawUserPrimitives(
                PrimitiveType.TriangleStrip, vertices, 0, 2);
        }