Exemplo n.º 1
0
        private static HierarchyMesh GetSelectedMesh(List <HierarchyObject> objects, int foundTriangle)
        {
            foreach (HierarchyObject hObject in objects)
            {
                if (hObject is HierarchyMesh)
                {
                    foreach (uint tr in ((HierarchyMesh)hObject).triangles)
                    {
                        if (tr == foundTriangle)
                        {
                            return((HierarchyMesh)hObject);
                        }
                    }
                }
                else if (hObject is HierarchyNode)
                {
                    foreach (HierarchyObject subObject in ((HierarchyNode)hObject).hObjects)
                    {
                        HierarchyMesh mesh = GetSelectedMesh(((HierarchyNode)subObject).hObjects, foundTriangle);

                        if (mesh != null)
                        {
                            return(mesh);
                        }
                    }
                }
            }

            return(null);
        }
Exemplo n.º 2
0
        public BoundingBox(Scene sc, HierarchyMesh obj)
        {
            float minx = float.MaxValue;
            float miny = float.MaxValue;
            float minz = float.MaxValue;
            float maxx = float.MinValue;
            float maxy = float.MinValue;
            float maxz = float.MinValue;
            float tmpx, tmpy, tmpz;

            foreach (uint triangleIdx in obj.triangles)
            {
                tmpx = sc.points[(int)(sc.triangles[(int)triangleIdx].p1)].x;
                if (minx > tmpx)
                {
                    minx = tmpx;
                }
                if (maxx < tmpx)
                {
                    maxx = tmpx;
                }

                tmpy = sc.points[(int)(sc.triangles[(int)triangleIdx].p1)].y;
                if (miny > tmpy)
                {
                    miny = tmpy;
                }
                if (maxy < tmpy)
                {
                    maxy = tmpy;
                }

                tmpz = sc.points[(int)(sc.triangles[(int)triangleIdx].p1)].z;
                if (minz > tmpz)
                {
                    minz = tmpz;
                }
                if (maxz < tmpz)
                {
                    maxz = tmpz;
                }

                tmpx = sc.points[(int)(sc.triangles[(int)triangleIdx].p2)].x;
                if (minx > tmpx)
                {
                    minx = tmpx;
                }
                if (maxx < tmpx)
                {
                    maxx = tmpx;
                }

                tmpy = sc.points[(int)(sc.triangles[(int)triangleIdx].p2)].y;
                if (miny > tmpy)
                {
                    miny = tmpy;
                }
                if (maxy < tmpy)
                {
                    maxy = tmpy;
                }

                tmpz = sc.points[(int)(sc.triangles[(int)triangleIdx].p2)].z;
                if (minz > tmpz)
                {
                    minz = tmpz;
                }
                if (maxz < tmpz)
                {
                    maxz = tmpz;
                }

                tmpx = sc.points[(int)(sc.triangles[(int)triangleIdx].p3)].x;
                if (minx > tmpx)
                {
                    minx = tmpx;
                }
                if (maxx < tmpx)
                {
                    maxx = tmpx;
                }

                tmpy = sc.points[(int)(sc.triangles[(int)triangleIdx].p3)].y;
                if (miny > tmpy)
                {
                    miny = tmpy;
                }
                if (maxy < tmpy)
                {
                    maxy = tmpy;
                }

                tmpz = sc.points[(int)(sc.triangles[(int)triangleIdx].p3)].z;
                if (minz > tmpz)
                {
                    minz = tmpz;
                }
                if (maxz < tmpz)
                {
                    maxz = tmpz;
                }
            }

            minBB = new Vector3D(minx, miny, minz);
            maxBB = new Vector3D(maxx, maxy, maxz);
        }
Exemplo n.º 3
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();
            }
        }
        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();
                    }
                }
            }
        }