private Profile GenerateProfile() { Profile profile = new Profile(); profile.Faces = new List<ProfileFace>(); profile.Positions = new List<Vector3>(); return profile; }
private static void BuildFace(ref Face face, LLObject.ObjectData prim, List<Vertex> vertices, Path path, Profile profile, LLObject.TextureEntryFace teFace) { if (teFace != null) face.TextureFace = teFace; else throw new ArgumentException("teFace cannot be null"); face.Vertices.Clear(); if ((face.Mask & FaceMask.Cap) != 0) { if (((face.Mask & FaceMask.Hollow) == 0) && ((face.Mask & FaceMask.Open) == 0) && (prim.PathBegin == 0f) && (prim.ProfileCurve == LLObject.ProfileCurve.Square) && (prim.PathCurve == LLObject.PathCurve.Line)) { CreateUnCutCubeCap(ref face, vertices, path, profile); } else { CreateCap(ref face, vertices, path, profile); } } else if ((face.Mask & FaceMask.End) != 0 || (face.Mask & FaceMask.Side) != 0) { CreateSide(ref face, prim, vertices, path, profile); } else { throw new RenderingException("Unknown/uninitialized face type"); } }
private static void CreateUnCutCubeCap(ref Face face, List<Vertex> primVertices, Path path, Profile profile) { int maxS = profile.Positions.Count; int maxT = path.Points.Count; int gridSize = (profile.Positions.Count - 1) / 4; int quadCount = gridSize * gridSize; //int numVertices = (gridSize + 1) * (gridSize + 1); //int numIndices = quadCount * 4; int offset = 0; if ((face.Mask & FaceMask.Top) != 0) offset = (maxT - 1) * maxS; else offset = face.BeginS; Vertex[] corners = new Vertex[4]; Vertex baseVert; for (int t = 0; t < 4; t++) { corners[t].Position = primVertices[offset + (gridSize * t)].Position; corners[t].TexCoord.X = profile.Positions[gridSize * t].X + 0.5f; corners[t].TexCoord.Y = 0.5f - profile.Positions[gridSize * t].Y; } baseVert.Normal = ((corners[1].Position - corners[0].Position) % (corners[2].Position - corners[1].Position)); baseVert.Normal = Vector3.Normalize(baseVert.Normal); if ((face.Mask & FaceMask.Top) != 0) { baseVert.Normal *= -1f; } else { // Swap the UVs on the U(X) axis for top face Vector2 swap; swap = corners[0].TexCoord; corners[0].TexCoord = corners[3].TexCoord; corners[3].TexCoord = swap; swap = corners[1].TexCoord; corners[1].TexCoord = corners[2].TexCoord; corners[2].TexCoord = swap; } baseVert.Binormal = CalcBinormalFromTriangle( corners[0].Position, corners[0].TexCoord, corners[1].Position, corners[1].TexCoord, corners[2].Position, corners[2].TexCoord); for (int t = 0; t < 4; t++) { corners[t].Binormal = baseVert.Binormal; corners[t].Normal = baseVert.Normal; } int vtop = face.Vertices.Count; for (int gx = 0; gx < gridSize + 1; gx++) { for (int gy = 0; gy < gridSize + 1; gy++) { Vertex newVert = new Vertex(); LerpPlanarVertex( corners[0], corners[1], corners[3], ref newVert, (float)gx / (float)gridSize, (float)gy / (float)gridSize); face.Vertices.Add(newVert); if (gx == 0 && gy == 0) face.MinExtent = face.MaxExtent = newVert.Position; else UpdateMinMax(ref face, newVert.Position); } } face.Center = (face.MinExtent + face.MaxExtent) * 0.5f; int[] idxs = new int[] { 0, 1, gridSize + 2, gridSize + 2, gridSize + 1, 0 }; for (int gx = 0; gx < gridSize; gx++) { for (int gy = 0; gy < gridSize; gy++) { if ((face.Mask & FaceMask.Top) != 0) { for (int i = 5; i >= 0; i--) face.Indices.Add((ushort)(vtop + (gy * (gridSize + 1)) + gx + idxs[i])); } else { for (int i = 0; i < 6; i++) face.Indices.Add((ushort)(vtop + (gy * (gridSize + 1)) + gx + idxs[i])); } } } }
private static void CreateSide(ref Face face, LLObject.ObjectData prim, List<Vertex> primVertices, Path path, Profile profile) { bool flat = (face.Mask & FaceMask.Flat) != 0; int maxS = profile.Positions.Count; int s, t, i; float ss, tt; int numVertices = face.NumS * face.NumT; int numIndices = (face.NumS - 1) * (face.NumT - 1) * 6; face.Center = Vector3.Zero; int beginSTex = (int)Math.Floor(profile.Positions[face.BeginS].Z); int numS = (((face.Mask & FaceMask.Inner) != 0) && ((face.Mask & FaceMask.Flat) != 0) && face.NumS > 2) ? face.NumS / 2 : face.NumS; int curVertex = 0; // Copy the vertices into the array for (t = face.BeginT; t < face.BeginT + face.NumT; t++) { tt = path.Points[t].TexT; for (s = 0; s < numS; s++) { if ((face.Mask & FaceMask.End) != 0) { if (s != 0) ss = 1f; else ss = 0f; } else { // Get s value for tex-coord if (!flat) ss = profile.Positions[face.BeginS + s].Z; else ss = profile.Positions[face.BeginS + s].Z - beginSTex; } // Check to see if this triangle wraps around the array if (face.BeginS + s >= maxS) i = face.BeginS + s + maxS * (t - 1); // We're wrapping else i = face.BeginS + s + maxS * t; Vertex vertex = new Vertex(); vertex.Position = primVertices[i].Position; vertex.TexCoord = new Vector2(ss, tt); vertex.Normal = Vector3.Zero; vertex.Binormal = Vector3.Zero; if (curVertex == 0) face.MinExtent = face.MaxExtent = primVertices[i].Position; else UpdateMinMax(ref face, primVertices[i].Position); face.Vertices.Add(vertex); ++curVertex; if (((face.Mask & FaceMask.Inner) != 0) && ((face.Mask & FaceMask.Flat) != 0) && face.NumS > 2 && s > 0) { vertex.Position = primVertices[i].Position; //vertex.TexCoord = new Vector2(ss, tt); //vertex.Normal = Vector3.Zero; //vertex.Binormal = Vector3.Zero; face.Vertices.Add(vertex); ++curVertex; } } if (((face.Mask & FaceMask.Inner) != 0) && ((face.Mask & FaceMask.Flat) != 0) && face.NumS > 2) { if ((face.Mask & FaceMask.Open) != 0) s = numS - 1; else s = 0; i = face.BeginS + s + maxS * t; ss = profile.Positions[face.BeginS + s].Z - beginSTex; Vertex vertex = new Vertex(); vertex.Position = primVertices[i].Position; vertex.TexCoord = new Vector2(ss, tt); vertex.Normal = Vector3.Zero; vertex.Binormal = Vector3.Zero; UpdateMinMax(ref face, vertex.Position); face.Vertices.Add(vertex); ++curVertex; } } face.Center = (face.MinExtent + face.MaxExtent) * 0.5f; bool flatFace = ((face.Mask & FaceMask.Flat) != 0); // Now we generate the indices for (t = 0; t < (face.NumT - 1); t++) { for (s = 0; s < (face.NumS - 1); s++) { face.Indices.Add((ushort)(s + face.NumS * t)); // Bottom left face.Indices.Add((ushort)(s + 1 + face.NumS * (t + 1))); // Top right face.Indices.Add((ushort)(s + face.NumS * (t + 1))); // Top left face.Indices.Add((ushort)(s + face.NumS * t)); // Bottom left face.Indices.Add((ushort)(s + 1 + face.NumS * t)); // Bottom right face.Indices.Add((ushort)(s + 1 + face.NumS * (t + 1))); // Top right face.Edge.Add((face.NumS - 1) * 2 * t + s * 2 + 1); // Bottom left/top right neighbor face if (t < face.NumT - 2) // Top right/top left neighbor face face.Edge.Add((face.NumS - 1) * 2 * (t + 1) + s * 2 + 1); else if (face.NumT <= 3 || path.Open) // No neighbor face.Edge.Add(-1); else // Wrap on T face.Edge.Add(s * 2 + 1); if (s > 0) // Top left/bottom left neighbor face face.Edge.Add((face.NumS - 1) * 2 * t + s * 2 - 1); else if (flatFace || profile.Open) // No neighbor face.Edge.Add(-1); else // Wrap on S face.Edge.Add((face.NumS - 1) * 2 * t + (face.NumS - 2) * 2 + 1); if (t > 0) // Bottom left/bottom right neighbor face face.Edge.Add((face.NumS - 1) * 2 * (t - 1) + s * 2); else if (face.NumT <= 3 || path.Open) // No neighbor face.Edge.Add(-1); else // Wrap on T face.Edge.Add((face.NumS - 1) * 2 * (face.NumT - 2) + s * 2); if (s < face.NumS - 2) // Bottom right/top right neighbor face face.Edge.Add((face.NumS - 1) * 2 * t + (s + 1) * 2); else if (flatFace || profile.Open) // No neighbor face.Edge.Add(-1); else // Wrap on S face.Edge.Add((face.NumS - 1) * 2 * t); face.Edge.Add((face.NumS - 1) * 2 * t + s * 2); // Top right/bottom left neighbor face } } // Generate normals, loop through each triangle for (i = 0; i < face.Indices.Count / 3; i++) { Vertex v0 = face.Vertices[face.Indices[i * 3 + 0]]; Vertex v1 = face.Vertices[face.Indices[i * 3 + 1]]; Vertex v2 = face.Vertices[face.Indices[i * 3 + 2]]; // Calculate triangle normal Vector3 norm = (v0.Position - v1.Position) % (v0.Position - v2.Position); // Calculate binormal Vector3 binorm = CalcBinormalFromTriangle(v0.Position, v0.TexCoord, v1.Position, v1.TexCoord, v2.Position, v2.TexCoord); // Add triangle normal to vertices for (int j = 0; j < 3; j++) { Vertex vertex = face.Vertices[face.Indices[i * 3 + j]]; vertex.Normal += norm; vertex.Binormal += binorm; face.Vertices[face.Indices[i * 3 + j]] = vertex; } // Even out quad contributions if (i % 2 == 0) { Vertex vertex = face.Vertices[face.Indices[i * 3 + 2]]; vertex.Normal += norm; vertex.Binormal += binorm; face.Vertices[face.Indices[i * 3 + 2]] = vertex; } else { Vertex vertex = face.Vertices[face.Indices[i * 3 + 1]]; vertex.Normal += norm; vertex.Binormal += binorm; face.Vertices[face.Indices[i * 3 + 1]] = vertex; } } // Adjust normals based on wrapping and stitching Vector3 test1 = face.Vertices[0].Position - face.Vertices[face.NumS * (face.NumT - 2)].Position; Vector3 test2 = face.Vertices[face.NumS - 1].Position - face.Vertices[face.NumS * (face.NumT - 2) + face.NumS - 1].Position; bool sBottomConverges = (test1.LengthSquared() < 0.000001f); bool sTopConverges = (test2.LengthSquared() < 0.000001f); // TODO: Sculpt support Primitive.SculptType sculptType = Primitive.SculptType.None; if (sculptType == Primitive.SculptType.None) { if (!path.Open) { // Wrap normals on T for (i = 0; i < face.NumS; i++) { Vector3 norm = face.Vertices[i].Normal + face.Vertices[face.NumS * (face.NumT - 1) + i].Normal; Vertex vertex = face.Vertices[i]; vertex.Normal = norm; face.Vertices[i] = vertex; vertex = face.Vertices[face.NumS * (face.NumT - 1) + i]; vertex.Normal = norm; face.Vertices[face.NumS * (face.NumT - 1) + i] = vertex; } } if (!profile.Open && !sBottomConverges) { // Wrap normals on S for (i = 0; i < face.NumT; i++) { Vector3 norm = face.Vertices[face.NumS * i].Normal + face.Vertices[face.NumS * i + face.NumS - 1].Normal; Vertex vertex = face.Vertices[face.NumS * i]; vertex.Normal = norm; face.Vertices[face.NumS * i] = vertex; vertex = face.Vertices[face.NumS * i + face.NumS - 1]; vertex.Normal = norm; face.Vertices[face.NumS * i + face.NumS - 1] = vertex; } } if (prim.PathCurve == LLObject.PathCurve.Circle && prim.ProfileCurve == LLObject.ProfileCurve.HalfCircle) { if (sBottomConverges) { // All lower S have same normal Vector3 unitX = new Vector3(1f, 0f, 0f); for (i = 0; i < face.NumT; i++) { Vertex vertex = face.Vertices[face.NumS * i]; vertex.Normal = unitX; face.Vertices[face.NumS * i] = vertex; } } if (sTopConverges) { // All upper S have same normal Vector3 negUnitX = new Vector3(-1f, 0f, 0f); for (i = 0; i < face.NumT; i++) { Vertex vertex = face.Vertices[face.NumS * i + face.NumS - 1]; vertex.Normal = negUnitX; face.Vertices[face.NumS * i + face.NumS - 1] = vertex; } } } } else { // FIXME: Sculpt support } // Normalize normals and binormals for (i = 0; i < face.Vertices.Count; i++) { Vertex vertex = face.Vertices[i]; vertex.Normal.Normalize(); vertex.Binormal.Normalize(); face.Vertices[i] = vertex; } }
private static void CreateCap(ref Face face, List<Vertex> primVertices, Path path, Profile profile) { int i; int numVertices = profile.Positions.Count; //int numIndices = (numVertices - 2) * 3; int maxS = profile.Positions.Count; int maxT = path.Points.Count; face.Center = Vector3.Zero; int offset = 0; if ((face.Mask & FaceMask.Top) != 0) offset = (maxT - 1) * maxS; else offset = face.BeginS; // Figure out the normal, assume all caps are flat faces. // Cross product to get normals Vector2 cuv; Vector2 minUV = Vector2.Zero; Vector2 maxUV = Vector2.Zero; // Copy the vertices into the array for (i = 0; i < numVertices; i++) { Vertex vertex = new Vertex(); if ((face.Mask & FaceMask.Top) != 0) { vertex.Position = primVertices[i + offset].Position; vertex.TexCoord.X = profile.Positions[i].X + 0.5f; vertex.TexCoord.Y = profile.Positions[i].Y + 0.5f; } else { // Mirror for underside vertex.Position = primVertices[(numVertices - 1) - i].Position; vertex.TexCoord.X = profile.Positions[i].X + 0.5f; vertex.TexCoord.Y = 0.5f - profile.Positions[i].Y; } if (i == 0) { face.MinExtent = face.MaxExtent = primVertices[offset].Position; minUV = maxUV = primVertices[offset].TexCoord; } else { UpdateMinMax(ref face, vertex.Position); UpdateMinMax(ref minUV, ref maxUV, vertex.TexCoord); } face.Vertices.Add(vertex); } face.Center = (face.MinExtent + face.MaxExtent) * 0.5f; cuv = (minUV + maxUV) * 0.5f; Vector3 binormal = CalcBinormalFromTriangle( face.Center, cuv, face.Vertices[0].Position, face.Vertices[0].TexCoord, face.Vertices[1].Position, face.Vertices[1].TexCoord); binormal.Normalize(); Vector3 d0 = face.Center - face.Vertices[0].Position; Vector3 d1 = face.Center - face.Vertices[1].Position; Vector3 normal = ((face.Mask & FaceMask.Top) != 0) ? (d0 % d1) : (d1 % d0); normal.Normalize(); // If not hollow and not open create a center point in the cap if ((face.Mask & FaceMask.Hollow) == 0 && (face.Mask & FaceMask.Open) == 0) { Vertex vertex = new Vertex(); vertex.Position = face.Center; vertex.Normal = normal; vertex.Binormal = binormal; vertex.TexCoord = cuv; face.Vertices.Add(vertex); numVertices++; } for (i = 0; i < numVertices; i++) { Vertex vertex = face.Vertices[i]; vertex.Binormal = binormal; vertex.Normal = normal; face.Vertices[i] = vertex; } if ((face.Mask & FaceMask.Hollow) != 0) { if ((face.Mask & FaceMask.Top) != 0) { // HOLLOW TOP int pt1 = 0; int pt2 = numVertices - 1; i = 0; while (pt2 - pt1 > 1) { if (use_tri_1a2(profile, pt1, pt2)) { face.Indices.Add((ushort)pt1); face.Indices.Add((ushort)(pt1 + 1)); face.Indices.Add((ushort)pt2); pt1++; } else { face.Indices.Add((ushort)pt1); face.Indices.Add((ushort)(pt2 - 1)); face.Indices.Add((ushort)pt2); pt2--; } } } else { // HOLLOW BOTTOM int pt1 = 0; int pt2 = numVertices - 1; i = 0; while (pt2 - pt1 > 1) { // Flipped backfacing from top if (use_tri_1a2(profile, pt1, pt2)) { face.Indices.Add((ushort)pt1); face.Indices.Add((ushort)pt2); face.Indices.Add((ushort)(pt1 + 1)); pt1++; } else { face.Indices.Add((ushort)pt1); face.Indices.Add((ushort)pt2); face.Indices.Add((ushort)(pt2 - 1)); pt2--; } } } } else { // SOLID OPEN TOP // SOLID CLOSED TOP // SOLID OPEN BOTTOM // SOLID CLOSED BOTTOM // Not hollow, generate the triangle fan. // This is a tri-fan, so we reuse the same first point for all triangles for (i = 0; i < numVertices - 2; i++) { face.Indices.Add((ushort)(numVertices - 1)); face.Indices.Add((ushort)i); face.Indices.Add((ushort)(i + 1)); } } }
private static Profile GenerateProfilePolygon(LLObject.ObjectData prim, int sides, float offset, float angScale) { // Create a polygon by starting at (1, 0) and proceeding counterclockwise generating vectors Profile profile = new Profile(); profile.Positions = new List<Vector3>(); profile.Faces = new List<ProfileFace>(); float scale = 0.5f; float t, tStep, tFirst, tFraction, ang, angStep; Vector3 pt1, pt2; float begin = prim.ProfileBegin; float end = prim.ProfileEnd; tStep = 1f / sides; angStep = 2f * F_PI * tStep * angScale; // Scale to have size "match" scale. Compensates to get object to generally fill bounding box int totalSides = MathHelper.Round(sides / angScale); if (totalSides < 8) scale = TABLE_SCALE[totalSides]; tFirst = (float)Math.Floor(begin * sides) / (float)sides; // pt1 is the first point on the fractional face. // Starting t and ang values for the first face t = tFirst; ang = 2f * F_PI * (t * angScale + offset); pt1 = new Vector3((float)Math.Cos(ang) * scale, (float)Math.Sin(ang) * scale, t); // Increment to the next point. // pt2 is the end point on the fractional face t += tStep; ang += angStep; pt2 = new Vector3((float)Math.Cos(ang) * scale, (float)Math.Sin(ang) * scale, t); tFraction = (begin - tFirst) * sides; // Only use if it's not almost exactly on an edge if (tFraction < 0.9999f) { Vector3 newPt = Vector3.Lerp(pt1, pt2, tFraction); float ptX = newPt.X; if (ptX < profile.MinX) profile.MinX = ptX; else if (ptX > profile.MaxX) profile.MaxX = ptX; profile.Positions.Add(newPt); } // There's lots of potential here for floating point error to generate unneeded extra points while (t < end) { // Iterate through all the integer steps of t. pt1 = new Vector3((float)Math.Cos(ang) * scale, (float)Math.Sin(ang) * scale, t); float ptX = pt1.X; if (ptX < profile.MinX) profile.MinX = ptX; else if (ptX > profile.MaxX) profile.MaxX = ptX; profile.Positions.Add(pt1); t += tStep; ang += angStep; } // pt1 is the first point on the fractional face // pt2 is the end point on the fractional face pt2 = new Vector3((float)Math.Cos(ang) * scale, (float)Math.Sin(ang) * scale, t); // Find the fraction that we need to add to the end point tFraction = (end - (t - tStep)) * sides; if (tFraction > 0.0001f) { Vector3 newPt = Vector3.Lerp(pt1, pt2, tFraction); float ptX = newPt.X; if (ptX < profile.MinX) profile.MinX = ptX; else if (ptX > profile.MaxX) profile.MaxX = ptX; profile.Positions.Add(newPt); } // If we're sliced, the profile is open if ((end - begin) * angScale < 0.99f) { if ((end - begin) * angScale > 0.5f) profile.Concave = true; else profile.Concave = false; profile.Open = true; // Put center point if not hollow if (prim.ProfileHollow == 0f) profile.Positions.Add(Vector3.Zero); } else { // The profile isn't open profile.Open = false; profile.Concave = false; } return profile; }
private static List<Face> CreateVolumeFaces(Primitive prim, Path path, Profile profile, List<Vertex> vertices) { int numFaces = profile.Faces.Count; List<Face> faces = new List<Face>(numFaces); // Initialize faces with parameter data for (int i = 0; i < numFaces; i++) { ProfileFace pf = profile.Faces[i]; Face face = new Face(); face.Vertices = new List<Vertex>(); face.Indices = new List<ushort>(); face.Edge = new List<int>(); face.BeginS = pf.Index; face.NumS = pf.Count; face.BeginT = 0; face.NumT = path.Points.Count; face.ID = i; // Set the type mask bits correctly if (prim.Data.ProfileHollow > 0f) face.Mask |= FaceMask.Hollow; if (profile.Open) face.Mask |= FaceMask.Open; if (pf.Cap) { face.Mask |= FaceMask.Cap; if (pf.Type == FaceType.PathBegin) face.Mask |= FaceMask.Top; else face.Mask |= FaceMask.Bottom; } else if (pf.Type == FaceType.ProfileBegin || pf.Type == FaceType.ProfileEnd) { face.Mask |= FaceMask.Flat; face.Mask |= FaceMask.End; } else { face.Mask |= FaceMask.Side; if (pf.Flat) face.Mask |= FaceMask.Flat; if (pf.Type == FaceType.InnerSide) { face.Mask |= FaceMask.Inner; if (pf.Flat && face.NumS > 2) face.NumS *= 2; // Flat inner faces have to copy vert normals } else { face.Mask |= FaceMask.Outer; } } faces.Add(face); } for (int i = 0; i < faces.Count; i++) { Face face = faces[i]; BuildFace(ref face, prim.Data, vertices, path, profile, prim.Textures.GetFace((uint)i)); faces[i] = face; } return faces; }
private static void GenerateProfileHole(LLObject.ObjectData prim, ref Profile profile, bool flat, float sides, float offset, float boxHollow, float angScale) { // Total add has number of vertices on outside profile.TotalOutsidePoints = profile.Positions.Count; // Create the hole Profile hole = GenerateProfilePolygon(prim, (int)Math.Floor(sides), offset, angScale); // FIXME: Should we overwrite profile.Values with hole.Values? // Apply the hollow scale modifier for (int i = 0; i < hole.Positions.Count; i++) { Vector3 point = hole.Positions[i]; point *= boxHollow; hole.Positions[i] = point; } // Reverse the order hole.Positions.Reverse(); // Add the hole to the profile profile.Positions.AddRange(hole.Positions); // Create the inner side face for the hole ProfileFace innerFace = CreateProfileFace(profile.TotalOutsidePoints, profile.Positions.Count - profile.TotalOutsidePoints, 0f, FaceType.InnerSide, flat); profile.Faces.Add(innerFace); }
private static List<ushort> GenerateIndices(LLObject.ObjectData prim, Path path, Profile profile) { bool open = profile.Open; bool hollow = (prim.ProfileHollow > 0f); int sizeS = profile.Positions.Count; int sizeSOut = profile.TotalOutsidePoints; int sizeT = path.Points.Count; int s, t, i; List<ushort> indices = new List<ushort>(); if (open) { if (hollow) { // Open hollow -- much like the closed solid, except we // we need to stitch up the gap between s=0 and s=size_s-1 for (t = 0; t < sizeT - 1; t++) { // The outer face, first cut, and inner face for (s = 0; s < sizeS - 1; s++) { i = s + t * sizeS; indices.Add((ushort)i); // x,y indices.Add((ushort)(i + 1)); // x+1,y indices.Add((ushort)(i + sizeS)); // x,y+1 indices.Add((ushort)(i + sizeS)); // x,y+1 indices.Add((ushort)(i + 1)); // x+1,y indices.Add((ushort)(i + sizeS + 1)); // x+1,y+1 } // The other cut face indices.Add((ushort)(s + t * sizeS)); // x,y indices.Add((ushort)(0 + t * sizeS)); // x+1,y indices.Add((ushort)(s + (t + 1) * sizeS)); // x,y+1 indices.Add((ushort)(s + (t + 1) * sizeS)); // x,y+1 indices.Add((ushort)(0 + t * sizeS)); // x+1,y indices.Add((ushort)(0 + (t + 1) * sizeS)); // x+1,y+1 } // Do the top and bottom caps, if necessary if (path.Open) { // Top cap int pt1 = 0; int pt2 = sizeS - 1; i = (sizeT - 1) * sizeS; while (pt2 - pt1 > 1) { if (use_tri_1a2(profile, pt1, pt2)) { indices.Add((ushort)(pt1 + i)); indices.Add((ushort)(pt1 + 1 + i)); indices.Add((ushort)(pt2 + i)); pt1++; } else { indices.Add((ushort)(pt1 + i)); indices.Add((ushort)(pt2 - 1 + i)); indices.Add((ushort)(pt2 + i)); pt2--; } } // Bottom cap pt1 = 0; pt2 = sizeS - 1; while (pt2 - pt1 > 1) { if (use_tri_1a2(profile, pt1, pt2)) { indices.Add((ushort)pt1); indices.Add((ushort)pt2); indices.Add((ushort)(pt1 + 1)); pt1++; } else { indices.Add((ushort)pt1); indices.Add((ushort)pt2); indices.Add((ushort)(pt2 - 1)); pt2--; } } } } else { // Open solid for (t = 0; t < sizeT - 1; t++) { // Outer face + 1 cut face for (s = 0; s < sizeS - 1; s++) { i = s + t * sizeS; indices.Add((ushort)i); // x,y indices.Add((ushort)(i + 1)); // x+1,y indices.Add((ushort)(i + sizeS)); // x,y+1 indices.Add((ushort)(i + sizeS)); // x,y+1 indices.Add((ushort)(i + 1)); // x+1,y indices.Add((ushort)(i + sizeS + 1)); // x+1,y+1 } // The other cut face indices.Add((ushort)((sizeS - 1) + (t * sizeS))); // x,y indices.Add((ushort)(0 + t * sizeS)); // x+1,y indices.Add((ushort)((sizeS - 1) + (t + 1) * sizeS)); // x,y+1 indices.Add((ushort)((sizeS - 1) + (t + 1) * sizeS)); // x,y+1 indices.Add((ushort)(0 + (t * sizeS))); // x+1,y indices.Add((ushort)(0 + (t + 1) * sizeS)); // x+1,y+1 } // Do the top and bottom caps, if necessary if (path.Open) { for (s = 0; s < sizeS - 2; s++) { indices.Add((ushort)(s + 1)); indices.Add((ushort)s); indices.Add((ushort)(sizeS - 1)); } // We've got a top cap int offset = (sizeT - 1) * sizeS; for (s = 0; s < sizeS - 2; s++) { // Inverted ordering from bottom cap indices.Add((ushort)(offset + sizeS - 1)); indices.Add((ushort)(offset + s)); indices.Add((ushort)(offset + s + 1)); } } } } else if (hollow) { // Closed hollow // Outer face for (t = 0; t < sizeT - 1; t++) { for (s = 0; s < sizeSOut - 1; s++) { i = s + t * sizeS; indices.Add((ushort)i); // x,y indices.Add((ushort)(i + 1)); // x+1,y indices.Add((ushort)(i + sizeS)); // x,y+1 indices.Add((ushort)(i + sizeS)); // x,y+1 indices.Add((ushort)(i + 1)); // x+1,y indices.Add((ushort)(i + 1 + sizeS)); // x+1,y+1 } } // Inner face // Invert facing from outer face for (t = 0; t < sizeT - 1; t++) { for (s = sizeSOut; s < sizeS - 1; s++) { i = s + t * sizeS; indices.Add((ushort)i); // x,y indices.Add((ushort)(i + 1)); // x+1,y indices.Add((ushort)(i + sizeS)); // x,y+1 indices.Add((ushort)(i + sizeS)); // x,y+1 indices.Add((ushort)(i + 1)); // x+1,y indices.Add((ushort)(i + 1 + sizeS)); // x+1,y+1 } } // Do the top and bottom caps, if necessary if (path.Open) { // Top cap int pt1 = 0; int pt2 = sizeS - 1; i = (sizeT - 1) * sizeS; while (pt2 - pt1 > 1) { if (use_tri_1a2(profile, pt1, pt2)) { indices.Add((ushort)(pt1 + i)); indices.Add((ushort)(pt2 + 1 + i)); indices.Add((ushort)(pt2 + i)); pt1++; } else { indices.Add((ushort)(pt1 + i)); indices.Add((ushort)(pt2 - 1 + i)); indices.Add((ushort)(pt2 + i)); pt2--; } } // Bottom cap pt1 = 0; pt2 = sizeS - 1; while (pt2 - pt1 > 1) { if (use_tri_1a2(profile, pt1, pt2)) { indices.Add((ushort)pt1); indices.Add((ushort)pt2); indices.Add((ushort)(pt1 + 1)); pt1++; } else { indices.Add((ushort)pt1); indices.Add((ushort)pt2); indices.Add((ushort)(pt2 - 1)); pt2--; } } } } else { // Closed solid. Easy case for (t = 0; t < sizeT - 1; t++) { for (s = 0; s < sizeS - 1; s++) { // Should wrap properly, but for now... i = s + t * sizeS; indices.Add((ushort)i); // x,y indices.Add((ushort)(i + 1)); // x+1,y indices.Add((ushort)(i + sizeS)); // x,y+1 indices.Add((ushort)(i + sizeS)); // x,y+1 indices.Add((ushort)(i + 1)); // x+1,y indices.Add((ushort)(i + sizeS + 1)); // x+1,y+1 } } // Do the top and bottom caps, if necessary if (path.Open) { // Bottom cap for (s = 1; s < sizeS - 2; s++) { indices.Add((ushort)(s + 1)); indices.Add((ushort)s); indices.Add((ushort)0); } // Top cap int offset = (sizeT - 1) * sizeS; for (s = 1; s < sizeS - 2; s++) { // Inverted ordering from bottom cap indices.Add((ushort)offset); indices.Add((ushort)(offset + s)); indices.Add((ushort)(offset + s + 1)); } } } return indices; }
private static List<Vertex> GenerateVertices(LLObject.ObjectData prim, float detail, Path path, Profile profile) { int sizeS = path.Points.Count; int sizeT = profile.Positions.Count; // Generate vertex positions List<Vertex> vertices = new List<Vertex>(sizeT * sizeS); for (int i = 0; i < sizeT * sizeS; i++) vertices.Add(new Vertex()); // Run along the path for (int s = 0; s < sizeS; ++s) { Vector2 scale = path.Points[s].Scale; Quaternion rot = path.Points[s].Rotation; Vector3 pos = path.Points[s].Position; // Run along the profile for (int t = 0; t < sizeT; ++t) { int m = s * sizeT + t; Vertex vertex = vertices[m]; vertex.Position.X = profile.Positions[t].X * scale.X; vertex.Position.Y = profile.Positions[t].Y * scale.Y; vertex.Position.Z = 0f; vertex.Position *= rot; vertex.Position += pos; vertices[m] = vertex; } } return vertices; }
private static bool use_tri_1a2(Profile profilePoints, int pt1, int pt2) { // Use the profile points instead of the mesh, since you want // the un-transformed profile distances Vector3 p1 = profilePoints.Positions[pt1]; Vector3 p2 = profilePoints.Positions[pt2]; Vector3 pa = profilePoints.Positions[pt1 + 1]; Vector3 pb = profilePoints.Positions[pt2 - 1]; p1.Z = 0f; p2.Z = 0f; pa.Z = 0f; pb.Z = 0f; // Use area of triangle to determine backfacing float area_1a2, area_1ba, area_21b, area_2ab; area_1a2 = (p1.X * pa.Y - pa.X * p1.Y) + (pa.X * p2.Y - p2.X * pa.Y) + (p2.X * p1.Y - p1.X * p2.Y); area_1ba = (p1.X * pb.Y - pb.X * p1.Y) + (pb.X * pa.Y - pa.X * pb.Y) + (pa.X * p1.Y - p1.X * pa.Y); area_21b = (p2.X * p1.Y - p1.X * p2.Y) + (p1.X * pb.Y - pb.X * p1.Y) + (pb.X * p2.Y - p2.X * pb.Y); area_2ab = (p2.X * pa.Y - pa.X * p2.Y) + (pa.X * pb.Y - pb.X * pa.Y) + (pb.X * p2.Y - p2.X * pb.Y); bool use_tri_1a2 = true; bool tri_1a2 = true; bool tri_21b = true; if (area_1a2 < 0) tri_1a2 = false; if (area_2ab < 0) tri_1a2 = false; // Can't use, because it contains point b if (area_21b < 0) tri_21b = false; if (area_1ba < 0) tri_21b = false; // Can't use, because it contains point b if (!tri_1a2) { use_tri_1a2 = false; } else if (!tri_21b) { use_tri_1a2 = true; } else { Vector3 d1 = p1 - pa; Vector3 d2 = p2 - pb; if (d1.LengthSquared() < d2.LengthSquared()) use_tri_1a2 = true; else use_tri_1a2 = false; } return use_tri_1a2; }