I realized that the thick line rendering I came up with in my previous post was deeply flawed. I had been drawing thick lines in 3D space but thinking in 2D. I had assumed that the normals for the thick lines would all be the same. Boy, was I wrong!

So, what I'm aiming to do is allow users (myself) to create thick lines in 3D space. To do that, they need to give me a vertex position, an orientation normal, and a line thickness. I need at least two vertices to draw a line.

Anyways, here is the bare bones "user code" to create a series of 5 lines down the X-axis, twisting 90 degrees on each subsequent segment:

In some cases, the end point of the last line segment may match the starting point of the first vertex. If this happens, I want my thick line segments to loop smoothly and seamlessly.

I did find the 2D thick line drawing code I was thinking about earlier (codeplex). However, like I realized earlier, it's

If you're interested in the code and mathematics behind this, here is the function which does it:

Next up... revisiting the thick lines with rounded corners. I'm honestly not looking forward to it since I anticipate it'll be a lot harder, but once that's done, my line drawing primitive system will finally be complete and ready for use. Whew! This is a lot harder than I thought it would be. Why did I choose to solo a 3D game in XNA, starting from scratch?!

So, what I'm aiming to do is allow users (myself) to create thick lines in 3D space. To do that, they need to give me a vertex position, an orientation normal, and a line thickness. I need at least two vertices to draw a line.

Anyways, here is the bare bones "user code" to create a series of 5 lines down the X-axis, twisting 90 degrees on each subsequent segment:

lc.AddThickVertex(new Vector3(0, 0, 0), Vector3.Backward, 0.5f); lc.AddThickVertex(new Vector3(1, 0, 0), Vector3.Backward, 0.5f); lc.AddThickVertex(new Vector3(2, 0, 0), Vector3.Up, 0.5f); lc.AddThickVertex(new Vector3(3, 0, 0), Vector3.Forward, 0.5f); lc.AddThickVertex(new Vector3(4, 0, 0), Vector3.Down, 0.5f); lc.AddThickVertex(new Vector3(5, 0, 0), Vector3.Backward, 0.5f);The result is a twisting thick line which looks like a twizzlers licorice stick:

In some cases, the end point of the last line segment may match the starting point of the first vertex. If this happens, I want my thick line segments to loop smoothly and seamlessly.

lc.AddThickVertex(new Vector3(0, 0, 0), Vector3.Backward, 0.5f); lc.AddThickVertex(new Vector3(1, 0, 0), Vector3.Backward, 0.5f); lc.AddThickVertex(new Vector3(1, 1, 0), Vector3.Backward, 0.5f); lc.AddThickVertex(new Vector3(0, 1, 0), Vector3.Backward, 0.5f); lc.AddThickVertex(new Vector3(0, 0, 0), Vector3.Backward, 0.5f);

I did find the 2D thick line drawing code I was thinking about earlier (codeplex). However, like I realized earlier, it's

*only for 2D*. The mathematics for 2D thick lines is pretty easy and straight forward because you don't have to worry about the thick line normal facing direction. You already know that the normal will always be (0,0,1) so you can make some implicit assumptions with your math. However, what do you do when you get something like this in 3D space?lc.AddThickVertex(new Vector3(0, 0, 0), Vector3.Up, 0.5f); lc.AddThickVertex(new Vector3(1, 0, 0), Vector3.Up, 0.5f); lc.AddThickVertex(new Vector3(1, 1, 0), Vector3.Left, 0.5f); lc.AddThickVertex(new Vector3(0, 1, 0), Vector3.Down, 0.5f); lc.AddThickVertex(new Vector3(0, 0, 0), Vector3.Right, 0.5f);This

*should*create four lines which all face inwards towards the center of the square, like so:If you're interested in the code and mathematics behind this, here is the function which does it:

/// <summary> /// Creates the appropriate vertex and index buffers for hard corners for a list of thick line segments. /// This thick line uses two primitives per line segment, so is best for thick lines with little variation between /// normals. /// </summary> /// <remarks>Pre-conditions: m_tlList should have at least one line segment</remarks> private void BuildHardCorners(out List<LineVertex> verts, out List<int> indices) { int sizeGuess = 0; foreach (KeyValuePair<int, List<ThickLineSegment>> kvp in m_tlList) sizeGuess += kvp.Value.Count; verts = new List<LineVertex>(sizeGuess * 4); indices = new List<int>(sizeGuess * 12); LineVertex[] lastVert = new LineVertex[3]; /*Note: We already know that the thick line list contains at least one line segment. The calling function takes care of that check.*/ foreach (KeyValuePair<int, List<ThickLineSegment>> kvp in m_tlList) { List<ThickLineSegment> SegmentList = kvp.Value; lastVert[0] = SegmentList[0].TopLeft; lastVert[1] = SegmentList[0].Start; lastVert[2] = SegmentList[0].BottomLeft; bool loop = false; /* Goal: Try to derive the purple lines for every vertex segment. If we can do this, we * just have to "connect the dots" to draw our line segment lists.*/ for (int a = 0; a < SegmentList.Count; a++) { ThickLineSegment curSegment = kvp.Value[a]; if (SegmentList.Count == 1) { //there's only one segment in the list, so this is super easy! verts.Add(curSegment.TopLeft); verts.Add(curSegment.TopRight); verts.Add(curSegment.BottomLeft); verts.Add(curSegment.BottomRight); } else { //we know there there are at least two segments in the list, so we have at least one corner //which we need to derive. We don't know how many segments we actually have, so we can't hard code in //a solution. We have to generalize it. //so, what do we have to do? We have a current line segment and possibly a next and previous line segment. //each line segment comes with the four vertex corners already defined (red lines). //we have to find the red lines for the adjacent line segments (blue lines). //from the red and blue lines, we can sum them together to get the purple lines, which becomes the shared vertex float height = (curSegment.Thickness / 2.0f); ThickLineSegment nextSegment = null; //GOAL: Create a list of vertices required to draw the thick lines if (SegmentList.Count >= a + 2) //do we have a next segment? { nextSegment = SegmentList[a + 1]; } else if(SegmentList[SegmentList.Count - 1].End.Position == SegmentList[0].Start.Position) { //we're at the end of the segment list, so let's check for looping nextSegment = SegmentList[0]; loop = true; } /* Possibilities: * 1. Next = null; * 2. Next = value; * GOAL: Find the shared vertex points between two line segments */ if (nextSegment != null) { //the math is somewhat straight forward here: To derive the purple lines, we just sum the verts from the current vertex and the next vertex. //then we normalize it to get a directional unit vector, scale it by the height, then translate it to where it should be. Vector3 redLine = curSegment.TopRight.Position - curSegment.End.Position; Vector3 blueLine = (Vector3.Dot(curSegment.Normal, nextSegment.Normal) >= 0) ? nextSegment.TopLeft.Position - nextSegment.Start.Position : nextSegment.BottomLeft.Position - nextSegment.Start.Position; Vector3 purpleLine = redLine + blueLine; purpleLine.Normalize(); Angle t = Angle.FindAngleBetween(-purpleLine, -redLine); float length = (float)(height * System.Math.Tan(t.Radian)); float hyp = (float)System.Math.Sqrt(length * length + height * height); purpleLine *= hyp; //create the adjusted left side LineVertex TopLeft = new LineVertex(lastVert[0].Position, curSegment.TopLeft); LineVertex BottomLeft = new LineVertex(lastVert[2].Position, curSegment.BottomLeft); LineVertex TopRight = new LineVertex(curSegment.End.Position + purpleLine, curSegment.TopRight); LineVertex BottomRight = new LineVertex(curSegment.End.Position - purpleLine, curSegment.BottomRight); verts.Add(TopLeft); verts.Add(TopRight); verts.Add(BottomLeft); verts.Add(BottomRight); lastVert[0] = TopRight; lastVert[1] = curSegment.End; lastVert[2] = BottomRight; if (loop) { //adjust the left verticies of the first line segment to match the verts of the last line segment LineVertex top = verts[0]; LineVertex bottom = verts[2]; top.Position = lastVert[0].Position; bottom.Position = lastVert[2].Position; verts[0] = top; verts[2] = bottom; } } else { //We're creating the last segment and its not looped. //we have to check the normals between this segment and the previous segment float normalDot = Vector3.Dot(curSegment.Normal, SegmentList[a - 1].Normal); LineVertex TopLeft = new LineVertex(lastVert[0].Position, curSegment.TopLeft); LineVertex TopRight = (normalDot >= 0) ? curSegment.TopRight : curSegment.BottomRight; LineVertex BottomLeft = new LineVertex(lastVert[2].Position, curSegment.BottomLeft); LineVertex BottomRight = (normalDot >= 0) ? curSegment.BottomRight : curSegment.TopRight; if (normalDot >= 0) { //Since we're swapping the top and bottom verts of the right side, we also gotta swap the UV coordinates Vector2 tmpUV = TopRight.UV; TopRight.UV = BottomRight.UV; BottomRight.UV = tmpUV; } verts.Add(TopLeft); verts.Add(TopRight); verts.Add(BottomLeft); verts.Add(BottomRight); } } } //We've now got a list of all of the vertices, in order. Now, we have to create the index buffer. //note that we're going to do both sides. for (int segmentID = 0; segmentID < SegmentList.Count + 1; segmentID++) { //clockwise indices.Add(segmentID * 4 + 0); indices.Add(segmentID * 4 + 1); indices.Add(segmentID * 4 + 2); indices.Add(segmentID * 4 + 3); indices.Add(segmentID * 4 + 2); indices.Add(segmentID * 4 + 1); //counter-clockwise indices.Add(segmentID * 4 + 2); indices.Add(segmentID * 4 + 1); indices.Add(segmentID * 4 + 0); indices.Add(segmentID * 4 + 1); indices.Add(segmentID * 4 + 2); indices.Add(segmentID * 4 + 3); } } }I struggled over this far longer than I should have, but I'm pretty happy with the result now.

Next up... revisiting the thick lines with rounded corners. I'm honestly not looking forward to it since I anticipate it'll be a lot harder, but once that's done, my line drawing primitive system will finally be complete and ready for use. Whew! This is a lot harder than I thought it would be. Why did I choose to solo a 3D game in XNA, starting from scratch?!