Lines with rounded corners
- I don't put in enough hours (~30-40/week)
- I am building my own game engine (dumb, yes, but it's something I'm enjoying a lot! I've never done it before.)
- I'm a perfectionist, so I take my time to make sure everything is just right (it works, is well tested, and very robust). I don't want to come back months down the road to refactor something I've forgotten everything about.
I'm currently working on building a robust primitive system which is engineered for maximum usability and performance. I'm making slow but steady progress on that.
A few weeks ago, I completed my quads, point sprites, and billboards. Since I'm taking advantage of hardware instancing, I can have thousands of textured quads on the screen and run at 900+ fps (on my crappy dual core dev box).
After finishing the textured quads, next on the list was creating line lists, line strips, thick textured lines, and thick textured rounded lines. I think the thick textured rounded lines was one of the hardest ones to implement because of the math involved (or that I'm just dumb). Long ago, I had seen someone else do the same thing and posting the code to it. I didn't bother to look for it, and I thought I could do a much better job, so I started writing my own.
After much sweat and effort, here's what I came up with!
The white lines are added to show the vertices for the thick line. I also decided to show the underlying triangle structure.
Here are a few tips for anyone else trying to do the same thing:
-Avoid using triangle strips: I tried this at first, reasoning that I could save a teensy tiny bit of memory. In truth, creating triangle fans using triangle strips will cost you extra memory because you have to use ghost vertices. Instead, use triangle lists with index buffers. I think this may even save a smidge more memory, but it's kind of irrelevant. The real savings comes in the code simplicity and maintaining programmer sanity.
-Plan, then code: I'm kind of dumb. I usually can't just start writing code for something complicated and have it work the first time (though, it does happen on occasion). I have to literally draw out pictures and diagrams of what I'm trying to build. Then, I ask myself how I'm going to find the values in my pictures. I might even figure out what the values should be before writing any code. After I have a good plan, I write code to implement it. I constantly check to make sure that my code is doing what I've planned it to do and that the values I get match what I calculated. When I'm done, I run my implementation and see if it works as planned. In some cases, my conceptual planning was wrong or missing something and I have to revise it. When it's all done, what do you do with your notes? Save them! If you need to refactor later, you've got all your working notes, models, and comments to reference.
-Get it working first, then generalize: I sometimes jump right in and start trying to write a generalized solution and end up getting seriously bogged down in the details and quite confused. Sometimes, I scratch my head for hours trying to figure out how to even do a generalized solution to the problem. It's not always possible to immediately see all the patterns and write a generalization for them. So, start from the ground and build your way up, brick by brick. Start with a super simple task and build on it. Hard code your values in first, when it works as planned, then you can generalize.
-Don't trust something works correctly until it's been rigorously tested. This fact alone probably kicks my ass the most. Got a complicated function? Test it by throwing everything you can at it and see if any combo of parameters gives you unexpected results. Example: I wrote a function which tells me how many degrees are between two angles. The angle is always calculated by winding counter-clockwise from the first angle to the second angle. Now, what happens if the first angle is 315 degrees and the second angle is 45 degrees? If the math/logic is too simplistic, it wouldn't be able to handle an unusual case. It may look like it works for every test, but when another function depends on it to work correctly and it doesn't... you're gonna have a bad time. That leads to my next point...
-Don't try to compact your code: You think you can write a function using one or two lines of clever code? Good for you! But don't do it. It's a lot more difficult to debug when you have to squint at a line of code which does five different things at once. Unpack all that code. It doesn't save enough memory and clock cycles to justify wasting extra programmer time unpacking it if there are errors in there. Keep it simple.
Anyways, looking ahead to next week, I think I'll be able to finally tackle my outdated particle system and get that working to perfection with all the primitives I have.