public static Mesh GetMesh() { if (sm_mesh != null) { return(new Mesh(sm_mesh)); } Mesh m = new Mesh(); m.Indeces.Clear(); m.Verteces.Clear(); for (int i = 0; i < 6; i++) { var vert = new VertexPositionNormalTexture(); vert.Position = __vertexPositions[i * 4 + 0]; vert.Uv = new Vector2(1, 1); m.Verteces.Add(vert); vert.Position = __vertexPositions[i * 4 + 1]; vert.Uv = new Vector2(1, 0); m.Verteces.Add(vert); vert.Position = __vertexPositions[i * 4 + 2]; vert.Uv = new Vector2(0, 0); m.Verteces.Add(vert); vert.Position = __vertexPositions[i * 4 + 3]; vert.Uv = new Vector2(0, 1); m.Verteces.Add(vert); } for (int i = 0; i < INDEXCOUNT; i++) { m.Indeces.Add(__vertexIndex[i]); } sm_mesh = m; return(new Mesh(m)); }
public static Mesh GetMesh() { if (sm_mesh != null) { return(new Mesh(sm_mesh)); } var m = new Mesh(); var tetrahedralAngle = Math.PI * 109.4712f / 180; var segmentAngle = (float)(Math.PI * 2 / 3); var currentAngle = 0f; float radius = 1; var v = new Vector3[4]; v[0] = new Vector3(0, radius, 0); for (var i = 1; i <= 3; i++) { v[i] = new Vector3((float)(radius * Math.Sin(currentAngle) * Math.Sin(tetrahedralAngle)), (float)(radius * Math.Cos(tetrahedralAngle)), (float)(radius * Math.Cos(currentAngle) * Math.Sin(tetrahedralAngle))); currentAngle = currentAngle + segmentAngle; } for (var i = 0; i <= 3; i++) { var t = new VertexPositionNormalTexture(); t.Position = v[i]; m.Verteces.Add(t); } m.Indeces.AddRange(new uint[] { 0, 1, 2, 1, 3, 2, 0, 2, 3, 0, 3, 1 }); sm_mesh = m; return(new Mesh(m)); }
public void loadSTL(string path) { VertexPositionNormalTexture vertex1 = new VertexPositionNormalTexture(); VertexPositionNormalTexture vertex2 = new VertexPositionNormalTexture(); VertexPositionNormalTexture vertex3 = new VertexPositionNormalTexture(); int phase = -1; //0 normal, 1 nothing, 2 3 4 vertex, 5 6 nothing int type = -1; //0 ascii, 1 binary Verteces.Clear(); Indeces.Clear(); StreamReader file = File.OpenText(path); string lh = file.ReadLine(); if (lh != null && lh.IndexOf("solid ", StringComparison.Ordinal) != -1) { type = 0; } else { type = 1; } file.Close(); if (type == 0) { file = File.OpenText(path); while (true) { string lineHeader = file.ReadLine(); if (file.EndOfStream || lineHeader == null) { break; } if (lineHeader.IndexOf("facet normal ", StringComparison.Ordinal) != -1) { phase = 0; } switch (phase) { case 0: var parts = lineHeader.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); vertex1.Normal = new Vector3(Convert.ToSingle(parts[2], ifp), Convert.ToSingle(parts[3], ifp), Convert.ToSingle(parts[4], ifp)); break; case 2: parts = lineHeader.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); vertex1.Position = new Vector3(Convert.ToSingle(parts[1], ifp), Convert.ToSingle(parts[2], ifp), Convert.ToSingle(parts[3], ifp)); break; case 3: parts = lineHeader.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); vertex2.Position = new Vector3(Convert.ToSingle(parts[1], ifp), Convert.ToSingle(parts[2], ifp), Convert.ToSingle(parts[3], ifp)); vertex2.Normal = vertex1.Normal; break; case 4: parts = lineHeader.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); vertex3.Position = new Vector3(Convert.ToSingle(parts[1], ifp), Convert.ToSingle(parts[2], ifp), Convert.ToSingle(parts[3], ifp)); vertex3.Normal = vertex1.Normal; Verteces.Add(vertex1); Verteces.Add(vertex2); Verteces.Add(vertex3); break; } phase++; } file.Close(); } else { var bfile = File.OpenRead(path); bfile.Read(new byte[80], 0, 80); //80b header var reader32 = new byte[4]; bfile.Read(reader32, 0, 4); uint ntri = BitConverter.ToUInt32(reader32, 0); for (int i = 0; i < ntri; i++) { bfile.Read(reader32, 0, 4); vertex1.Normal.X = BitConverter.ToSingle(reader32, 0); bfile.Read(reader32, 0, 4); vertex1.Normal.Y = BitConverter.ToSingle(reader32, 0); bfile.Read(reader32, 0, 4); vertex1.Normal.Z = BitConverter.ToSingle(reader32, 0); bfile.Read(reader32, 0, 4); vertex1.Position.X = BitConverter.ToSingle(reader32, 0); bfile.Read(reader32, 0, 4); vertex1.Position.Y = BitConverter.ToSingle(reader32, 0); bfile.Read(reader32, 0, 4); vertex1.Position.Z = BitConverter.ToSingle(reader32, 0); bfile.Read(reader32, 0, 4); vertex2.Position.X = BitConverter.ToSingle(reader32, 0); bfile.Read(reader32, 0, 4); vertex2.Position.Y = BitConverter.ToSingle(reader32, 0); bfile.Read(reader32, 0, 4); vertex2.Position.Z = BitConverter.ToSingle(reader32, 0); vertex2.Normal = vertex1.Normal; bfile.Read(reader32, 0, 4); vertex3.Position.X = BitConverter.ToSingle(reader32, 0); bfile.Read(reader32, 0, 4); vertex3.Position.Y = BitConverter.ToSingle(reader32, 0); bfile.Read(reader32, 0, 4); vertex3.Position.Z = BitConverter.ToSingle(reader32, 0); vertex3.Normal = vertex1.Normal; Verteces.Add(vertex1); Verteces.Add(vertex2); Verteces.Add(vertex3); bfile.Read(reader32, 0, 2); } bfile.Close(); } for (int i = 0; i < Verteces.Count; i++) { Indeces.Add((uint)i); } }
bool FullIntersect(Ray r, VertexPositionNormalTexture Vertex1, VertexPositionNormalTexture Vertex2, VertexPositionNormalTexture Vertex3) { if (Vector3.Dot(r.direction, Vertex1.Normal) < 0) { return(false); } Vector3 a, b, v; a = Vertex1.Position - r.position; b = Vertex2.Position - r.position; v = Vector3.Cross(a, b); var ip1 = Vector3.Dot(r.direction, v); a = Vertex2.Position - r.position; b = Vertex3.Position - r.position; v = Vector3.Cross(a, b); var ip2 = Vector3.Dot(r.direction, v); a = Vertex3.Position - r.position; b = Vertex1.Position - r.position; v = Vector3.Cross(a, b); var ip3 = Vector3.Dot(r.direction, v); //if (((ip1 < 0) && (ip2 < 0) && (ip3 < 0)) || ((ip1 > 0) && (ip2 > 0) && (ip3 > 0))) //{ // var d = dot(Normal, r->direction); // if (fabs(d) < epsilon) return false; // ips.t[0] = (-dot(Normal, r->position) - distanse) / d; // return ips.t[0] >= 0; //} return(false); }
public static float TriangleSquare(VertexPositionNormalTexture v0, VertexPositionNormalTexture v1, VertexPositionNormalTexture v2) { var a = (v0.Position - v1.Position).Length; var b = (v1.Position - v2.Position).Length; var c = (v0.Position - v2.Position).Length; var s = (a + b + c) / 2.0f; var sq = (float)(Math.Sqrt(s * (s - a) * (s - b) * (s - c)) / Math.PI); return(sq); }