コード例 #1
0
        /// <summary>
        /// Działa tylko dla widoków ortogonalnych.
        /// </summary>
        /// <param name="bezierSurface"></param>
        /// <param name="bezierCam"></param>
        /// <param name="viewportType"></param>
        /// <param name="pos"></param>
        /// <param name="size"></param>
        /// <param name="orthoSize"></param>
        /// <param name="orthoPos"></param>
        /// <param name="orthoLookAt"></param>
        public static void SelectBezierControlPoint(BezierSurface bezierSurface, Camera bezierCam, ViewportType viewportType,
                                                    Point pos, Point size, Vector2 orthoSize, Vector3 orthoPos, Vector3 orthoLookAt)
        {
            Vector3 outCamPos = new Vector3(), outSurfPos = new Vector3();

            switch (viewportType)
            {
            case ViewportType.Perspective:
                CalcPerspCoords(pos, size, bezierCam.fovAngle, bezierCam.rotateAngle,
                                bezierCam.position, bezierCam.lookAt, out outCamPos, out outSurfPos);
                break;

            case ViewportType.Orto:
                CalcOrthoCoords(pos, size, orthoSize, orthoPos, orthoLookAt, out outCamPos, out outSurfPos);
                break;
            }

            Vector3 rayDir = Vector3.Normalize(outSurfPos - outCamPos);

            SlimDX.Ray  ray     = new SlimDX.Ray(outCamPos + 0.01f * rayDir, rayDir);
            float       dist    = float.PositiveInfinity;
            float       tmpDist = -1;
            BoundingBox bb;

            for (int i = 0; i < bezierSurface.ControlPoints.Length; i++)
            {
                bb = new BoundingBox(new Vector3(bezierSurface.ControlPoints[i].x - 0.05f,
                                                 bezierSurface.ControlPoints[i].y - 0.05f,
                                                 bezierSurface.ControlPoints[i].z - 0.05f),
                                     new Vector3(bezierSurface.ControlPoints[i].x + 0.05f,
                                                 bezierSurface.ControlPoints[i].y + 0.05f,
                                                 bezierSurface.ControlPoints[i].z + 0.05f));

                if (SlimDX.Ray.Intersects(ray, bb, out tmpDist))
                {
                    if (tmpDist < dist)
                    {
                        dist = tmpDist;
                        bezierSurface.selectedPointIdx = i;
                    }
                }
            }

            if (dist == float.PositiveInfinity)
            {
                bezierSurface.selectedPointIdx = -1;
            }
        }
コード例 #2
0
        public override bool Intersect(SlimDX.Ray ray, ref float dist, out IADTChunk hitChunk)
        {
            hitChunk = null;
            if (mMesh == null)
            {
                return(false);
            }

            float nDist = 0.0f;

            if (mMesh.Intersects(ray, out nDist))
            {
                hitChunk = this;
                dist     = nDist;
                return(true);
            }
            return(false);
        }
コード例 #3
0
        public override bool Intersect(SlimDX.Ray ray, float maxDistance, out float dist, out T obj)
        {
            T     minObj = default(T);
            float minD   = maxDistance + float.Epsilon;     // we are only using less-than-checks
            bool  hit    = false;

            if (OptimizeGroundIntersection && ray.Direction.X == 0 && ray.Direction.Y == 0)
            {
                // set minD by doing ground intersection here
                float height = GroundHeight(Common.Math.ToVector2(ray.Position));
                float d      = ray.Position.Z - height;
                if (ray.Direction.Z < 0 && d < minD)
                {
                    minD = d;
                    hit  = true;
                }
                // Todo: obj?
            }
            if (root != null)
            {
                hit |= root.Intersect(ray, ref minD, ref minObj);
            }

            foreach (var nb in nonfittableObjects.Keys)
            {
                RayIntersection rOut;
                var             bounding = nonfittableObjects[nb];
                if (bounding.Intersectable)
                {
                    if (Intersection.Intersect <RayIntersection>(bounding.Bounding, ray, out rOut) && rOut.Distance < minD)
                    {
                        minD   = rOut.Distance;
                        minObj = (T)rOut.Userdata;
                        hit    = true;
                    }
                }
            }

            dist = minD;
            obj  = minObj;

            return(hit);
        }
コード例 #4
0
        public static void SelectElems(Scene scene, ViewportType viewportType, Point pos, Point size, Vector2 orthoSize, Vector3 orthoPos, Vector3 orthoLookAt)
        {
            Vector3 outCamPos = new Vector3(), outSurfPos = new Vector3();

            switch(viewportType)
            {
                case ViewportType.Perspective:
                    CalcPerspCoords(pos, size, scene.cams.ElementAt(scene.activeCamera).fovAngle, scene.cams.ElementAt(scene.activeCamera).rotateAngle,
                scene.cams.ElementAt(scene.activeCamera).position, scene.cams.ElementAt(scene.activeCamera).lookAt, out outCamPos, out outSurfPos);
                    break;

                case ViewportType.Orto:
                    CalcOrthoCoords(pos, size, orthoSize, orthoPos, orthoLookAt, out outCamPos, out outSurfPos);
                    break;
            }

            List<Triang> triangs = new List<Triang>();
            for(int i = 0; i < scene.triangles.Count; ++i)
            {
                Triangle triangle = scene.triangles.ElementAt(i);
                triangs.Add(new Triang(scene.points.ElementAt((int)triangle.p1), scene.points.ElementAt((int)triangle.p2), scene.points.ElementAt((int)triangle.p3)));
            }

            const float lightDiameter = 1;
            const float cameraDiameter = 1;

            Vector3 rayDir = Vector3.Normalize(outSurfPos - outCamPos);

            SlimDX.Ray ray = new SlimDX.Ray(outCamPos + 0.01f * rayDir, rayDir);

            float[] triangleDist = new float[triangs.Count];

            for(int i = 0; i < triangs.Count; ++i)
            {
                float dist;

                if(SlimDX.Ray.Intersects(ray, triangs[i].p1, triangs[i].p2, triangs[i].p3, out dist))
                {
                    triangleDist[i] = dist;
                }
                else
                {
                    triangleDist[i] = -1;
                }
            }

            float minDist = float.PositiveInfinity;
            int minIndex = -1;

            for(int i = 0; i < triangleDist.Length; ++i)
            {
                if(triangleDist[i] >= 0 && triangleDist[i] < minDist)
                {
                    minIndex = i;
                    minDist = triangleDist[i];
                }
            }

            int foundTriangle = minIndex;
            Light_ foundLight = null; // TODO wyliczyć
            Camera foundCamera = null; // TODO wyliczyć

            if(foundTriangle >= 0)
            {
                HierarchyMesh mesh = GetSelectedMesh(scene.hierarchy.objects, foundTriangle);
                if(scene.selTriangles.Contains(mesh) == false)
                {
                    scene.selTriangles.Add(mesh);
                }
            }
            else if(foundLight != null)
            {

            }
            else if(foundCamera != null)
            {

            }
            else
            {
                scene.selTriangles.Clear();
                scene.selLights.Clear();
                scene.selCams.Clear();
            }
        }
コード例 #5
0
        public static void SelectElems(Scene scene, List<Vector3> camsPoints, Pair<List<Vector3>, List<int> > lightsPoints,
            ViewportType viewportType, Point pos, Point size, Vector2 orthoSize, Vector3 orthoPos, Vector3 orthoLookAt, bool ctrl)
        {
            Vector3 outCamPos = new Vector3(), outSurfPos = new Vector3();

            switch(viewportType)
            {
                case ViewportType.Perspective:
                    CalcPerspCoords(pos, size, scene.cams[scene.activeCamera].fovAngle, scene.cams[scene.activeCamera].rotateAngle,
                scene.cams[scene.activeCamera].position, scene.cams[scene.activeCamera].lookAt, out outCamPos, out outSurfPos);
                    break;

                case ViewportType.Orto:
                    CalcOrthoCoords(pos, size, orthoSize, orthoPos, orthoLookAt, out outCamPos, out outSurfPos);
                    break;
            }

            List<Triang> triangsCam = new List<Triang>();
            for(int i = 0; i < camsPoints.Count / 8; ++i)
            {
                for(int j = RenderCamera.triangles.Length - 1; j < RenderCamera.triangles.Length; ++j)
                {
                    triangsCam.Add(new Triang(camsPoints[3 * i + (int)RenderCamera.triangles[j].p1],
                                              camsPoints[3 * i + (int)RenderCamera.triangles[j].p2],
                                              camsPoints[3 * i + (int)RenderCamera.triangles[j].p3]));
                }
            }

            List<Triang> triangsLight = new List<Triang>();
            int sumPoints = 0;
            for(int i = 0; i < lightsPoints.Second.Count; ++i)
            {
                for(int j = 0; j < lightsPoints.Second[i]; ++j)
                {
                    triangsLight.Add(new Triang(lightsPoints.First[sumPoints + (int)RenderLight.triangles[j].p1],
                                                lightsPoints.First[sumPoints + (int)RenderLight.triangles[j].p2],
                                                lightsPoints.First[sumPoints + (int)RenderLight.triangles[j].p3]));
                }

                sumPoints += lightsPoints.Second[i] == RenderLight.trianglesSpotNum ? RenderLight.pointsSpotNum : RenderLight.pointsPointNum;
            }

            bool clipped = false;
            float clipMin = 0, clipMax = 0;
            Vector3 rayShift = new Vector3(0);

            if(viewportType == ViewportType.Orto && Renderer.Clipping == true)
            {
                float xmin, xplus, ymin, yplus, zmin, zplus;

                if(outCamPos.Z > 40000)
                {
                    xmin = Renderer.GetClipPlanePosition(ClipPlaneType.XMIN);
                    xplus = Renderer.GetClipPlanePosition(ClipPlaneType.XPLUS);
                    ymin = Renderer.GetClipPlanePosition(ClipPlaneType.YMIN);
                    yplus = Renderer.GetClipPlanePosition(ClipPlaneType.YPLUS);
                    zmin = Renderer.GetClipPlanePosition(ClipPlaneType.ZMIN);
                    zplus = Renderer.GetClipPlanePosition(ClipPlaneType.ZPLUS);

                    if(outCamPos.X < xmin || outCamPos.X > xplus || outCamPos.Y < ymin || outCamPos.Y > yplus)
                    {
                        clipped = true;
                    }
                    else
                    {
                        clipMin = 50001 - zplus;
                        clipMax = zplus - zmin;

                        rayShift = new Vector3(0, 0, clipMin);
                    }
                }
                else if(outCamPos.X > 40000)
                {
                    xmin = Renderer.GetClipPlanePosition(ClipPlaneType.ZPLUS);
                    xplus = Renderer.GetClipPlanePosition(ClipPlaneType.ZMIN);
                    ymin = Renderer.GetClipPlanePosition(ClipPlaneType.YMIN);
                    yplus = Renderer.GetClipPlanePosition(ClipPlaneType.YPLUS);
                    zmin = Renderer.GetClipPlanePosition(ClipPlaneType.XMIN);
                    zplus = Renderer.GetClipPlanePosition(ClipPlaneType.XPLUS);

                    if(outCamPos.Z > xmin || outCamPos.Z < xplus || outCamPos.Y < ymin || outCamPos.Y > yplus)
                    {
                        clipped = true;
                    }
                    else
                    {
                        clipMin = 50001 - zplus;
                        clipMax = zplus - zmin;

                        rayShift = new Vector3(clipMin, 0, 0);
                    }
                }
                else if(outCamPos.Y > 40000)
                {
                    xmin = Renderer.GetClipPlanePosition(ClipPlaneType.XMIN);
                    xplus = Renderer.GetClipPlanePosition(ClipPlaneType.XPLUS);
                    ymin = Renderer.GetClipPlanePosition(ClipPlaneType.ZPLUS);
                    yplus = Renderer.GetClipPlanePosition(ClipPlaneType.ZMIN);
                    zmin = Renderer.GetClipPlanePosition(ClipPlaneType.YMIN);
                    zplus = Renderer.GetClipPlanePosition(ClipPlaneType.YPLUS);

                    if(outCamPos.X < xmin || outCamPos.X > xplus || outCamPos.Z > ymin || outCamPos.Z < yplus)
                    {
                        clipped = true;
                    }
                    else
                    {
                        clipMin = 50001 - zplus;
                        clipMax = zplus - zmin;

                        rayShift = new Vector3(0, clipMin, 0);
                    }
                }
            }

            Vector3 rayDir = Vector3.Normalize(outSurfPos - outCamPos);

            SlimDX.Ray ray = new SlimDX.Ray(outCamPos + 0.01f * rayDir - rayShift, rayDir);

            float[] triangleDist = new float[scene.triangles.Count];
            float minDist = float.PositiveInfinity;
            int minIndex = -1;

            if(clipped == false)
            {
                if(viewportType == ViewportType.Orto && Renderer.Clipping == true)
                {
                    for(int i = 0; i < scene.triangles.Count; ++i)
                    {
                        float dist;

                        if(SlimDX.Ray.Intersects(ray, scene.points[(int)scene.triangles[i].p1], scene.points[(int)scene.triangles[i].p2],
                            scene.points[(int)scene.triangles[i].p3], out dist))
                        {
                            if(dist >= 0 && dist < minDist && dist < clipMax)
                            {
                                minIndex = i;
                                minDist = dist;
                            }
                        }
                    }
                }
                else
                {
                    for(int i = 0; i < scene.triangles.Count; ++i)
                    {
                        float dist;

                        if(SlimDX.Ray.Intersects(ray, scene.points[(int)scene.triangles[i].p1], scene.points[(int)scene.triangles[i].p2],
                            scene.points[(int)scene.triangles[i].p3], out dist))
                        {
                            if(dist >= 0 && dist < minDist)
                            {
                                minIndex = i;
                                minDist = dist;
                            }
                        }
                    }
                }
            }

            minDist += clipMin;

            ray = new SlimDX.Ray(outCamPos + 0.01f * rayDir, rayDir);

            float[] camsTriangleDist = new float[triangsCam.Count];
            float camsMinDist = float.PositiveInfinity;
            int camsMinIndex = -1;

            if(viewportType != ViewportType.Perspective)
            {
                for(int i = 0; i < triangsCam.Count; ++i)
                {
                    float dist;

                    if(SlimDX.Ray.Intersects(ray, triangsCam[i].p1, triangsCam[i].p2, triangsCam[i].p3, out dist))
                    {
                        if(dist >= 0 && dist < camsMinDist)
                        {
                            camsMinIndex = i;
                            camsMinDist = dist;
                        }
                    }
                }
            }

            float[] lightsTriangleDist = new float[triangsLight.Count];
            float lightsMinDist = float.PositiveInfinity;
            int lightsMinIndex = -1;

            if(viewportType != ViewportType.Perspective)
            {
                for(int i = 0; i < triangsLight.Count; ++i)
                {
                    float dist;

                    if(SlimDX.Ray.Intersects(ray, triangsLight[i].p1, triangsLight[i].p2, triangsLight[i].p3, out dist))
                    {
                        if(dist >= 0 && dist < lightsMinDist)
                        {
                            lightsMinIndex = i;
                            lightsMinDist = lightsTriangleDist[i];
                        }
                    }
                }
            }

            List<float> pointsDist = new List<float>();
            float pointsMinDist = float.PositiveInfinity;
            int pointsMinIndex = -1;

            if(viewportType != ViewportType.Perspective)
            {
                for(int i = 0; i < scene.lights.Count; ++i)
                {
                    if(scene.lights[i].type == Light_Type.Spot || scene.lights[i].type == Light_Type.Goniometric)
                    {
                        float dist;

                        Vector3 lightPos = scene.lights[i].position + scene.lights[i].direction * Renderer.spotLightDist * orthoSize.X / 10;
                        if(SlimDX.Ray.Intersects(ray, new BoundingBox(new Vector3(lightPos.X - Renderer.pointSize * orthoSize.X / 20,
                                                                                  lightPos.Y - Renderer.pointSize * orthoSize.X / 20,
                                                                                  lightPos.Z - Renderer.pointSize * orthoSize.X / 20),
                                                                      new Vector3(lightPos.X + Renderer.pointSize * orthoSize.X / 20,
                                                                                  lightPos.Y + Renderer.pointSize * orthoSize.X / 20,
                                                                                  lightPos.Z + Renderer.pointSize * orthoSize.X / 20)), out dist))
                        {
                            pointsDist.Add(dist);
                        }
                        else
                        {
                            pointsDist.Add(-1);
                        }
                    }
                }

                for(int i = 0; i < scene.cams.Count; ++i)
                {
                    float dist;

                    Vector3 camPos = scene.cams[i].position;
                    if(SlimDX.Ray.Intersects(ray, new BoundingBox(new Vector3(camPos.X - Renderer.pointSize * orthoSize.X / 20,
                                                                                camPos.Y - Renderer.pointSize * orthoSize.X / 20,
                                                                                camPos.Z - Renderer.pointSize * orthoSize.X / 20),
                                                                    new Vector3(camPos.X + Renderer.pointSize * orthoSize.X / 20,
                                                                                camPos.Y + Renderer.pointSize * orthoSize.X / 20,
                                                                                camPos.Z + Renderer.pointSize * orthoSize.X / 20)), out dist))
                    {
                        pointsDist.Add(dist);
                    }
                    else
                    {
                        pointsDist.Add(-1);
                    }

                    Vector3 camLookAt = Renderer.camsLookAtPoints[i];
                    if(SlimDX.Ray.Intersects(ray, new BoundingBox(new Vector3(camLookAt.X - Renderer.pointSize * orthoSize.X / 20,
                                                                                camLookAt.Y - Renderer.pointSize * orthoSize.X / 20,
                                                                                camLookAt.Z - Renderer.pointSize * orthoSize.X / 20),
                                                                    new Vector3(camLookAt.X + Renderer.pointSize * orthoSize.X / 20,
                                                                                camLookAt.Y + Renderer.pointSize * orthoSize.X / 20,
                                                                                camLookAt.Z + Renderer.pointSize * orthoSize.X / 20)), out dist))
                    {
                        pointsDist.Add(dist);
                    }
                    else
                    {
                        pointsDist.Add(-1);
                    }
                }

                for(int i = 0; i < pointsDist.Count; ++i)
                {
                    if(pointsDist[i] >= 0 && pointsDist[i] < pointsMinDist)
                    {
                        pointsMinIndex = i;
                        pointsMinDist = pointsDist[i];
                    }
                }
            }

            int foundTriangle = minIndex;
            int foundLight = lightsMinIndex;
            int foundCamera = camsMinIndex;
            int foundPoint = pointsMinIndex;

            if(foundPoint >= 0 && pointsMinDist < minDist && pointsMinDist < lightsMinDist)
            {
                pointFound = pointsMinIndex;

                scene.selTriangles.Clear();
                scene.selLights.Clear();
                scene.selCams.Clear();
            }
            else
            {
                pointFound = -1;

                if(foundLight >= 0 && lightsMinDist < minDist && lightsMinDist < camsMinDist && lightsMinDist < pointsMinDist)
                {
                    foundTriangle = -1;
                    foundCamera = -1;
                }
                if(foundCamera >= 0 && camsMinDist < minDist && camsMinDist < lightsMinDist && camsMinDist < pointsMinDist)
                {
                    foundTriangle = -1;
                    foundLight = -1;
                }

                if(ctrl == false)
                {
                    scene.selTriangles.Clear();
                    scene.selLights.Clear();
                    scene.selCams.Clear();
                }

                if(foundTriangle >= 0)
                {
                    HierarchyMesh mesh = Hierarchy.GetSelectedMesh(scene.hierarchy.objects, foundTriangle);
                    if(scene.selTriangles.Contains(mesh) == false)
                    {
                        scene.selTriangles.Add(mesh);
                    }
                    else
                    {
                        if(ctrl == true)
                        {
                            scene.selTriangles.Remove(mesh);
                        }
                    }
                }
                else if(foundLight >= 0)
                {
                    int i = 0;
                    int index = lightsPoints.Second[i];
                    while(foundLight > index)
                    {
                        index += lightsPoints.Second[++i];
                    }
                    int lightIndex = i;

                    if(scene.selLights.Contains(lightIndex) == false)
                    {
                        scene.selLights.Add(lightIndex);
                    }
                    else
                    {
                        if(ctrl == true)
                        {
                            scene.selLights.Remove(lightIndex);
                        }
                    }
                }
                else if(foundCamera >= 0)
                {
                    int cameraIndex = foundCamera / RenderCamera.triangles.Length;
                    if(scene.selCams.Contains(cameraIndex) == false)
                    {
                        scene.selCams.Add(cameraIndex);
                    }
                    else
                    {
                        if(ctrl == true)
                        {
                            scene.selCams.Remove(cameraIndex);
                        }
                    }
                }
                else
                {
                    if(ctrl == false)
                    {
                        scene.selTriangles.Clear();
                        scene.selLights.Clear();
                        scene.selCams.Clear();
                    }
                }
            }
        }
コード例 #6
0
        /// <summary>
        /// Działa tylko dla widoków ortogonalnych.
        /// </summary>
        /// <param name="bezierSurface"></param>
        /// <param name="bezierCam"></param>
        /// <param name="viewportType"></param>
        /// <param name="pos"></param>
        /// <param name="size"></param>
        /// <param name="orthoSize"></param>
        /// <param name="orthoPos"></param>
        /// <param name="orthoLookAt"></param>
        public static void SelectBezierControlPoint(BezierSurface bezierSurface, Camera bezierCam, ViewportType viewportType, 
            Point pos, Point size, Vector2 orthoSize, Vector3 orthoPos, Vector3 orthoLookAt)
        {
            Vector3 outCamPos = new Vector3(), outSurfPos = new Vector3();

            switch (viewportType)
            {
                case ViewportType.Perspective:
                    CalcPerspCoords(pos, size, bezierCam.fovAngle, bezierCam.rotateAngle,
                                    bezierCam.position, bezierCam.lookAt, out outCamPos, out outSurfPos);
                    break;

                case ViewportType.Orto:
                    CalcOrthoCoords(pos, size, orthoSize, orthoPos, orthoLookAt, out outCamPos, out outSurfPos);
                    break;
            }

            Vector3 rayDir = Vector3.Normalize(outSurfPos - outCamPos);

            SlimDX.Ray ray = new SlimDX.Ray(outCamPos + 0.01f * rayDir, rayDir);
            float dist = float.PositiveInfinity;
            float tmpDist = -1;
            BoundingBox bb;

            for (int i = 0; i < bezierSurface.ControlPoints.Length; i++)
            {
                bb = new BoundingBox(new Vector3(bezierSurface.ControlPoints[i].x - 0.05f,
                                                 bezierSurface.ControlPoints[i].y - 0.05f,
                                                 bezierSurface.ControlPoints[i].z - 0.05f),
                                     new Vector3(bezierSurface.ControlPoints[i].x + 0.05f,
                                                 bezierSurface.ControlPoints[i].y + 0.05f,
                                                 bezierSurface.ControlPoints[i].z + 0.05f));

                if (SlimDX.Ray.Intersects(ray, bb, out tmpDist))
                {
                    if (tmpDist < dist)
                    {
                        dist = tmpDist;
                        bezierSurface.selectedPointIdx = i;
                    }
                }
            }

            if (dist == float.PositiveInfinity)
            {
                bezierSurface.selectedPointIdx = -1;
            }
        }
コード例 #7
0
ファイル: IADTChunk.cs プロジェクト: corefan/sharpwow
 public abstract bool Intersect(SlimDX.Ray ray, ref float dist, out IADTChunk hitChunk);
コード例 #8
0
        public static void SelectElems(Scene scene, ViewportType viewportType, Point pos, Point size, Vector2 orthoSize, Vector3 orthoPos, Vector3 orthoLookAt)
        {
            Vector3 outCamPos = new Vector3(), outSurfPos = new Vector3();

            switch (viewportType)
            {
            case ViewportType.Perspective:
                CalcPerspCoords(pos, size, scene.cams.ElementAt(scene.activeCamera).fovAngle, scene.cams.ElementAt(scene.activeCamera).rotateAngle,
                                scene.cams.ElementAt(scene.activeCamera).position, scene.cams.ElementAt(scene.activeCamera).lookAt, out outCamPos, out outSurfPos);
                break;

            case ViewportType.Orto:
                CalcOrthoCoords(pos, size, orthoSize, orthoPos, orthoLookAt, out outCamPos, out outSurfPos);
                break;
            }

            List <Triang> triangs = new List <Triang>();

            for (int i = 0; i < scene.triangles.Count; ++i)
            {
                Triangle triangle = scene.triangles.ElementAt(i);
                triangs.Add(new Triang(scene.points.ElementAt((int)triangle.p1), scene.points.ElementAt((int)triangle.p2), scene.points.ElementAt((int)triangle.p3)));
            }

            const float lightDiameter  = 1;
            const float cameraDiameter = 1;

            Vector3 rayDir = Vector3.Normalize(outSurfPos - outCamPos);

            SlimDX.Ray ray = new SlimDX.Ray(outCamPos + 0.01f * rayDir, rayDir);

            float[] triangleDist = new float[triangs.Count];

            for (int i = 0; i < triangs.Count; ++i)
            {
                float dist;

                if (SlimDX.Ray.Intersects(ray, triangs[i].p1, triangs[i].p2, triangs[i].p3, out dist))
                {
                    triangleDist[i] = dist;
                }
                else
                {
                    triangleDist[i] = -1;
                }
            }

            float minDist  = float.PositiveInfinity;
            int   minIndex = -1;

            for (int i = 0; i < triangleDist.Length; ++i)
            {
                if (triangleDist[i] >= 0 && triangleDist[i] < minDist)
                {
                    minIndex = i;
                    minDist  = triangleDist[i];
                }
            }

            int    foundTriangle = minIndex;
            Light_ foundLight    = null; // TODO wyliczyć
            Camera foundCamera   = null; // TODO wyliczyć

            if (foundTriangle >= 0)
            {
                HierarchyMesh mesh = GetSelectedMesh(scene.hierarchy.objects, foundTriangle);
                if (scene.selTriangles.Contains(mesh) == false)
                {
                    scene.selTriangles.Add(mesh);
                }
            }
            else if (foundLight != null)
            {
            }
            else if (foundCamera != null)
            {
            }
            else
            {
                scene.selTriangles.Clear();
                scene.selLights.Clear();
                scene.selCams.Clear();
            }
        }
コード例 #9
0
        public static void SelectElems(Scene scene, List <Vector3> camsPoints, Pair <List <Vector3>, List <int> > lightsPoints,
                                       ViewportType viewportType, Point pos, Point size, Vector2 orthoSize, Vector3 orthoPos, Vector3 orthoLookAt, bool ctrl)
        {
            Vector3 outCamPos = new Vector3(), outSurfPos = new Vector3();

            switch (viewportType)
            {
            case ViewportType.Perspective:
                CalcPerspCoords(pos, size, scene.cams[scene.activeCamera].fovAngle, scene.cams[scene.activeCamera].rotateAngle,
                                scene.cams[scene.activeCamera].position, scene.cams[scene.activeCamera].lookAt, out outCamPos, out outSurfPos);
                break;

            case ViewportType.Orto:
                CalcOrthoCoords(pos, size, orthoSize, orthoPos, orthoLookAt, out outCamPos, out outSurfPos);
                break;
            }

            List <Triang> triangsCam = new List <Triang>();

            for (int i = 0; i < camsPoints.Count / 8; ++i)
            {
                for (int j = RenderCamera.triangles.Length - 1; j < RenderCamera.triangles.Length; ++j)
                {
                    triangsCam.Add(new Triang(camsPoints[3 * i + (int)RenderCamera.triangles[j].p1],
                                              camsPoints[3 * i + (int)RenderCamera.triangles[j].p2],
                                              camsPoints[3 * i + (int)RenderCamera.triangles[j].p3]));
                }
            }

            List <Triang> triangsLight = new List <Triang>();
            int           sumPoints    = 0;

            for (int i = 0; i < lightsPoints.Second.Count; ++i)
            {
                for (int j = 0; j < lightsPoints.Second[i]; ++j)
                {
                    triangsLight.Add(new Triang(lightsPoints.First[sumPoints + (int)RenderLight.triangles[j].p1],
                                                lightsPoints.First[sumPoints + (int)RenderLight.triangles[j].p2],
                                                lightsPoints.First[sumPoints + (int)RenderLight.triangles[j].p3]));
                }

                sumPoints += lightsPoints.Second[i] == RenderLight.trianglesSpotNum ? RenderLight.pointsSpotNum : RenderLight.pointsPointNum;
            }

            bool    clipped = false;
            float   clipMin = 0, clipMax = 0;
            Vector3 rayShift = new Vector3(0);

            if (viewportType == ViewportType.Orto && Renderer.Clipping == true)
            {
                float xmin, xplus, ymin, yplus, zmin, zplus;

                if (outCamPos.Z > 40000)
                {
                    xmin  = Renderer.GetClipPlanePosition(ClipPlaneType.XMIN);
                    xplus = Renderer.GetClipPlanePosition(ClipPlaneType.XPLUS);
                    ymin  = Renderer.GetClipPlanePosition(ClipPlaneType.YMIN);
                    yplus = Renderer.GetClipPlanePosition(ClipPlaneType.YPLUS);
                    zmin  = Renderer.GetClipPlanePosition(ClipPlaneType.ZMIN);
                    zplus = Renderer.GetClipPlanePosition(ClipPlaneType.ZPLUS);

                    if (outCamPos.X < xmin || outCamPos.X > xplus || outCamPos.Y < ymin || outCamPos.Y > yplus)
                    {
                        clipped = true;
                    }
                    else
                    {
                        clipMin = 50001 - zplus;
                        clipMax = zplus - zmin;

                        rayShift = new Vector3(0, 0, clipMin);
                    }
                }
                else if (outCamPos.X > 40000)
                {
                    xmin  = Renderer.GetClipPlanePosition(ClipPlaneType.ZPLUS);
                    xplus = Renderer.GetClipPlanePosition(ClipPlaneType.ZMIN);
                    ymin  = Renderer.GetClipPlanePosition(ClipPlaneType.YMIN);
                    yplus = Renderer.GetClipPlanePosition(ClipPlaneType.YPLUS);
                    zmin  = Renderer.GetClipPlanePosition(ClipPlaneType.XMIN);
                    zplus = Renderer.GetClipPlanePosition(ClipPlaneType.XPLUS);

                    if (outCamPos.Z > xmin || outCamPos.Z < xplus || outCamPos.Y < ymin || outCamPos.Y > yplus)
                    {
                        clipped = true;
                    }
                    else
                    {
                        clipMin = 50001 - zplus;
                        clipMax = zplus - zmin;

                        rayShift = new Vector3(clipMin, 0, 0);
                    }
                }
                else if (outCamPos.Y > 40000)
                {
                    xmin  = Renderer.GetClipPlanePosition(ClipPlaneType.XMIN);
                    xplus = Renderer.GetClipPlanePosition(ClipPlaneType.XPLUS);
                    ymin  = Renderer.GetClipPlanePosition(ClipPlaneType.ZPLUS);
                    yplus = Renderer.GetClipPlanePosition(ClipPlaneType.ZMIN);
                    zmin  = Renderer.GetClipPlanePosition(ClipPlaneType.YMIN);
                    zplus = Renderer.GetClipPlanePosition(ClipPlaneType.YPLUS);

                    if (outCamPos.X < xmin || outCamPos.X > xplus || outCamPos.Z > ymin || outCamPos.Z < yplus)
                    {
                        clipped = true;
                    }
                    else
                    {
                        clipMin = 50001 - zplus;
                        clipMax = zplus - zmin;

                        rayShift = new Vector3(0, clipMin, 0);
                    }
                }
            }

            Vector3 rayDir = Vector3.Normalize(outSurfPos - outCamPos);

            SlimDX.Ray ray = new SlimDX.Ray(outCamPos + 0.01f * rayDir - rayShift, rayDir);

            float[] triangleDist = new float[scene.triangles.Count];
            float   minDist      = float.PositiveInfinity;
            int     minIndex     = -1;

            if (clipped == false)
            {
                if (viewportType == ViewportType.Orto && Renderer.Clipping == true)
                {
                    for (int i = 0; i < scene.triangles.Count; ++i)
                    {
                        float dist;

                        if (SlimDX.Ray.Intersects(ray, scene.points[(int)scene.triangles[i].p1], scene.points[(int)scene.triangles[i].p2],
                                                  scene.points[(int)scene.triangles[i].p3], out dist))
                        {
                            if (dist >= 0 && dist < minDist && dist < clipMax)
                            {
                                minIndex = i;
                                minDist  = dist;
                            }
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < scene.triangles.Count; ++i)
                    {
                        float dist;

                        if (SlimDX.Ray.Intersects(ray, scene.points[(int)scene.triangles[i].p1], scene.points[(int)scene.triangles[i].p2],
                                                  scene.points[(int)scene.triangles[i].p3], out dist))
                        {
                            if (dist >= 0 && dist < minDist)
                            {
                                minIndex = i;
                                minDist  = dist;
                            }
                        }
                    }
                }
            }

            minDist += clipMin;

            ray = new SlimDX.Ray(outCamPos + 0.01f * rayDir, rayDir);

            float[] camsTriangleDist = new float[triangsCam.Count];
            float   camsMinDist      = float.PositiveInfinity;
            int     camsMinIndex     = -1;

            if (viewportType != ViewportType.Perspective)
            {
                for (int i = 0; i < triangsCam.Count; ++i)
                {
                    float dist;

                    if (SlimDX.Ray.Intersects(ray, triangsCam[i].p1, triangsCam[i].p2, triangsCam[i].p3, out dist))
                    {
                        if (dist >= 0 && dist < camsMinDist)
                        {
                            camsMinIndex = i;
                            camsMinDist  = dist;
                        }
                    }
                }
            }

            float[] lightsTriangleDist = new float[triangsLight.Count];
            float   lightsMinDist      = float.PositiveInfinity;
            int     lightsMinIndex     = -1;

            if (viewportType != ViewportType.Perspective)
            {
                for (int i = 0; i < triangsLight.Count; ++i)
                {
                    float dist;

                    if (SlimDX.Ray.Intersects(ray, triangsLight[i].p1, triangsLight[i].p2, triangsLight[i].p3, out dist))
                    {
                        if (dist >= 0 && dist < lightsMinDist)
                        {
                            lightsMinIndex = i;
                            lightsMinDist  = lightsTriangleDist[i];
                        }
                    }
                }
            }

            List <float> pointsDist     = new List <float>();
            float        pointsMinDist  = float.PositiveInfinity;
            int          pointsMinIndex = -1;

            if (viewportType != ViewportType.Perspective)
            {
                for (int i = 0; i < scene.lights.Count; ++i)
                {
                    if (scene.lights[i].type == Light_Type.Spot || scene.lights[i].type == Light_Type.Goniometric)
                    {
                        float dist;

                        Vector3 lightPos = scene.lights[i].position + scene.lights[i].direction * Renderer.spotLightDist * orthoSize.X / 10;
                        if (SlimDX.Ray.Intersects(ray, new BoundingBox(new Vector3(lightPos.X - Renderer.pointSize * orthoSize.X / 20,
                                                                                   lightPos.Y - Renderer.pointSize * orthoSize.X / 20,
                                                                                   lightPos.Z - Renderer.pointSize * orthoSize.X / 20),
                                                                       new Vector3(lightPos.X + Renderer.pointSize * orthoSize.X / 20,
                                                                                   lightPos.Y + Renderer.pointSize * orthoSize.X / 20,
                                                                                   lightPos.Z + Renderer.pointSize * orthoSize.X / 20)), out dist))
                        {
                            pointsDist.Add(dist);
                        }
                        else
                        {
                            pointsDist.Add(-1);
                        }
                    }
                }

                for (int i = 0; i < scene.cams.Count; ++i)
                {
                    float dist;

                    Vector3 camPos = scene.cams[i].position;
                    if (SlimDX.Ray.Intersects(ray, new BoundingBox(new Vector3(camPos.X - Renderer.pointSize * orthoSize.X / 20,
                                                                               camPos.Y - Renderer.pointSize * orthoSize.X / 20,
                                                                               camPos.Z - Renderer.pointSize * orthoSize.X / 20),
                                                                   new Vector3(camPos.X + Renderer.pointSize * orthoSize.X / 20,
                                                                               camPos.Y + Renderer.pointSize * orthoSize.X / 20,
                                                                               camPos.Z + Renderer.pointSize * orthoSize.X / 20)), out dist))
                    {
                        pointsDist.Add(dist);
                    }
                    else
                    {
                        pointsDist.Add(-1);
                    }

                    Vector3 camLookAt = Renderer.camsLookAtPoints[i];
                    if (SlimDX.Ray.Intersects(ray, new BoundingBox(new Vector3(camLookAt.X - Renderer.pointSize * orthoSize.X / 20,
                                                                               camLookAt.Y - Renderer.pointSize * orthoSize.X / 20,
                                                                               camLookAt.Z - Renderer.pointSize * orthoSize.X / 20),
                                                                   new Vector3(camLookAt.X + Renderer.pointSize * orthoSize.X / 20,
                                                                               camLookAt.Y + Renderer.pointSize * orthoSize.X / 20,
                                                                               camLookAt.Z + Renderer.pointSize * orthoSize.X / 20)), out dist))
                    {
                        pointsDist.Add(dist);
                    }
                    else
                    {
                        pointsDist.Add(-1);
                    }
                }

                for (int i = 0; i < pointsDist.Count; ++i)
                {
                    if (pointsDist[i] >= 0 && pointsDist[i] < pointsMinDist)
                    {
                        pointsMinIndex = i;
                        pointsMinDist  = pointsDist[i];
                    }
                }
            }

            int foundTriangle = minIndex;
            int foundLight    = lightsMinIndex;
            int foundCamera   = camsMinIndex;
            int foundPoint    = pointsMinIndex;

            if (foundPoint >= 0 && pointsMinDist < minDist && pointsMinDist < lightsMinDist)
            {
                pointFound = pointsMinIndex;

                scene.selTriangles.Clear();
                scene.selLights.Clear();
                scene.selCams.Clear();
            }
            else
            {
                pointFound = -1;

                if (foundLight >= 0 && lightsMinDist < minDist && lightsMinDist < camsMinDist && lightsMinDist < pointsMinDist)
                {
                    foundTriangle = -1;
                    foundCamera   = -1;
                }
                if (foundCamera >= 0 && camsMinDist < minDist && camsMinDist < lightsMinDist && camsMinDist < pointsMinDist)
                {
                    foundTriangle = -1;
                    foundLight    = -1;
                }

                if (ctrl == false)
                {
                    scene.selTriangles.Clear();
                    scene.selLights.Clear();
                    scene.selCams.Clear();
                }

                if (foundTriangle >= 0)
                {
                    HierarchyMesh mesh = Hierarchy.GetSelectedMesh(scene.hierarchy.objects, foundTriangle);
                    if (scene.selTriangles.Contains(mesh) == false)
                    {
                        scene.selTriangles.Add(mesh);
                    }
                    else
                    {
                        if (ctrl == true)
                        {
                            scene.selTriangles.Remove(mesh);
                        }
                    }
                }
                else if (foundLight >= 0)
                {
                    int i     = 0;
                    int index = lightsPoints.Second[i];
                    while (foundLight > index)
                    {
                        index += lightsPoints.Second[++i];
                    }
                    int lightIndex = i;

                    if (scene.selLights.Contains(lightIndex) == false)
                    {
                        scene.selLights.Add(lightIndex);
                    }
                    else
                    {
                        if (ctrl == true)
                        {
                            scene.selLights.Remove(lightIndex);
                        }
                    }
                }
                else if (foundCamera >= 0)
                {
                    int cameraIndex = foundCamera / RenderCamera.triangles.Length;
                    if (scene.selCams.Contains(cameraIndex) == false)
                    {
                        scene.selCams.Add(cameraIndex);
                    }
                    else
                    {
                        if (ctrl == true)
                        {
                            scene.selCams.Remove(cameraIndex);
                        }
                    }
                }
                else
                {
                    if (ctrl == false)
                    {
                        scene.selTriangles.Clear();
                        scene.selLights.Clear();
                        scene.selCams.Clear();
                    }
                }
            }
        }