示例#1
0
    public int BVHTreeToArrayDfs(BVHNode node, List <RTBVHNode> BVHTreeList, List <RTTriangle> triangleList, int listIndex)
    {
        if (node == null)
        {
            return(0);
        }

        if (node.left == null && node.right == null)
        {
            RTBVHNode tempNode = new RTBVHNode()
            {
                isLeaf        = 1,
                startIndex    = (uint)triangleList.Count,
                triangleCount = (uint)node.triangles.Count,
                subTreeCount  = 0,
            };

            BVHTreeList.Add(tempNode);

            foreach (Triangle tri in node.triangles)
            {
                RTTriangle tempTriangle = new RTTriangle()
                {
                    v0       = tri.v0,
                    v1       = tri.v1,
                    v2       = tri.v2,
                    normal   = tri.normal,
                    matIndex = tri.matIndex,
                };
                triangleList.Add(tempTriangle);
            }
            return(1);
        }

        RTBVHNode tempNode2 = new RTBVHNode();

        BVHTreeList.Add(tempNode2);
        int index = BVHTreeList.Count - 1;

        int leftCount  = BVHTreeToArrayDfs(node.left, BVHTreeList, triangleList, listIndex + 1);
        int rightCount = BVHTreeToArrayDfs(node.right, BVHTreeList, triangleList, listIndex + 1);

        int count = leftCount + rightCount;

        RTBVHNode tempNode1 = new RTBVHNode()
        {
            isLeaf         = 0,
            boundingBoxMin = node.bbox.min,
            boundingBoxMax = node.bbox.max,
            subTreeCount   = (uint)count,
        };

        BVHTreeList[index] = tempNode1;


        return(count + 1);
    }
    public static RayHit LocalGridTrace(Ray ray, SpatialGridIndex index, List <int> geomGridList, List <RTTriangle_t> triangles, SpatialGrid grids, int excludeGeometry)
    {
        RayHit bestHit    = RayHit.CreateRayHit();
        RayHit currentHit = RayHit.CreateRayHit();

        int start = 0;
        int count = 0;

        GetNumberOfGeometryInGrid(index, geomGridList, grids.dimension, out start, out count);

        if (count == 0)
        {
            return(bestHit);
        }

        Vector3 localMin = SpatialGridCompute.GetGridLocalMin(grids, index);
        Vector3 localMax = SpatialGridCompute.GetGridLocalMax(grids, index);

        for (int t = 0; t < count; t++)
        {
            if (triangles[start + t].id == excludeGeometry)
            {
                continue;
            }
            RTTriangle.IntersectTriangle(ray, ref currentHit, triangles[start + t]);

            if (!(localMin.x <= currentHit.hitPoint.x && currentHit.hitPoint.x <= localMax.x))
            {
                currentHit = bestHit;   // Reset currentHit
                continue;
            }

            if (!(localMin.y <= currentHit.hitPoint.y && currentHit.hitPoint.y <= localMax.y))
            {
                currentHit = bestHit;   // Reset currentHit
                continue;
            }

            if (!(localMin.z <= currentHit.hitPoint.z && currentHit.hitPoint.z <= localMax.z))
            {
                currentHit = bestHit;   // Reset currentHit
                continue;
            }

            bestHit = currentHit;
        }

        return(bestHit);
    }
        internal void AddRTScene(RayTracer_552.RTCamera c, RayTracer_552.SceneDatabase rtScene)
        {
            UWB_Primitive prim;

            NewSceneDatabase();
            SceneResource <RTGeometry> allGeom = rtScene.GetAllGeom();

            for (int i = 0; i < allGeom.Count; i++)
            {
                RTGeometry g = (RTGeometry)allGeom.ResourceLookup(i);
                switch (g.GeomType())
                {
                case RTGeometry.RTGeometryType.Sphere:
                    RTSphere s = (RTSphere)g;
                    prim = CreateSphereMesh();
                    SetMeshMaterial(prim, rtScene.GetMaterial(s.GetMaterialIndex()));
                    float scale = s.Radius / 2f;
                    CreateNode(s.Center, scale, scale, scale, prim);
                    break;

                case RTGeometry.RTGeometryType.Rectangle:
                    RTRectangle r = (RTRectangle)g;
                    prim = CreateRectangle(r);
                    SetMeshMaterial(prim, rtScene.GetMaterial(r.GetMaterialIndex()));
                    UWB_SceneNode node = CreateNode(r.GetCenter(), r.GetUSize() / 2f, 1f, r.GetVSize() / 2f, prim);
                    // now rotate the y-vector of node to point towards r.Normal;
                    float dot = (float)Math.Abs(Vector3.Dot(Vector3.UnitY, r.GetNormal()));
                    if (dot < 0.9999f)
                    {
                        float   angle = (float)Math.Acos(dot);
                        Vector3 axis  = Vector3.Cross(Vector3.UnitY, r.GetNormal());
                        axis.Normalize();
                        Quaternion    q  = Quaternion.CreateFromAxisAngle(axis, angle);
                        UWB_XFormInfo xf = node.getXFormInfo();
                        xf.SetRotationQuat(q);
                        node.setXFormInfo(xf);
                    }
                    break;

                case RTGeometry.RTGeometryType.Triangle:
                    RTTriangle t = (RTTriangle)g;
                    Vector3[]  v = t.GetVertices();
                    prim = new UWB_PrimitiveTriangle(v[0], v[1], v[2]);
                    prim.EnableLighting(true);
                    prim.EnableTexturing(false);
                    SetMeshMaterial(prim, rtScene.GetMaterial(t.GetMaterialIndex()));
                    CreateNode(Vector3.Zero, 1f, 1f, 1f, prim);
                    break;
                }
            }
            AddCamera(c);
            AddLights(rtScene);

            // to show ray list
            mShownRayX  = mShownRayY = 0;
            mRaysToShow = new UWB_PrimitiveList();

            mDebugInfo = new UWB_SceneNode();
            mDebugInfo.setPrimitive(mRaysToShow);
            mDebugInfo.insertChildNode(mPixelsToShow.GetAllPixels());
            mDebugInfo.insertChildNode(mPixelInWorld.GetAllPixels());
        }