public static HierarchyMesh GetSelectedMesh(List <HierarchyObject> objects, int triangle)
        {
            foreach (HierarchyObject hObject in objects)
            {
                if (hObject is HierarchyMesh)
                {
                    foreach (uint tr in ((HierarchyMesh)hObject).triangles)
                    {
                        if (tr == triangle)
                        {
                            return((HierarchyMesh)hObject);
                        }
                    }
                }
                else if (hObject is HierarchyNode)
                {
                    {
                        HierarchyMesh mesh = GetSelectedMesh(((HierarchyNode)hObject).hObjects, triangle);

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

            return(null);
        }
Esempio n. 2
0
        private void deleteMesh(int firstTriangleIndex, List <HierarchyObject> listHObj) //usuwa z hierarchii
        {
            List <HierarchyObject> treeList = new List <HierarchyObject>();

            foreach (HierarchyObject ho in listHObj)
            {
                if (ho is HierarchyMesh)  //jeśli jest to mesh
                {
                    HierarchyMesh hm = (HierarchyMesh)ho;
                    if ((int)hm.triangles.ElementAt(0) == firstTriangleIndex)  //jeśli jest to mesh którego szukamy
                    {
                        treeList.Add(ho);
                    }
                }
                else if (ho is HierarchyNode) //jeśli jest to node
                {
                    HierarchyNode hn = (HierarchyNode)ho;
                    deleteMesh(firstTriangleIndex, hn.hObjects);
                }
            }
            foreach (HierarchyObject ho in treeList)
            {
                listHObj.Remove(ho);
            }
        }
Esempio n. 3
0
        public void AddObject(Scene sceneObject, string hierarchyName, Vector3 translation)
        {
            if (sceneObject != null)
            {
                modified = true;
                uint point_idx = (uint)points.Count();
                foreach (Vector3D point in sceneObject.points)
                {
                    point.x += translation.X;
                    point.y += translation.Y;
                    point.z += translation.Z;
                }
                points.AddRange(sceneObject.points);

                int triangles_idx = triangles.Count();
                foreach (Triangle triangle in sceneObject.triangles)
                {
                    triangle.p1 += point_idx;
                    triangle.p2 += point_idx;
                    triangle.p3 += point_idx;
                }

                triangles.AddRange(sceneObject.triangles);

                if (!ContainsMaterialName("mat1"))
                {
                    materials.Add(new Material_("mat1", 0.6f, 0.6f, 0.6f, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 1));
                }
                parts.Add(new Part(new List <int>()));
                materialAssign.Add("mat1");

                HierarchyMesh obj = new HierarchyMesh(hierarchyName);

                for (int i = 0; i < sceneObject.triangles.Count(); i++)
                {
                    parts[parts.Count - 1].triangles.Add(triangles_idx);
                    obj.triangles.Add((uint)triangles_idx++);
                }

                hierarchy.objects.Add(obj);
                hierarchyChange = true;
                //Renderer.RecalculateData(this);
            }
        }
        //po modyfikacjach w panelu hierarchii "odświeżamy" hierarchię sceny
        private void refreshHierarchy()
        {
            Hierarchy hierarchy = new Hierarchy();

            if (((TreeViewItem)treeView1.Items.GetItemAt(0)).HasItems)
            {
                List<HierarchyMesh> meshes = currScene.hierarchy.GetAllMeshes();
                List<HierarchyLight> lights = currScene.hierarchy.GetAllLights();
                foreach (TreeViewItem tvi in ((TreeViewItem)treeView1.Items.GetItemAt(0)).Items)
                {
                    if (tvi.HasItems)
                    {
                        HierarchyNode hn = new HierarchyNode(tvi.Header.ToString());
                        buildHierarchy(hn, tvi, meshes, lights);
                        hierarchy.objects.Add(hn);
                        //Console.WriteLine("budujemy hierarchie - node :" + hn.name.ToString());
                    }
                    else
                    {
                        char[] delimiterChars = { '_' };
                        string[] words = tvi.Name.Split(delimiterChars);
                        if (words[0].Equals("mesh"))
                        {
                            HierarchyMesh hm = new HierarchyMesh(tvi.Header.ToString());
                            foreach (HierarchyMesh hmesh in meshes)
                            {
                                if (Convert.ToInt32(words[1]) == (int)hmesh.triangles.ElementAt(0))
                                {
                                    hm = hmesh;
                                    hm.name = tvi.Header.ToString();
                                    break;
                                }
                            }
                            //Console.WriteLine("budujemy hierarchie - mesh :" + hm.name.ToString());
                            hierarchy.objects.Add(hm);
                        }
                        else //if(words[0].Equals("light"));
                        {
                            HierarchyLight hl = new HierarchyLight(tvi.Header.ToString(), Convert.ToInt32(words[1]));
                            hl.name = tvi.Header.ToString();
                            hierarchy.objects.Add(hl);
                        }
                    }
                }
            }
            currScene.hierarchy = hierarchy;
        }
        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);
        }
        private static void ReadHierarchy(HierarchyNode node, List<Light_> lights, List<string> text, ref int pointer)
        {
            string objNumLabel = File.GetAttribute(text[pointer], 0);
            if(objNumLabel != "node_count")
            {
                throw new Exception();
            }
            uint objNum = uint.Parse(File.GetAttribute(text[pointer++], 1));

            for(int i = 0; i < objNum; ++i)
            {
                HierarchyObject newObject = null;
                string objStrLabel = File.GetAttribute(text[pointer], 0);
                if(objStrLabel != "hier_type")
                {
                    throw new Exception();
                }
                string objStr = File.GetAttribute(text[pointer++], 1);

                string[] objNameAtts = File.GetAttributes(text[pointer]);
                if(objNameAtts[0] != "hier_name")
                {
                    throw new Exception();
                }
                string objName = objName = File.CutFirstString(text[pointer]);
                ++pointer;

                switch(objStr)
                {
                    case "Mesh":
                        newObject = new HierarchyMesh(objName);
                        node.hObjects.Add(newObject);

                        string triangleNumLabel = File.GetAttribute(text[pointer], 0);
                        if(triangleNumLabel != "triangle_count")
                        {
                            new Exception();
                        }
                        uint triangleNum = uint.Parse(File.GetAttribute(text[pointer++], 1));
                        List<uint> triangles = new List<uint>();

                        string[] triangs = File.GetAttributes(text[pointer]);
                        for(int j = 0; j < triangleNum; ++j)
                        {
                            triangles.Add(uint.Parse(triangs[j]));
                        }
                        ++pointer;

                        ((HierarchyMesh)newObject).triangles = triangles;
                        break;

                    case "Light":
                        string lightIdxLabel = File.GetAttribute(text[pointer], 0);
                        if (lightIdxLabel != "light_index")
                        {
                            new Exception();
                        }
                        int lightIdx = int.Parse(File.GetAttribute(text[pointer++], 1));
                        newObject = new HierarchyLight(objName, lightIdx);
                        node.hObjects.Add(newObject);
                        break;

                    case "Node":
                        newObject = new HierarchyNode(objName);
                        node.hObjects.Add(newObject);

                        ReadHierarchy((HierarchyNode)newObject, lights, text, ref pointer);
                        break;
                }
            }
        }
        private static void ReadHierarchy(HierarchyNode node, List<Light_> lights, List<string> text, int pointer)
        {
            string objNumLabel = File.GetAttribute(text[pointer], 0);
            if(objNumLabel != "node_count")
            {
                node = null;
            }
            uint objNum = uint.Parse(File.GetAttribute(text[pointer++], 1));

            for(int i = 0; i < objNum; ++i)
            {
                HierarchyObject newObject = null;
                string objStrLabel = File.GetAttribute(text[pointer], 0);
                if(objStrLabel != "hier_type")
                {
                    node = null;
                }
                string objStr = File.GetAttribute(text[pointer++], 1);
                string objNameLabel = File.GetAttribute(text[pointer], 0);
                if(objNameLabel != "hier_name")
                {
                    node = null;
                }
                string objName = File.GetAttribute(text[pointer++], 1);

                switch(objStr)
                {
                    case "Mesh":
                        newObject = new HierarchyMesh(objName);
                        node.hObjects.Add(newObject);

                        string triangleNumLabel = File.GetAttribute(text[pointer], 0);
                        if(triangleNumLabel != "triangle_count")
                        {
                            node = null;
                        }
                        uint triangleNum = uint.Parse(File.GetAttribute(text[pointer++], 1));
                        List<uint> triangles = new List<uint>();

                        string[] triangs = File.GetAttributes(text[pointer]);
                        for(int j = 0; j < triangleNum; ++j)
                        {
                            triangles.Add(uint.Parse(triangs[j]));
                        }
                        ++pointer;

                        ((HierarchyMesh)newObject).triangles = triangles;
                        break;

                    case "Light":
                        newObject = new HierarchyLight(GetLightByName(lights, objName));
                        node.hObjects.Add(newObject);
                        break;

                    case "Node":
                        newObject = new HierarchyNode(objName);
                        node.hObjects.Add(newObject);

                        ReadHierarchy((HierarchyNode)newObject, lights, text, pointer);
                        break;
                }
            }
        }
Esempio n. 8
0
        public Scene SceneFromSelection(out Vector3D center)
        {
            Scene retScene = new Scene();

            HashSet <uint>  selectedTriangIdx = new HashSet <uint>();
            HashSet <uint>  selectedPointsIdx = new HashSet <uint>();
            List <Triangle> newTriangles      = new List <Triangle>();
            List <Vector3D> newPoints         = new List <Vector3D>();
            List <uint>     newTriangleIdx    = new List <uint>();
            List <int>      newLightIdx       = new List <int>();
            uint            idx = 0;

            for (int i = 0; i < triangles.Count; ++i)
            {
                newTriangleIdx.Add(0);
            }
            //List<HierarchyMesh> selectedMeshes = new List<HierarchyMesh>();
            bool[] selectedPoints  = new bool[points.Count];
            bool[] selectedTriangs = new bool[triangles.Count];
            for (int i = 0; i < selectedPoints.Length; i++)
            {
                selectedPoints[i] = false;
            }
            for (int i = 0; i < selectedTriangs.Length; i++)
            {
                selectedTriangs[i] = false;
            }

            foreach (HierarchyMesh mesh in selTriangles)
            //for (int i = 0; i < selTriangles.Count; i++)
            {
                //selectedMeshes.Add(Hierarchy.GetSelectedMesh(hierarchy.objects, (int)mesh.triangles[0]));
                for (int j = 0; j < mesh.triangles.Count; j++)
                {
                    int oldIdx = (int)mesh.triangles[j];
                    newTriangleIdx[oldIdx] = idx++;
                    selectedTriangIdx.Add(mesh.triangles[j]);
                    int triang = (int)mesh.triangles[j];
                    selectedTriangs[mesh.triangles[j]] = true;

                    newTriangles.Add(new Triangle(triangles[triang].p1, triangles[triang].p2, triangles[triang].p3));

                    selectedPointsIdx.Add(triangles[(int)mesh.triangles[j]].p1);
                    selectedPointsIdx.Add(triangles[(int)mesh.triangles[j]].p2);
                    selectedPointsIdx.Add(triangles[(int)mesh.triangles[j]].p3);

                    selectedPoints[triangles[(int)mesh.triangles[j]].p1] = true;
                    selectedPoints[triangles[(int)mesh.triangles[j]].p2] = true;
                    selectedPoints[triangles[(int)mesh.triangles[j]].p3] = true;
                }
            }

            idx = 0;
            for (int i = 0; i < lights.Count; i++)
            {
                newLightIdx.Add(-1);
            }
            for (int i = 0; i < selLights.Count; i++)
            {
                newLightIdx[selLights[i]] = (int)idx++;
            }

            for (int i = 0; i < points.Count; i++)
            {
                if (selectedPoints[i])
                {
                    newPoints.Add(new Vector3D(points[i]));
                }
            }


            // Lista zawiera indeksy poczatka i konca usuwanych punktow (beda
            // one wystepowaly sekcjami)
            List <int> pointStartEnd = new List <int>();
            bool       flag          = true;

            for (int i = 0; i < selectedPoints.Length; i++)
            {
                if (!selectedPoints[i] && flag)
                {
                    pointStartEnd.Add(i);
                    flag = false;
                }
                if (selectedPoints[i] && !flag)
                {
                    pointStartEnd.Add(i - 1);
                    flag = true;
                }
            }
            if (!flag)
            {
                pointStartEnd.Add(selectedPoints.Length - 1);
            }

            // Lista zawiera indeksy poczatka i konca usuwanych trojkatow
            List <int> triangStartEnd = new List <int>();

            flag = true;
            for (int i = 0; i < selectedTriangs.Length; i++)
            {
                if (!selectedTriangs[i] && flag)
                {
                    triangStartEnd.Add(i);
                    flag = false;
                }
                if (selectedTriangs[i] && !flag)
                {
                    triangStartEnd.Add(i - 1);
                    flag = true;
                }
            }
            if (!flag)
            {
                triangStartEnd.Add(selectedTriangs.Length - 1);
            }

            int offset = 0;

            for (int i = 0; i < pointStartEnd.Count; i += 2)
            {
                int diffP = pointStartEnd[i + 1] - pointStartEnd[i] + 1;
                foreach (Triangle triangle in newTriangles)
                {
                    if (triangle.p1 > pointStartEnd[i + 1] - offset)
                    {
                        triangle.p1 -= (uint)diffP;
                    }
                    if (triangle.p2 > pointStartEnd[i + 1] - offset)
                    {
                        triangle.p2 -= (uint)diffP;
                    }
                    if (triangle.p3 > pointStartEnd[i + 1] - offset)
                    {
                        triangle.p3 -= (uint)diffP;
                    }
                }
                offset += diffP;
            }
            List <HierarchyObject> newHierarchy = new List <HierarchyObject>();

            if (selectedHierObj != null)
            {
                HierarchyNode newNode = new HierarchyNode(selectedHierObj);
                newHierarchy.Add(newNode);
                RebuildHierarchy(newHierarchy, newTriangleIdx, newLightIdx);
            }
            else
            {
                foreach (HierarchyMesh mesh in selTriangles)
                {
                    HierarchyMesh newMesh = (new HierarchyMesh(mesh.name));
                    for (int i = 0; i < mesh.triangles.Count; i++)
                    {
                        newMesh.triangles.Add(newTriangleIdx[(int)mesh.triangles[i]]);
                    }
                    newHierarchy.Add(newMesh);
                }
            }

            List <Part>   newParts          = new List <Part>();
            List <String> newMaterialAssign = new List <string>();

            for (int i = 0; i < parts.Count; i++)
            {
                Part newPart = (new Part(new List <int>()));
                bool used    = false;
                for (int j = 0; j < parts[i].triangles.Count; j++)
                {
                    newPart.triangles.Add((int)newTriangleIdx[parts[i].triangles[j]]);
                    if ((int)newTriangleIdx[parts[i].triangles[j]] > 0)
                    {
                        used = true;
                    }
                }
                if (used)
                {
                    newParts.Add(newPart);
                    newMaterialAssign.Add(materialAssign[i]);
                }
            }

            foreach (Material_ material in materials)
            {
                retScene.materials.Add(new Material_(material));
            }
            center = new Data.Scene.Vector3D(0, 0, 0);
            int count = newPoints.Count + selCams.Count + selLights.Count;


            foreach (Data.Scene.Vector3D v in newPoints)
            {
                center.x = center.x + v.x / count;
                center.y = center.y + v.y / count;
                center.z = center.z + v.z / count;
            }
            //foreach (int c in selCams)
            //{
            //    Camera cam = cams[c];
            //    center.x = center.x + cam.position.X / count;
            //    center.y = center.y + cam.position.Y / count;
            //    center.z = center.z + cam.position.Z / count;
            //}
            foreach (int l in selLights)
            {
                Light_ light = lights[l];
                center.x = center.x + light.position.X / count;
                center.y = center.y + light.position.Y / count;
                center.z = center.z + light.position.Z / count;
            }
            foreach (Data.Scene.Vector3D v in newPoints)
            {
                v.x -= center.x;
                v.y -= center.y;
                v.z -= center.z;
            }

            List <Light_> newwLights1 = new List <Light_>();

            foreach (int lightIndex in selLights)
            {
                Light_ light = new Light_(lights[lightIndex]);
                newwLights1.Add(light);
                newwLights1[newwLights1.Count - 1].position -= center;
                int tmp = newwLights1.Count - 1;

                //dodawanie światel do hierarchii
                if (selectedHierObj == null)
                {
                    HierarchyLight newLight = new HierarchyLight(light.name.ToString(), tmp);
                    newHierarchy.Add(newLight);
                }
                //newLight.name = light.name;
                Console.WriteLine("dodawanie do hierarchiii");
            }

            //List<Camera> newCams = new List<Camera>();
            //foreach (int camIndex in selCams)
            //{
            //    newCams.Add(new Camera(cams[camIndex]));
            //    newCams[newCams.Count - 1].position -= center;
            //}

            retScene.points            = newPoints;
            retScene.triangles         = newTriangles;
            retScene.hierarchy.objects = newHierarchy;
            retScene.parts             = newParts;
            retScene.materialAssign    = newMaterialAssign;
            retScene.lights            = newwLights1;
            //retScene.cams = newCams;

            hierarchyChange = true;
            return(retScene);
        }
Esempio n. 9
0
        public void AddObject(Scene sceneObject, string hierarchyName, Vector3 translation)
        {
            if (sceneObject != null)
            {
                modified = true;
                uint point_idx = (uint)points.Count();
                foreach (Vector3D point in sceneObject.points)
                {
                    point.x += translation.X;
                    point.y += translation.Y;
                    point.z += translation.Z;
                }
                points.AddRange(sceneObject.points);

                int triangles_idx = triangles.Count();
                foreach (Triangle triangle in sceneObject.triangles)
                {
                    triangle.p1 += point_idx;
                    triangle.p2 += point_idx;
                    triangle.p3 += point_idx;
                }

                triangles.AddRange(sceneObject.triangles);

                if (!ContainsMaterialName("mat1"))
                {
                    materials.Add(new Material_("mat1", 0.6f, 0.6f, 0.6f, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 1));
                }
                parts.Add(new Part(new List<int>()));
                materialAssign.Add("mat1");

                HierarchyMesh obj = new HierarchyMesh(hierarchyName);

                for (int i = 0; i < sceneObject.triangles.Count(); i++)
                {
                    parts[parts.Count-1].triangles.Add(triangles_idx);
                    obj.triangles.Add((uint)triangles_idx++);
                }

                hierarchy.objects.Add(obj);
                hierarchyChange = true;
                //Renderer.RecalculateData(this);
            }
        }
Esempio n. 10
0
        public static Scene GetExampleScene()
        {
            Scene scene = new Scene();

            scene.points.Add(new Vector3D(-1, -1, 1));
            scene.points.Add(new Vector3D(1, -1, 1));
            scene.points.Add(new Vector3D(1, -1, -1));
            scene.points.Add(new Vector3D(-1, -1, -1));
            scene.points.Add(new Vector3D(-1, 1, 1));
            scene.points.Add(new Vector3D(1, 1, 1));
            scene.points.Add(new Vector3D(1, 1, -1));
            scene.points.Add(new Vector3D(-1, 1, -1));

            scene.points.Add(new Vector3D(1.5f, -0.5f, 0.5f));
            scene.points.Add(new Vector3D(3, -0.3f, 0));
            scene.points.Add(new Vector3D(2.5f, 0, -0.8f));
            scene.points.Add(new Vector3D(2, 2, -0.2f));

            scene.triangles.Add(new Triangle(0, 1, 5));
            scene.triangles.Add(new Triangle(0, 5, 4));
            scene.triangles.Add(new Triangle(1, 2, 5));
            scene.triangles.Add(new Triangle(2, 6, 5));
            scene.triangles.Add(new Triangle(2, 3, 6));
            scene.triangles.Add(new Triangle(3, 6, 7));
            scene.triangles.Add(new Triangle(3, 0, 7));
            scene.triangles.Add(new Triangle(0, 7, 4));
            scene.triangles.Add(new Triangle(0, 1, 2));
            scene.triangles.Add(new Triangle(0, 2, 3));
            scene.triangles.Add(new Triangle(4, 5, 6));
            scene.triangles.Add(new Triangle(6, 7, 4));

            scene.triangles.Add(new Triangle(8, 9, 10));
            scene.triangles.Add(new Triangle(8, 9, 11));
            scene.triangles.Add(new Triangle(9, 10, 11));
            scene.triangles.Add(new Triangle(8, 10, 11));

            List <int> part1 = new List <int>();

            part1.Add(0);
            part1.Add(1);
            part1.Add(2);
            part1.Add(3);
            part1.Add(4);
            part1.Add(5);
            part1.Add(6);
            part1.Add(7);
            part1.Add(8);
            part1.Add(9);
            part1.Add(10);
            part1.Add(11);
            scene.parts.Add(new Part(part1));

            List <int> part2 = new List <int>();

            part2.Add(12);
            part2.Add(13);
            part2.Add(14);
            part2.Add(15);
            scene.parts.Add(new Part(part2));

            scene.materials.Add(new Material_("mat1", 0.6f, 0.95f, 0.5f, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1));
            scene.materials.Add(new Material_("mat2", 0.4f, 0.3f, 0.9f, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1));

            scene.materialAssign.Add("mat1");
            scene.materialAssign.Add("mat2");

            scene.cams[0] = new Camera("Cam1", 800, 600, new Vector3(-8, -4, -10), new Vector3(3, 2, 2), 60, 0);

            scene.activeCamera = 0;

            HierarchyMesh cube = new HierarchyMesh("Cube");

            cube.triangles.AddRange(new uint[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 });

            HierarchyMesh pyramid = new HierarchyMesh("Pyramid");

            pyramid.triangles.AddRange(new uint[] { 12, 13, 14, 15 });

            scene.hierarchy.objects.AddRange(new HierarchyMesh[] { cube, pyramid });

            return(scene);
        }
Esempio n. 11
0
        public void AddObject(Scene sceneObject, string hierarchyName)
        {
            if (sceneObject != null)
            {
                uint point_idx = (uint)points.Count();
                points.AddRange(sceneObject.points);

                int triangles_idx = triangles.Count();
                foreach (Triangle triangle in sceneObject.triangles)
                {
                    triangle.p1 += point_idx;
                    triangle.p2 += point_idx;
                    triangle.p3 += point_idx;
                }

                int offset = triangles.Count;

                triangles.AddRange(sceneObject.triangles);

                // Jeśli w scenie nie ma jeszcze zadnego obiektu, tworzony zostaje
                // domyslny material.

                if (parts.Count == 0)
                {
                    materials.Add(new Material_("default", 0.6f, 0.95f, 0.5f, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1));
                }
                parts.Add(new Part(new List <int>()));
                materialAssign.Add("default");

                HierarchyMesh obj = new HierarchyMesh(hierarchyName);

                for (int i = 0; i < sceneObject.triangles.Count(); i++)
                {
                    parts[parts.Count - 1].triangles.Add(triangles_idx);
                    obj.triangles.Add((uint)triangles_idx++);
                }

                hierarchy.objects.Add(obj);

                addedObject.Add(obj);

                //int offset = indices.Length;

                int vertTriangOffset = vertexTriangle.Length;

                Array.Resize <int>(ref indices, indices.Length + 3 * sceneObject.triangles.Count);
                Array.Resize <int>(ref selIndices, selIndices.Length + 3 * sceneObject.triangles.Count);
                Array.Resize <Vertex>(ref vertices, vertices.Length + sceneObject.points.Count);
                Array.Resize <List <int> >(ref vertexTriangle, vertexTriangle.Length + sceneObject.points.Count);
                Parallel.For(vertTriangOffset, vertexTriangle.Length, index => vertexTriangle[index] = new List <int>());

                //bool[] selected = GetSelectedTriangles();
                for (int i = 0; i < sceneObject.triangles.Count; i++)
                {
                    indices[numIndices++] = (int)sceneObject.triangles[i].p1;
                    indices[numIndices++] = (int)sceneObject.triangles[i].p3;
                    indices[numIndices++] = (int)sceneObject.triangles[i].p2;

                    vertexTriangle[sceneObject.triangles[i].p1].Add(offset + i);
                    vertexTriangle[sceneObject.triangles[i].p2].Add(offset + i);
                    vertexTriangle[sceneObject.triangles[i].p3].Add(offset + i);
                }
            }
        }
Esempio n. 12
0
 public HierarchyMesh(HierarchyMesh copy) : base(copy.name)
 {
     triangles = new List <uint>(copy.triangles);
 }
Esempio n. 13
0
        public void AddObject(Scene sceneObject, string hierarchyName)
        {
            if (sceneObject != null)
            {
                uint point_idx = (uint)points.Count();
                points.AddRange(sceneObject.points);

                int triangles_idx = triangles.Count();
                foreach (Triangle triangle in sceneObject.triangles)
                {
                    triangle.p1 += point_idx;
                    triangle.p2 += point_idx;
                    triangle.p3 += point_idx;
                }

                int offset = triangles.Count;

                triangles.AddRange(sceneObject.triangles);

                // Jeśli w scenie nie ma jeszcze zadnego obiektu, tworzony zostaje
                // domyslny material.

                if (parts.Count == 0)
                {
                    materials.Add(new Material_("default", 0.6f, 0.95f, 0.5f, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1));
                }
                parts.Add(new Part(new List<int>()));
                materialAssign.Add("default");

                HierarchyMesh obj = new HierarchyMesh(hierarchyName);

                for (int i = 0; i < sceneObject.triangles.Count(); i++)
                {
                    parts[parts.Count-1].triangles.Add(triangles_idx);
                    obj.triangles.Add((uint)triangles_idx++);
                }

                hierarchy.objects.Add(obj);

                addedObject.Add(obj);

                //int offset = indices.Length;

                int vertTriangOffset = vertexTriangle.Length;

                Array.Resize<int>(ref indices, indices.Length + 3 * sceneObject.triangles.Count);
                Array.Resize<int>(ref selIndices, selIndices.Length + 3 * sceneObject.triangles.Count);
                Array.Resize<Vertex>(ref vertices, vertices.Length + sceneObject.points.Count);
                Array.Resize<List<int>>(ref vertexTriangle, vertexTriangle.Length + sceneObject.points.Count);
                Parallel.For(vertTriangOffset, vertexTriangle.Length, index => vertexTriangle[index] = new List<int>());

                //bool[] selected = GetSelectedTriangles();
                for (int i = 0; i < sceneObject.triangles.Count; i++)
                {
                    indices[numIndices++] = (int)sceneObject.triangles[i].p1;
                    indices[numIndices++] = (int)sceneObject.triangles[i].p3;
                    indices[numIndices++] = (int)sceneObject.triangles[i].p2;

                    vertexTriangle[sceneObject.triangles[i].p1].Add(offset + i);
                    vertexTriangle[sceneObject.triangles[i].p2].Add(offset + i);
                    vertexTriangle[sceneObject.triangles[i].p3].Add(offset + i);
                }
            }
        }
Esempio n. 14
0
        public static Scene GetExampleScene()
        {
            Scene scene = new Scene();

            scene.points.Add(new Vector3D(-1, -1, 1));
            scene.points.Add(new Vector3D(1, -1, 1));
            scene.points.Add(new Vector3D(1, -1, -1));
            scene.points.Add(new Vector3D(-1, -1, -1));
            scene.points.Add(new Vector3D(-1, 1, 1));
            scene.points.Add(new Vector3D(1, 1, 1));
            scene.points.Add(new Vector3D(1, 1, -1));
            scene.points.Add(new Vector3D(-1, 1, -1));

            scene.points.Add(new Vector3D(1.5f, -0.5f, 0.5f));
            scene.points.Add(new Vector3D(3, -0.3f, 0));
            scene.points.Add(new Vector3D(2.5f, 0, -0.8f));
            scene.points.Add(new Vector3D(2, 2, -0.2f));

            scene.triangles.Add(new Triangle(0, 1, 5));
            scene.triangles.Add(new Triangle(0, 5, 4));
            scene.triangles.Add(new Triangle(1, 2, 5));
            scene.triangles.Add(new Triangle(2, 6, 5));
            scene.triangles.Add(new Triangle(2, 3, 6));
            scene.triangles.Add(new Triangle(3, 6, 7));
            scene.triangles.Add(new Triangle(3, 0, 7));
            scene.triangles.Add(new Triangle(0, 7, 4));
            scene.triangles.Add(new Triangle(0, 1, 2));
            scene.triangles.Add(new Triangle(0, 2, 3));
            scene.triangles.Add(new Triangle(4, 5, 6));
            scene.triangles.Add(new Triangle(6, 7, 4));

            scene.triangles.Add(new Triangle(8, 9, 10));
            scene.triangles.Add(new Triangle(8, 9, 11));
            scene.triangles.Add(new Triangle(9, 10, 11));
            scene.triangles.Add(new Triangle(8, 10, 11));

            List<int> part1 = new List<int>();
            part1.Add(0);
            part1.Add(1);
            part1.Add(2);
            part1.Add(3);
            part1.Add(4);
            part1.Add(5);
            part1.Add(6);
            part1.Add(7);
            part1.Add(8);
            part1.Add(9);
            part1.Add(10);
            part1.Add(11);
            scene.parts.Add(new Part(part1));

            List<int> part2 = new List<int>();
            part2.Add(12);
            part2.Add(13);
            part2.Add(14);
            part2.Add(15);
            scene.parts.Add(new Part(part2));

            scene.materials.Add(new Material_("mat1", 0.6f, 0.95f, 0.5f, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1));
            scene.materials.Add(new Material_("mat2", 0.4f, 0.3f, 0.9f, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1));

            scene.materialAssign.Add("mat1");
            scene.materialAssign.Add("mat2");

            scene.cams[0] = new Camera("Cam1", 800, 600, new Vector3(-8, -4, -10), new Vector3(3, 2, 2), 60, 0);

            scene.activeCamera = 0;

            HierarchyMesh cube = new HierarchyMesh("Cube");
            cube.triangles.AddRange(new uint[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 });

            HierarchyMesh pyramid = new HierarchyMesh("Pyramid");
            pyramid.triangles.AddRange(new uint[] { 12, 13, 14, 15 });

            scene.hierarchy.objects.AddRange( new HierarchyMesh[] { cube, pyramid } );

            return scene;
        }
Esempio n. 15
0
        private static void ReadHierarchy(HierarchyNode node, List <Light_> lights, List <string> text, int pointer)
        {
            string objNumLabel = File.GetAttribute(text[pointer], 0);

            if (objNumLabel != "node_count")
            {
                node = null;
            }
            uint objNum = uint.Parse(File.GetAttribute(text[pointer++], 1));

            for (int i = 0; i < objNum; ++i)
            {
                HierarchyObject newObject   = null;
                string          objStrLabel = File.GetAttribute(text[pointer], 0);
                if (objStrLabel != "hier_type")
                {
                    node = null;
                }
                string objStr       = File.GetAttribute(text[pointer++], 1);
                string objNameLabel = File.GetAttribute(text[pointer], 0);
                if (objNameLabel != "hier_name")
                {
                    node = null;
                }
                string objName = File.GetAttribute(text[pointer++], 1);

                switch (objStr)
                {
                case "Mesh":
                    newObject = new HierarchyMesh(objName);
                    node.hObjects.Add(newObject);

                    string triangleNumLabel = File.GetAttribute(text[pointer], 0);
                    if (triangleNumLabel != "triangle_count")
                    {
                        node = null;
                    }
                    uint        triangleNum = uint.Parse(File.GetAttribute(text[pointer++], 1));
                    List <uint> triangles   = new List <uint>();

                    string[] triangs = File.GetAttributes(text[pointer]);
                    for (int j = 0; j < triangleNum; ++j)
                    {
                        triangles.Add(uint.Parse(triangs[j]));
                    }
                    ++pointer;

                    ((HierarchyMesh)newObject).triangles = triangles;
                    break;

                case "Light":
                    newObject = new HierarchyLight(GetLightByName(lights, objName));
                    node.hObjects.Add(newObject);
                    break;

                case "Node":
                    newObject = new HierarchyNode(objName);
                    node.hObjects.Add(newObject);

                    ReadHierarchy((HierarchyNode)newObject, lights, text, pointer);
                    break;
                }
            }
        }
 private void addTreeViewMesh(HierarchyMesh hobj, TreeViewItem parent)
 {
     TreeViewItem child = new TreeViewItem();
     child.Header = hobj.ToString();
     child.Name = "mesh"+"_"+hobj.triangles.ElementAt(0).ToString(); //identyfikator mesha
     child.IsSelected = false;
     parent.Items.Add(child);
 }
        private void buildHierarchy(HierarchyNode nodeParent, TreeViewItem treeItem, 
            List<HierarchyMesh> meshes, List<HierarchyLight> lights)
        {
            foreach (TreeViewItem tvi in treeItem.Items)
            {
                if (tvi.HasItems)
                {
                    HierarchyNode hn = new HierarchyNode(tvi.Header.ToString());
                    //Console.WriteLine("nazwa nodea " + tvi.Header.ToString());
                    buildHierarchy(hn, tvi, meshes, lights);
                    //Console.WriteLine("budujemy hierarchie - node :" + hn.name.ToString());
                    nodeParent.hObjects.Add(hn);
                }
                else //mesh || light
                {
                    char[] delimiterChars = { '_' };
                    string[] words = tvi.Name.Split(delimiterChars);
                    if (words[0].Equals("mesh"))  //mesh
                    {
                        HierarchyMesh hm = new HierarchyMesh(tvi.Header.ToString());
                        foreach (HierarchyMesh hmesh in meshes)
                        {
                            if (Convert.ToInt32(words[1]) == (int)hmesh.triangles.ElementAt(0))
                            {
                                hm = hmesh;
                                hm.name = tvi.Header.ToString();
                                break;
                            }
                        }
                        //Console.WriteLine("budujemy hierarchie - mesh :" + hm.name.ToString());
                        nodeParent.hObjects.Add(hm);
                    }
                    else //if(words[0].Equals("light"));
                    {

                        HierarchyLight hl = new HierarchyLight(tvi.Name.ToString(), (Convert.ToInt32(words[1])));
                        hl.name = tvi.Header.ToString();
                        nodeParent.hObjects.Add(hl);
                    }
                }
            }
        }
Esempio n. 18
0
        private static void ReadHierarchy(HierarchyNode node, List <Light_> lights, List <string> text, ref int pointer)
        {
            string objNumLabel = File.GetAttribute(text[pointer], 0);

            if (objNumLabel != "node_count")
            {
                throw new Exception();
            }
            uint objNum = uint.Parse(File.GetAttribute(text[pointer++], 1));

            for (int i = 0; i < objNum; ++i)
            {
                HierarchyObject newObject   = null;
                string          objStrLabel = File.GetAttribute(text[pointer], 0);
                if (objStrLabel != "hier_type")
                {
                    throw new Exception();
                }
                string objStr = File.GetAttribute(text[pointer++], 1);

                string[] objNameAtts = File.GetAttributes(text[pointer]);
                if (objNameAtts[0] != "hier_name")
                {
                    throw new Exception();
                }
                string objName = objName = File.CutFirstString(text[pointer]);
                ++pointer;

                switch (objStr)
                {
                case "Mesh":
                    newObject = new HierarchyMesh(objName);
                    node.hObjects.Add(newObject);

                    string triangleNumLabel = File.GetAttribute(text[pointer], 0);
                    if (triangleNumLabel != "triangle_count")
                    {
                        new Exception();
                    }
                    uint        triangleNum = uint.Parse(File.GetAttribute(text[pointer++], 1));
                    List <uint> triangles   = new List <uint>();

                    string[] triangs = File.GetAttributes(text[pointer]);
                    for (int j = 0; j < triangleNum; ++j)
                    {
                        triangles.Add(uint.Parse(triangs[j]));
                    }
                    ++pointer;

                    ((HierarchyMesh)newObject).triangles = triangles;
                    break;

                case "Light":
                    string lightIdxLabel = File.GetAttribute(text[pointer], 0);
                    if (lightIdxLabel != "light_index")
                    {
                        new Exception();
                    }
                    int lightIdx = int.Parse(File.GetAttribute(text[pointer++], 1));
                    newObject = new HierarchyLight(objName, lightIdx);
                    node.hObjects.Add(newObject);
                    break;

                case "Node":
                    newObject = new HierarchyNode(objName);
                    node.hObjects.Add(newObject);

                    ReadHierarchy((HierarchyNode)newObject, lights, text, ref pointer);
                    break;
                }
            }
        }