private static void Subdivide(IList<Vector3D> positions, IndicesUnsignedInt indices, TriangleIndicesUnsignedInt triangle, int level) { if (level > 0) { positions.Add(((positions[triangle.I0] + positions[triangle.I1]) * 0.5).Normalize()); positions.Add(((positions[triangle.I1] + positions[triangle.I2]) * 0.5).Normalize()); positions.Add(((positions[triangle.I2] + positions[triangle.I0]) * 0.5).Normalize()); int i01 = positions.Count - 3; int i12 = positions.Count - 2; int i20 = positions.Count - 1; // // Subdivide input triangle into four triangles // --level; Subdivide(positions, indices, new TriangleIndicesUnsignedInt(triangle.I0, i01, i20), level); Subdivide(positions, indices, new TriangleIndicesUnsignedInt(i01, triangle.I1, i12), level); Subdivide(positions, indices, new TriangleIndicesUnsignedInt(i01, i12, i20), level); Subdivide(positions, indices, new TriangleIndicesUnsignedInt(i20, i12, triangle.I2), level); } else { indices.AddTriangle(triangle); } }
public void TriangleIndicesTest() { TriangleIndicesUnsignedInt triangle = new TriangleIndicesUnsignedInt(0, 1, 2); Assert.AreEqual(0, triangle.UI0); Assert.AreEqual(1, triangle.UI1); Assert.AreEqual(2, triangle.UI2); TriangleIndicesUnsignedInt triangle2 = triangle; Assert.AreEqual(triangle, triangle2); TriangleIndicesUnsignedInt triangle3 = new TriangleIndicesUnsignedInt(3, 4, 5); Assert.AreNotEqual(triangle, triangle3); }
public void AddTriangle(TriangleIndicesUnsignedInt triangle) { _values.Add(triangle.UI0); _values.Add(triangle.UI1); _values.Add(triangle.UI2); }
private static void Subdivide(SubdivisionMesh subdivisionMesh, TriangleIndicesUnsignedInt triangle, int level) { if (level > 0) { IList<Vector3D> positions = subdivisionMesh.Positions; Vector3D n01 = ((positions[triangle.I0] + positions[triangle.I1]) * 0.5).Normalize(); Vector3D n12 = ((positions[triangle.I1] + positions[triangle.I2]) * 0.5).Normalize(); Vector3D n20 = ((positions[triangle.I2] + positions[triangle.I0]) * 0.5).Normalize(); Vector3D p01 = n01.MultiplyComponents(subdivisionMesh.Ellipsoid.Radii); Vector3D p12 = n12.MultiplyComponents(subdivisionMesh.Ellipsoid.Radii); Vector3D p20 = n20.MultiplyComponents(subdivisionMesh.Ellipsoid.Radii); positions.Add(p01); positions.Add(p12); positions.Add(p20); int i01 = positions.Count - 3; int i12 = positions.Count - 2; int i20 = positions.Count - 1; if ((subdivisionMesh.Normals != null) || (subdivisionMesh.TextureCoordinate != null)) { Vector3D d01 = subdivisionMesh.Ellipsoid.GeodeticSurfaceNormal(p01); Vector3D d12 = subdivisionMesh.Ellipsoid.GeodeticSurfaceNormal(p12); Vector3D d20 = subdivisionMesh.Ellipsoid.GeodeticSurfaceNormal(p20); if (subdivisionMesh.Normals != null) { subdivisionMesh.Normals.Add(d01.ToVector3H()); subdivisionMesh.Normals.Add(d12.ToVector3H()); subdivisionMesh.Normals.Add(d20.ToVector3H()); } if (subdivisionMesh.TextureCoordinate != null) { subdivisionMesh.TextureCoordinate.Add(SubdivisionUtility.ComputeTextureCoordinate(d01)); subdivisionMesh.TextureCoordinate.Add(SubdivisionUtility.ComputeTextureCoordinate(d12)); subdivisionMesh.TextureCoordinate.Add(SubdivisionUtility.ComputeTextureCoordinate(d20)); } } // // Subdivide input triangle into four triangles // --level; Subdivide(subdivisionMesh, new TriangleIndicesUnsignedInt(triangle.I0, i01, i20), level); Subdivide(subdivisionMesh, new TriangleIndicesUnsignedInt(i01, triangle.I1, i12), level); Subdivide(subdivisionMesh, new TriangleIndicesUnsignedInt(i01, i12, i20), level); Subdivide(subdivisionMesh, new TriangleIndicesUnsignedInt(i20, i12, triangle.I2), level); } else { subdivisionMesh.Indices.AddTriangle(triangle); } }
private static void Subdivide(SubdivisionMesh subdivisionMesh, TriangleIndicesUnsignedInt triangle, int level) { if (level > 0) { IList<Vector3D> positions = subdivisionMesh.Positions; Vector3D p01 = ((positions[triangle.I0] + positions[triangle.I1]) * 0.5).Normalize(); Vector3D p12 = ((positions[triangle.I1] + positions[triangle.I2]) * 0.5).Normalize(); Vector3D p20 = ((positions[triangle.I2] + positions[triangle.I0]) * 0.5).Normalize(); positions.Add(p01); positions.Add(p12); positions.Add(p20); int i01 = positions.Count - 3; int i12 = positions.Count - 2; int i20 = positions.Count - 1; if (subdivisionMesh.Normals != null) { subdivisionMesh.Normals.Add(p01.ToVector3H()); subdivisionMesh.Normals.Add(p12.ToVector3H()); subdivisionMesh.Normals.Add(p20.ToVector3H()); } if (subdivisionMesh.TextureCoordinate != null) { subdivisionMesh.TextureCoordinate.Add(SubdivisionUtility.ComputeTextureCoordinate(p01)); subdivisionMesh.TextureCoordinate.Add(SubdivisionUtility.ComputeTextureCoordinate(p12)); subdivisionMesh.TextureCoordinate.Add(SubdivisionUtility.ComputeTextureCoordinate(p20)); } // // Subdivide input triangle into four triangles // --level; Subdivide(subdivisionMesh, new TriangleIndicesUnsignedInt(triangle.I0, i01, i20), level); Subdivide(subdivisionMesh, new TriangleIndicesUnsignedInt(i01, triangle.I1, i12), level); Subdivide(subdivisionMesh, new TriangleIndicesUnsignedInt(i01, i12, i20), level); Subdivide(subdivisionMesh, new TriangleIndicesUnsignedInt(i20, i12, triangle.I2), level); } else { subdivisionMesh.Indices.AddTriangle(triangle); } }