public Scene() { points = new List<Vector3D>(); triangles = new List<Triangle>(); parts = new List<Part>(); materials = new List<Material_>(); materialAssign = new List<String>(); lights = new List<Light_>(); cams = new List<Camera>(); hierarchy = new Hierarchy(); selTriangles = new List<HierarchyMesh>(); addedObject = new List<HierarchyMesh>(); selLights = new List<Light_>(); selCams = new List<Camera>(); cams.Add(new Camera("Cam1", 800, 600, new Vector3(5, 2, 5), new Vector3(-1, 0, -1), 60, 0)); activeCamera = 0; indices = new int[3 * triangles.Count]; selIndices = new int[3 * triangles.Count]; numIndices = 0; numSelIndices = 0; vertices = new Vertex[points.Count]; vertexTriangle = new List<int>[points.Count]; normals = new List<Vector3D>(); }
public Scene() { points = new List<Vector3D>(); triangles = new List<Triangle>(); parts = new List<Part>(); materials = new List<Material_>(); materialAssign = new List<String>(); lights = new List<Light_>(); cams = new List<Camera>(); hierarchy = new Hierarchy(); selTriangles = new List<HierarchyMesh>(); selLights = new List<int>(); selCams = new List<int>(); //Light_ defaultLight = new Light_("default", Light_Type.Point, true, 1, 1, 1, 30, new Vector3(1, 1, 1)); //lights.Add(defaultLight); //HierarchyLight defaultHierLight = new HierarchyLight(defaultLight.name, lights.IndexOf(defaultLight)); //hierarchy.objects.Add(defaultHierLight); cams.Add(new Camera("Cam1", 800, 600, new Vector3(4.9f, 2, 5), new Vector3(3.7f, 1.5f, 3.5f), 60, 0)); activeCamera = 0; hierarchyChange = true; normals = new List<Vector3D>(); }
//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 static Scene ReadSceneFromFile(string file) { Scene scene = new Scene(); try { scene.filePath = file; scene.modified = false; List<string> text = File.ReadFileLines(file); int pointer = 0; string pointsNumLabel = File.GetAttribute(text[pointer], 0); if(pointsNumLabel != "points_count") { return null; } uint pointsNum = uint.Parse(File.GetAttribute(text[pointer++], 1)); List<Vector3D> points = new List<Vector3D>(); for(int i = 0; i < pointsNum; ++i) { string[] attsPoint = File.GetAttributes(text[pointer++]); points.Add(new Vector3D(float.Parse(attsPoint[0], CultureInfo.InvariantCulture), float.Parse(attsPoint[1], CultureInfo.InvariantCulture), float.Parse(attsPoint[2], CultureInfo.InvariantCulture))); } string triangleNumLabel = File.GetAttribute(text[pointer], 0); if(triangleNumLabel != "triangles_count") { return null; } uint triangleNum = uint.Parse(File.GetAttribute(text[pointer++], 1)); List<Triangle> triangles = new List<Triangle>(); for(int i = 0; i < triangleNum; ++i) { string[] attsTriangle = File.GetAttributes(text[pointer++]); triangles.Add(new Triangle(uint.Parse(attsTriangle[0]), uint.Parse(attsTriangle[1]), uint.Parse(attsTriangle[2]))); } string partsNumLabel = File.GetAttribute(text[pointer], 0); if(partsNumLabel != "parts_count") { return null; } uint partsNum = uint.Parse(File.GetAttribute(text[pointer++], 1)); List<uint> trPart = new List<uint>(); string[] atts = File.GetAttributes(text[pointer]); for(int i = 0; i < triangleNum; ++i) { trPart.Add(uint.Parse(atts[i])); } ++pointer; List<Part> parts = new List<Part>(); for(int i = 0; i < partsNum; ++i) { List<int> partTriangles = new List<int>(); for(int j = 0; j < trPart.Count; ++j) { if(trPart[j] == i) { partTriangles.Add(j); } } parts.Add(new Part(partTriangles)); } string matNumLabel = File.GetAttribute(text[pointer], 0); if(matNumLabel != "materials_count") { return null; } uint matNum = uint.Parse(File.GetAttribute(text[pointer++], 1)); List<Material_> materials = new List<Material_>(); for(int i = 0; i < matNum; ++i) { string[] matName = File.GetAttributes(text[pointer]); if(matName[0] != "mat_name") { return null; } string name = File.CutFirstString(text[pointer]); ++pointer; string rgbLabel = File.GetAttribute(text[pointer], 0); if(rgbLabel != "rgb") { return null; } float colorR = float.Parse(File.GetAttribute(text[pointer], 1), CultureInfo.InvariantCulture); float colorG = float.Parse(File.GetAttribute(text[pointer], 2), CultureInfo.InvariantCulture); float colorB = float.Parse(File.GetAttribute(text[pointer++], 3), CultureInfo.InvariantCulture); string kdCrLabel = File.GetAttribute(text[pointer], 0); float kdCr = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); if(kdCrLabel != "kdCr") { return null; } string kdCgLabel = File.GetAttribute(text[pointer], 0); float kdCg = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); if(kdCgLabel != "kdCg") { return null; } string kdCbLabel = File.GetAttribute(text[pointer], 0); float kdCb = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); if(kdCbLabel != "kdCb") { return null; } string ksCrLabel = File.GetAttribute(text[pointer], 0); float ksCr = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); if(ksCrLabel != "ksCr") { return null; } string ksCgLabel = File.GetAttribute(text[pointer], 0); float ksCg = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); if(ksCgLabel != "ksCg") { return null; } string ksCbLabel = File.GetAttribute(text[pointer], 0); float ksCb = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); if(ksCbLabel != "ksCb") { return null; } string krCrLabel = File.GetAttribute(text[pointer], 0); float krCr = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); if(krCrLabel != "krCr") { return null; } string krCgLabel = File.GetAttribute(text[pointer], 0); float krCg = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); if(krCgLabel != "krCg") { return null; } string krCbLabel = File.GetAttribute(text[pointer], 0); float krCb = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); if(krCbLabel != "krCb") { return null; } string kaCrLabel = File.GetAttribute(text[pointer], 0); float kaCr = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); if(kaCrLabel != "kaCr") { return null; } string kaCgLabel = File.GetAttribute(text[pointer], 0); float kaCg = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); if(kaCgLabel != "kaCg") { return null; } string kaCbLabel = File.GetAttribute(text[pointer], 0); float kaCb = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); if(kaCbLabel != "kaCb") { return null; } string gLabel = File.GetAttribute(text[pointer], 0); float g = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); if(gLabel != "g") { return null; } string nLabel = File.GetAttribute(text[pointer], 0); float n = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); if(nLabel != "n") { return null; } materials.Add(new Material_(name, colorR, colorG, colorB, kdCr, kdCg, kdCb, ksCr, ksCg, ksCb, krCr, krCg, krCb, kaCr, kaCg, kaCb, g, n)); } List<string> matAssign = new List<string>(); for(int i = 0; i < partsNum; ++i) { string mat = File.CutFirstString(text[pointer++]); matAssign.Add(mat); } string lightsLabel = File.GetAttribute(text[pointer], 0); if(lightsLabel != "lights_count") { return null; } uint lightsNum = uint.Parse(File.GetAttribute(text[pointer++], 1)); List<Light_> lights = new List<Light_>(); for(int i = 0; i < lightsNum; ++i) { string[] lightName = File.GetAttributes(text[pointer]); if(lightName[0] != "light_name") { return null; } string name = File.CutFirstString(text[pointer]); ++pointer; string enabledLabel = File.GetAttribute(text[pointer], 0); if(enabledLabel != "enabled") { return null; } bool enabled = int.Parse(File.GetAttribute(text[pointer++], 1)) == 1 ? true : false; string typeLabel = File.GetAttribute(text[pointer], 0); if(typeLabel != "light_type") { return null; } Light_Type type = Light_Type.Point; switch(File.GetAttribute(text[pointer++], 1)) { case "point": type = Light_Type.Point; break; case "spot": type = Light_Type.Spot; break; case "goniometric": type = Light_Type.Goniometric; break; } string colorLabel = File.GetAttribute(text[pointer], 0); if(colorLabel != "rgb") { return null; } float colorR = float.Parse(File.GetAttribute(text[pointer], 1), CultureInfo.InvariantCulture); float colorG = float.Parse(File.GetAttribute(text[pointer], 2), CultureInfo.InvariantCulture); float colorB = float.Parse(File.GetAttribute(text[pointer++], 3), CultureInfo.InvariantCulture); string powerLabel = File.GetAttribute(text[pointer], 0); if(powerLabel != "power") { return null; } float power = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); string posLabel = File.GetAttribute(text[pointer], 0); if(posLabel != "pos") { return null; } Vector3 pos = new Vector3(float.Parse(File.GetAttribute(text[pointer], 1), CultureInfo.InvariantCulture), float.Parse(File.GetAttribute(text[pointer], 2), CultureInfo.InvariantCulture), float.Parse(File.GetAttribute(text[pointer++], 3), CultureInfo.InvariantCulture)); string dirLabel = File.GetAttribute(text[pointer], 0); if(dirLabel != "dir") { return null; } Vector3 dir = new Vector3(float.Parse(File.GetAttribute(text[pointer], 1), CultureInfo.InvariantCulture), float.Parse(File.GetAttribute(text[pointer], 2), CultureInfo.InvariantCulture), float.Parse(File.GetAttribute(text[pointer++], 3), CultureInfo.InvariantCulture)); string innerAngleLabel = File.GetAttribute(text[pointer], 0); if(innerAngleLabel != "inner_angle") { return null; } float innerAngle = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); string outerAngleLabel = File.GetAttribute(text[pointer], 0); if(outerAngleLabel != "outer_angle") { return null; } float outerAngle = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); SortedList<float, float> goniometric = new SortedList<float, float>(); string gonioNumLabel = File.GetAttribute(text[pointer], 0); if(gonioNumLabel != "gonio_count") { return null; } uint gonioNum = uint.Parse(File.GetAttribute(text[pointer++], 1)); for(int j = 0; j < gonioNum; ++j) { float gonioIndex = float.Parse(File.GetAttribute(text[pointer], 0), CultureInfo.InvariantCulture); float gonioValue = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); goniometric.Add(gonioIndex, gonioValue); } lights.Add(new Light_(name, type, enabled, colorR, colorG, colorB, power, pos)); lights[lights.Count - 1].direction = new Vector3(dir.X, dir.Y, dir.Z); lights[lights.Count - 1].innerAngle = innerAngle; lights[lights.Count - 1].outerAngle = outerAngle; lights[lights.Count - 1].goniometric = goniometric; } string camsNumLabel = File.GetAttribute(text[pointer], 0); if(camsNumLabel != "cams_count") { return null; } uint camsNum = uint.Parse(File.GetAttribute(text[pointer++], 1)); List<Camera> cams = new List<Camera>(); string activeCamLabel = File.GetAttribute(text[pointer], 0); if(activeCamLabel != "active") { return null; } uint activeCam = uint.Parse(File.GetAttribute(text[pointer++], 1)); for(int i = 0; i < camsNum; ++i) { string nameLabel = File.GetAttribute(text[pointer], 0); if(nameLabel != "cam_name") { return null; } string name = File.CutFirstString(text[pointer]); ++pointer; string resLabel = File.GetAttribute(text[pointer], 0); if(resLabel != "resolution") { return null; } Pair<int, int> res = new Pair<int, int>(int.Parse(File.GetAttribute(text[pointer], 1)), int.Parse(File.GetAttribute(text[pointer++], 2))); string posLabel = File.GetAttribute(text[pointer], 0); if(posLabel != "pos") { return null; } Vector3 pos = new Vector3(float.Parse(File.GetAttribute(text[pointer], 1), CultureInfo.InvariantCulture), float.Parse(File.GetAttribute(text[pointer], 2), CultureInfo.InvariantCulture), float.Parse(File.GetAttribute(text[pointer++], 3), CultureInfo.InvariantCulture)); string lookAtLabel = File.GetAttribute(text[pointer], 0); if(lookAtLabel != "lookAt") { return null; } Vector3 lookAt = new Vector3(float.Parse(File.GetAttribute(text[pointer], 1), CultureInfo.InvariantCulture), float.Parse(File.GetAttribute(text[pointer], 2), CultureInfo.InvariantCulture), float.Parse(File.GetAttribute(text[pointer++], 3), CultureInfo.InvariantCulture)); string fovAngleLabel = File.GetAttribute(text[pointer], 0); if(fovAngleLabel != "fov") { return null; } float fovAngle = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); string rotateAngleLabel = File.GetAttribute(text[pointer], 0); if(rotateAngleLabel != "rotation") { return null; } float rotateAngle = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); cams.Add(new Camera(name, res.First, res.Second, pos, lookAt, fovAngle, rotateAngle)); } HierarchyNode root = new HierarchyNode("Hierarchy"); ReadHierarchy(root, lights, text, ref pointer); if(root == null) { return null; } Hierarchy hierarchy = new Hierarchy(); hierarchy.objects = root.hObjects; scene.points = points; scene.triangles = triangles; scene.parts = parts; scene.materials = materials; scene.materialAssign = matAssign; scene.lights = lights; scene.cams = cams; scene.activeCamera = (int)activeCam; scene.hierarchy = hierarchy; } catch(Exception) { return null; } return scene; }
public void DeleteSelected() { modified = true; // Najpierw stworz listy indeksow zaznaczonych elementow: trojkatow // punktow, swiatel, kamer // Obiekty nalezy usunac rowniez z hierarchii #region usuwanie_geometrii HashSet <uint> selectedTriangIdx = new HashSet <uint>(); HashSet <uint> selectedPointsIdx = new HashSet <uint>(); List <HierarchyMesh> selectedMeshes = new List <HierarchyMesh>(); bool[] deletedPoints = new bool[points.Count]; bool[] deletedTriangs = new bool[triangles.Count]; for (int i = 0; i < deletedPoints.Length; i++) { deletedPoints[i] = false; } for (int i = 0; i < deletedTriangs.Length; i++) { deletedTriangs[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++) { selectedTriangIdx.Add(mesh.triangles[j]); deletedTriangs[mesh.triangles[j]] = true; selectedPointsIdx.Add(triangles[(int)mesh.triangles[j]].p1); selectedPointsIdx.Add(triangles[(int)mesh.triangles[j]].p2); selectedPointsIdx.Add(triangles[(int)mesh.triangles[j]].p3); deletedPoints[triangles[(int)mesh.triangles[j]].p1] = true; deletedPoints[triangles[(int)mesh.triangles[j]].p2] = true; deletedPoints[triangles[(int)mesh.triangles[j]].p3] = true; } } // Usuwanie z hierarchii foreach (HierarchyMesh mesh in selectedMeshes) { deleteMesh((int)mesh.triangles.ElementAt(0), hierarchy.objects); } // 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 < deletedPoints.Length; i++) { if (deletedPoints[i] && flag) { pointStartEnd.Add(i); flag = false; } if (!deletedPoints[i] && !flag) { pointStartEnd.Add(i - 1); flag = true; } } if (!flag) { pointStartEnd.Add(deletedPoints.Length - 1); } // Lista zawiera indeksy poczatka i konca usuwanych trojkatow List <int> triangStartEnd = new List <int>(); flag = true; for (int i = 0; i < deletedTriangs.Length; i++) { if (deletedTriangs[i] && flag) { triangStartEnd.Add(i); flag = false; } if (!deletedTriangs[i] && !flag) { triangStartEnd.Add(i - 1); flag = true; } } if (!flag) { triangStartEnd.Add(deletedTriangs.Length - 1); } // Usuwanie punktow oraz trojkatow i czesci int offset = 0; for (int i = 0; i < pointStartEnd.Count; i += 2) { int diff = pointStartEnd[i + 1] - pointStartEnd[i] + 1; points.RemoveRange(pointStartEnd[i] - offset, diff); offset += diff; } offset = 0; List <int> idxToDelete = new List <int>(); for (int i = 0; i < triangStartEnd.Count; i += 2) { int diff = triangStartEnd[i + 1] - triangStartEnd[i] + 1; triangles.RemoveRange(triangStartEnd[i] - offset, diff); offset += diff; for (int j = 0; j < parts.Count; j++) { //if (parts[j].triangles.Contains(triangStartEnd[i])) // Szemrana poprawka z tym ifem //if (parts[j].triangles.Count > 0) //{ if (parts[j].triangles.Min() >= triangStartEnd[i] && parts[j].triangles.Max() <= triangStartEnd[i + 1]) { idxToDelete.Add(j); } //} } //if (idxToDelete > -1) //{ // parts.RemoveAt(idxToDelete); // materialAssign.RemoveAt(idxToDelete); //} } offset = 0; foreach (int i in idxToDelete) { parts.RemoveAt(i - offset); materialAssign.RemoveAt(i - offset); ++offset; } if (parts.Count == 0) { materials.RemoveRange(0, materials.Count); materialAssign.RemoveRange(0, materialAssign.Count); } // Przebudowa sceny - nalezy pozmieniac indeksy punktow do ktorych // odwoluja sie pozostale trojkaty, oraz naprawic indeksy trojkatow // w hierarchii List <HierarchyMesh> meshes = hierarchy.GetAllMeshes(); offset = 0; for (int i = 0; i < pointStartEnd.Count; i += 2) { int diffP = pointStartEnd[i + 1] - pointStartEnd[i] + 1; foreach (Triangle triangle in triangles) { 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; } offset = 0; for (int i = 0; i < triangStartEnd.Count; i += 2) { int diffT = triangStartEnd[i + 1] - triangStartEnd[i] + 1; foreach (Part part in parts) { for (int j = 0; j < part.triangles.Count; j++) { if (part.triangles[j] > triangStartEnd[i + 1] - offset) { part.triangles[j] -= diffT; } } } foreach (HierarchyMesh mesh in meshes) { for (int j = 0; j < mesh.triangles.Count; j++) { if (mesh.triangles[j] > triangStartEnd[i + 1] - offset) { mesh.triangles[j] -= (uint)diffT; } } } offset += diffT; } ClearSelectedTriangles(); #endregion #region usuwanie_kamer if (cams.Count > 1 && selCams.Count > 0) { if (activeCamera == cams.Count() - 1) { activeCamera--; } cams.RemoveAt(selCams[0]); //if (selCams[0] == activeCamera) //if (activeCamera < 1) //{ // activeCamera++; //} //else //{ // activeCamera--; //} selCams.RemoveRange(0, selCams.Count); cameraRemoved = true; } #endregion #region usuwanie_swiatel offset = 0; selLights.Sort(); Dictionary <int, int> newLightIdxOffsset = new Dictionary <int, int>(); foreach (int i in selLights) { deleteLight(i, hierarchy.objects); } for (int i = 0; i < lights.Count; i++) { newLightIdxOffsset[i] = offset; if (selLights.Contains(i)) { offset++; } } for (int i = 0; i < selLights.Count; i++) { lights.RemoveAt(selLights[i] - i); } RebuildLightHierarchy(hierarchy.objects, newLightIdxOffsset); selLights.RemoveRange(0, selLights.Count); //// Usuwanie z hierarchii świateł //foreach (int hLight in selLights) //{ // deleteLight(lights.ElementAt(hLight).name, hierarchy.objects); //} hierarchyChange = true; #endregion }
public void AddPreparedElement(PreparedElement element, Vector3 translation) { modified = true; uint pCount = (uint)points.Count; for (int i = 0; i < element.Scene.points.Count; i++) { points.Add(new Vector3D( element.Scene.points[i].x + translation.X, element.Scene.points[i].y + translation.Y, element.Scene.points[i].z + translation.Z)); } uint tCount = (uint)triangles.Count; for (int i = 0; i < element.Scene.triangles.Count; i++) { triangles.Add(new Modeler.Data.Scene.Triangle( element.Scene.triangles[i].p1 + pCount, element.Scene.triangles[i].p2 + pCount, element.Scene.triangles[i].p3 + pCount)); } int lCount = lights.Count; foreach (Light_ light in element.Scene.lights) { lights.Add(new Light_(light)); lights[lights.Count - 1].position += translation; } for (int i = 0; i < element.Scene.parts.Count; i++) { Part part = new Part(new List <int>()); for (int j = 0; j < element.Scene.parts[i].triangles.Count; j++) { part.triangles.Add(element.Scene.parts[i].triangles[j] + (int)tCount); } parts.Add(part); materialAssign.Add(element.Scene.materialAssign[i]); } Hierarchy newHierarchy = new Hierarchy(); newHierarchy.objects = new List <HierarchyObject>(); foreach (HierarchyObject ho in element.Scene.hierarchy.objects) { if (ho is HierarchyLight) { newHierarchy.objects.Add(new HierarchyLight((HierarchyLight)ho)); } else if (ho is HierarchyMesh) { newHierarchy.objects.Add(new HierarchyMesh((HierarchyMesh)ho)); } else if (ho is HierarchyNode) { newHierarchy.objects.Add(new HierarchyNode((HierarchyNode)ho)); } } RebuildAddedHierarchy(newHierarchy.objects, (int)tCount, lCount); hierarchy.objects.AddRange(newHierarchy.objects); foreach (Material_ material in element.Scene.materials) { if (!ContainsMaterialName(material.name)) { materials.Add(new Material_(material)); } } hierarchyChange = true; }
public Scene(Scene copy) { points = new List <Vector3D>(); foreach (Vector3D copyElem in copy.points) { points.Add(new Vector3D(copyElem)); } triangles = new List <Triangle>(); foreach (Triangle copyElem in copy.triangles) { triangles.Add(new Triangle(copyElem.p1, copyElem.p2, copyElem.p3)); } parts = new List <Part>(); foreach (Part copyElem in copy.parts) { parts.Add(new Part(copyElem.triangles)); } materials = new List <Material_>(); foreach (Material_ copyElem in copy.materials) { materials.Add(new Material_(copyElem)); } materialAssign = new List <String>(copy.materialAssign); lights = new List <Light_>(); foreach (Light_ copyElem in copy.lights) { lights.Add(new Light_(copyElem)); } cams = new List <Camera>(copy.cams); hierarchy = new Hierarchy(); hierarchy.objects = new List <HierarchyObject>(copy.hierarchy.objects); selTriangles = new List <HierarchyMesh>(copy.selTriangles); selLights = new List <Light_>(copy.selLights); selCams = new List <Camera>(copy.selCams); addedObject = new List <HierarchyMesh>(copy.addedObject); normals = new List <Vector3D>(copy.normals); numIndices = copy.numIndices; numSelIndices = copy.numSelIndices; indices = new int[3 * copy.triangles.Count]; selIndices = new int[3 * copy.triangles.Count]; //for (int i = 0; i < indices.Length; i++) //{ // indices[i] = copy.indices[i]; // selIndices[i] = copy.selIndices[i]; //} vertices = new Vertex[copy.points.Count]; //for (int i = 0; i < vertices.Length; i++) //{ // vertices[i] = copy.vertices[i]; //} vertexTriangle = new List <int> [copy.points.Count]; Parallel.For(0, vertexTriangle.Length, index => vertexTriangle[index] = new List <int>()); activeCamera = copy.activeCamera; }
public Scene(Scene copy) { points = new List<Vector3D>(); foreach (Vector3D copyElem in copy.points) points.Add(new Vector3D(copyElem)); triangles = new List<Triangle>(); foreach (Triangle copyElem in copy.triangles) triangles.Add(new Triangle(copyElem.p1,copyElem.p2,copyElem.p3)); parts = new List<Part>(); foreach (Part copyElem in copy.parts) parts.Add(new Part(copyElem.triangles)); materials = new List<Material_>(); foreach (Material_ copyElem in copy.materials) materials.Add(new Material_(copyElem)); materialAssign = new List<String>(copy.materialAssign); lights = new List<Light_>(); foreach (Light_ copyElem in copy.lights) lights.Add(new Light_(copyElem)); cams = new List<Camera>(copy.cams); hierarchy = new Hierarchy(); hierarchy.objects = new List<HierarchyObject>(copy.hierarchy.objects); selTriangles = new List<HierarchyMesh>(copy.selTriangles); selLights = new List<Light_>(copy.selLights); selCams = new List<Camera>(copy.selCams); addedObject = new List<HierarchyMesh>(copy.addedObject); normals = new List<Vector3D>(copy.normals); numIndices = copy.numIndices; numSelIndices = copy.numSelIndices; indices = new int[3 * copy.triangles.Count]; selIndices = new int[3 * copy.triangles.Count]; //for (int i = 0; i < indices.Length; i++) //{ // indices[i] = copy.indices[i]; // selIndices[i] = copy.selIndices[i]; //} vertices = new Vertex[copy.points.Count]; //for (int i = 0; i < vertices.Length; i++) //{ // vertices[i] = copy.vertices[i]; //} vertexTriangle = new List<int>[copy.points.Count]; Parallel.For(0, vertexTriangle.Length, index => vertexTriangle[index] = new List<int>()); activeCamera = copy.activeCamera; }
public static Scene ReadSceneFromFile(string file) { Scene scene = new Scene(); try { List <string> text = File.ReadFileLines(file); int pointer = 0; string pointsNumLabel = File.GetAttribute(text[pointer], 0); if (pointsNumLabel != "points_count") { return(null); } uint pointsNum = uint.Parse(File.GetAttribute(text[pointer++], 1)); List <Vector3D> points = new List <Vector3D>(); for (int i = 0; i < pointsNum; ++i) { points.Add(new Vector3D(float.Parse(File.GetAttribute(text[pointer], 0), CultureInfo.InvariantCulture), float.Parse(File.GetAttribute(text[pointer], 1), CultureInfo.InvariantCulture), float.Parse(File.GetAttribute(text[pointer++], 2), CultureInfo.InvariantCulture))); } string triangleNumLabel = File.GetAttribute(text[pointer], 0); if (triangleNumLabel != "triangles_count") { return(null); } uint triangleNum = uint.Parse(File.GetAttribute(text[pointer++], 1)); List <Triangle> triangles = new List <Triangle>(); for (int i = 0; i < triangleNum; ++i) { triangles.Add(new Triangle(uint.Parse(File.GetAttribute(text[pointer], 0)), uint.Parse(File.GetAttribute(text[pointer], 1)), uint.Parse(File.GetAttribute(text[pointer++], 2)))); } string partsNumLabel = File.GetAttribute(text[pointer], 0); if (partsNumLabel != "parts_count") { return(null); } uint partsNum = uint.Parse(File.GetAttribute(text[pointer++], 1)); List <uint> trPart = new List <uint>(); string[] atts = File.GetAttributes(text[pointer]); for (int i = 0; i < triangleNum; ++i) { trPart.Add(uint.Parse(atts[i])); } ++pointer; List <Part> parts = new List <Part>(); for (int i = 0; i < partsNum; ++i) { List <int> partTriangles = new List <int>(); for (int j = 0; j < trPart.Count; ++j) { if (trPart[j] == i) { partTriangles.Add(j); } } parts.Add(new Part(partTriangles)); } string matNumLabel = File.GetAttribute(text[pointer], 0); if (matNumLabel != "materials_count") { return(null); } uint matNum = uint.Parse(File.GetAttribute(text[pointer++], 1)); List <Material_> materials = new List <Material_>(); for (int i = 0; i < matNum; ++i) { string matNameLabel = File.GetAttribute(text[pointer], 0); if (matNameLabel != "mat_name") { return(null); } string name = File.GetAttribute(text[pointer++], 1); string rgbLabel = File.GetAttribute(text[pointer], 0); if (rgbLabel != "rgb") { return(null); } float colorR = float.Parse(File.GetAttribute(text[pointer], 1), CultureInfo.InvariantCulture); float colorG = float.Parse(File.GetAttribute(text[pointer], 2), CultureInfo.InvariantCulture); float colorB = float.Parse(File.GetAttribute(text[pointer++], 3), CultureInfo.InvariantCulture); string kdCrLabel = File.GetAttribute(text[pointer], 0); float kdCr = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); if (kdCrLabel != "kdCr") { return(null); } string kdCgLabel = File.GetAttribute(text[pointer], 0); float kdCg = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); if (kdCgLabel != "kdCg") { return(null); } string kdCbLabel = File.GetAttribute(text[pointer], 0); float kdCb = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); if (kdCbLabel != "kdCb") { return(null); } string ksCrLabel = File.GetAttribute(text[pointer], 0); float ksCr = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); if (ksCrLabel != "ksCr") { return(null); } string ksCgLabel = File.GetAttribute(text[pointer], 0); float ksCg = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); if (ksCgLabel != "ksCg") { return(null); } string ksCbLabel = File.GetAttribute(text[pointer], 0); float ksCb = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); if (ksCbLabel != "ksCb") { return(null); } string krCrLabel = File.GetAttribute(text[pointer], 0); float krCr = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); if (krCrLabel != "krCr") { return(null); } string krCgLabel = File.GetAttribute(text[pointer], 0); float krCg = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); if (krCgLabel != "krCg") { return(null); } string krCbLabel = File.GetAttribute(text[pointer], 0); float krCb = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); if (krCbLabel != "krCb") { return(null); } string kaCrLabel = File.GetAttribute(text[pointer], 0); float kaCr = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); if (kaCrLabel != "kaCr") { return(null); } string kaCgLabel = File.GetAttribute(text[pointer], 0); float kaCg = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); if (kaCgLabel != "kaCg") { return(null); } string kaCbLabel = File.GetAttribute(text[pointer], 0); float kaCb = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); if (kaCbLabel != "kaCb") { return(null); } string gLabel = File.GetAttribute(text[pointer], 0); float g = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); if (gLabel != "g") { return(null); } string nLabel = File.GetAttribute(text[pointer], 0); float n = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); if (nLabel != "n") { return(null); } materials.Add(new Material_(name, colorR, colorG, colorB, kdCr, kdCg, kdCb, ksCr, ksCg, ksCb, krCr, krCg, krCb, kaCr, kaCg, kaCb, g, n)); } List <string> matAssign = new List <string>(); for (int i = 0; i < partsNum; ++i) { matAssign.Add(File.GetAttribute(text[pointer++], 1)); } string lightsLabel = File.GetAttribute(text[pointer], 0); if (lightsLabel != "lights_count") { return(null); } uint lightsNum = uint.Parse(File.GetAttribute(text[pointer++], 1)); List <Light_> lights = new List <Light_>(); for (int i = 0; i < lightsNum; ++i) { string nameLabel = File.GetAttribute(text[pointer], 0); if (nameLabel != "light_name") { return(null); } string name = File.GetAttribute(text[pointer++], 1); string enabledLabel = File.GetAttribute(text[pointer], 0); if (enabledLabel != "enabled") { return(null); } bool enabled = int.Parse(File.GetAttribute(text[pointer++], 1)) == 1 ? true : false; string typeLabel = File.GetAttribute(text[pointer], 0); if (typeLabel != "light_type") { return(null); } Light_Type type = Light_Type.Point; switch (File.GetAttribute(text[pointer++], 1)) { case "point": type = Light_Type.Point; break; case "spot": type = Light_Type.Spot; break; case "goniometric": type = Light_Type.Goniometric; break; } string colorLabel = File.GetAttribute(text[pointer], 0); if (colorLabel != "rgb") { return(null); } float colorR = float.Parse(File.GetAttribute(text[pointer], 1), CultureInfo.InvariantCulture); float colorG = float.Parse(File.GetAttribute(text[pointer], 2), CultureInfo.InvariantCulture); float colorB = float.Parse(File.GetAttribute(text[pointer++], 3), CultureInfo.InvariantCulture); string powerLabel = File.GetAttribute(text[pointer], 0); if (powerLabel != "power") { return(null); } float power = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); string posLabel = File.GetAttribute(text[pointer], 0); if (posLabel != "pos") { return(null); } Vector3 pos = new Vector3(float.Parse(File.GetAttribute(text[pointer], 1), CultureInfo.InvariantCulture), float.Parse(File.GetAttribute(text[pointer], 2), CultureInfo.InvariantCulture), float.Parse(File.GetAttribute(text[pointer++], 3), CultureInfo.InvariantCulture)); string dirLabel = File.GetAttribute(text[pointer], 0); if (dirLabel != "dir") { return(null); } Vector3 dir = new Vector3(float.Parse(File.GetAttribute(text[pointer], 1), CultureInfo.InvariantCulture), float.Parse(File.GetAttribute(text[pointer], 2), CultureInfo.InvariantCulture), float.Parse(File.GetAttribute(text[pointer++], 3), CultureInfo.InvariantCulture)); string innerAngleLabel = File.GetAttribute(text[pointer], 0); if (innerAngleLabel != "inner_angle") { return(null); } float innerAngle = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); string outerAngleLabel = File.GetAttribute(text[pointer], 0); if (outerAngleLabel != "outer_angle") { return(null); } float outerAngle = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); SortedList <float, float> goniometric = new SortedList <float, float>(); string gonioNumLabel = File.GetAttribute(text[pointer], 0); if (gonioNumLabel != "gonio_count") { return(null); } uint gonioNum = uint.Parse(File.GetAttribute(text[pointer++], 1)); for (int j = 0; j < gonioNum; ++j) { float gonioIndex = float.Parse(File.GetAttribute(text[pointer], 0), CultureInfo.InvariantCulture); float gonioValue = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); goniometric.Add(gonioIndex, gonioValue); } lights.Add(new Light_(name, type, enabled, colorR, colorG, colorB, power, pos.X, pos.Y, pos.Z)); lights[lights.Count - 1].direction = new Vector3(dir.X, dir.Y, dir.Z); lights[lights.Count - 1].innerAngle = innerAngle; lights[lights.Count - 1].outerAngle = outerAngle; lights[lights.Count - 1].goniometric = goniometric; } string camsNumLabel = File.GetAttribute(text[pointer], 0); if (camsNumLabel != "cams_count") { return(null); } uint camsNum = uint.Parse(File.GetAttribute(text[pointer++], 1)); List <Camera> cams = new List <Camera>(); string activeCamLabel = File.GetAttribute(text[pointer], 0); if (activeCamLabel != "active") { return(null); } uint activeCam = uint.Parse(File.GetAttribute(text[pointer++], 1)); for (int i = 0; i < camsNum; ++i) { string nameLabel = File.GetAttribute(text[pointer], 0); if (nameLabel != "cam_name") { return(null); } string name = File.GetAttribute(text[pointer++], 1); string resLabel = File.GetAttribute(text[pointer], 0); if (resLabel != "resolution") { return(null); } Pair <int, int> res = new Pair <int, int>(int.Parse(File.GetAttribute(text[pointer], 1)), int.Parse(File.GetAttribute(text[pointer++], 2))); string posLabel = File.GetAttribute(text[pointer], 0); if (posLabel != "pos") { return(null); } Vector3 pos = new Vector3(float.Parse(File.GetAttribute(text[pointer], 1), CultureInfo.InvariantCulture), float.Parse(File.GetAttribute(text[pointer], 2), CultureInfo.InvariantCulture), float.Parse(File.GetAttribute(text[pointer++], 3), CultureInfo.InvariantCulture)); string lookAtLabel = File.GetAttribute(text[pointer], 0); if (lookAtLabel != "lookAt") { return(null); } Vector3 lookAt = new Vector3(float.Parse(File.GetAttribute(text[pointer], 1), CultureInfo.InvariantCulture), float.Parse(File.GetAttribute(text[pointer], 2), CultureInfo.InvariantCulture), float.Parse(File.GetAttribute(text[pointer++], 3), CultureInfo.InvariantCulture)); string fovAngleLabel = File.GetAttribute(text[pointer], 0); if (fovAngleLabel != "fov") { return(null); } float fovAngle = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); string rotateAngleLabel = File.GetAttribute(text[pointer], 0); if (rotateAngleLabel != "rotation") { return(null); } float rotateAngle = float.Parse(File.GetAttribute(text[pointer++], 1), CultureInfo.InvariantCulture); cams.Add(new Camera(name, res.First, res.Second, pos, lookAt, fovAngle, rotateAngle)); } HierarchyNode root = new HierarchyNode("Hierarchy"); ReadHierarchy(root, lights, text, pointer); if (root == null) { return(null); } Hierarchy hierarchy = new Hierarchy(); hierarchy.objects = root.hObjects; scene.points = points; scene.triangles = triangles; scene.parts = parts; scene.materials = materials; scene.materialAssign = matAssign; scene.lights = lights; scene.cams = cams; scene.activeCamera = (int)activeCam; scene.hierarchy = hierarchy; } catch (Exception) { return(null); } return(scene); }
public Scene(Scene copy) { points = new List<Vector3D>(); foreach (Vector3D copyElem in copy.points) points.Add(new Vector3D(copyElem)); triangles = new List<Triangle>(); foreach (Triangle copyElem in copy.triangles) triangles.Add(new Triangle(copyElem.p1,copyElem.p2,copyElem.p3)); parts = new List<Part>(); foreach (Part copyElem in copy.parts) parts.Add(new Part(copyElem.triangles)); materials = new List<Material_>(); foreach (Material_ copyElem in copy.materials) materials.Add(new Material_(copyElem)); materialAssign = new List<String>(copy.materialAssign); lights = new List<Light_>(); foreach (Light_ copyElem in copy.lights) lights.Add(new Light_(copyElem)); cams = new List<Camera>(); foreach (Camera copyElem in copy.cams) { cams.Add(new Camera(copyElem)); } hierarchy = new Hierarchy(); hierarchy.objects = new List<HierarchyObject>(); foreach (HierarchyObject hierarchyObject in copy.hierarchy.objects) { if (hierarchyObject is HierarchyNode) { hierarchy.objects.Add(new HierarchyNode((HierarchyNode)hierarchyObject)); } else if (hierarchyObject is HierarchyMesh) { hierarchy.objects.Add(new HierarchyMesh((HierarchyMesh)hierarchyObject)); } else if (hierarchyObject is HierarchyLight) { hierarchy.objects.Add(new HierarchyLight((HierarchyLight)hierarchyObject)); } } selTriangles = new List<HierarchyMesh>(copy.selTriangles); selLights = new List<int>(copy.selLights); selCams = new List<int>(copy.selCams); normals = new List<Vector3D>(copy.normals); activeCamera = copy.activeCamera; hierarchyChange = true; }
public void AddPreparedElement(PreparedElement element, Vector3 translation) { modified = true; uint pCount = (uint)points.Count; for (int i = 0; i < element.Scene.points.Count; i++) points.Add(new Vector3D( element.Scene.points[i].x + translation.X, element.Scene.points[i].y + translation.Y, element.Scene.points[i].z + translation.Z)); uint tCount = (uint)triangles.Count; for (int i = 0; i < element.Scene.triangles.Count; i++) { triangles.Add(new Modeler.Data.Scene.Triangle( element.Scene.triangles[i].p1 + pCount, element.Scene.triangles[i].p2 + pCount, element.Scene.triangles[i].p3 + pCount)); } int lCount = lights.Count; foreach (Light_ light in element.Scene.lights) { lights.Add(new Light_(light)); lights[lights.Count - 1].position += translation; } for (int i = 0; i < element.Scene.parts.Count; i++) { Part part = new Part(new List<int>()); for (int j = 0; j < element.Scene.parts[i].triangles.Count; j++) { part.triangles.Add(element.Scene.parts[i].triangles[j] + (int) tCount); } parts.Add(part); materialAssign.Add(element.Scene.materialAssign[i]); } Hierarchy newHierarchy = new Hierarchy(); newHierarchy.objects = new List<HierarchyObject>(); foreach (HierarchyObject ho in element.Scene.hierarchy.objects) { if (ho is HierarchyLight) { newHierarchy.objects.Add(new HierarchyLight((HierarchyLight)ho)); } else if (ho is HierarchyMesh) { newHierarchy.objects.Add(new HierarchyMesh((HierarchyMesh)ho)); } else if (ho is HierarchyNode) { newHierarchy.objects.Add(new HierarchyNode((HierarchyNode)ho)); } } RebuildAddedHierarchy(newHierarchy.objects, (int)tCount, lCount); hierarchy.objects.AddRange(newHierarchy.objects); foreach (Material_ material in element.Scene.materials) { if (!ContainsMaterialName(material.name)) { materials.Add(new Material_(material)); } } hierarchyChange = true; }