private void buttonOpen_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = "Open Scene File"; ofd.Filter = "Wavefront OBJ Files|*.obj;*.obj.gz" + "|All scene types|*.obj"; ofd.FilterIndex = 1; ofd.FileName = ""; if (ofd.ShowDialog() != DialogResult.OK) { return; } WavefrontObj objReader = new WavefrontObj(); objReader.MirrorConversion = false; int faces = objReader.ReadBrep(ofd.FileName, scene); scene.BuildCornerTable(); int errors = scene.CheckCornerTable(null); labelFaces.Text = string.Format("{0} faces, {1} errors", faces, errors); redraw(); }
private void buttonOpen_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = "Open Scene File"; ofd.Filter = "Wavefront OBJ Files|*.obj;*.obj.gz" + "|All scene types|*.obj"; ofd.FilterIndex = 1; ofd.FileName = ""; if (ofd.ShowDialog() != DialogResult.OK) { cn = null; points = 0; return; } WavefrontObj objReader = new WavefrontObj(); objReader.MirrorConversion = false; int faces = objReader.ReadBrep(ofd.FileName, scene); scene.BuildCornerTable(); diameter = scene.GetDiameter(out center); ResetCamera(); cn = new Subdivision(); points = cn.Subdivide(scene, (float)numericSubdivision.Value * diameter, checkNormals.Checked, checkColors.Checked, false, 0.0f, textParam.Text); labelFile.Text = string.Format("{0}: {1} f, {2} pts", ofd.SafeFileName, faces, points); PrepareDataBuffers(); glControl1.Invalidate(); }
private void buttonOpen_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = "Open Scene File"; ofd.Filter = "Wavefront OBJ Files|*.obj;*.obj.gz" + "|All scene types|*.obj"; ofd.FilterIndex = 1; ofd.FileName = ""; if (ofd.ShowDialog() != DialogResult.OK) { return; } WavefrontObj objReader = new WavefrontObj(); objReader.MirrorConversion = false; int faces = objReader.ReadBrep(ofd.FileName, scene); scene.BuildCornerTable(); diameter = scene.GetDiameter(out center); scene.GenerateColors(12); ResetCamera(); labelFile.Text = string.Format("{0}: {1} faces", ofd.SafeFileName, faces); PrepareDataBuffers(); glControl1.Invalidate(); }
private void buttonLoad_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = "Open Scene File"; ofd.Filter = "Wavefront OBJ Files|*.obj;*.obj.gz" + "|All scene types|*.obj"; ofd.FilterIndex = 1; ofd.FileName = ""; if (ofd.ShowDialog() != DialogResult.OK) { return; } WavefrontObj objReader = new WavefrontObj(); objReader.MirrorConversion = false; int faces = objReader.ReadBrep(ofd.FileName, brepScene); brepScene.BuildCornerTable(); labelSample.Text = string.Format("{0} faces", faces); imf = null; // reset the scene object (to be sure) rend = null; // reset the renderer as well.. }
private void buttonOpen_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = "Open Scene File"; ofd.Filter = "Wavefront OBJ Files|*.obj;*.obj.gz" + "|All scene types|*.obj"; ofd.FilterIndex = 1; ofd.FileName = ""; if (ofd.ShowDialog() != DialogResult.OK) { return; } WavefrontObj objReader = new WavefrontObj(); objReader.MirrorConversion = false; objReader.TextureUpsideDown = checkOrientation.Checked; objReader.ReadBrep(ofd.FileName, scene); // Scene postprocessing. scene.BuildCornerTable(); scene.GenerateColors(12); scene.ComputeNormals(); diameter = scene.GetDiameter(out center); // Viewport update. UpdateParams(textParam.Text); tb.Center = center; tb.Diameter = diameter; tb.Reset(); camera.Center = center; camera.Diameter = diameter; camera.Reset(); SetupViewport(); SetLight(diameter, ref light); modelStatus = $"{ofd.SafeFileName}: {scene.Vertices}v, {scene.statEdges}e({scene.statShared}), {scene.Triangles + scene.Lines}f"; labelFile.Text = modelStatus; // Prepare rendering system. PrepareDataBuffers(); glControl1.Invalidate(); }
private void buttonOpen_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = "Open Scene File"; ofd.Filter = "Wavefront OBJ Files|*.obj;*.obj.gz" + "|Polygon File Format|*.ply" + "|All scene types|*.obj;*.ply"; ofd.FilterIndex = 1; ofd.FileName = ""; if (ofd.ShowDialog() != DialogResult.OK) { return; } string ext = Path.GetExtension(ofd.FileName); StanfordPly plyReader; WavefrontObj objReader; if (ext == ".ply") { plyReader = new StanfordPly(); if (plyReader.ReadBrep(ofd.FileName, scene) == -1) { MessageBox.Show("Invalid .ply file"); } } else { objReader = new WavefrontObj(); objReader.MirrorConversion = false; objReader.TextureUpsideDown = checkOrientation.Checked; objReader.ReadBrep(ofd.FileName, scene); } scene.BuildCornerTable(); diameter = scene.GetDiameter(out center); scene.GenerateColors(12); scene.ComputeNormals(); UpdateParams(textParam.Text); tb.Center = center; tb.Diameter = diameter; SetLight(diameter, ref light); tb.Reset(); labelFile.Text = string.Format("{0} @ {1} vertices, {2} edges ({3}), {4} faces", ofd.SafeFileName, scene.Vertices, scene.statEdges, scene.statShared, scene.Triangles); PrepareDataBuffers(); glControl1.Invalidate(); // Supports point selection selectedPoints = new Selection(scene); commandManager = new CommandManager(); }
public static long TestScene(IRayScene sc, string[] names) { Debug.Assert(sc != null); Vector3 center = Vector3.Zero; // center of the mesh Vector3d dir = new Vector3d(0.1, -0.3, 0.9); dir.Normalize(); // normalized viewing vector of the camera float diameter = 2.0f; // default scene diameter double FoVy = 60.0; // Field of View in degrees int faces = 0; // CSG scene: CSGInnerNode root = new CSGInnerNode(SetOperation.Union); // OBJ file to read: if (names.Length == 0 || names[0].Length == 0) { names = new string[] { "teapot.obj" } } ; string[] paths = Scenes.SmartFindFiles(names); if (paths[0] == null || paths[0].Length == 0) { for (int i = 0; i < names.Length; i++) { if (names[i].Length > 0) { names[i] += ".gz"; } } paths = Scenes.SmartFindFiles(names); } if (paths[0] == null || paths[0].Length == 0) { root.InsertChild(new Sphere(), Matrix4d.Identity); } else { // B-rep scene construction: WavefrontObj objReader = new WavefrontObj(); objReader.MirrorConversion = false; SceneBrep brep = new SceneBrep(); faces = objReader.ReadBrep(paths[0], brep); brep.BuildCornerTable(); diameter = brep.GetDiameter(out center); TriangleMesh m = new FastTriangleMesh(brep); root.InsertChild(m, Matrix4d.Identity); } root.SetAttribute(PropertyName.REFLECTANCE_MODEL, new PhongModel()); root.SetAttribute(PropertyName.MATERIAL, new PhongMaterial(new double[] { 0.5, 0.5, 0.5 }, 0.2, 0.5, 0.4, 12)); root.SetAttribute(PropertyName.COLOR, new double[] { 1.0, 0.6, 0.0 }); sc.Intersectable = root; // Background color: sc.BackgroundColor = new double[] { 0.0, 0.05, 0.07 }; // Camera: double dist = (0.6 * diameter) / Math.Tan(MathHelper.DegreesToRadians((float)(0.5 * FoVy))); Vector3d cam = (Vector3d)center - dist * dir; sc.Camera = new StaticCamera(cam, dir, FoVy); // Light sources: sc.Sources = new LinkedList <ILightSource>(); sc.Sources.Add(new AmbientLightSource(0.8)); sc.Sources.Add(new PointLightSource(new Vector3d(-20.0, 12.0, -12.0), 1.0)); return(faces); } }
/// <summary> /// Parses one object from input .obj file. /// </summary> /// <param name="reader">Reader with current position in .obj file.</param> /// <returns> /// Triangle mesh with name of the associated material. /// If material name is an empty string, no material was used. /// </returns> private Tuple <string, FastTriangleMesh> parseObject(StreamReader reader, ref int verticesCount, ref int txtCoordsCount, ref int normalsCount) { SceneBrep scene = new SceneBrep(); string materialName = ""; int v0 = scene.Vertices; int lastVertex = v0 - 1; int faces = 0; List <Vector2> txtCoords = new List <Vector2>(256); List <Vector3> normals = new List <Vector3>(256); int lastTxtCoord = -1; int lastNormal = -1; int[] f = new int[3]; while (!reader.EndOfStream) { // Check if there is defined "o" tag, if yes, it is the end of definition of one object // and return this object. if ((char)reader.Peek() == 'o') { break; } string line = reader.ReadLine(); string[] tokens = line.Split(); if (tokens.Length == 0) { continue; } switch (tokens[0]) { case "v": if (tokens.Length != 4) { throw new Exception("Incorrect .obj file."); } // Add vertex. float x, y, z; if (!float.TryParse(tokens[1], NumberStyles.Float, CultureInfo.InvariantCulture, out x) || !float.TryParse(tokens[2], NumberStyles.Float, CultureInfo.InvariantCulture, out y) || !float.TryParse(tokens[3], NumberStyles.Float, CultureInfo.InvariantCulture, out z)) { throw new Exception("Incorrect .obj file."); } lastVertex = scene.AddVertex(new Vector3(x, y, z)); break; case "vt": if (tokens.Length != 3) { throw new Exception("Incorrect .obj file."); } // Add vertex. float u, v; if (!float.TryParse(tokens[1], NumberStyles.Float, CultureInfo.InvariantCulture, out u) || !float.TryParse(tokens[2], NumberStyles.Float, CultureInfo.InvariantCulture, out v)) { throw new Exception("Incorrect .obj file."); } txtCoords.Add(new Vector2(u, v)); ++lastTxtCoord; break; case "vn": if (tokens.Length != 4) { throw new Exception("Incorrect .obj file."); } // Add vertex. float nx, ny, nz; if (!float.TryParse(tokens[1], NumberStyles.Float, CultureInfo.InvariantCulture, out nx) || !float.TryParse(tokens[2], NumberStyles.Float, CultureInfo.InvariantCulture, out ny) || !float.TryParse(tokens[3], NumberStyles.Float, CultureInfo.InvariantCulture, out nz)) { throw new Exception("Incorrect .obj file."); } normals.Add(new Vector3(nx, ny, nz)); break; case "usemtl": // Set name of the material used for this object. if (tokens.Length != 2) { throw new Exception("Incorrect .obj file."); } materialName = tokens[1]; break; case "f": // Face must be formed by at least three vertices. if (tokens.Length < 4) { continue; } // Number of vertices. int N = tokens.Length - 1; // Reuse same array for each face and resize it if needed. if (f.Length < N) { f = new int[N]; } // Shrink it if less vertices is used. if (N < f.Length) { f = new int[N]; } int i; for (i = 0; i < N; i++) // read indices for one vertex { string[] vt = tokens[i + 1].Split('/'); int ti, ni; ti = ni = 0; // 0 => value not present // 0 .. vertex coord index if (!int.TryParse(vt[0], out f[i]) || f[i] == 0) { break; } if (f[i] > 0) { f[i] = v0 + f[i] - 1 - verticesCount; } else { f[i] = lastVertex + 1 - f[i]; } if (vt.Length > 1) { // 1 .. texture coord index (not yet) if (!int.TryParse(vt[1], out ti)) { ti = 0; } if (vt.Length > 2) { // 2 .. normal vector index if (!int.TryParse(vt[2], out ni)) { ni = 0; } } } // there was a texture coord.. if (ti != 0) { if (ti > 0) { ti = ti - txtCoordsCount - 1; } else { ti = lastTxtCoord + 1 - ti; } if (ti >= 0 && ti < txtCoords.Count) { scene.SetTxtCoord(f[i], txtCoords[ti]); } } // there was a normal.. if (ni != 0) { if (ni > 0) { ni = ni - normalsCount - 1; } else { ni = lastNormal + 1 - ni; } if (ni >= 0 && ni < normals.Count) { scene.SetNormal(f[i], normals[ni]); } } } N = i; for (i = 1; i < N - 1; i++) { scene.AddTriangle(f[0], f[i], f[i + 1]); faces++; } break; } } // Update count of vertices, txt coords and normals. verticesCount += scene.Vertices; txtCoordsCount += txtCoords.Count; normalsCount += normals.Count; scene.BuildCornerTable(); scene.BuildBoundingBox(); return(new Tuple <string, FastTriangleMesh>(materialName, new FastTriangleMesh(scene))); }