internal static bool Equal(Polygon x, Polygon y) { if (x == null || y == null) return false; return TransformationEqual(x.Transformation, y.Transformation) && VerticesEqual(x.Vertices, y.Vertices); }
/// <summary> /// Add a texture coordinated used on faces. /// </summary> /// <param name="polygon">The poligoone where the texture coordinate are to be added.</param> protected static void AddTextureCoordinates(Polygon polygon) { polygon.UVs.Add(new UV(0, 0)); polygon.UVs.Add(new UV(0, 1)); polygon.UVs.Add(new UV(1, 1)); polygon.UVs.Add(new UV(1, 0)); }
/// <summary> /// Construct a new mesh from an ASE GeomObject /// </summary> /// <param name="obj">ASE GeomObject to read from</param> public Mesh(ASE.GeomObject obj) { children = new List<Node>(); name = obj.name; color = Colors.Random(); polygon = new Polygon(); polygon.Material = null; //Vertices foreach (ASE.Vector3D v in obj.mesh.verticies) polygon.Vertices.Add(new Vertex(v.x, v.y, v.z)); //Normals foreach (ASE.Vector3D v in obj.mesh.vertexNormals) polygon.Normals.Add(new Vertex(v.x, v.y, v.z)); //Texture coordinates foreach (ASE.Vector3D uv in obj.mesh.textureCoordinates) polygon.UVs.Add(new UV(uv.x, uv.y)); //Faces foreach (ASE.Face face in obj.mesh.faces) { Face f = new Face(); foreach (int i in face.vertex) f.Indices.Add(new Index(i, -1, i)); f.Material = new Material(); polygon.Faces.Add(f); } setColor(); bone = null; }
protected static void AddTriangleFace(Polygon polygon, Material material, int verticesCount, params int[] verticeOffsets) { var face = new Face(); if (material != null) face.Material = material; foreach (var verticeOffset in verticeOffsets) face.Indices.Add(new Index(verticesCount - verticeOffset, 0)); polygon.Faces.Add(face); }
/// <summary> /// Construct a new Mesh /// </summary> public Mesh() { children = new List<Node>(); name = ""; color = Colors.Random(); polygon = new Polygon(); bone = null; setColor(); }
/// <summary> /// Gets the surface normal. /// </summary> /// <param name="parent">The parent.</param> /// <returns></returns> public Vertex GetSurfaceNormal(Polygon parent) { // Do we have enough vertices for a normal? if (indices.Count < 3) return new Vertex(0, 0, 0); Vertex v1 = parent.Vertices[indices[0].Vertex]; Vertex v2 = parent.Vertices[indices[1].Vertex]; Vertex v3 = parent.Vertices[indices[2].Vertex]; Vertex va = v1 - v2; Vertex vb = v2 - v3; return va.VectorProduct(vb); }
/// <summary> /// Add a face between the point in position x, y, z=0, the point in the position x, y, offsetZ and positions of two last vertices. /// </summary> /// <param name="x">Coordinate X.</param> /// <param name="y">Coordinate Y.</param> /// <param name="offsetZ">The offset od coordinate Z where the second point is located to form the edge.</param> /// <param name="polygon">The poligone where the face is to be added.</param> /// <param name="addendum"></param> /// <param name="initVerticesCount"></param> /// <param name="material"></param> protected static VerticesPair AddFace(float x, float y, float offsetZ, Polygon polygon, float addendum, int initVerticesCount, Material material = null) { var pair = new VerticesPair(x, y, offsetZ, addendum); polygon.Vertices.Add(pair.Top); polygon.Vertices.Add(pair.Base); var verticesCount = polygon.Vertices.Count; if (verticesCount - initVerticesCount <= 2) return pair; AddTriangleFace(polygon, material, verticesCount, 3, 2, 1); AddTriangleFace(polygon, material, verticesCount, 3, 2, 4); return pair; }
public BuildablePolygon(Polygon poly) { Attributes = poly.Attributes; currentContext = poly.CurrentContext; drawNormals = poly.DrawNormals; faces = poly.Faces; Name = poly.Name; normals = poly.Normals; Rotate = poly.Rotate; Scale = poly.Scale; TransformOrder = poly.TransformOrder; Translate = poly.Translate; uvs = poly.UVs; vertices = poly.Vertices; }
/// <summary> /// This function reverses the order of the indices, i.e changes which direction /// this face faces in. /// </summary> /// <param name="parent">The parent polygon.</param> public void Reorder(Polygon parent) { // Create a new index collection. List<Index> newIndices = new List<Index>(); // Go through every old index and add it. for (int i = 0; i < indices.Count; i++) newIndices.Add(indices[indices.Count - (i + 1)]); // Set the new index array. indices = newIndices; // Recreate each normal. GenerateNormals(parent); }
/// <summary> /// Returns the plane equation (ax + by + cz + d = 0) of the face. /// </summary> /// <param name="parent">The parent polygon.</param> /// <returns>An array of four coefficients a,b,c,d.</returns> public float[] GetPlaneEquation(Polygon parent) { // Get refs to vertices. Vertex v1 = parent.Vertices[indices[0].Vertex]; Vertex v2 = parent.Vertices[indices[1].Vertex]; Vertex v3 = parent.Vertices[indices[2].Vertex]; float a = v1.Y * (v2.Z - v3.Z) + v2.Y * (v3.Z - v1.Z) + v3.Y * (v1.Z - v2.Z); float b = v1.Z * (v2.X - v3.X) + v2.Z * (v3.X - v1.X) + v3.Z * (v1.X - v2.X); float c = v1.X * (v2.Y - v3.Y) + v2.X * (v3.Y - v1.Y) + v3.X * (v1.Y - v2.Y); float d = -(v1.X * (v2.Y * v3.Z - v3.Y * v2.Z) + v2.X * (v3.Y * v1.Z - v1.Y * v3.Z) + v3.X * (v1.Y * v2.Z - v2.Y * v1.Z)); return new float[] { a, b, c, d }; }
public BuildablePolygon(Polygon poly) { Attributes = poly.Attributes; castsShadow = poly.CastsShadow; currentContext = poly.CurrentContext; drawNormals = poly.DrawNormals; faces = poly.Faces; material = poly.Material; name = poly.Name; normals = poly.Normals; rotate = poly.Rotate; scale = poly.Scale; shadowSize = poly.ShadowSize; transformationOrder = poly.TransformOrder; translate = poly.Translate; uvs = poly.UVs; vertices = poly.Vertices; }
/// <summary> /// Build cubic <see cref="Polygon" />. /// </summary> /// <param name="height"></param> /// <returns>The <see cref="Polygon" /> with cubic geometry.</returns> public static Polygon Create(float height) { var hh = height/2; var polygon = new Polygon(); polygon.UVs.AddRange(new[] { new UV(0, 0), new UV(0, 1), new UV(1, 1), new UV(1, 0) }); polygon.Vertices.AddRange(new[] { new Vertex(-hh, -hh, -hh), new Vertex(hh, -hh, -hh), new Vertex(hh, -hh, hh), new Vertex(-hh, -hh, hh), new Vertex(-hh, hh, -hh), new Vertex(hh, hh, -hh), new Vertex(hh, hh, hh), new Vertex(-hh, hh, hh) }); var face = new Face(); // The bottom face face.Indices.AddRange(CreateIndices(1, 2, 3, 0)); polygon.Faces.Add(face); face = new Face(); // The top face face.Indices.AddRange(CreateIndices(7, 6, 5, 4)); polygon.Faces.Add(face); face = new Face(); // The right face face.Indices.AddRange(CreateIndices(5, 6, 2, 1)); polygon.Faces.Add(face); face = new Face(); // The left face face.Indices.AddRange(CreateIndices(7, 4, 0, 3)); polygon.Faces.Add(face); face = new Face(); // The front face face.Indices.AddRange(CreateIndices(4, 5, 1, 0)); polygon.Faces.Add(face); face = new Face(); // The back face face.Indices.AddRange(CreateIndices(6, 7, 3, 2)); polygon.Faces.Add(face); return polygon; }
/// <summary> /// This function generates normals for every vertex. /// </summary> /// <param name="parent">The parent polygon.</param> public void GenerateNormals(Polygon parent) { if(Indices.Count >= 3) { foreach(Index index in Indices) { // Do we have enough vertices for a normal? if(Indices.Count >= 3) { // Create a normal. Vertex vNormal = GetSurfaceNormal(parent); vNormal.UnitLength(); // Add it to the normals, setting the index for next time. if(index.Normal != -1) parent.Normals.RemoveAt(index.Normal); index.Normal = parent.Normals.Add(new Normal(vNormal)); } } } }
private static void AddNormal(Polygon polygon, string line, char[] split) { // Get the normal coord strings. string[] values = line.Substring(3).Split(split, StringSplitOptions.RemoveEmptyEntries); // Parse normal coordinates. float x = float.Parse(values[0]); float y = float.Parse(values[1]); float z = float.Parse(values[2]); // Add the normal. polygon.Normals.Add(new Vertex(x, y, z)); }
private static void AddFace(ISceneEngine sceneEngine, Polygon polygon, string mtlName, string line, char[] split) { var face = new Face(); if (!string.IsNullOrWhiteSpace(mtlName)) face.Material = sceneEngine.GetAssets<Material>().FirstOrDefault(t => t.Name == mtlName); // Get the face indices string[] indices = line.Substring(2).Split(split, StringSplitOptions.RemoveEmptyEntries); // Add each index. AddIndexesToFace(indices, face); // Add the face. polygon.Faces.Add(face); }
private static void LoadGeometryFromFile(string fullFileName, ISceneEngine sceneEngine, Polygon polygon, ActionResult<SceneElement> actionResult) { var split = new[] {' '}; string mtlName = null; using (var reader = new StreamReader(fullFileName)) { string line; while ((line = reader.ReadLine()) != null) { // Skip any comments (lines that start with '#'). if (line.StartsWith("#")) continue; if (line.StartsWith("vt")) //texture coordinate { AddTextureCoordinate(polygon, line, split); continue; } if (line.StartsWith("vn")) //normal coordinate { AddNormal(polygon, line, split); continue; } if (line.StartsWith("v")) //vertex { AddVertices(polygon, line, split); continue; } if (line.StartsWith("f")) //Face { AddFace(sceneEngine, polygon, mtlName, line, split); continue; } AddMaterials(fullFileName, sceneEngine, line, ref mtlName, actionResult); } } }
/// <summary> /// Load of 3D geometry from the file /// </summary> /// <param name="fullFileName">The path to the file with 3D geometry</param> /// <param name="sceneEngine">The engine of the scene</param> /// <returns></returns> public ActionResult<SceneElement> LoadGeometry(string fullFileName, ISceneEngine sceneEngine) { var polygon = new Polygon(); var actionResult = new ActionResult<SceneElement>("Import Wavefront format geometry") {Value = polygon}; if (!File.Exists(fullFileName)) { actionResult.AddError(Resources.Message_LoadGeometry_File_N_not_found, fullFileName); return actionResult; } try { LoadGeometryFromFile(fullFileName, sceneEngine, polygon, actionResult); } catch (Exception e) { actionResult.AddError(e); } return actionResult; }
/// <summary> /// This function reads a polygon. /// </summary> /// <param name="reader"></param> /// <returns></returns> protected override object ReadData(BinaryReader reader) { // Create a polygon. Polygon poly = new Polygon(); // Read a name chunk. CaligariName name = new CaligariName(); name.Read(reader); poly.Name = name.name; // Read the local axies. CaligariAxies axies = new CaligariAxies(); axies.Read(reader); poly.Translate = axies.centre; // poly.Rotate = axies.rotate; // Read the position matrix. CaligariPosition pos = new CaligariPosition(); pos.Read(reader); // Read number of verticies. int verticesCount = reader.ReadInt32(); // Get them all for(int i=0; i<verticesCount; i++) { // Read a vertex. Vertex vertex = new Vertex(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); // Multiply it by the position matrix. vertex = vertex * pos.matrix; // Add it. poly.Vertices.Add(vertex); } // Read UV count. int uvsCount = reader.ReadInt32(); // Read all of the UVs for(int i=0; i<uvsCount; i++) poly.UVs.Add(new UV(reader.ReadInt32(), reader.ReadInt32())); // Read faces count. int faces = reader.ReadInt32(); // Read each face. for(int f= 0; f<faces; f++) { Face face = new Face(); poly.Faces.Add(face); // Read face type flags. byte flags = reader.ReadByte(); // Read vertex count short verticesInFace = reader.ReadInt16(); // Do we read a material number? if((flags&0x08) == 0) // is it a 'hole' face? reader.ReadInt16(); // Now read the indices, a vertex index and a uv index. for(short j=0; j<verticesInFace; j++) face.Indices.Add(new Index(reader.ReadInt32(), reader.ReadInt32())); } // Any extra stuff? if(header.minorVersion > 4) { // read flags. reader.ReadChars(4); if((header.minorVersion > 5) && (header.minorVersion < 8)) reader.ReadChars(2); } // Now that we've loaded the polygon, we triangulate it. poly.Triangulate(); // Finally, we update normals. poly.Validate(true); return poly; }
private void buttonHeightMap_Click(object sender, System.EventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "Image Files (*.bmp,*.jpg,*.gif)|*.jpg;*.bmp;*.gif"; if(dialog.ShowDialog(this) == DialogResult.OK) { Polygon poly = new Polygon(); poly.CreateFromMap(dialog.FileName, 40, 40); AddObjectToScene(poly); } }
/// <summary> /// This function creates the internal display lists. /// </summary> /// <param name="gl">OpenGL object.</param> public virtual void Create(OpenGL gl) { // Create the arrow. arrow.Generate(gl); arrow.New(gl, DisplayList.DisplayListMode.Compile); // Draw the 'line' of the arrow. gl.Begin(OpenGL.LINES); gl.Vertex(0, 0, 0); gl.Vertex(0, 0, 1); gl.End(); // Draw the arrowhead. gl.Begin(OpenGL.TRIANGLE_FAN); gl.Vertex(0, 0, 1); gl.Vertex(0.2f, 0.8f, 0.2f); gl.Vertex(0.2f, 0.8f, -0.2f); gl.Vertex(-0.2f, 0.8f, -0.2f); gl.Vertex(-0.2f, 0.8f, 0.2f); gl.End(); // End the arrow list. arrow.End(gl); // Create the grid. grid.Generate(gl); grid.New(gl, DisplayList.DisplayListMode.Compile); gl.PushAttrib(OpenGL.LIGHTING_BIT); gl.Disable(OpenGL.LIGHTING); gl.Color(1, 1, 1); gl.Begin(OpenGL.LINES); for(int i = -10; i <= 10; i++) { gl.Vertex(i, 0, -10); gl.Vertex(i, 0, 10); gl.Vertex(-10, 0, i); gl.Vertex(10, 0, i); } gl.End(); gl.PopAttrib(); grid.End(gl); // Create the axies. axies.Generate(gl); axies.New(gl, DisplayList.DisplayListMode.Compile); gl.PushAttrib(OpenGL.ALL_ATTRIB_BITS); gl.Disable(OpenGL.LIGHTING); gl.Disable(OpenGL.DEPTH_TEST); gl.LineWidth(2.0f); gl.Begin(OpenGL.LINES); gl.Color(1, 0, 0, 1); gl.Vertex(0, 0, 0); gl.Vertex(3, 0, 0); gl.Color(0, 1, 0, 1); gl.Vertex(0, 0, 0); gl.Vertex(0, 3, 0); gl.Color(0, 0, 1, 1); gl.Vertex(0, 0, 0); gl.Vertex(0, 0, 3); gl.End(); gl.PopAttrib(); axies.End(gl); camera.Generate(gl); camera.New(gl, DisplayList.DisplayListMode.Compile); Polygon poly = new Polygon(); poly.CreateCube(); poly.Scale.Set(.2f ,0.2f, 0.2f); poly.Draw(gl); // poly.Dispose(); camera.End(gl); }
public Scene LoadData(string path) { char[] split = new char[] { ' ' }; // Create a scene and polygon. Scene scene = new Scene(); Polygon polygon = new Polygon(); string mtlName = null; // Create a stream reader. using (StreamReader reader = new StreamReader(path)) { // Read line by line. string line = null; while ((line = reader.ReadLine()) != null) { // Skip any comments (lines that start with '#'). if (line.StartsWith("#")) continue; // Do we have a texture coordinate? if (line.StartsWith("vt")) { // Get the texture coord strings. string[] values = line.Substring(3).Split(split, StringSplitOptions.RemoveEmptyEntries); float x = float.Parse(values[0]); float y = float.Parse(values[1]); // Parse texture coordinates. float u = float.Parse(values[0]); float v = float.Parse(values[1]); // Add the texture coordinate. polygon.UVs.Add(new UV(u, v)); continue; } // Do we have a normal coordinate? if (line.StartsWith("vn")) { // Get the normal coord strings. string[] values = line.Substring(3).Split(split, StringSplitOptions.RemoveEmptyEntries); values[0] = values[0].Replace(".", ","); values[1] = values[1].Replace(".", ","); values[2] = values[2].Replace(".", ","); // Parse normal coordinates. float x = float.Parse(values[0]); float y = float.Parse(values[1]); float z = float.Parse(values[2]); // Add the normal. polygon.Normals.Add(new Vertex(x, y, z)); continue; } // Do we have a vertex? if (line.StartsWith("v")) { // Get the vertex coord strings. string[] values = line.Substring(2).Split(split, StringSplitOptions.RemoveEmptyEntries); values[0] = values[0].Replace(".", ","); values[1] = values[1].Replace(".", ","); values[2] = values[2].Replace(".", ","); // Parse vertex coordinates. float x = float.Parse(values[0]); float y = float.Parse(values[1]); float z = float.Parse(values[2]); // Add the vertices. polygon.Vertices.Add(new Vertex(x, y, z)); continue; } // Do we have a face? if (line.StartsWith("f")) { Face face = new Face(); if (!String.IsNullOrWhiteSpace(mtlName)) face.Material = scene.Assets.Where(t => t.Name == mtlName).FirstOrDefault() as Material; // Get the face indices string[] indices = line.Substring(2).Split(split, StringSplitOptions.RemoveEmptyEntries); // Add each index. foreach (var index in indices) { // Split the parts. string[] parts = index.Split(new char[] { '/' }, StringSplitOptions.None); // Add each part. face.Indices.Add(new Index( (parts.Length > 0 && parts[0].Length > 0) ? int.Parse(parts[0]) - 1 : -1, (parts.Length > 1 && parts[1].Length > 0) ? int.Parse(parts[1]) - 1 : -1, (parts.Length > 2 && parts[2].Length > 0) ? int.Parse(parts[2]) - 1 : -1)); } // Add the face. polygon.Faces.Add(face); continue; } if (line.StartsWith("mtllib")) { // Set current directory in case a relative path to material file is used. Environment.CurrentDirectory = Path.GetDirectoryName(path); // Load materials file. string mtlPath = ReadMaterialValue(line); LoadMaterials(mtlPath, scene); } if (line.StartsWith("usemtl")) mtlName = ReadMaterialValue(line); } } scene.SceneContainer.AddChild(polygon); return scene; }
private void buttonLoad_Click(object sender, System.EventArgs e) { // Create a persistence engine. SharpGL.Persistence.PersistenceEngine engine = new SharpGL.Persistence.PersistenceEngine(); // Load the polygon. object data = engine.UserLoad(typeof(Polygon)); if(data != null) { // Set the new polygon. BuildingPolygon = (Polygon)data; // Remove the old one. openGLCtrlPolybuild.Scene.Polygons.Clear(); openGLCtrlPolybuild.Scene.Polygons.Add(polygon); // Update the controls. PopulateControls(); } }
public PlaneBuilder() { Polygon plane = new Polygon(); plane.Vertices.Add(new Vertex()); plane.Vertices.Add(new Vertex()); plane.Vertices.Add(new Vertex()); plane.Vertices.Add(new Vertex()); plane.UVs.Add(new UV(0, 0)); plane.UVs.Add(new UV(0, 1)); plane.UVs.Add(new UV(1, 1)); plane.UVs.Add(new UV(1, 0)); Face face = new Face(); face.Indices.Add(new Index(2, 0)); face.Indices.Add(new Index(3, 1)); face.Indices.Add(new Index(1, 2)); face.Indices.Add(new Index(0, 3)); plane.Faces.Add(face); buildingObject = plane; }
protected override object LoadData(Stream stream) { // We use a binary reader for this data. BinaryReader reader = new BinaryReader(stream, System.Text.Encoding.ASCII); // Create a polygon. Polygon poly = new Polygon(); // Read the translate, scale, rotate. poly.Translate.X = reader.ReadSingle(); poly.Translate.Y = reader.ReadSingle(); poly.Translate.Z = reader.ReadSingle(); poly.Scale.X = reader.ReadSingle(); poly.Scale.Y = reader.ReadSingle(); poly.Scale.Z = reader.ReadSingle(); poly.Rotate.X = reader.ReadSingle(); poly.Rotate.Y = reader.ReadSingle(); poly.Rotate.Z = reader.ReadSingle(); // Read number of verticies. int verticesCount = reader.ReadInt32(); // Get them all for(int i=0; i<verticesCount; i++) { // Read a vertex. Vertex vertex = new Vertex(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); // Add it. poly.Vertices.Add(vertex); } // Read UV count. int uvsCount = reader.ReadInt32(); // Read all of the UVs for(int i=0; i<uvsCount; i++) poly.UVs.Add(new UV(reader.ReadInt32(), reader.ReadInt32())); // Read faces count. int faces = reader.ReadInt32(); // Read each face. for(int f= 0; f<faces; f++) { Face face = new Face(); poly.Faces.Add(face); // Read index count int indices = reader.ReadInt32(); // Now read the indices, a vertex index and a uv index and a color index. for(int j=0; j<indices; j++) { face.Indices.Add(new Index(reader.ReadInt32(), reader.ReadInt32())); int colour = reader.ReadInt32(); //not used. } } // Finally, we update normals. poly.Validate(true); return poly; }
public virtual Polygon ToPolygon() { Polygon poly = new Polygon(); poly.Attributes = Attributes; poly.CastsShadow = castsShadow; poly.CurrentContext = currentContext; poly.DrawNormals = drawNormals; poly.Faces = faces; poly.Material = material; poly.Name = name; poly.Normals = normals; poly.Rotate = rotate; poly.Scale = scale; poly.ShadowSize = shadowSize; poly.TransformOrder = transformationOrder; poly.Translate = translate; poly.UVs = uvs; poly.Vertices = vertices; return poly; }
private static void AddTextureCoordinate(Polygon polygon, string line, char[] split) { // Get the texture coord strings. string[] values = line.Substring(3).Split(split, StringSplitOptions.RemoveEmptyEntries); // Parse texture coordinates. float u = float.Parse(values[0]); float v = float.Parse(values[1]); // Add the texture coordinate. polygon.UVs.Add(new UV(u, v)); }
/* public override void DrawPick(OpenGL gl) { base.DrawPick( }*/ public virtual Polygon ToPolygon() { Polygon poly = new Polygon(); poly.Attributes = Attributes; poly.CurrentContext = currentContext; poly.DrawNormals = drawNormals; poly.Faces = faces; poly.Name = Name; poly.Normals = normals; poly.Rotate = Rotate; poly.Scale = Scale; poly.TransformOrder = TransformOrder; poly.Translate = Translate; poly.UVs = uvs; poly.Vertices = vertices; return poly; }
public override void ReadData(Scene scene, BinaryReader stream) { // A triangle mesh is basicly a Polygon, so create it. Polygon poly = new Polygon(); Matrix matrix = new Matrix(); do { // Peep at the next chunk. MAXChunkHeader next = MAXChunkHeader.Peep(stream); if(next.type == ChunkType.CHUNK_VERTLIST) { // Read the vertices. VertexListChunk chunk = new VertexListChunk(); chunk.Read(scene, stream); // Set them into the polygon. poly.Vertices = chunk.vertices; } else if(next.type == ChunkType.CHUNK_FACELIST) { // Read the faces. FaceListChunk chunk = new FaceListChunk(); chunk.Read(scene, stream); // Set them into the polygon. poly.Faces = chunk.faces; } else if(next.type == ChunkType.CHUNK_MAPLIST) { // Read the uvs. MapListChunk chunk = new MapListChunk(); chunk.Read(scene, stream); // Set them into the polygon. poly.UVs = chunk.uvs; } else if(next.type == ChunkType.CHUNK_TRMATRIX) { // Here we just read the matrix (we'll use it later). TrMatrixChunk chunk = new TrMatrixChunk(); chunk.Read(scene, stream); matrix = chunk.matrix; } else { // We don't know what this chunk is, so just read the generic one. MAXChunk chunk = new MAXChunk(); chunk.Read(scene, stream); } } while (MoreChunks(stream)); // Now we multiply each vertex by the matrix. for(int i = 0; i < poly.Vertices.Count; i++) poly.Vertices[i] *= matrix; // Add the poly to the scene. scene.Polygons.Add(poly); }
public Scene LoadData(string path) { char[] split = new char[] { ' '}; // Create a scene and polygon. Scene scene = new Scene(); Polygon polygon = new Polygon(); // Create a stream reader. using (StreamReader reader = new StreamReader(path)) { // Read line by line. string line = null; while( (line = reader.ReadLine()) != null) { // Skip any comments (lines that start with '#'). if (line.StartsWith("#")) continue; // Do we have a texture coordinate? if (line.StartsWith("vt")) { // Get the texture coord strings. string[] values = line.Substring(3).Split(split, StringSplitOptions.RemoveEmptyEntries); // Parse texture coordinates. float u = float.Parse(values[0]); float v = float.Parse(values[1]); // Add the texture coordinate. polygon.UVs.Add(new UV(u, v)); continue; } // Do we have a normal coordinate? if (line.StartsWith("vn")) { // Get the normal coord strings. string[] values = line.Substring(3).Split(split, StringSplitOptions.RemoveEmptyEntries); // Parse normal coordinates. float x = float.Parse(values[0]); float y = float.Parse(values[1]); float z = float.Parse(values[2]); // Add the normal. polygon.Normals.Add(new Vertex(x, y, z)); continue; } // Do we have a vertex? if (line.StartsWith("v")) { // Get the vertex coord strings. string[] values = line.Substring(2).Split(split, StringSplitOptions.RemoveEmptyEntries); // Parse vertex coordinates. float x = float.Parse(values[0]); float y = float.Parse(values[1]); float z = float.Parse(values[2]); // Add the vertices. polygon.Vertices.Add(new Vertex(x, y, z)); continue; } // Do we have a face? if (line.StartsWith("f")) { Face face = new Face(); // Get the face indices string[] indices = line.Substring(2).Split(split, StringSplitOptions.RemoveEmptyEntries); // Add each index. foreach (var index in indices) { // Split the parts. string[] parts = index.Split(new char[] {'/'}, StringSplitOptions.None); // Add each part. face.Indices.Add(new Index( (parts.Length > 0 && parts[0].Length > 0) ? int.Parse(parts[0]) - 1 : -1, (parts.Length > 1 && parts[1].Length > 0) ? int.Parse(parts[1]) - 1 : -1, (parts.Length > 2 && parts[2].Length > 0) ? int.Parse(parts[2]) - 1: -1)); } // Add the face. polygon.Faces.Add(face); continue; } if (line.StartsWith("mtllib")) continue; if (line.StartsWith("usemtl")) continue; } } scene.SceneContainer.AddChild(polygon); return scene; }