• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
  • entries
  • comments
  • views

Thick line rendering revisited.

Sign in to follow this  
Followers 0


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:
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:
/// /// 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./// /// Pre-conditions: m_tlList should have at least one line segmentprivate void BuildHardCorners(out List verts, out List indices){ int sizeGuess = 0; foreach (KeyValuePair> kvp in m_tlList) sizeGuess += kvp.Value.Count; verts = new List(sizeGuess * 4); indices = new List(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> kvp in m_tlList) { List 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?!

Sign in to follow this  
Followers 0

1 Comment

[quote]Why did I choose to solo a 3D game in XNA, starting from scratch?![/quote] For the fun of solving something like this :)

Share this comment

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now