//---------------------------------------------------------------------------- static public WorldcraftObject FromTokenReader(TokenReader tr, Textures textures) { WorldcraftObject wo = new WorldcraftObject(); Debug.Assert(tr.LookAhead == "{"); tr.GetToken(); while (tr.LookAhead != null && tr.LookAhead != "}") { if (tr.LookAhead == "{") { tr.GetToken(); WorldcraftSidePlanes sidePlanes = new WorldcraftSidePlanes(); while (tr.LookAhead != null && tr.LookAhead == "(") { sidePlanes.Add(WorldcraftSidePlane.FromTokenReader(tr, textures)); } wo.Faces.AddRange(ConvertSidePlanesToFaces(wo, sidePlanes, textures)); Debug.Assert(tr.LookAhead == "}"); tr.GetToken(); } else { string key = tr.GetToken(); string val = tr.GetToken(); if (wo._properties.Contains(key) == false) { wo._properties.Add(key, val); } } } Debug.Assert(tr.LookAhead != null); tr.GetToken(); Debug.Assert(wo.ClassName != null); /*Color clrMaterial = wo.MaterialColor; * foreach( Face face in wo.Faces ) { * face.Color = clrMaterial; * }*/ return(wo); }
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() ); * }*/ }
static public WorldcraftSidePlane FromTokenReader(TokenReader tr, Textures textures) { WorldcraftSidePlane sp = new WorldcraftSidePlane(); Debug.Assert(tr.LookAhead == "("); tr.GetToken(); Vector3D pt1 = ReadVector3D(tr); Debug.Assert(tr.LookAhead == ")"); tr.GetToken(); Debug.Assert(tr.LookAhead == "("); tr.GetToken(); Vector3D pt2 = ReadVector3D(tr); Debug.Assert(tr.LookAhead == ")"); tr.GetToken(); Debug.Assert(tr.LookAhead == "("); tr.GetToken(); Vector3D pt3 = ReadVector3D(tr); Debug.Assert(tr.LookAhead == ")"); tr.GetToken(); sp.plane = Plane3D.FromCoplanarPoints(pt2, pt1, pt3); sp.plane.Flip(); //Debug.Assert( sp.plane != null ); sp.texture = textures.RequestTexture(tr.GetToken()); Debug.Assert(sp.texture != null); if (sp.texture.IsBitmapLoaded == false) { sp.texture.LoadBitmap(); } Debug.Assert(tr.LookAhead == "["); tr.GetToken(); Vector3D sAxis = ReadVector3D(tr); float sOffset = float.Parse(tr.GetToken()); Debug.Assert(tr.LookAhead == "]"); tr.GetToken(); Debug.Assert(tr.LookAhead == "["); tr.GetToken(); Vector3D tAxis = ReadVector3D(tr); float tOffset = float.Parse(tr.GetToken()); Debug.Assert(tr.LookAhead == "]"); tr.GetToken(); Vector3D rAxis = Vector3D.Cross(sAxis, tAxis); float rotation = float.Parse(tr.GetToken()); float sScale = float.Parse(tr.GetToken()); float tScale = float.Parse(tr.GetToken()); Matrix3D xfrm = new Matrix3D(); //xfrm.Scale( sScale, tScale, 1 ); //xfrm.Translate( -sOffset, -tOffset, 0 ); sp.sAxis = sAxis; sp.tAxis = tAxis; //xfrm.ChangeBasis( sAxis, tAxis, rAxis ); //xfrm.Scale( sp.texture.Width, sp.texture.Height, 1 ); //xfrm.Invert(); //sp.xfrm = xfrm; return(sp); }
static protected Vector3D ReadVector3D(TokenReader tr) { return(Vector3D.FromXYZ(float.Parse(tr.GetToken()), float.Parse(tr.GetToken()), float.Parse(tr.GetToken()))); }