Beispiel #1
0
        public GpuLBVHNode[] BuildTDData(out TriangleDataInfo[] sortedTris)
        {
            var helper = new TopDownBvhAccell();
            helper.Init(scene, null);
            sortedTris = helper.triangles;

            return helper.GetData().ToArray();
        }
Beispiel #2
0
 public LNode[] BuildLData(out TriangleDataInfo[] sortedTris)
 {
     var helper =
         new
             LinearBvhAccelleration();
     //BottomUpBvhAccelleration();
     helper.Init(scene, null);
     sortedTris = helper.triangles;
     return helper.GetData().ToArray();
 }
Beispiel #3
0
        public TriangleMesh(GeometryInfo geoInfo)
        {
            MeshProfile = new SurfaceProfile();
            MaterialName = geoInfo.MaterialName;
            MeshName = geoInfo.Name;
            Vertices = geoInfo.VertexData.ToPointList().ToArray();
            Normals = geoInfo.NormalData.ToNormalList().ToArray();
            TexCoords = geoInfo.TextureData.TodVectorList().ToArray();
            var triangles = new List<TriangleDataInfo>();
            int index = 0;
            if (geoInfo.IndexData.Any())
            {
                int[] ni = null;

                int[] ti = null;
                var hasNormals = geoInfo.NormalIndexData != null && geoInfo.NormalIndexData.Any();
                var hasTexCoords = geoInfo.TextureIndexData != null && geoInfo.TextureIndexData.Any();
                if (hasTexCoords)
                {
                     ti = geoInfo.TextureIndexData.ToArray();
                }
                if (hasNormals)
                {
                     ni = geoInfo.NormalIndexData.ToArray();
                }
                HasNormals = hasNormals;
                HasTexCoords = hasTexCoords;
                for (int i = 0; i < geoInfo.IndexData.Count; i += 3)
                {
                    index++;
                    var newTriangle = new TriangleDataInfo()
                    {
                        v0 = new MeshVertexInfo()
                        {
                            NormalIndex = hasNormals ? ni[i] - 1 : -1,
                            VertexIndex = geoInfo.IndexData[i] - delta,
                            TexCoordIndex = hasTexCoords ? ti[i] - 1 : -1
                        },
                        v1 = new MeshVertexInfo()
                        {
                            NormalIndex = hasNormals ? ni[i + 1] - 1 : -1,
                            VertexIndex = geoInfo.IndexData[i + 1] - delta,
                            TexCoordIndex = hasTexCoords ? ti[i + 1] - 1 : -1
                        },
                        v2 = new MeshVertexInfo()
                        {
                            NormalIndex = hasNormals ? ni[i + 2] - 1 : -1,
                            VertexIndex = geoInfo.IndexData[i + 2] - delta,
                            TexCoordIndex = hasTexCoords ? ti[i + 2] - 1 : -1
                        },

                    };
                    triangles.Add(newTriangle);
                }
                WorldBound = new AABB(this.Vertices);
                this.Triangles = triangles.ToArray();
                this.BottomLevelBVH = new AccellerationData() {data = BvhDataAdapter.CreateBVH(Triangles, Vertices)};
            }
        }
Beispiel #4
0
        public bool Intersect(ref TriangleDataInfo ti, Point[] meshVertices, ref RayData ray, ref float thit, ref float u, ref float v)
        {
            var p0 = meshVertices[ti.v0.VertexIndex];
            var p1 = meshVertices[ti.v1.VertexIndex];
            var p2 = meshVertices[ti.v2.VertexIndex];
            Vector e1, e2, s1;
            Point.Sub(ref p1, ref p0, out e1);
            Point.Sub(ref p2, ref p0, out e2);

            Vector.Cross(ref ray.Dir, ref e2, out s1);


            var divisor = Vector.Dot(ref s1, ref e1);
            if (divisor < MathLab.Epsilon)
                return false;

            var invDivisor = 1f / divisor;


            Vector d, s2;
            Point.Sub(ref ray.Org, ref p0, out d);
            var b1 = Vector.Dot(ref d, ref s1) * invDivisor;
            if (b1 < 0f)
                return false;

            Vector.Cross(ref d, ref  e1, out s2);
            var b2 = Vector.Dot(ref ray.Dir, ref  s2) * invDivisor;
            if (b2 < 0f)
                return false;

            var b0 = 1f - b1 - b2;
            if (b0 < 0f)
                return false;

            var t = Vector.Dot(ref e2, ref s2) * invDivisor;
            if (t < ray.minT || t > ray.maxT)
                return false;

            thit = t;
            u = b1;
            v = b2;

            return true;
        }
Beispiel #5
0
        public static void Sample(ref TriangleDataInfo tri,Point[] meshVertices, float u0, float u1, out Point samplePoint, out float b0, out float b1, out float b2)
            {
            b0 = 0f;
            b1 = 0f;
            MC.UniformSampleTriangle(u0, u1, ref  b0, ref b1);

            var p0 = meshVertices[tri.v0.VertexIndex];
            var p1 = meshVertices[tri.v1.VertexIndex] - p0;
            var p2 = meshVertices[tri.v2.VertexIndex] - p0;
            b2 = 1f - (b0) - (b1);


            samplePoint = new Point(
                ((b0) * p0.x + (b1) * p1.x + (b2) * p2.x),
                ((b0) * p0.y + (b1) * p1.y + (b2) * p2.y),
                ((b0) * p0.z + (b1) * p1.z + (b2) * p2.z));
        }
Beispiel #6
0
 public int CompareTriangles(Triangle t1, Triangle t2, Axis a = Axis.PLANE_Y)
 {
     var p1 = t1.Center(sceneVertices);
     var p2 = t2.Center(sceneVertices);
     return p1.Component(a).CompareTo(p2.Component(a));
 }