public PLY(FileStream fileStream) { tokenReader = new TokenReader(fileStream); ReadHeader(); ReadData(); tokenReader.Close(); }
static public WorldcraftMap FromFile(string strFileName, string strTexturePath) { //Debug2.Push( "Reading Worldcraft Map" ); WorldcraftMap wm = new WorldcraftMap(); wm.FileName = strFileName; //wm.Textures.DefaultPath = strTexturePath; //wm.Textures.DefaultExtension = ".bmp"; // progressTracker.UpdateProgress( 20 ); // progressTracker.UpdateTask( "Parsing worldcraft map: '" + strFileName + "'..." ); TokenReader tr = new TokenReader(strFileName, ' '); int faceCount = 0; while (tr.LookAhead != null && tr.LookAhead == "{") { WorldcraftObject wo = WorldcraftObject.FromTokenReader(tr, wm.Textures); faceCount += wo.Faces.Count; wm.Objects.Add(wo); } tr.Close(); //Thread.Sleep( 200 ); // progressTracker.UpdateTask( " worldcraft objects: " + wm.Objects.Count ); // progressTracker.UpdateTask( " totals faces count: " + faceCount ); // progressTracker.UpdateTask( " # of textures in use: " + wm.Textures.Count ); //Debug2.Pop(); return(wm); }
public void LoadDataSet(string fileName, Color clr) { //Debug.Assert( this.Faces.Count == 0 ); //Debug2.Push( "Parse Object Definition File" ); TokenReader tr = new TokenReader(fileName, ' '); int vertexCount = int.Parse(tr.GetToken()); int triangleCount = int.Parse(tr.GetToken()); _vertices = new Vector3D[vertexCount]; _normals = new Vector3D[vertexCount]; _triangles = new int[triangleCount * 3]; _trianglePlanes = new Plane3D[triangleCount]; for (int v = 0; v < vertexCount; v++) { tr.GetToken(); _vertices[v] = new Vector3D( float.Parse(tr.GetToken()), float.Parse(tr.GetToken()), float.Parse(tr.GetToken())); _normals[v] = new Vector3D(0, 0, 0); } // translate vertices Vector3D ptOldMin, ptOldMax; FaceUtils.GetExtents(_vertices, out ptOldMin, out ptOldMax); Vector3D ptOldMidPoint = (ptOldMin + ptOldMax) * 0.5f; float fOldScale = (ptOldMidPoint - ptOldMin).GetMagnitude(); for (int i = 0; i < vertexCount; i++) { _vertices[i] = (_vertices[i] - ptOldMidPoint) / fOldScale * this.Scale + this.MidPoint; } for (int t = 0; t < triangleCount; t++) { tr.GetToken(); // triangle vertices _triangles[t * 3 + 0] = int.Parse(tr.GetToken()); _triangles[t * 3 + 1] = int.Parse(tr.GetToken()); _triangles[t * 3 + 2] = int.Parse(tr.GetToken()); // skip extra triangle info tr.GetToken(); tr.GetToken(); tr.GetToken(); // calculate triangle normal Vector3D vertexA = _vertices[_triangles[t * 3 + 0]]; Vector3D vertexB = _vertices[_triangles[t * 3 + 1]]; Vector3D vertexC = _vertices[_triangles[t * 3 + 2]]; _trianglePlanes[t] = Plane3D.FromCoplanarPoints(vertexA, vertexB, vertexC); //Vector3D.Cross( vertexC - vertexB, vertexA - vertexB ).GetUnit(); } tr.Close(); //Debug2.Pop(); /*for( int v = 0; v < vertexCount; v ++ ) { * if( vertexNormals[ v ].GetMagnitude() > 0.1 ) { * vertexNormals[ v ].Normalize(); * } * else { * vertexNormals[ v ] = new Vector3D( 1, 0, 0 ); * } * } */ //Debug2.Push( "Calculate Face Normals" ); float fLimit = 0f; for (int t = 0; t < triangleCount; t++) { for (int v = 0; v < 3; v++) { int vIndex = _triangles[t * 3 + v]; Vector3D normalSum = _trianglePlanes[t].Normal; Vector3D normal = _trianglePlanes[t].Normal; for (int t2 = 0; t2 < triangleCount; t2++) { if (t2 == t) { continue; } bool bMatch = false; for (int v2 = 0; v2 < 3; v2++) { if (vIndex == _triangles[t2 * 3 + v2]) { bMatch = true; break; } } if (bMatch == true) { Vector3D normal2 = _trianglePlanes[t2].Normal; if (Vector3D.Dot(normal, normal2) > fLimit) { normalSum += normal2; } } } if (normalSum.GetMagnitude() > 0.01) { _normals[vIndex] = normalSum.GetUnit(); } else { _normals[vIndex] = (Vector3D)normal.Clone(); } } } //Debug2.Pop(); /* * int a = faceIndices[ f, 0 ]; * int b = faceIndices[ f, 1 ]; * int c = faceIndices[ f, 2 ]; * * Face face = this.Faces[f]; * face.VertexNormals = new Vector3DCollection(); * face.VertexNormals.Add( vertexNormals[ a ].Clone() ); * face.VertexNormals.Add( vertexNormals[ b ].Clone() ); * face.VertexNormals.Add( vertexNormals[ c ].Clone() ); * }*/ }